1+ use std:: assert_matches:: debug_assert_matches;
2+
13use rustc_hir:: def:: DefKind ;
24use rustc_hir:: def_id:: DefId ;
35use rustc_session:: lint;
@@ -18,14 +20,18 @@ impl<'tcx> TyCtxt<'tcx> {
1820 /// generic parameter is used within the constant `ErrorHandled::TooGeneric` will be returned.
1921 #[ instrument( skip( self ) , level = "debug" ) ]
2022 pub fn const_eval_poly ( self , def_id : DefId ) -> EvalToConstValueResult < ' tcx > {
23+ debug_assert_matches ! (
24+ self . def_kind( def_id) ,
25+ DefKind :: Const | DefKind :: AnonConst | DefKind :: InlineConst
26+ ) ;
2127 // In some situations def_id will have generic parameters within scope, but they aren't allowed
2228 // to be used. So we can't use `Instance::mono`, instead we feed unresolved generic parameters
2329 // into `const_eval` which will return `ErrorHandled::TooGeneric` if any of them are
2430 // encountered.
2531 let args = GenericArgs :: identity_for_item ( self , def_id) ;
26- let instance = ty:: Instance :: new_raw ( def_id, args) ;
27- let cid = GlobalId { instance, promoted : None } ;
2832 let typing_env = ty:: TypingEnv :: post_analysis ( self , def_id) ;
33+ let instance = ty:: Instance :: expect_resolve ( self , typing_env, def_id, args, DUMMY_SP ) ;
34+ let cid = GlobalId { instance, promoted : None } ;
2935 self . const_eval_global_id ( typing_env, cid, DUMMY_SP )
3036 }
3137
@@ -34,14 +40,18 @@ impl<'tcx> TyCtxt<'tcx> {
3440 /// generic parameter is used within the constant `ErrorHandled::TooGeneric` will be returned.
3541 #[ instrument( skip( self ) , level = "debug" ) ]
3642 pub fn const_eval_poly_to_alloc ( self , def_id : DefId ) -> EvalToAllocationRawResult < ' tcx > {
43+ debug_assert_matches ! (
44+ self . def_kind( def_id) ,
45+ DefKind :: Const | DefKind :: AnonConst | DefKind :: InlineConst
46+ ) ;
3747 // In some situations def_id will have generic parameters within scope, but they aren't allowed
3848 // to be used. So we can't use `Instance::mono`, instead we feed unresolved generic parameters
3949 // into `const_eval` which will return `ErrorHandled::TooGeneric` if any of them are
4050 // encountered.
4151 let args = GenericArgs :: identity_for_item ( self , def_id) ;
42- let instance = ty:: Instance :: new_raw ( def_id, args) ;
43- let cid = GlobalId { instance, promoted : None } ;
4452 let typing_env = ty:: TypingEnv :: post_analysis ( self , def_id) ;
53+ let instance = ty:: Instance :: expect_resolve ( self , typing_env, def_id, args, DUMMY_SP ) ;
54+ let cid = GlobalId { instance, promoted : None } ;
4555 let inputs = self . erase_regions ( typing_env. as_query_input ( cid) ) ;
4656 self . eval_to_allocation_raw ( inputs)
4757 }
@@ -203,14 +213,24 @@ impl<'tcx> TyCtxtEnsureOk<'tcx> {
203213 /// generic parameter is used within the constant `ErrorHandled::TooGeneric` will be returned.
204214 #[ instrument( skip( self ) , level = "debug" ) ]
205215 pub fn const_eval_poly ( self , def_id : DefId ) {
216+ debug_assert_matches ! (
217+ self . tcx. def_kind( def_id) ,
218+ DefKind :: Const | DefKind :: AnonConst | DefKind :: InlineConst
219+ ) ;
206220 // In some situations def_id will have generic parameters within scope, but they aren't allowed
207221 // to be used. So we can't use `Instance::mono`, instead we feed unresolved generic parameters
208222 // into `const_eval` which will return `ErrorHandled::TooGeneric` if any of them are
209223 // encountered.
210224 let args = GenericArgs :: identity_for_item ( self . tcx , def_id) ;
211- let instance = ty:: Instance :: new_raw ( def_id, self . tcx . erase_regions ( args) ) ;
212- let cid = GlobalId { instance, promoted : None } ;
213225 let typing_env = ty:: TypingEnv :: post_analysis ( self . tcx , def_id) ;
226+ let instance = ty:: Instance :: expect_resolve (
227+ self . tcx ,
228+ typing_env,
229+ def_id,
230+ self . tcx . erase_regions ( args) ,
231+ DUMMY_SP ,
232+ ) ;
233+ let cid = GlobalId { instance, promoted : None } ;
214234 // Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
215235 // improve caching of queries.
216236 let inputs = self . tcx . erase_regions ( typing_env. as_query_input ( cid) ) ;
0 commit comments