Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion crates/hir-ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1597,7 +1597,6 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
tried_resolving_once = true;

ty = self.table.insert_type_vars(ty);
ty = self.table.normalize_associated_types_in(ty);
ty = self.table.structurally_resolve_type(ty);
if ty.is_ty_error() {
return (self.err_ty(), None);
Expand Down
10 changes: 5 additions & 5 deletions crates/hir-ty/src/infer/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ impl<'db> CastCheck<'db> {
&mut self,
ctx: &mut InferenceContext<'_, 'db>,
) -> Result<(), InferenceDiagnostic<'db>> {
self.expr_ty = ctx.table.eagerly_normalize_and_resolve_shallow_in(self.expr_ty);
self.cast_ty = ctx.table.eagerly_normalize_and_resolve_shallow_in(self.cast_ty);
self.expr_ty = ctx.table.try_structurally_resolve_type(self.expr_ty);
self.cast_ty = ctx.table.try_structurally_resolve_type(self.cast_ty);

// This should always come first so that we apply the coercion, which impacts infer vars.
if ctx
Expand Down Expand Up @@ -159,7 +159,7 @@ impl<'db> CastCheck<'db> {
TyKind::FnDef(..) => {
let sig =
self.expr_ty.callable_sig(ctx.interner()).expect("FnDef had no sig");
let sig = ctx.table.eagerly_normalize_and_resolve_shallow_in(sig);
let sig = ctx.table.normalize_associated_types_in(sig);
let fn_ptr = Ty::new_fn_ptr(ctx.interner(), sig);
if ctx
.coerce(
Expand Down Expand Up @@ -191,7 +191,7 @@ impl<'db> CastCheck<'db> {
},
// array-ptr-cast
CastTy::Ptr(t, m) => {
let t = ctx.table.eagerly_normalize_and_resolve_shallow_in(t);
let t = ctx.table.try_structurally_resolve_type(t);
if !ctx.table.is_sized(t) {
return Err(CastError::IllegalCast);
}
Expand Down Expand Up @@ -375,7 +375,7 @@ fn pointer_kind<'db>(
ty: Ty<'db>,
ctx: &mut InferenceContext<'_, 'db>,
) -> Result<Option<PointerKind<'db>>, ()> {
let ty = ctx.table.eagerly_normalize_and_resolve_shallow_in(ty);
let ty = ctx.table.try_structurally_resolve_type(ty);

if ctx.table.is_sized(ty) {
return Ok(Some(PointerKind::Thin));
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1819,7 +1819,7 @@ impl<'db> InferenceContext<'_, 'db> {
indices_to_skip,
is_varargs,
);
self.table.normalize_associated_types_in(ret_ty)
self.table.structurally_resolve_type(ret_ty)
}

fn infer_method_call(
Expand Down Expand Up @@ -1999,7 +1999,7 @@ impl<'db> InferenceContext<'_, 'db> {
self.table.unify(formal_receiver_ty, receiver_ty);

self.check_call_arguments(tgt_expr, &param_tys, ret_ty, expected, args, &[], is_varargs);
self.table.normalize_associated_types_in(ret_ty)
self.table.structurally_resolve_type(ret_ty)
}

/// Generic function that factors out common logic from function calls,
Expand Down
15 changes: 2 additions & 13 deletions crates/hir-ty/src/infer/unify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,6 @@ impl<'db> InferenceTable<'db> {
.unwrap_or(ty)
}

/// Works almost same as [`Self::normalize_associated_types_in`], but this also resolves shallow
/// the inference variables
pub(crate) fn eagerly_normalize_and_resolve_shallow_in<T>(&mut self, ty: T) -> T
where
T: TypeFoldable<DbInterner<'db>>,
{
let ty = self.resolve_vars_with_obligations(ty);
let ty = self.normalize_associated_types_in(ty);
self.resolve_vars_with_obligations(ty)
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ugly function was introduced by me (with some visitors back then) to fix some errors on non-resolved nested opaque types during mir lowering but it isn't needed anymore 😄

pub(crate) fn normalize_alias_ty(&mut self, alias: Ty<'db>) -> Ty<'db> {
self.infer_ctxt
.at(&ObligationCause::new(), self.trait_env.env)
Expand Down Expand Up @@ -762,7 +751,7 @@ impl<'db> InferenceTable<'db> {
}

let mut ty = ty;
ty = self.eagerly_normalize_and_resolve_shallow_in(ty);
ty = self.try_structurally_resolve_type(ty);
if let Some(sized) = short_circuit_trivial_tys(ty) {
return sized;
}
Expand All @@ -784,7 +773,7 @@ impl<'db> InferenceTable<'db> {
// Structs can have DST as its last field and such cases are not handled
// as unsized by the chalk, so we do this manually.
ty = last_field_ty;
ty = self.eagerly_normalize_and_resolve_shallow_in(ty);
ty = self.try_structurally_resolve_type(ty);
if let Some(sized) = short_circuit_trivial_tys(ty) {
return sized;
}
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-ty/src/tests/macros.rs
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some tests are seemingly regressed but I believe they are okay because they are errornous types

Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ fn expr_macro_def_expanded_in_various_places() {
39..442 '{ ...!(); }': {unknown}
73..94 'spam!(...am!())': {unknown}
100..119 'for _ ...!() {}': fn into_iter<isize>(isize) -> <isize as IntoIterator>::IntoIter
100..119 'for _ ...!() {}': <isize as IntoIterator>::IntoIter
100..119 'for _ ...!() {}': {unknown}
100..119 'for _ ...!() {}': !
100..119 'for _ ...!() {}': {unknown}
100..119 'for _ ...!() {}': &'? mut {unknown}
Expand Down Expand Up @@ -291,7 +291,7 @@ fn expr_macro_rules_expanded_in_various_places() {
53..456 '{ ...!(); }': {unknown}
87..108 'spam!(...am!())': {unknown}
114..133 'for _ ...!() {}': fn into_iter<isize>(isize) -> <isize as IntoIterator>::IntoIter
114..133 'for _ ...!() {}': <isize as IntoIterator>::IntoIter
114..133 'for _ ...!() {}': {unknown}
114..133 'for _ ...!() {}': !
114..133 'for _ ...!() {}': {unknown}
114..133 'for _ ...!() {}': &'? mut {unknown}
Expand Down
6 changes: 3 additions & 3 deletions crates/hir-ty/src/tests/never_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ fn diverging_expression_3_break() {
140..141 'x': u32
149..175 '{ for ...; }; }': u32
151..172 'for a ...eak; }': fn into_iter<{unknown}>({unknown}) -> <{unknown} as IntoIterator>::IntoIter
151..172 'for a ...eak; }': <{unknown} as IntoIterator>::IntoIter
151..172 'for a ...eak; }': {unknown}
151..172 'for a ...eak; }': !
151..172 'for a ...eak; }': {unknown}
151..172 'for a ...eak; }': &'? mut {unknown}
Expand All @@ -379,7 +379,7 @@ fn diverging_expression_3_break() {
226..227 'x': u32
235..253 '{ for ... {}; }': u32
237..250 'for a in b {}': fn into_iter<{unknown}>({unknown}) -> <{unknown} as IntoIterator>::IntoIter
237..250 'for a in b {}': <{unknown} as IntoIterator>::IntoIter
237..250 'for a in b {}': {unknown}
237..250 'for a in b {}': !
237..250 'for a in b {}': {unknown}
237..250 'for a in b {}': &'? mut {unknown}
Expand All @@ -395,7 +395,7 @@ fn diverging_expression_3_break() {
304..305 'x': u32
313..340 '{ for ...; }; }': u32
315..337 'for a ...urn; }': fn into_iter<{unknown}>({unknown}) -> <{unknown} as IntoIterator>::IntoIter
315..337 'for a ...urn; }': <{unknown} as IntoIterator>::IntoIter
315..337 'for a ...urn; }': {unknown}
315..337 'for a ...urn; }': !
315..337 'for a ...urn; }': {unknown}
315..337 'for a ...urn; }': &'? mut {unknown}
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/tests/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn infer_pattern() {
83..84 '1': i32
86..93 '"hello"': &'static str
101..151 'for (e... }': fn into_iter<{unknown}>({unknown}) -> <{unknown} as IntoIterator>::IntoIter
101..151 'for (e... }': <{unknown} as IntoIterator>::IntoIter
101..151 'for (e... }': {unknown}
101..151 'for (e... }': !
101..151 'for (e... }': {unknown}
101..151 'for (e... }': &'? mut {unknown}
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-ty/src/tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ fn infer_std_crash_5() {
expect![[r#"
26..322 '{ ... } }': ()
32..320 'for co... }': fn into_iter<{unknown}>({unknown}) -> <{unknown} as IntoIterator>::IntoIter
32..320 'for co... }': <{unknown} as IntoIterator>::IntoIter
32..320 'for co... }': {unknown}
32..320 'for co... }': !
32..320 'for co... }': {unknown}
32..320 'for co... }': &'? mut {unknown}
Expand Down Expand Up @@ -1255,7 +1255,7 @@ fn test() {
expect![[r#"
10..68 '{ ... } }': ()
16..66 'for _ ... }': fn into_iter<()>(()) -> <() as IntoIterator>::IntoIter
16..66 'for _ ... }': <() as IntoIterator>::IntoIter
16..66 'for _ ... }': {unknown}
16..66 'for _ ... }': !
16..66 'for _ ... }': {unknown}
16..66 'for _ ... }': &'? mut {unknown}
Expand Down
86 changes: 86 additions & 0 deletions crates/hir-ty/src/tests/regression/new_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,89 @@ where
"#]],
);
}

#[test]
fn regression_20975() {
check_infer(
r#"
//- minicore: future, iterators, range
use core::future::Future;

struct Foo<T>(T);

trait X {}

impl X for i32 {}
impl X for i64 {}

impl<T: X> Iterator for Foo<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
self.next_spec()
}
}

trait Bar {
type Item;

fn next_spec(&mut self) -> Option<Self::Item>;
}

impl<T: X> Bar for Foo<T> {
type Item = T;

fn next_spec(&mut self) -> Option<Self::Item> {
None
}
}

struct JoinAll<F>
where
F: Future,
{
f: F,
}

fn join_all<I>(iter: I) -> JoinAll<<I as IntoIterator>::Item>
where
I: IntoIterator,
<I as IntoIterator>::Item: Future,
{
loop {}
}

fn main() {
let x = Foo(42).filter_map(|_| Some(async {}));
join_all(x);
}
"#,
expect![[r#"
164..168 'self': &'? mut Foo<T>
192..224 '{ ... }': Option<T>
202..206 'self': &'? mut Foo<T>
202..218 'self.n...spec()': Option<T>
278..282 'self': &'? mut Self
380..384 'self': &'? mut Foo<T>
408..428 '{ ... }': Option<T>
418..422 'None': Option<T>
501..505 'iter': I
614..629 '{ loop {} }': JoinAll<impl Future>
620..627 'loop {}': !
625..627 '{}': ()
641..713 '{ ...(x); }': ()
651..652 'x': FilterMap<Foo<i32>, impl FnMut(i32) -> Option<impl Future<Output = ()>>>
655..658 'Foo': fn Foo<i32>(i32) -> Foo<i32>
655..662 'Foo(42)': Foo<i32>
655..693 'Foo(42...c {}))': FilterMap<Foo<i32>, impl FnMut(i32) -> Option<impl Future<Output = ()>>>
659..661 '42': i32
674..692 '|_| So...nc {})': impl FnMut(i32) -> Option<impl Future<Output = ()>>
675..676 '_': i32
678..682 'Some': fn Some<impl Future<Output = ()>>(impl Future<Output = ()>) -> Option<impl Future<Output = ()>>
678..692 'Some(async {})': Option<impl Future<Output = ()>>
683..691 'async {}': impl Future<Output = ()>
699..707 'join_all': fn join_all<FilterMap<Foo<i32>, impl FnMut(i32) -> Option<impl Future<Output = ()>>>>(FilterMap<Foo<i32>, impl FnMut(i32) -> Option<impl Future<Output = ()>>>) -> JoinAll<<FilterMap<Foo<i32>, impl FnMut(i32) -> Option<impl Future<Output = ()>>> as IntoIterator>::Item>
699..710 'join_all(x)': JoinAll<impl Future<Output = ()>>
708..709 'x': FilterMap<Foo<i32>, impl FnMut(i32) -> Option<impl Future<Output = ()>>>
"#]],
);
}
2 changes: 1 addition & 1 deletion crates/hir-ty/src/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3891,7 +3891,7 @@ fn foo<T: Bar>() {
expect![[r#"
110..127 '{ ...z(); }': ()
116..122 'T::baz': fn baz<T>() -> <{unknown} as Foo>::Gat<'?>
116..124 'T::baz()': <{unknown} as Foo>::Gat<'?>
116..124 'T::baz()': {unknown}
"#]],
);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/tests/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4274,7 +4274,7 @@ fn f<'a>(v: &dyn Trait<Assoc<i32> = &'a i32>) {
127..128 'v': &'? (dyn Trait<Assoc<i32> = &'a i32> + 'static)
164..195 '{ ...f(); }': ()
170..171 'v': &'? (dyn Trait<Assoc<i32> = &'a i32> + 'static)
170..184 'v.get::<i32>()': <dyn Trait<Assoc<i32> = &'a i32> + 'static as Trait>::Assoc<i32>
170..184 'v.get::<i32>()': {unknown}
170..192 'v.get:...eref()': {unknown}
"#]],
);
Expand Down