@@ -70,14 +70,13 @@ impl<'tcx> InferCtxt<'tcx> {
7070 //
7171 // We then relate `generalized_ty <: source_ty`, adding constraints like `'x: '?2` and
7272 // `?1 <: ?3`.
73- let Generalization { value_may_be_infer : generalized_ty, has_unconstrained_ty_var } = self
74- . generalize (
75- relation. span ( ) ,
76- relation. structurally_relate_aliases ( ) ,
77- target_vid,
78- instantiation_variance,
79- source_ty,
80- ) ?;
73+ let Generalization { value_may_be_infer : generalized_ty } = self . generalize (
74+ relation. span ( ) ,
75+ relation. structurally_relate_aliases ( ) ,
76+ target_vid,
77+ instantiation_variance,
78+ source_ty,
79+ ) ?;
8180
8281 // Constrain `b_vid` to the generalized type `generalized_ty`.
8382 if let & ty:: Infer ( ty:: TyVar ( generalized_vid) ) = generalized_ty. kind ( ) {
@@ -86,11 +85,6 @@ impl<'tcx> InferCtxt<'tcx> {
8685 self . inner . borrow_mut ( ) . type_variables ( ) . instantiate ( target_vid, generalized_ty) ;
8786 }
8887
89- // See the comment on `Generalization::has_unconstrained_ty_var`.
90- if has_unconstrained_ty_var {
91- relation. register_predicates ( [ ty:: ClauseKind :: WellFormed ( generalized_ty. into ( ) ) ] ) ;
92- }
93-
9488 // Finally, relate `generalized_ty` to `source_ty`, as described in previous comment.
9589 //
9690 // FIXME(#16847): This code is non-ideal because all these subtype
@@ -210,19 +204,15 @@ impl<'tcx> InferCtxt<'tcx> {
210204 ) -> RelateResult < ' tcx , ( ) > {
211205 // FIXME(generic_const_exprs): Occurs check failures for unevaluated
212206 // constants and generic expressions are not yet handled correctly.
213- let Generalization { value_may_be_infer : generalized_ct, has_unconstrained_ty_var } = self
214- . generalize (
215- relation. span ( ) ,
216- relation. structurally_relate_aliases ( ) ,
217- target_vid,
218- ty:: Invariant ,
219- source_ct,
220- ) ?;
207+ let Generalization { value_may_be_infer : generalized_ct } = self . generalize (
208+ relation. span ( ) ,
209+ relation. structurally_relate_aliases ( ) ,
210+ target_vid,
211+ ty:: Invariant ,
212+ source_ct,
213+ ) ?;
221214
222215 debug_assert ! ( !generalized_ct. is_ct_infer( ) ) ;
223- if has_unconstrained_ty_var {
224- bug ! ( "unconstrained ty var when generalizing `{source_ct:?}`" ) ;
225- }
226216
227217 self . inner
228218 . borrow_mut ( )
@@ -281,12 +271,10 @@ impl<'tcx> InferCtxt<'tcx> {
281271 ambient_variance,
282272 in_alias : false ,
283273 cache : Default :: default ( ) ,
284- has_unconstrained_ty_var : false ,
285274 } ;
286275
287276 let value_may_be_infer = generalizer. relate ( source_term, source_term) ?;
288- let has_unconstrained_ty_var = generalizer. has_unconstrained_ty_var ;
289- Ok ( Generalization { value_may_be_infer, has_unconstrained_ty_var } )
277+ Ok ( Generalization { value_may_be_infer } )
290278 }
291279}
292280
@@ -376,9 +364,6 @@ struct Generalizer<'me, 'tcx> {
376364 in_alias : bool ,
377365
378366 cache : SsoHashMap < ( Ty < ' tcx > , ty:: Variance , bool ) , Ty < ' tcx > > ,
379-
380- /// See the field `has_unconstrained_ty_var` in `Generalization`.
381- has_unconstrained_ty_var : bool ,
382367}
383368
384369impl < ' tcx > Generalizer < ' _ , ' tcx > {
@@ -391,10 +376,8 @@ impl<'tcx> Generalizer<'_, 'tcx> {
391376 }
392377
393378 /// Create a new type variable in the universe of the target when
394- /// generalizing an alias. This has to set `has_unconstrained_ty_var`
395- /// if we're currently in a bivariant context.
379+ /// generalizing an alias.
396380 fn next_ty_var_for_alias ( & mut self ) -> Ty < ' tcx > {
397- self . has_unconstrained_ty_var |= self . ambient_variance == ty:: Bivariant ;
398381 self . infcx . next_ty_var_in_universe ( self . span , self . for_universe )
399382 }
400383
@@ -540,14 +523,8 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
540523 }
541524 }
542525
543- // Bivariant: make a fresh var, but remember that
544- // it is unconstrained. See the comment in
545- // `Generalization`.
546- ty:: Bivariant => self . has_unconstrained_ty_var = true ,
547-
548- // Co/contravariant: this will be
549- // sufficiently constrained later on.
550- ty:: Covariant | ty:: Contravariant => ( ) ,
526+ // We do need a fresh type variable otherwise.
527+ ty:: Bivariant | ty:: Covariant | ty:: Contravariant => ( ) ,
551528 }
552529
553530 let origin = inner. type_variables ( ) . var_origin ( vid) ;
@@ -766,32 +743,8 @@ struct Generalization<T> {
766743 /// for `?0` generalization returns an inference
767744 /// variable.
768745 ///
769- /// This has to be handled wotj care as it can
746+ /// This has to be handled with care as it can
770747 /// otherwise very easily result in infinite
771748 /// recursion.
772749 pub value_may_be_infer : T ,
773-
774- /// In general, we do not check whether all types which occur during
775- /// type checking are well-formed. We only check wf of user-provided types
776- /// and when actually using a type, e.g. for method calls.
777- ///
778- /// This means that when subtyping, we may end up with unconstrained
779- /// inference variables if a generalized type has bivariant parameters.
780- /// A parameter may only be bivariant if it is constrained by a projection
781- /// bound in a where-clause. As an example, imagine a type:
782- ///
783- /// struct Foo<A, B> where A: Iterator<Item = B> {
784- /// data: A
785- /// }
786- ///
787- /// here, `A` will be covariant, but `B` is unconstrained.
788- ///
789- /// However, whatever it is, for `Foo` to be WF, it must be equal to `A::Item`.
790- /// If we have an input `Foo<?A, ?B>`, then after generalization we will wind
791- /// up with a type like `Foo<?C, ?D>`. When we enforce `Foo<?A, ?B> <: Foo<?C, ?D>`,
792- /// we will wind up with the requirement that `?A <: ?C`, but no particular
793- /// relationship between `?B` and `?D` (after all, these types may be completely
794- /// different). If we do nothing else, this may mean that `?D` goes unconstrained
795- /// (as in #41677). To avoid this we emit a `WellFormed` obligation in these cases.
796- pub has_unconstrained_ty_var : bool ,
797750}
0 commit comments