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 ;
@@ -12,7 +14,7 @@ use rustc_span::{Span, DUMMY_SP};
12
14
13
15
impl < ' tcx > TyCtxt < ' tcx > {
14
16
/// Evaluates a constant without providing any generic parameters. This is useful to evaluate consts
15
- /// that can't take any generic arguments like statics, const items or enum discriminants. If a
17
+ /// that can't take any generic arguments like const items or enum discriminants. If a
16
18
/// generic parameter is used within the constant `ErrorHandled::ToGeneric` will be returned.
17
19
#[ instrument( skip( self ) , level = "debug" ) ]
18
20
pub fn const_eval_poly ( self , def_id : DefId ) -> EvalToConstValueResult < ' tcx > {
@@ -26,6 +28,25 @@ impl<'tcx> TyCtxt<'tcx> {
26
28
let param_env = self . param_env ( def_id) . with_reveal_all_normalized ( self ) ;
27
29
self . const_eval_global_id ( param_env, cid, None )
28
30
}
31
+
32
+ /// Evaluates a constant without providing any generic parameters. This is useful to evaluate consts
33
+ /// that can't take any generic arguments like const items or enum discriminants. If a
34
+ /// generic parameter is used within the constant `ErrorHandled::ToGeneric` will be returned.
35
+ #[ instrument( skip( self ) , level = "debug" ) ]
36
+ pub fn const_eval_poly_to_alloc ( self , def_id : DefId ) -> EvalToAllocationRawResult < ' tcx > {
37
+ // In some situations def_id will have generic parameters within scope, but they aren't allowed
38
+ // to be used. So we can't use `Instance::mono`, instead we feed unresolved generic parameters
39
+ // into `const_eval` which will return `ErrorHandled::ToGeneric` if any of them are
40
+ // encountered.
41
+ let args = GenericArgs :: identity_for_item ( self , def_id) ;
42
+ let instance = ty:: Instance :: new ( def_id, args) ;
43
+ let cid = GlobalId { instance, promoted : None } ;
44
+ let param_env = self . param_env ( def_id) . with_reveal_all_normalized ( self ) ;
45
+ let inputs = self . erase_regions ( param_env. and ( cid) ) ;
46
+ // The query doesn't know where it is being invoked, so we need to fix the span.
47
+ self . eval_to_allocation_raw ( inputs)
48
+ }
49
+
29
50
/// Resolves and evaluates a constant.
30
51
///
31
52
/// The constant can be located on a trait like `<A as B>::C`, in which case the given
@@ -177,7 +198,7 @@ impl<'tcx> TyCtxt<'tcx> {
177
198
178
199
impl < ' tcx > TyCtxtEnsure < ' tcx > {
179
200
/// 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
201
+ /// that can't take any generic arguments like const items or enum discriminants. If a
181
202
/// generic parameter is used within the constant `ErrorHandled::ToGeneric` will be returned.
182
203
#[ instrument( skip( self ) , level = "debug" ) ]
183
204
pub fn const_eval_poly ( self , def_id : DefId ) {
0 commit comments