1
- use super :: { ErrorHandled , EvalToConstValueResult , EvalToValTreeResult , GlobalId } ;
1
+ use super :: {
2
+ ErrorHandled , EvalToAllocationRawResult , EvalToConstValueResult , EvalToValTreeResult , GlobalId ,
3
+ } ;
2
4
3
5
use crate :: mir;
4
6
use crate :: query:: TyCtxtEnsure ;
@@ -13,7 +15,7 @@ use tracing::{debug, instrument};
13
15
14
16
impl < ' tcx > TyCtxt < ' tcx > {
15
17
/// 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
17
19
/// generic parameter is used within the constant `ErrorHandled::ToGeneric` will be returned.
18
20
#[ instrument( skip( self ) , level = "debug" ) ]
19
21
pub fn const_eval_poly ( self , def_id : DefId ) -> EvalToConstValueResult < ' tcx > {
@@ -27,6 +29,24 @@ impl<'tcx> TyCtxt<'tcx> {
27
29
let param_env = self . param_env ( def_id) . with_reveal_all_normalized ( self ) ;
28
30
self . const_eval_global_id ( param_env, cid, DUMMY_SP )
29
31
}
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
+
30
50
/// Resolves and evaluates a constant.
31
51
///
32
52
/// 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> {
177
197
178
198
impl < ' tcx > TyCtxtEnsure < ' tcx > {
179
199
/// 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
181
201
/// generic parameter is used within the constant `ErrorHandled::ToGeneric` will be returned.
182
202
#[ instrument( skip( self ) , level = "debug" ) ]
183
203
pub fn const_eval_poly ( self , def_id : DefId ) {
0 commit comments