Skip to content

Commit c679482

Browse files
committed
Add method resolution deref inference var test
1 parent d21f888 commit c679482

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

crates/hir-ty/src/infer/expr.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,13 @@ impl InferenceContext<'_> {
312312
Expr::Call { callee, args, .. } => {
313313
let callee_ty = self.infer_expr(*callee, &Expectation::none());
314314
let mut derefs = Autoderef::new(&mut self.table, callee_ty.clone(), false);
315-
let (res, derefed_callee) = 'b: {
316-
// manual loop to be able to access `derefs.table`
317-
while let Some((callee_deref_ty, _)) = derefs.next() {
318-
let res = derefs.table.callable_sig(&callee_deref_ty, args.len());
319-
if res.is_some() {
320-
break 'b (res, callee_deref_ty);
321-
}
315+
let (res, derefed_callee) = loop {
316+
let Some((callee_deref_ty, _)) = derefs.next() else {
317+
break (None, callee_ty.clone());
318+
};
319+
if let Some(res) = derefs.table.callable_sig(&callee_deref_ty, args.len()) {
320+
break (Some(res), callee_deref_ty);
322321
}
323-
(None, callee_ty.clone())
324322
};
325323
// if the function is unresolved, we use is_varargs=true to
326324
// suppress the arg count diagnostic here

crates/hir-ty/src/infer/unify.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,14 @@ impl<'a> InferenceTable<'a> {
289289
}
290290

291291
fn fallback_value(&self, iv: InferenceVar, kind: TyVariableKind) -> Ty {
292+
let is_diverging = self
293+
.type_variable_table
294+
.get(iv.index() as usize)
295+
.map_or(false, |data| data.contains(TypeVariableFlags::DIVERGING));
296+
if is_diverging {
297+
return TyKind::Never.intern(Interner);
298+
}
292299
match kind {
293-
_ if self
294-
.type_variable_table
295-
.get(iv.index() as usize)
296-
.map_or(false, |data| data.contains(TypeVariableFlags::DIVERGING)) =>
297-
{
298-
TyKind::Never
299-
}
300300
TyVariableKind::General => TyKind::Error,
301301
TyVariableKind::Integer => TyKind::Scalar(Scalar::Int(IntTy::I32)),
302302
TyVariableKind::Float => TyKind::Scalar(Scalar::Float(FloatTy::F64)),
@@ -438,6 +438,7 @@ impl<'a> InferenceTable<'a> {
438438
where
439439
T: HasInterner<Interner = Interner> + TypeFoldable<Interner>,
440440
{
441+
// TODO check this vec here
441442
self.resolve_with_fallback_inner(&mut Vec::new(), t, &fallback)
442443
}
443444

@@ -798,7 +799,7 @@ impl<'a> InferenceTable<'a> {
798799
let trait_data = self.db.trait_data(fn_once_trait);
799800
let output_assoc_type = trait_data.associated_type_by_name(&name![Output])?;
800801

801-
let mut arg_tys = vec![];
802+
let mut arg_tys = Vec::with_capacity(num_args);
802803
let arg_ty = TyBuilder::tuple(num_args)
803804
.fill(|it| {
804805
let arg = match it {

crates/hir-ty/src/tests/method_resolution.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,21 @@ fn test() {
17951795
);
17961796
}
17971797

1798+
#[test]
1799+
fn deref_into_inference_var() {
1800+
check_types(
1801+
r#"
1802+
//- minicore:deref
1803+
struct A<T>(T);
1804+
impl core::ops::Deref for A<u32> {}
1805+
impl A<i32> { fn foo(&self) {} }
1806+
fn main() {
1807+
A(0).foo();
1808+
//^^^^^^^^^^ ()
1809+
}
1810+
"#,
1811+
);
1812+
}
17981813
#[test]
17991814
fn receiver_adjustment_autoref() {
18001815
check(

0 commit comments

Comments
 (0)