1
- use std:: assert_matches:: assert_matches;
2
1
use std:: fmt:: Debug ;
3
2
4
3
use rustc_data_structures:: stack:: ensure_sufficient_stack;
@@ -16,7 +15,6 @@ use tracing::instrument;
16
15
use super :: { FulfillmentCtxt , NextSolverError } ;
17
16
use crate :: error_reporting:: InferCtxtErrorExt ;
18
17
use crate :: error_reporting:: traits:: OverflowCause ;
19
- use crate :: traits:: query:: evaluate_obligation:: InferCtxtExt ;
20
18
use crate :: traits:: { BoundVarReplacer , PlaceholderReplacer , ScrubbedTraitError } ;
21
19
22
20
/// Deeply normalize all aliases in `value`. This does not handle inference and expects
@@ -97,19 +95,18 @@ impl<'tcx, E> NormalizationFolder<'_, 'tcx, E>
97
95
where
98
96
E : FromSolverError < ' tcx , NextSolverError < ' tcx > > ,
99
97
{
100
- fn normalize_alias_ty ( & mut self , alias_ty : Ty < ' tcx > ) -> Result < Ty < ' tcx > , Vec < E > > {
101
- assert_matches ! ( alias_ty. kind( ) , ty:: Alias ( ..) ) ;
102
-
98
+ fn normalize_alias_term (
99
+ & mut self ,
100
+ alias_term : ty:: Term < ' tcx > ,
101
+ ) -> Result < ty:: Term < ' tcx > , Vec < E > > {
103
102
let infcx = self . at . infcx ;
104
103
let tcx = infcx. tcx ;
105
104
let recursion_limit = tcx. recursion_limit ( ) ;
106
105
if !recursion_limit. value_within_limit ( self . depth ) {
107
- let ty:: Alias ( _, data) = * alias_ty. kind ( ) else {
108
- unreachable ! ( ) ;
109
- } ;
106
+ let term = alias_term. to_alias_term ( ) . unwrap ( ) ;
110
107
111
108
self . at . infcx . err_ctxt ( ) . report_overflow_error (
112
- OverflowCause :: DeeplyNormalize ( data . into ( ) ) ,
109
+ OverflowCause :: DeeplyNormalize ( term ) ,
113
110
self . at . cause . span ,
114
111
true ,
115
112
|_| { } ,
@@ -118,14 +115,14 @@ where
118
115
119
116
self . depth += 1 ;
120
117
121
- let new_infer_ty = infcx. next_ty_var ( self . at . cause . span ) ;
118
+ let infer_term = infcx. next_term_var_of_kind ( alias_term , self . at . cause . span ) ;
122
119
let obligation = Obligation :: new (
123
120
tcx,
124
121
self . at . cause . clone ( ) ,
125
122
self . at . param_env ,
126
123
ty:: PredicateKind :: AliasRelate (
127
- alias_ty . into ( ) ,
128
- new_infer_ty . into ( ) ,
124
+ alias_term . into ( ) ,
125
+ infer_term . into ( ) ,
129
126
ty:: AliasRelationDirection :: Equate ,
130
127
) ,
131
128
) ;
@@ -135,50 +132,13 @@ where
135
132
136
133
// Alias is guaranteed to be fully structurally resolved,
137
134
// so we can super fold here.
138
- let ty = infcx. resolve_vars_if_possible ( new_infer_ty) ;
139
- let result = ty. try_super_fold_with ( self ) ?;
140
- self . depth -= 1 ;
141
- Ok ( result)
142
- }
143
-
144
- fn normalize_unevaluated_const (
145
- & mut self ,
146
- uv : ty:: UnevaluatedConst < ' tcx > ,
147
- ) -> Result < ty:: Const < ' tcx > , Vec < E > > {
148
- let infcx = self . at . infcx ;
149
- let tcx = infcx. tcx ;
150
- let recursion_limit = tcx. recursion_limit ( ) ;
151
- if !recursion_limit. value_within_limit ( self . depth ) {
152
- self . at . infcx . err_ctxt ( ) . report_overflow_error (
153
- OverflowCause :: DeeplyNormalize ( uv. into ( ) ) ,
154
- self . at . cause . span ,
155
- true ,
156
- |_| { } ,
157
- ) ;
158
- }
159
-
160
- self . depth += 1 ;
161
-
162
- let new_infer_ct = infcx. next_const_var ( self . at . cause . span ) ;
163
- let obligation = Obligation :: new (
164
- tcx,
165
- self . at . cause . clone ( ) ,
166
- self . at . param_env ,
167
- ty:: NormalizesTo { alias : uv. into ( ) , term : new_infer_ct. into ( ) } ,
168
- ) ;
169
-
170
- let result = if infcx. predicate_may_hold ( & obligation) {
171
- self . fulfill_cx . register_predicate_obligation ( infcx, obligation) ;
172
- let errors = self . fulfill_cx . select_where_possible ( infcx) ;
173
- if !errors. is_empty ( ) {
174
- return Err ( errors) ;
175
- }
176
- let ct = infcx. resolve_vars_if_possible ( new_infer_ct) ;
177
- ct. try_fold_with ( self ) ?
178
- } else {
179
- ty:: Const :: new_unevaluated ( tcx, uv) . try_super_fold_with ( self ) ?
135
+ let term = infcx. resolve_vars_if_possible ( infer_term) ;
136
+ // super-folding the `term` will directly fold the `Ty` or `Const` so
137
+ // we have to match on the term and super-fold them manually.
138
+ let result = match term. kind ( ) {
139
+ ty:: TermKind :: Ty ( ty) => ty. try_super_fold_with ( self ) ?. into ( ) ,
140
+ ty:: TermKind :: Const ( ct) => ct. try_super_fold_with ( self ) ?. into ( ) ,
180
141
} ;
181
-
182
142
self . depth -= 1 ;
183
143
Ok ( result)
184
144
}
@@ -238,7 +198,8 @@ where
238
198
if ty. has_escaping_bound_vars ( ) {
239
199
let ( ty, mapped_regions, mapped_types, mapped_consts) =
240
200
BoundVarReplacer :: replace_bound_vars ( infcx, & mut self . universes , ty) ;
241
- let result = ensure_sufficient_stack ( || self . normalize_alias_ty ( ty) ) ?;
201
+ let result =
202
+ ensure_sufficient_stack ( || self . normalize_alias_term ( ty. into ( ) ) ) ?. expect_type ( ) ;
242
203
Ok ( PlaceholderReplacer :: replace_placeholders (
243
204
infcx,
244
205
mapped_regions,
@@ -248,7 +209,7 @@ where
248
209
result,
249
210
) )
250
211
} else {
251
- ensure_sufficient_stack ( || self . normalize_alias_ty ( ty) )
212
+ Ok ( ensure_sufficient_stack ( || self . normalize_alias_term ( ty. into ( ) ) ) ? . expect_type ( ) )
252
213
}
253
214
}
254
215
@@ -260,15 +221,13 @@ where
260
221
return Ok ( ct) ;
261
222
}
262
223
263
- let uv = match ct. kind ( ) {
264
- ty:: ConstKind :: Unevaluated ( ct) => ct,
265
- _ => return ct. try_super_fold_with ( self ) ,
266
- } ;
224
+ let ty:: ConstKind :: Unevaluated ( ..) = ct. kind ( ) else { return ct. try_super_fold_with ( self ) } ;
267
225
268
- if uv. has_escaping_bound_vars ( ) {
269
- let ( uv, mapped_regions, mapped_types, mapped_consts) =
270
- BoundVarReplacer :: replace_bound_vars ( infcx, & mut self . universes , uv) ;
271
- let result = ensure_sufficient_stack ( || self . normalize_unevaluated_const ( uv) ) ?;
226
+ if ct. has_escaping_bound_vars ( ) {
227
+ let ( ct, mapped_regions, mapped_types, mapped_consts) =
228
+ BoundVarReplacer :: replace_bound_vars ( infcx, & mut self . universes , ct) ;
229
+ let result =
230
+ ensure_sufficient_stack ( || self . normalize_alias_term ( ct. into ( ) ) ) ?. expect_const ( ) ;
272
231
Ok ( PlaceholderReplacer :: replace_placeholders (
273
232
infcx,
274
233
mapped_regions,
@@ -278,7 +237,7 @@ where
278
237
result,
279
238
) )
280
239
} else {
281
- ensure_sufficient_stack ( || self . normalize_unevaluated_const ( uv ) )
240
+ Ok ( ensure_sufficient_stack ( || self . normalize_alias_term ( ct . into ( ) ) ) ? . expect_const ( ) )
282
241
}
283
242
}
284
243
}
0 commit comments