Skip to content

Commit 5bd20bb

Browse files
committed
relate: first alias, then infer
we'd just generalize the alias and emit an alias-relate goal regardless
1 parent 0bcbae2 commit 5bd20bb

8 files changed

Lines changed: 26 additions & 31 deletions

File tree

compiler/rustc_borrowck/src/type_check/relate_tys.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,19 +377,20 @@ impl<'b, 'tcx> TypeRelation<TyCtxt<'tcx>> for NllTypeRelating<'_, 'b, 'tcx> {
377377
);
378378
}
379379

380+
(_, ty::Alias(..)) | (ty::Alias(..), _) if infcx.next_trait_solver() => {
381+
self.register_alias_relate_predicate(a, b);
382+
}
383+
380384
(&ty::Infer(ty::TyVar(a_vid)), _) => {
381385
infcx.instantiate_ty_var(self, true, a_vid, self.ambient_variance, b)?
382386
}
383387

384388
(
385389
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: a_def_id, .. }),
386390
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }),
387-
) if a_def_id == b_def_id || infcx.next_trait_solver() => {
391+
) if a_def_id == b_def_id => {
392+
debug_assert!(!infcx.next_trait_solver());
388393
super_combine_tys(&infcx.infcx, self, a, b).map(|_| ()).or_else(|err| {
389-
// This behavior is only there for the old solver, the new solver
390-
// shouldn't ever fail. Instead, it unconditionally emits an
391-
// alias-relate goal.
392-
assert!(!self.type_checker.infcx.next_trait_solver());
393394
self.cx().dcx().span_delayed_bug(
394395
self.span(),
395396
"failure to relate an opaque to itself should result in an error later on",
@@ -399,7 +400,7 @@ impl<'b, 'tcx> TypeRelation<TyCtxt<'tcx>> for NllTypeRelating<'_, 'b, 'tcx> {
399400
}
400401
(&ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }), _)
401402
| (_, &ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }))
402-
if def_id.is_local() && !self.type_checker.infcx.next_trait_solver() =>
403+
if def_id.is_local() =>
403404
{
404405
self.relate_opaques(a, b)?;
405406
}

compiler/rustc_infer/src/infer/relate/generalize.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ impl<'tcx> InferCtxt<'tcx> {
279279
normalize,
280280
};
281281

282+
debug!(?root_vid, ?generalizer.root_term, "generalize");
283+
282284
let value_may_be_infer = generalizer.relate(source_term, source_term)?;
283285
Ok(Generalization { value_may_be_infer })
284286
}
@@ -443,7 +445,7 @@ impl<'tcx> Generalizer<'_, 'tcx> {
443445
let normalized_alias = (self.normalize)(alias);
444446

445447
self.state = GeneralizerState::ShallowStructurallyRelateAliases;
446-
// recursively generalize, treat the outer alias as rigid to avoid infinite recursion
448+
// recursively generalize, trCeat the outer alias as rigid to avoid infinite recursion
447449
let res = self.relate(normalized_alias, normalized_alias);
448450

449451
// only one way to get here

compiler/rustc_type_ir/src/relate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub type RelateResult<I, T> = Result<T, TypeError<I>>;
2020
/// This should always be `No` unless in a few special-cases when
2121
/// instantiating canonical responses and in the new solver. Each
2222
/// such case should have a comment explaining why it is used.
23-
#[derive(Debug, Copy, Clone)]
23+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
2424
pub enum StructurallyRelateAliases {
2525
Yes,
2626
No,

compiler/rustc_type_ir/src/relate/combine.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,14 @@ where
117117
panic!("We do not expect to encounter `Fresh` variables in the new solver")
118118
}
119119

120-
(_, ty::Alias(..)) | (ty::Alias(..), _) if infcx.next_trait_solver() => {
121-
match relation.structurally_relate_aliases() {
122-
StructurallyRelateAliases::Yes => structurally_relate_tys(relation, a, b),
123-
StructurallyRelateAliases::No => {
124-
relation.register_alias_relate_predicate(a, b);
125-
Ok(a)
126-
}
127-
}
128-
}
129-
130120
// All other cases of inference are errors
131121
(ty::Infer(_), _) | (_, ty::Infer(_)) => Err(TypeError::Sorts(ExpectedFound::new(a, b))),
132122

123+
(ty::Alias(..), _) | (_, ty::Alias(..)) if infcx.next_trait_solver() => {
124+
assert_eq!(relation.structurally_relate_aliases(), StructurallyRelateAliases::Yes);
125+
structurally_relate_tys(relation, a, b)
126+
}
127+
133128
(ty::Alias(ty::Opaque, _), _) | (_, ty::Alias(ty::Opaque, _)) => {
134129
assert!(!infcx.next_trait_solver());
135130
match infcx.typing_mode() {

compiler/rustc_type_ir/src/relate/solver_relating.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ where
203203
}
204204

205205
match (a.kind(), b.kind()) {
206+
(_, ty::Alias(..)) | (ty::Alias(..), _)
207+
if matches!(self.structurally_relate_aliases, StructurallyRelateAliases::No) =>
208+
{
209+
self.register_alias_relate_predicate(a, b);
210+
}
211+
206212
(ty::Infer(ty::TyVar(a_id)), ty::Infer(ty::TyVar(b_id))) => {
207213
match self.ambient_variance {
208214
ty::Covariant => {

tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.current.fixed

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ fn main() {
88
//[next]~^^ ERROR expected a `FnMut(&<std::ops::RangeInclusive<{integer}> as Iterator>::Item)` closure, found
99
let _ = (-10..=10).find(|x: &i32| x.signum() == 0);
1010
//[current]~^ ERROR type mismatch in closure arguments
11-
//[next]~^^ ERROR expected `RangeInclusive<{integer}>` to be an iterator that yields `&&i32`, but it yields `{integer}`
12-
//[next]~| ERROR expected a `FnMut(&<std::ops::RangeInclusive<{integer}> as Iterator>::Item)` closure, found
11+
//[next]~^^ ERROR expected a `FnMut(&<std::ops::RangeInclusive<{integer}> as Iterator>::Item)` closure, found
1312
}

tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.next.stderr

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ LL | let _ = (-10..=10).find(|x: i32| x.signum() == 0);
1212
note: required by a bound in `find`
1313
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
1414

15-
error[E0271]: expected `RangeInclusive<{integer}>` to be an iterator that yields `&&i32`, but it yields `{integer}`
16-
--> $DIR/closure-arg-type-mismatch-issue-45727.rs:9:24
17-
|
18-
LL | let _ = (-10..=10).find(|x: &&&i32| x.signum() == 0);
19-
| ^^^^ expected `&&i32`, found integer
20-
2115
error[E0277]: expected a `FnMut(&<std::ops::RangeInclusive<{integer}> as Iterator>::Item)` closure, found `{closure@$DIR/closure-arg-type-mismatch-issue-45727.rs:9:29: 9:40}`
2216
--> $DIR/closure-arg-type-mismatch-issue-45727.rs:9:29
2317
|
@@ -32,7 +26,6 @@ LL | let _ = (-10..=10).find(|x: &&&i32| x.signum() == 0);
3226
note: required by a bound in `find`
3327
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
3428

35-
error: aborting due to 3 previous errors
29+
error: aborting due to 2 previous errors
3630

37-
Some errors have detailed explanations: E0271, E0277.
38-
For more information about an error, try `rustc --explain E0271`.
31+
For more information about this error, try `rustc --explain E0277`.

tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ fn main() {
88
//[next]~^^ ERROR expected a `FnMut(&<std::ops::RangeInclusive<{integer}> as Iterator>::Item)` closure, found
99
let _ = (-10..=10).find(|x: &&&i32| x.signum() == 0);
1010
//[current]~^ ERROR type mismatch in closure arguments
11-
//[next]~^^ ERROR expected `RangeInclusive<{integer}>` to be an iterator that yields `&&i32`, but it yields `{integer}`
12-
//[next]~| ERROR expected a `FnMut(&<std::ops::RangeInclusive<{integer}> as Iterator>::Item)` closure, found
11+
//[next]~^^ ERROR expected a `FnMut(&<std::ops::RangeInclusive<{integer}> as Iterator>::Item)` closure, found
1312
}

0 commit comments

Comments
 (0)