@@ -27,12 +27,13 @@ impl From<ty::ParamConst> for Parameter {
2727
2828/// Returns the set of parameters constrained by the impl header.
2929pub fn parameters_for_impl < ' tcx > (
30+ tcx : TyCtxt < ' tcx > ,
3031 impl_self_ty : Ty < ' tcx > ,
3132 impl_trait_ref : Option < ty:: TraitRef < ' tcx > > ,
3233) -> FxHashSet < Parameter > {
3334 let vec = match impl_trait_ref {
34- Some ( tr) => parameters_for ( & tr, false ) ,
35- None => parameters_for ( & impl_self_ty, false ) ,
35+ Some ( tr) => parameters_for ( tcx , & tr, false ) ,
36+ None => parameters_for ( tcx , & impl_self_ty, false ) ,
3637 } ;
3738 vec. into_iter ( ) . collect ( )
3839}
@@ -43,26 +44,46 @@ pub fn parameters_for_impl<'tcx>(
4344/// of parameters whose values are needed in order to constrain `ty` - these
4445/// differ, with the latter being a superset, in the presence of projections.
4546pub fn parameters_for < ' tcx > (
47+ tcx : TyCtxt < ' tcx > ,
4648 t : & impl TypeVisitable < TyCtxt < ' tcx > > ,
4749 include_nonconstraining : bool ,
4850) -> Vec < Parameter > {
49- let mut collector = ParameterCollector { parameters : vec ! [ ] , include_nonconstraining } ;
51+ let mut collector =
52+ ParameterCollector { tcx, parameters : vec ! [ ] , include_nonconstraining, depth : 0 } ;
5053 t. visit_with ( & mut collector) ;
5154 collector. parameters
5255}
5356
54- struct ParameterCollector {
57+ struct ParameterCollector < ' tcx > {
58+ tcx : TyCtxt < ' tcx > ,
5559 parameters : Vec < Parameter > ,
5660 include_nonconstraining : bool ,
61+ depth : usize ,
5762}
5863
59- impl < ' tcx > TypeVisitor < TyCtxt < ' tcx > > for ParameterCollector {
64+ impl < ' tcx > TypeVisitor < TyCtxt < ' tcx > > for ParameterCollector < ' tcx > {
6065 fn visit_ty ( & mut self , t : Ty < ' tcx > ) -> ControlFlow < Self :: BreakTy > {
6166 match * t. kind ( ) {
62- ty:: Alias ( ..) if !self . include_nonconstraining => {
63- // projections are not injective
67+ ty:: Alias ( ty:: Projection | ty:: Inherent | ty:: Opaque , _)
68+ if !self . include_nonconstraining =>
69+ {
70+ // Projections are not injective in general.
6471 return ControlFlow :: Continue ( ( ) ) ;
6572 }
73+ ty:: Alias ( ty:: Weak , alias) if !self . include_nonconstraining => {
74+ if !self . tcx . recursion_limit ( ) . value_within_limit ( self . depth ) {
75+ // Other constituent types may still constrain some generic params, consider
76+ // `<T> (Overflow, T)` for example. Therefore we want to continue instead of
77+ // breaking. Only affects diagnostics.
78+ return ControlFlow :: Continue ( ( ) ) ;
79+ }
80+ self . depth += 1 ;
81+ return self
82+ . tcx
83+ . type_of ( alias. def_id )
84+ . instantiate ( self . tcx , alias. args )
85+ . visit_with ( self ) ;
86+ }
6687 ty:: Param ( data) => {
6788 self . parameters . push ( Parameter :: from ( data) ) ;
6889 }
@@ -82,7 +103,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector {
82103 fn visit_const ( & mut self , c : ty:: Const < ' tcx > ) -> ControlFlow < Self :: BreakTy > {
83104 match c. kind ( ) {
84105 ty:: ConstKind :: Unevaluated ( ..) if !self . include_nonconstraining => {
85- // Constant expressions are not injective
106+ // Constant expressions are not injective in general.
86107 return c. ty ( ) . visit_with ( self ) ;
87108 }
88109 ty:: ConstKind :: Param ( data) => {
@@ -201,12 +222,12 @@ pub fn setup_constraining_predicates<'tcx>(
201222 // `<<T as Bar>::Baz as Iterator>::Output = <U as Iterator>::Output`
202223 // Then the projection only applies if `T` is known, but it still
203224 // does not determine `U`.
204- let inputs = parameters_for ( & projection. projection_ty , true ) ;
225+ let inputs = parameters_for ( tcx , & projection. projection_ty , true ) ;
205226 let relies_only_on_inputs = inputs. iter ( ) . all ( |p| input_parameters. contains ( p) ) ;
206227 if !relies_only_on_inputs {
207228 continue ;
208229 }
209- input_parameters. extend ( parameters_for ( & projection. term , false ) ) ;
230+ input_parameters. extend ( parameters_for ( tcx , & projection. term , false ) ) ;
210231 } else {
211232 continue ;
212233 }
0 commit comments