Skip to content

Commit d2e2ad5

Browse files
committed
Auto merge of #40570 - nikomatsakis:inference-subtype-through-obligation, r=arielb1
Handle subtyping in inference through obligations We currently store subtyping relations in the `TypeVariables` structure as a kind of special case. This branch uses normal obligations to propagate subtyping, thus converting our inference variables into normal fallback. It also does a few other things: - Removes the (unstable, outdated) support for custom type inference fallback. - It's not clear how we want this to work, but we know that we don't want it to work the way it currently does. - The existing support was also just getting in my way. - Fixes #30225, which was caused by the trait caching code pretending type variables were normal unification variables, when indeed they were not (but now are). There is one fishy part of these changes: when computing the LUB/GLB of a "bivariant" type parameter, I currently return the `a` value. Bivariant type parameters are only allowed in a very particular situation, where the type parameter is only used as an associated type output, like this: ```rust pub struct Foo<A, B> where A: Fn() -> B { data: A } ``` In principle, if one had `T=Foo<A, &'a u32>` and `U=Foo<A, &'b u32>` and (e.g.) `A: for<'a> Fn() -> &'a u32`, then I think that computing the LUB of `T` and `U` might do the wrong thing. Probably the right behavior is just to create a fresh type variable. However, that particular example would not compile (because the where-clause is illegal; `'a` does not appear in any input type). I was not able to make an example that *would* compile and demonstrate this shortcoming, and handling the LUB/GLB was mildly inconvenient, so I left it as is. I am considering whether to revisit this or what. I have started a crater run to test the impact of these changes.
2 parents 14481f7 + 1cc7621 commit d2e2ad5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+732
-910
lines changed

src/librustc/ich/impls_ty.rs

+4
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ impl_stable_hash_for!(enum ty::Visibility {
164164
impl_stable_hash_for!(struct ty::TraitRef<'tcx> { def_id, substs });
165165
impl_stable_hash_for!(struct ty::TraitPredicate<'tcx> { trait_ref });
166166
impl_stable_hash_for!(tuple_struct ty::EquatePredicate<'tcx> { t1, t2 });
167+
impl_stable_hash_for!(struct ty::SubtypePredicate<'tcx> { a_is_expected, a, b });
167168

168169
impl<'a, 'tcx, A, B> HashStable<StableHashingContext<'a, 'tcx>> for ty::OutlivesPredicate<A, B>
169170
where A: HashStable<StableHashingContext<'a, 'tcx>>,
@@ -194,6 +195,9 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::Predicate<'tcx
194195
ty::Predicate::Equate(ref pred) => {
195196
pred.hash_stable(hcx, hasher);
196197
}
198+
ty::Predicate::Subtype(ref pred) => {
199+
pred.hash_stable(hcx, hasher);
200+
}
197201
ty::Predicate::RegionOutlives(ref pred) => {
198202
pred.hash_stable(hcx, hasher);
199203
}

src/librustc/infer/bivariate.rs

-123
This file was deleted.

0 commit comments

Comments
 (0)