@@ -3,9 +3,9 @@ use super::{ErrorHandled, EvalToConstValueResult, GlobalId};
33use crate :: mir;
44use crate :: ty:: fold:: TypeFoldable ;
55use crate :: ty:: subst:: InternalSubsts ;
6- use crate :: ty:: { self , TyCtxt } ;
6+ use crate :: ty:: { self , query :: TyCtxtAt , TyCtxt } ;
77use rustc_hir:: def_id:: DefId ;
8- use rustc_span:: Span ;
8+ use rustc_span:: { Span , DUMMY_SP } ;
99
1010impl < ' tcx > TyCtxt < ' tcx > {
1111 /// Evaluates a constant without providing any substitutions. This is useful to evaluate consts
@@ -86,14 +86,25 @@ impl<'tcx> TyCtxt<'tcx> {
8686 }
8787 }
8888
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 > {
89100 /// Evaluate a static's initializer, returning the allocation of the initializer's memory.
90101 pub fn eval_static_initializer (
91102 self ,
92103 def_id : DefId ,
93104 ) -> Result < mir:: ConstAllocation < ' tcx > , ErrorHandled > {
94105 trace ! ( "eval_static_initializer: Need to compute {:?}" , def_id) ;
95106 assert ! ( self . is_static( def_id) ) ;
96- let instance = ty:: Instance :: mono ( self , def_id) ;
107+ let instance = ty:: Instance :: mono ( * self , def_id) ;
97108 let gid = GlobalId { instance, promoted : None } ;
98109 self . eval_to_allocation ( gid, ty:: ParamEnv :: reveal_all ( ) )
99110 }
@@ -109,7 +120,9 @@ impl<'tcx> TyCtxt<'tcx> {
109120 let raw_const = self . eval_to_allocation_raw ( param_env. and ( gid) ) ?;
110121 Ok ( self . global_alloc ( raw_const. alloc_id ) . unwrap_memory ( ) )
111122 }
123+ }
112124
125+ impl < ' tcx > TyCtxt < ' tcx > {
113126 /// Destructure a type-level constant ADT or array into its variant index and its field values.
114127 /// Panics if the destructuring fails, use `try_destructure_const` for fallible version.
115128 pub fn destructure_const (
0 commit comments