@@ -14,6 +14,44 @@ enum Evaluated<'tcx> {
1414 Value ( mir:: ConstValue , Ty < ' tcx > ) ,
1515}
1616
17+ impl GlobalRef {
18+ /// The mutability of this global.
19+ pub fn mutability ( self , tcx : TyCtxt < ' _ > ) -> mir:: Mutability {
20+ match self {
21+ GlobalRef :: Static ( def_id) => tcx. static_mutability ( def_id) . unwrap ( ) ,
22+ GlobalRef :: Alloc ( _) => self . memory ( tcx) . unwrap ( ) . inner ( ) . mutability ,
23+ }
24+ }
25+
26+ /// The memory of this global. Returns `None` for a static whose evaluation fails, or that has
27+ /// no initializer (in an `extern` block).
28+ pub fn memory < ' tcx > ( self , tcx : TyCtxt < ' tcx > ) -> Option < interpret:: ConstAllocation < ' tcx > > {
29+ let def_id = match self {
30+ GlobalRef :: Static ( def_id) if tcx. is_foreign_item ( def_id) => return None ,
31+ GlobalRef :: Static ( def_id) => def_id,
32+ GlobalRef :: Alloc ( alloc_id) => match tcx. global_alloc ( alloc_id) {
33+ interpret:: GlobalAlloc :: Memory ( alloc) => return Some ( alloc) ,
34+ // A nested static.
35+ interpret:: GlobalAlloc :: Static ( def_id) => def_id,
36+ alloc => unreachable ! ( "unexpected GlobalRef::Alloc: {alloc:?}" ) ,
37+ } ,
38+ } ;
39+ tcx. eval_static_initializer ( def_id) . ok ( )
40+ }
41+ }
42+
43+ /// The type at which we view an allocation without a corresponding item: `MaybeUninit<[u8; N]>`.
44+ pub fn alloc_as_untyped_ty < ' tcx > ( tcx : TyCtxt < ' tcx > , alloc_id : interpret:: AllocId ) -> Ty < ' tcx > {
45+ let size = GlobalRef :: Alloc ( alloc_id)
46+ . memory ( tcx)
47+ . unwrap ( )
48+ . inner ( )
49+ . size ( ) ;
50+ let bytes = Ty :: new_array ( tcx, tcx. types . u8 , size. bytes ( ) ) ;
51+ let maybe_uninit = tcx. require_lang_item ( LangItem :: MaybeUninit , rustc_span:: DUMMY_SP ) ;
52+ Ty :: new_adt ( tcx, tcx. adt_def ( maybe_uninit) , tcx. mk_args ( & [ bytes. into ( ) ] ) )
53+ }
54+
1755/// Call `f` on the MIR of the given promoted constant.
1856pub fn promoted_body < ' tcx , R > (
1957 tcx : TyCtxt < ' tcx > ,
@@ -131,35 +169,22 @@ impl<'tcx> ConstReader<'tcx> {
131169 Evaluated :: Value ( val, instantiate ( ty, args) . skip_normalization ( ) )
132170 }
133171 ConstSource :: Global ( global) => {
134- let alloc_id = match global {
135- // Statics in `extern` blocks have no initializer.
136- GlobalRef :: Static ( def_id) if tcx. is_foreign_item ( def_id) => {
137- return Err ( ReadError :: NotEvaluable ) ;
138- }
139- GlobalRef :: Static ( def_id) => {
140- let alloc = tcx
141- . eval_static_initializer ( def_id)
142- . map_err ( |_| ReadError :: NotEvaluable ) ?;
143- // A static whose type has interior mutability gets a mutable allocation,
144- // which const-eval refuses to read. We don't care though, so we reintern
145- // it as immutable.
146- let alloc = if alloc. inner ( ) . mutability . is_mut ( ) {
147- let mut alloc = alloc. inner ( ) . clone ( ) ;
148- alloc. mutability = mir:: Mutability :: Not ;
149- tcx. mk_const_alloc ( alloc)
150- } else {
151- alloc
152- } ;
153- // `eval_static_initializer` returns an interned allocation without an
154- // `AllocId`; give it one so we can read it like any other allocation.
155- // This creates a fresh `AllocId` each time we read the static.
156- // FIXME: this breaks sharing.
157- tcx. reserve_and_set_memory_alloc ( alloc)
158- }
159- GlobalRef :: Alloc ( alloc_id) => alloc_id,
172+ let alloc = global. memory ( tcx) . ok_or ( ReadError :: NotEvaluable ) ?;
173+ // A mutable allocation (e.g. of a `static mut`, or of a static whose type has
174+ // interior mutability) can't be read by const-eval. We don't care though, so we
175+ // reintern it as immutable.
176+ let alloc = if alloc. inner ( ) . mutability . is_mut ( ) {
177+ let mut alloc = alloc. inner ( ) . clone ( ) ;
178+ alloc. mutability = mir:: Mutability :: Not ;
179+ tcx. mk_const_alloc ( alloc)
180+ } else {
181+ alloc
160182 } ;
183+ // Give the allocation a fresh `AllocId` so we can read it like any other
184+ // allocation. This creates a fresh `AllocId` each time we read the global. This
185+ // doesn't break sharing.
161186 let val = mir:: ConstValue :: Indirect {
162- alloc_id,
187+ alloc_id : tcx . reserve_and_set_memory_alloc ( alloc ) ,
163188 offset : rustc_abi:: Size :: ZERO ,
164189 } ;
165190 Evaluated :: Value ( val, self . global_ty ( global) )
@@ -207,15 +232,7 @@ impl<'tcx> ConstReader<'tcx> {
207232 . instantiate_identity ( )
208233 . skip_normalization ( ) ,
209234 ) ,
210- GlobalRef :: Alloc ( alloc_id) => {
211- let ( size, _) = tcx
212- . global_alloc ( alloc_id)
213- . size_and_align ( tcx, self . typing_env ) ;
214- let bytes = Ty :: new_array ( tcx, tcx. types . u8 , size. bytes ( ) ) ;
215- let maybe_uninit =
216- tcx. require_lang_item ( LangItem :: MaybeUninit , rustc_span:: DUMMY_SP ) ;
217- Ty :: new_adt ( tcx, tcx. adt_def ( maybe_uninit) , tcx. mk_args ( & [ bytes. into ( ) ] ) )
218- }
235+ GlobalRef :: Alloc ( alloc_id) => alloc_as_untyped_ty ( tcx, alloc_id) ,
219236 }
220237 }
221238
0 commit comments