Skip to content

Commit 7b07fc3

Browse files
committed
get_or_create_thread_local_alloc_id: share code with Memory::get_global_alloc
1 parent 0a4e8ca commit 7b07fc3

File tree

1 file changed

+3
-21
lines changed

1 file changed

+3
-21
lines changed

src/thread.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use log::trace;
1111
use rustc_data_structures::fx::FxHashMap;
1212
use rustc_hir::def_id::DefId;
1313
use rustc_index::vec::{Idx, IndexVec};
14-
use rustc_middle::ty::{self, Instance};
1514

1615
use crate::sync::SynchronizationState;
1716
use crate::*;
@@ -497,9 +496,6 @@ impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mi
497496
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
498497
/// Get a thread-specific allocation id for the given thread-local static.
499498
/// If needed, allocate a new one.
500-
///
501-
/// FIXME: This method should be replaced as soon as
502-
/// https://github.com/rust-lang/rust/issues/70685 gets fixed.
503499
fn get_or_create_thread_local_alloc_id(&self, def_id: DefId) -> InterpResult<'tcx, AllocId> {
504500
let this = self.eval_context_ref();
505501
let tcx = this.tcx;
@@ -511,29 +507,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
511507
// We need to allocate a thread-specific allocation id for this
512508
// thread-local static.
513509
//
514-
// At first, we invoke the `const_eval_raw` query and extract the
515-
// allocation from it. Unfortunately, we have to duplicate the code
516-
// from `Memory::get_global_alloc` that does this.
517-
//
510+
// At first, we compute the initial value for this static.
518511
// Then we store the retrieved allocation back into the `alloc_map`
519512
// to get a fresh allocation id, which we can use as a
520513
// thread-specific allocation id for the thread-local static.
514+
// On first access to that allocation, it will be copied over to the machine memory.
521515
if tcx.is_foreign_item(def_id) {
522516
throw_unsup_format!("foreign thread-local statics are not supported");
523517
}
524-
// Invoke the `const_eval_raw` query.
525-
let instance = Instance::mono(tcx.tcx, def_id);
526-
let gid = GlobalId { instance, promoted: None };
527-
let raw_const =
528-
tcx.const_eval_raw(ty::ParamEnv::reveal_all().and(gid)).map_err(|err| {
529-
// no need to report anything, the const_eval call takes care of that
530-
// for statics
531-
assert!(tcx.is_static(def_id));
532-
err
533-
})?;
534-
let id = raw_const.alloc_id;
535-
// Extract the allocation from the query result.
536-
let allocation = tcx.global_alloc(id).unwrap_memory();
518+
let allocation = interpret::get_static(*tcx, def_id)?;
537519
// Create a new allocation id for the same allocation in this hacky
538520
// way. Internally, `alloc_map` deduplicates allocations, but this
539521
// is fine because Miri will make a copy before a first mutable

0 commit comments

Comments
 (0)