Skip to content

Commit

Permalink
freeze -> static
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Feb 7, 2017
1 parent fd3bbfd commit 98cda6c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ impl<'tcx> Error for EvalError<'tcx> {
EvalError::TypeNotPrimitive(_) =>
"expected primitive type, got nonprimitive",
EvalError::ReallocatedStaticMemory =>
"tried to reallocate frozen memory",
"tried to reallocate static memory",
EvalError::DeallocatedStaticMemory =>
"tried to deallocate frozen memory",
"tried to deallocate static memory",
EvalError::Layout(_) =>
"rustc layout computation failed",
EvalError::UnterminatedCString(_) =>
Expand Down
10 changes: 4 additions & 6 deletions src/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
match frame.return_to_block {
StackPopCleanup::MarkStatic(mutable) => if let Lvalue::Global(id) = frame.return_lvalue {
let global_value = self.globals.get_mut(&id)
.expect("global should have been cached (freeze/static)");
.expect("global should have been cached (static)");
match global_value.value {
Value::ByRef(ptr) => self.memory.mark_static(ptr.alloc_id, mutable)?,
Value::ByVal(val) => if let PrimVal::Ptr(ptr) = val {
Expand All @@ -332,7 +332,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
assert!(global_value.mutable);
global_value.mutable = mutable;
} else {
bug!("StackPopCleanup::Freeze on: {:?}", frame.return_lvalue);
bug!("StackPopCleanup::MarkStatic on: {:?}", frame.return_lvalue);
},
StackPopCleanup::Goto(target) => self.goto_block(target),
StackPopCleanup::None => {},
Expand All @@ -343,10 +343,8 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
trace!("deallocating local");
self.memory.dump_alloc(ptr.alloc_id);
match self.memory.deallocate(ptr) {
// Any frozen memory means that it belongs to a constant or something referenced
// by a constant. We could alternatively check whether the alloc_id is frozen
// before calling deallocate, but this is much simpler and is probably the
// rare case.
// We could alternatively check whether the alloc_id is static before calling
// deallocate, but this is much simpler and is probably the rare case.
Ok(()) | Err(EvalError::DeallocatedStaticMemory) => {},
other => return other,
}
Expand Down
6 changes: 3 additions & 3 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct Allocation {
pub align: u64,
/// Whether the allocation may be modified.
/// Use the `mark_static` method of `Memory` to ensure that an error occurs, if the memory of this
/// allocation is modified in the future.
/// allocation is modified or deallocated in the future.
pub static_kind: StaticKind,
}

Expand Down Expand Up @@ -626,7 +626,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
impl<'a, 'tcx> Memory<'a, 'tcx> {
/// mark an allocation as static, either mutable or not
pub fn mark_static(&mut self, alloc_id: AllocId, mutable: bool) -> EvalResult<'tcx> {
// do not use `self.get_mut(alloc_id)` here, because we might have already frozen a
// do not use `self.get_mut(alloc_id)` here, because we might have already marked a
// sub-element or have circular pointers (e.g. `Rc`-cycles)
let relocations = match self.alloc_map.get_mut(&alloc_id) {
Some(&mut Allocation { ref mut relocations, static_kind: ref mut kind @ StaticKind::NotStatic, .. }) => {
Expand All @@ -636,7 +636,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
StaticKind::Immutable
};
// take out the relocations vector to free the borrow on self, so we can call
// freeze recursively
// mark recursively
mem::replace(relocations, Default::default())
},
None if alloc_id == NEVER_ALLOC_ID || alloc_id == ZST_ALLOC_ID => return Ok(()),
Expand Down
2 changes: 1 addition & 1 deletion tests/compile-fail/modifying_constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() {
let x = &1; // the `&1` is promoted to a constant, but it used to be that only the pointer is frozen, not the pointee
let x = &1; // the `&1` is promoted to a constant, but it used to be that only the pointer is marked static, not the pointee
let y = unsafe { &mut *(x as *const i32 as *mut i32) };
*y = 42; //~ ERROR tried to modify constant memory
assert_eq!(*x, 42);
Expand Down

0 comments on commit 98cda6c

Please sign in to comment.