Skip to content

Commit b8a40aa

Browse files
committed
memory: make getting the alloc for a static an associate function for easier calling
1 parent cdeef61 commit b8a40aa

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

src/librustc_mir/interpret/memory.rs

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -122,45 +122,6 @@ impl<'a, 'mir, 'tcx, M> Hash for Memory<'a, 'mir, 'tcx, M>
122122
}
123123
}
124124

125-
/// Helper function to obtain the global (tcx) allocation for a static
126-
fn const_eval_static<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>>(
127-
tcx: TyCtxtAt<'a, 'tcx, 'tcx>,
128-
id: AllocId
129-
) -> EvalResult<'tcx, &'tcx Allocation> {
130-
let alloc = tcx.alloc_map.lock().get(id);
131-
let def_id = match alloc {
132-
Some(AllocType::Memory(mem)) => {
133-
return Ok(mem)
134-
}
135-
Some(AllocType::Function(..)) => {
136-
return err!(DerefFunctionPointer)
137-
}
138-
Some(AllocType::Static(did)) => {
139-
did
140-
}
141-
None =>
142-
return err!(DanglingPointerDeref),
143-
};
144-
// We got a "lazy" static that has not been computed yet, do some work
145-
trace!("static_alloc: Need to compute {:?}", def_id);
146-
if tcx.is_foreign_item(def_id) {
147-
return M::find_foreign_static(tcx, def_id);
148-
}
149-
let instance = Instance::mono(tcx.tcx, def_id);
150-
let gid = GlobalId {
151-
instance,
152-
promoted: None,
153-
};
154-
tcx.const_eval(ty::ParamEnv::reveal_all().and(gid)).map_err(|err| {
155-
// no need to report anything, the const_eval call takes care of that for statics
156-
assert!(tcx.is_static(def_id).is_some());
157-
EvalErrorKind::ReferencedConstant(err).into()
158-
}).map(|val| {
159-
// FIXME We got our static (will be a ByRef), now we make a *copy*?!?
160-
tcx.const_to_allocation(val)
161-
})
162-
}
163-
164125
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
165126
pub fn new(tcx: TyCtxtAt<'a, 'tcx, 'tcx>, data: M::MemoryData) -> Self {
166127
Memory {
@@ -343,13 +304,52 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
343304

344305
/// Allocation accessors
345306
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
307+
/// Helper function to obtain the global (tcx) allocation for a static
308+
fn get_static_alloc(
309+
tcx: TyCtxtAt<'a, 'tcx, 'tcx>,
310+
id: AllocId,
311+
) -> EvalResult<'tcx, &'tcx Allocation> {
312+
let alloc = tcx.alloc_map.lock().get(id);
313+
let def_id = match alloc {
314+
Some(AllocType::Memory(mem)) => {
315+
return Ok(mem)
316+
}
317+
Some(AllocType::Function(..)) => {
318+
return err!(DerefFunctionPointer)
319+
}
320+
Some(AllocType::Static(did)) => {
321+
did
322+
}
323+
None =>
324+
return err!(DanglingPointerDeref),
325+
};
326+
// We got a "lazy" static that has not been computed yet, do some work
327+
trace!("static_alloc: Need to compute {:?}", def_id);
328+
if tcx.is_foreign_item(def_id) {
329+
return M::find_foreign_static(tcx, def_id);
330+
}
331+
let instance = Instance::mono(tcx.tcx, def_id);
332+
let gid = GlobalId {
333+
instance,
334+
promoted: None,
335+
};
336+
tcx.const_eval(ty::ParamEnv::reveal_all().and(gid)).map_err(|err| {
337+
// no need to report anything, the const_eval call takes care of that for statics
338+
assert!(tcx.is_static(def_id).is_some());
339+
EvalErrorKind::ReferencedConstant(err).into()
340+
}).map(|val| {
341+
// FIXME We got our static (will be a ByRef), now we make a *copy*?!?
342+
tcx.const_to_allocation(val)
343+
})
344+
}
345+
346346
pub fn get(&self, id: AllocId) -> EvalResult<'tcx, &Allocation> {
347347
match self.alloc_map.get(&id) {
348348
// Normal alloc?
349349
Some(alloc) => Ok(&alloc.1),
350350
// Static. No need to make any copies, just provide read access to the global static
351351
// memory in tcx.
352-
None => const_eval_static::<M>(self.tcx, id),
352+
None => Self::get_static_alloc(self.tcx, id),
353353
}
354354
}
355355

@@ -381,7 +381,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
381381
if ptr.offset.bytes() != 0 {
382382
return err!(InvalidFunctionPointer);
383383
}
384-
debug!("reading fn ptr: {}", ptr.alloc_id);
384+
trace!("reading fn ptr: {}", ptr.alloc_id);
385385
match self.tcx.alloc_map.lock().get(ptr.alloc_id) {
386386
Some(AllocType::Function(instance)) => Ok(instance),
387387
_ => Err(EvalErrorKind::ExecuteMemory.into()),
@@ -610,7 +610,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
610610
id: AllocId,
611611
kind: MemoryKind<M::MemoryKinds>,
612612
) -> EvalResult<'tcx> {
613-
let alloc = const_eval_static::<M>(self.tcx, id)?;
613+
let alloc = Self::get_static_alloc(self.tcx, id)?;
614614
if alloc.mutability == Mutability::Immutable {
615615
return err!(ModifiedConstantMemory);
616616
}

0 commit comments

Comments
 (0)