@@ -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,46 @@ 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 > {
60
65
fn visit_ty ( & mut self , t : Ty < ' tcx > ) -> ControlFlow < Self :: BreakTy > {
61
66
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.
64
71
return ControlFlow :: Continue ( ( ) ) ;
65
72
}
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
+ }
66
87
ty:: Param ( data) => {
67
88
self . parameters . push ( Parameter :: from ( data) ) ;
68
89
}
@@ -82,7 +103,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector {
82
103
fn visit_const ( & mut self , c : ty:: Const < ' tcx > ) -> ControlFlow < Self :: BreakTy > {
83
104
match c. kind ( ) {
84
105
ty:: ConstKind :: Unevaluated ( ..) if !self . include_nonconstraining => {
85
- // Constant expressions are not injective
106
+ // Constant expressions are not injective in general.
86
107
return c. ty ( ) . visit_with ( self ) ;
87
108
}
88
109
ty:: ConstKind :: Param ( data) => {
@@ -201,12 +222,12 @@ pub fn setup_constraining_predicates<'tcx>(
201
222
// `<<T as Bar>::Baz as Iterator>::Output = <U as Iterator>::Output`
202
223
// Then the projection only applies if `T` is known, but it still
203
224
// does not determine `U`.
204
- let inputs = parameters_for ( & projection. projection_ty , true ) ;
225
+ let inputs = parameters_for ( tcx , & projection. projection_ty , true ) ;
205
226
let relies_only_on_inputs = inputs. iter ( ) . all ( |p| input_parameters. contains ( p) ) ;
206
227
if !relies_only_on_inputs {
207
228
continue ;
208
229
}
209
- input_parameters. extend ( parameters_for ( & projection. term , false ) ) ;
230
+ input_parameters. extend ( parameters_for ( tcx , & projection. term , false ) ) ;
210
231
} else {
211
232
continue ;
212
233
}
0 commit comments