Skip to content

Commit a5a751d

Browse files
committed
get rid of FinishStatic hack from stack clenaup; const_eval can do that itself
1 parent b8f8fe9 commit a5a751d

File tree

4 files changed

+28
-42
lines changed

4 files changed

+28
-42
lines changed

src/librustc_mir/const_eval.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,6 @@ fn eval_body_using_ecx<'a, 'mir, 'tcx>(
157157
let layout = ecx.layout_of(mir.return_ty().subst(tcx, cid.instance.substs))?;
158158
assert!(!layout.is_unsized());
159159
let ret = ecx.allocate(layout, MemoryKind::Stack)?;
160-
let internally_mutable = !layout.ty.is_freeze(tcx, param_env, mir.span);
161-
let is_static = tcx.is_static(cid.instance.def_id());
162-
let mutability = if is_static == Some(hir::Mutability::MutMutable) || internally_mutable {
163-
Mutability::Mutable
164-
} else {
165-
Mutability::Immutable
166-
};
167-
let cleanup = StackPopCleanup::FinishStatic(mutability);
168160

169161
let name = ty::tls::with(|tcx| tcx.item_path_str(cid.instance.def_id()));
170162
let prom = cid.promoted.map_or(String::new(), |p| format!("::promoted[{:?}]", p));
@@ -175,12 +167,22 @@ fn eval_body_using_ecx<'a, 'mir, 'tcx>(
175167
mir.span,
176168
mir,
177169
Place::Ptr(*ret),
178-
cleanup,
170+
StackPopCleanup::None { cleanup: false },
179171
)?;
180172

181173
// The main interpreter loop.
182174
ecx.run()?;
183175

176+
// Intern the result
177+
let internally_mutable = !layout.ty.is_freeze(tcx, param_env, mir.span);
178+
let is_static = tcx.is_static(cid.instance.def_id());
179+
let mutability = if is_static == Some(hir::Mutability::MutMutable) || internally_mutable {
180+
Mutability::Mutable
181+
} else {
182+
Mutability::Immutable
183+
};
184+
ecx.memory.intern_static(ret.ptr.to_ptr()?.alloc_id, mutability)?;
185+
184186
debug!("eval_body_using_ecx done: {:?}", *ret);
185187
Ok(ret.into())
186188
}

src/librustc_mir/interpret/eval_context.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use rustc::mir::interpret::{
3232
};
3333

3434
use syntax::source_map::{self, Span};
35-
use syntax::ast::Mutability;
3635

3736
use super::{
3837
Value, Operand, MemPlace, MPlaceTy, Place, PlaceExtra,
@@ -159,15 +158,14 @@ impl<'mir, 'tcx: 'mir> Hash for Frame<'mir, 'tcx> {
159158

160159
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
161160
pub enum StackPopCleanup {
162-
/// The stackframe existed to compute the initial value of a static/constant.
163-
/// Call `M::intern_static` on the return value and all allocations it references
164-
/// when this is done. Must have a valid pointer as return place.
165-
FinishStatic(Mutability),
166161
/// Jump to the next block in the caller, or cause UB if None (that's a function
167162
/// that may never return).
168163
Goto(Option<mir::BasicBlock>),
169-
/// Just do nohing: Used by Main and for the box_alloc hook in miri
170-
None,
164+
/// Just do nohing: Used by Main and for the box_alloc hook in miri.
165+
/// `cleanup` says whether locals are deallocated. Static computation
166+
/// wants them leaked to intern what they need (and just throw away
167+
/// the entire `ecx` when it is done).
168+
None { cleanup: bool },
171169
}
172170

173171
// State of a local variable
@@ -631,18 +629,15 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
631629
"tried to pop a stack frame, but there were none",
632630
);
633631
match frame.return_to_block {
634-
StackPopCleanup::FinishStatic(mutability) => {
635-
let mplace = frame.return_place.to_mem_place();
636-
// to_ptr should be okay here; it is the responsibility of whoever pushed
637-
// this frame to make sure that this works.
638-
let ptr = mplace.ptr.to_ptr()?;
639-
assert_eq!(ptr.offset.bytes(), 0);
640-
self.memory.mark_static_initialized(ptr.alloc_id, mutability)?;
641-
}
642632
StackPopCleanup::Goto(block) => {
643633
self.goto_block(block)?;
644634
}
645-
StackPopCleanup::None => { }
635+
StackPopCleanup::None { cleanup } => {
636+
if !cleanup {
637+
// Leak the locals
638+
return Ok(());
639+
}
640+
}
646641
}
647642
// deallocate all locals that are backed by an allocation
648643
for local in frame.locals {

src/librustc_mir/interpret/memory.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -570,22 +570,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
570570

571571
/// Reading and writing
572572
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
573-
/// mark an allocation pointed to by a static as static and initialized
574-
fn mark_inner_allocation_initialized(
575-
&mut self,
576-
alloc: AllocId,
577-
mutability: Mutability,
578-
) -> EvalResult<'tcx> {
579-
match self.alloc_map.contains_key(&alloc) {
580-
// already interned
581-
false => Ok(()),
582-
// this still needs work
583-
true => self.mark_static_initialized(alloc, mutability),
584-
}
585-
}
586-
587573
/// mark an allocation as static and initialized, either mutable or not
588-
pub fn mark_static_initialized(
574+
pub fn intern_static(
589575
&mut self,
590576
alloc_id: AllocId,
591577
mutability: Mutability,
@@ -613,7 +599,10 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
613599
// at references. So whenever we follow a reference, we should likely
614600
// assume immutability -- and we should make sure that the compiler
615601
// does not permit code that would break this!
616-
self.mark_inner_allocation_initialized(alloc, mutability)?;
602+
if self.alloc_map.contains_key(&alloc) {
603+
// Not yet interned, so proceed recursively
604+
self.intern_static(alloc, mutability)?;
605+
}
617606
}
618607
Ok(())
619608
}

src/librustc_mir/interpret/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
6868
}
6969
}
7070

71-
self.memory.mark_static_initialized(
71+
self.memory.intern_static(
7272
vtable.alloc_id,
7373
Mutability::Immutable,
7474
)?;

0 commit comments

Comments
 (0)