@@ -27,12 +27,13 @@ impl From<ty::ParamConst> for Parameter {
27
27
28
28
/// Returns the set of parameters constrained by the impl header.
29
29
pub fn parameters_for_impl < ' tcx > (
30
+ tcx : TyCtxt < ' tcx > ,
30
31
impl_self_ty : Ty < ' tcx > ,
31
32
impl_trait_ref : Option < ty:: TraitRef < ' tcx > > ,
32
33
) -> FxHashSet < Parameter > {
33
34
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 ) ,
36
37
} ;
37
38
vec. into_iter ( ) . collect ( )
38
39
}
@@ -43,26 +44,45 @@ pub fn parameters_for_impl<'tcx>(
43
44
/// of parameters whose values are needed in order to constrain `ty` - these
44
45
/// differ, with the latter being a superset, in the presence of projections.
45
46
pub fn parameters_for < ' tcx > (
47
+ tcx : TyCtxt < ' tcx > ,
46
48
t : & impl TypeVisitable < TyCtxt < ' tcx > > ,
47
49
include_nonconstraining : bool ,
48
50
) -> Vec < Parameter > {
49
- let mut collector = ParameterCollector { parameters : vec ! [ ] , include_nonconstraining } ;
51
+ let mut collector =
52
+ ParameterCollector { tcx, parameters : vec ! [ ] , include_nonconstraining, depth : 0 } ;
50
53
t. visit_with ( & mut collector) ;
51
54
collector. parameters
52
55
}
53
56
54
- struct ParameterCollector {
57
+ struct ParameterCollector < ' tcx > {
58
+ tcx : TyCtxt < ' tcx > ,
55
59
parameters : Vec < Parameter > ,
56
60
include_nonconstraining : bool ,
61
+ depth : usize ,
57
62
}
58
63
59
- impl < ' tcx > TypeVisitor < TyCtxt < ' tcx > > for ParameterCollector {
64
+ impl < ' tcx > TypeVisitor < TyCtxt < ' tcx > > for ParameterCollector < ' tcx > {
65
+ type BreakTy = ( ) ;
66
+
60
67
fn visit_ty ( & mut self , t : Ty < ' tcx > ) -> ControlFlow < Self :: BreakTy > {
61
68
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.
64
73
return ControlFlow :: Continue ( ( ) ) ;
65
74
}
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
+ }
66
86
ty:: Param ( data) => {
67
87
self . parameters . push ( Parameter :: from ( data) ) ;
68
88
}
@@ -82,7 +102,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector {
82
102
fn visit_const ( & mut self , c : ty:: Const < ' tcx > ) -> ControlFlow < Self :: BreakTy > {
83
103
match c. kind ( ) {
84
104
ty:: ConstKind :: Unevaluated ( ..) if !self . include_nonconstraining => {
85
- // Constant expressions are not injective
105
+ // Constant expressions are not injective in general.
86
106
return c. ty ( ) . visit_with ( self ) ;
87
107
}
88
108
ty:: ConstKind :: Param ( data) => {
@@ -201,12 +221,12 @@ pub fn setup_constraining_predicates<'tcx>(
201
221
// `<<T as Bar>::Baz as Iterator>::Output = <U as Iterator>::Output`
202
222
// Then the projection only applies if `T` is known, but it still
203
223
// does not determine `U`.
204
- let inputs = parameters_for ( & projection. projection_ty , true ) ;
224
+ let inputs = parameters_for ( tcx , & projection. projection_ty , true ) ;
205
225
let relies_only_on_inputs = inputs. iter ( ) . all ( |p| input_parameters. contains ( p) ) ;
206
226
if !relies_only_on_inputs {
207
227
continue ;
208
228
}
209
- input_parameters. extend ( parameters_for ( & projection. term , false ) ) ;
229
+ input_parameters. extend ( parameters_for ( tcx , & projection. term , false ) ) ;
210
230
} else {
211
231
continue ;
212
232
}
0 commit comments