1- use super :: { ErrorHandled , EvalToConstValueResult , EvalToValTreeResult , GlobalId } ;
1+ use super :: {
2+ ErrorHandled , EvalToAllocationRawResult , EvalToConstValueResult , EvalToValTreeResult , GlobalId ,
3+ } ;
24
35use crate :: mir;
46use crate :: query:: TyCtxtEnsure ;
@@ -13,7 +15,7 @@ use tracing::{debug, instrument};
1315
1416impl < ' tcx > TyCtxt < ' tcx > {
1517 /// Evaluates a constant without providing any generic parameters. This is useful to evaluate consts
16- /// that can't take any generic arguments like statics, const items or enum discriminants. If a
18+ /// that can't take any generic arguments like const items or enum discriminants. If a
1719 /// generic parameter is used within the constant `ErrorHandled::ToGeneric` will be returned.
1820 #[ instrument( skip( self ) , level = "debug" ) ]
1921 pub fn const_eval_poly ( self , def_id : DefId ) -> EvalToConstValueResult < ' tcx > {
@@ -27,6 +29,24 @@ impl<'tcx> TyCtxt<'tcx> {
2729 let param_env = self . param_env ( def_id) . with_reveal_all_normalized ( self ) ;
2830 self . const_eval_global_id ( param_env, cid, DUMMY_SP )
2931 }
32+
33+ /// Evaluates a constant without providing any generic parameters. This is useful to evaluate consts
34+ /// that can't take any generic arguments like const items or enum discriminants. If a
35+ /// generic parameter is used within the constant `ErrorHandled::ToGeneric` will be returned.
36+ #[ instrument( skip( self ) , level = "debug" ) ]
37+ pub fn const_eval_poly_to_alloc ( self , def_id : DefId ) -> EvalToAllocationRawResult < ' tcx > {
38+ // In some situations def_id will have generic parameters within scope, but they aren't allowed
39+ // to be used. So we can't use `Instance::mono`, instead we feed unresolved generic parameters
40+ // into `const_eval` which will return `ErrorHandled::ToGeneric` if any of them are
41+ // encountered.
42+ let args = GenericArgs :: identity_for_item ( self , def_id) ;
43+ let instance = ty:: Instance :: new ( def_id, args) ;
44+ let cid = GlobalId { instance, promoted : None } ;
45+ let param_env = self . param_env ( def_id) . with_reveal_all_normalized ( self ) ;
46+ let inputs = self . erase_regions ( param_env. and ( cid) ) ;
47+ self . eval_to_allocation_raw ( inputs)
48+ }
49+
3050 /// Resolves and evaluates a constant.
3151 ///
3252 /// The constant can be located on a trait like `<A as B>::C`, in which case the given
@@ -177,7 +197,7 @@ impl<'tcx> TyCtxt<'tcx> {
177197
178198impl < ' tcx > TyCtxtEnsure < ' tcx > {
179199 /// Evaluates a constant without providing any generic parameters. This is useful to evaluate consts
180- /// that can't take any generic arguments like statics, const items or enum discriminants. If a
200+ /// that can't take any generic arguments like const items or enum discriminants. If a
181201 /// generic parameter is used within the constant `ErrorHandled::ToGeneric` will be returned.
182202 #[ instrument( skip( self ) , level = "debug" ) ]
183203 pub fn const_eval_poly ( self , def_id : DefId ) {
0 commit comments