@@ -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,45 @@ 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 > {
65+ type BreakTy = ( ) ;
66+
6067 fn visit_ty ( & mut self , t : Ty < ' tcx > ) -> ControlFlow < Self :: BreakTy > {
6168 match * t. kind ( ) {
62- ty:: Alias ( ..) if !self . include_nonconstraining => {
63- // projections are not injective
69+ ty:: Alias ( ty:: Projection | ty:: Inherent | ty:: Opaque , _)
70+ if !self . include_nonconstraining =>
71+ {
72+ // Projections are not injective in general.
6473 return ControlFlow :: Continue ( ( ) ) ;
6574 }
75+ ty:: Alias ( ty:: Weak , alias) if !self . include_nonconstraining => {
76+ if !self . tcx . recursion_limit ( ) . value_within_limit ( self . depth ) {
77+ return ControlFlow :: Break ( ( ) ) ;
78+ }
79+ self . depth += 1 ;
80+ return self
81+ . tcx
82+ . type_of ( alias. def_id )
83+ . instantiate ( self . tcx , alias. args )
84+ . visit_with ( self ) ;
85+ }
6686 ty:: Param ( data) => {
6787 self . parameters . push ( Parameter :: from ( data) ) ;
6888 }
@@ -82,7 +102,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector {
82102 fn visit_const ( & mut self , c : ty:: Const < ' tcx > ) -> ControlFlow < Self :: BreakTy > {
83103 match c. kind ( ) {
84104 ty:: ConstKind :: Unevaluated ( ..) if !self . include_nonconstraining => {
85- // Constant expressions are not injective
105+ // Constant expressions are not injective in general.
86106 return c. ty ( ) . visit_with ( self ) ;
87107 }
88108 ty:: ConstKind :: Param ( data) => {
@@ -201,12 +221,12 @@ pub fn setup_constraining_predicates<'tcx>(
201221 // `<<T as Bar>::Baz as Iterator>::Output = <U as Iterator>::Output`
202222 // Then the projection only applies if `T` is known, but it still
203223 // does not determine `U`.
204- let inputs = parameters_for ( & projection. projection_ty , true ) ;
224+ let inputs = parameters_for ( tcx , & projection. projection_ty , true ) ;
205225 let relies_only_on_inputs = inputs. iter ( ) . all ( |p| input_parameters. contains ( p) ) ;
206226 if !relies_only_on_inputs {
207227 continue ;
208228 }
209- input_parameters. extend ( parameters_for ( & projection. term , false ) ) ;
229+ input_parameters. extend ( parameters_for ( tcx , & projection. term , false ) ) ;
210230 } else {
211231 continue ;
212232 }
0 commit comments