1- use std:: assert_matches:: assert_matches;
21use std:: fmt:: Debug ;
32
43use rustc_data_structures:: stack:: ensure_sufficient_stack;
@@ -16,7 +15,6 @@ use tracing::instrument;
1615use super :: { FulfillmentCtxt , NextSolverError } ;
1716use crate :: error_reporting:: InferCtxtErrorExt ;
1817use crate :: error_reporting:: traits:: OverflowCause ;
19- use crate :: traits:: query:: evaluate_obligation:: InferCtxtExt ;
2018use crate :: traits:: { BoundVarReplacer , PlaceholderReplacer , ScrubbedTraitError } ;
2119
2220/// Deeply normalize all aliases in `value`. This does not handle inference and expects
@@ -97,19 +95,18 @@ impl<'tcx, E> NormalizationFolder<'_, 'tcx, E>
9795where
9896 E : FromSolverError < ' tcx , NextSolverError < ' tcx > > ,
9997{
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 > > {
103102 let infcx = self . at . infcx ;
104103 let tcx = infcx. tcx ;
105104 let recursion_limit = tcx. recursion_limit ( ) ;
106105 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 ( ) ;
110107
111108 self . at . infcx . err_ctxt ( ) . report_overflow_error (
112- OverflowCause :: DeeplyNormalize ( data . into ( ) ) ,
109+ OverflowCause :: DeeplyNormalize ( term ) ,
113110 self . at . cause . span ,
114111 true ,
115112 |_| { } ,
@@ -118,14 +115,14 @@ where
118115
119116 self . depth += 1 ;
120117
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 ) ;
122119 let obligation = Obligation :: new (
123120 tcx,
124121 self . at . cause . clone ( ) ,
125122 self . at . param_env ,
126123 ty:: PredicateKind :: AliasRelate (
127- alias_ty . into ( ) ,
128- new_infer_ty . into ( ) ,
124+ alias_term . into ( ) ,
125+ infer_term . into ( ) ,
129126 ty:: AliasRelationDirection :: Equate ,
130127 ) ,
131128 ) ;
@@ -135,50 +132,13 @@ where
135132
136133 // Alias is guaranteed to be fully structurally resolved,
137134 // 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 ( ) ,
180141 } ;
181-
182142 self . depth -= 1 ;
183143 Ok ( result)
184144 }
@@ -238,7 +198,8 @@ where
238198 if ty. has_escaping_bound_vars ( ) {
239199 let ( ty, mapped_regions, mapped_types, mapped_consts) =
240200 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 ( ) ;
242203 Ok ( PlaceholderReplacer :: replace_placeholders (
243204 infcx,
244205 mapped_regions,
@@ -248,7 +209,7 @@ where
248209 result,
249210 ) )
250211 } else {
251- ensure_sufficient_stack ( || self . normalize_alias_ty ( ty) )
212+ Ok ( ensure_sufficient_stack ( || self . normalize_alias_term ( ty. into ( ) ) ) ? . expect_type ( ) )
252213 }
253214 }
254215
@@ -260,15 +221,13 @@ where
260221 return Ok ( ct) ;
261222 }
262223
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 ) } ;
267225
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 ( ) ;
272231 Ok ( PlaceholderReplacer :: replace_placeholders (
273232 infcx,
274233 mapped_regions,
@@ -278,7 +237,7 @@ where
278237 result,
279238 ) )
280239 } else {
281- ensure_sufficient_stack ( || self . normalize_unevaluated_const ( uv ) )
240+ Ok ( ensure_sufficient_stack ( || self . normalize_alias_term ( ct . into ( ) ) ) ? . expect_const ( ) )
282241 }
283242 }
284243}
0 commit comments