@@ -3,9 +3,9 @@ use super::{ErrorHandled, EvalToConstValueResult, GlobalId};
3
3
use crate :: mir;
4
4
use crate :: ty:: fold:: TypeFoldable ;
5
5
use crate :: ty:: subst:: InternalSubsts ;
6
- use crate :: ty:: { self , TyCtxt } ;
6
+ use crate :: ty:: { self , query :: TyCtxtAt , TyCtxt } ;
7
7
use rustc_hir:: def_id:: DefId ;
8
- use rustc_span:: Span ;
8
+ use rustc_span:: { Span , DUMMY_SP } ;
9
9
10
10
impl < ' tcx > TyCtxt < ' tcx > {
11
11
/// Evaluates a constant without providing any substitutions. This is useful to evaluate consts
@@ -86,14 +86,25 @@ impl<'tcx> TyCtxt<'tcx> {
86
86
}
87
87
}
88
88
89
+ /// Evaluate a static's initializer, returning the allocation of the initializer's memory.
90
+ #[ inline( always) ]
91
+ pub fn eval_static_initializer (
92
+ self ,
93
+ def_id : DefId ,
94
+ ) -> Result < mir:: ConstAllocation < ' tcx > , ErrorHandled > {
95
+ self . at ( DUMMY_SP ) . eval_static_initializer ( def_id)
96
+ }
97
+ }
98
+
99
+ impl < ' tcx > TyCtxtAt < ' tcx > {
89
100
/// Evaluate a static's initializer, returning the allocation of the initializer's memory.
90
101
pub fn eval_static_initializer (
91
102
self ,
92
103
def_id : DefId ,
93
104
) -> Result < mir:: ConstAllocation < ' tcx > , ErrorHandled > {
94
105
trace ! ( "eval_static_initializer: Need to compute {:?}" , def_id) ;
95
106
assert ! ( self . is_static( def_id) ) ;
96
- let instance = ty:: Instance :: mono ( self , def_id) ;
107
+ let instance = ty:: Instance :: mono ( * self , def_id) ;
97
108
let gid = GlobalId { instance, promoted : None } ;
98
109
self . eval_to_allocation ( gid, ty:: ParamEnv :: reveal_all ( ) )
99
110
}
@@ -109,7 +120,9 @@ impl<'tcx> TyCtxt<'tcx> {
109
120
let raw_const = self . eval_to_allocation_raw ( param_env. and ( gid) ) ?;
110
121
Ok ( self . global_alloc ( raw_const. alloc_id ) . unwrap_memory ( ) )
111
122
}
123
+ }
112
124
125
+ impl < ' tcx > TyCtxt < ' tcx > {
113
126
/// Destructure a type-level constant ADT or array into its variant index and its field values.
114
127
/// Panics if the destructuring fails, use `try_destructure_const` for fallible version.
115
128
pub fn destructure_const (
0 commit comments