From 6d1f0068d11188fd5497329f70aaf3a6a51d25da Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 13 Aug 2018 19:02:42 +0200 Subject: [PATCH] Implement nested and mutable statics (fixes #9) --- examples/mini_core.rs | 1 + examples/mini_core_hello_world.rs | 8 ++++++-- src/constant.rs | 21 +++++++++++++++++---- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/examples/mini_core.rs b/examples/mini_core.rs index 5ff9c93a975..d2a205856a9 100644 --- a/examples/mini_core.rs +++ b/examples/mini_core.rs @@ -44,6 +44,7 @@ unsafe impl Sync for i16 {} unsafe impl Sync for i32 {} unsafe impl Sync for isize {} unsafe impl Sync for char {} +unsafe impl<'a, T: ?Sized> Sync for &'a T {} #[lang = "freeze"] trait Freeze {} diff --git a/examples/mini_core_hello_world.rs b/examples/mini_core_hello_world.rs index e077a37b5dc..ffda837fac2 100644 --- a/examples/mini_core_hello_world.rs +++ b/examples/mini_core_hello_world.rs @@ -16,7 +16,8 @@ extern "C" { fn puts(s: *const u8); } -static NUM: u8 = 6 * 7; +static mut NUM: u8 = 6 * 7; +static NUM_REF: &'static u8 = unsafe { &NUM }; #[lang = "start"] fn start(_main: *const u8, i: isize, _: *const *const u8) -> isize { @@ -25,5 +26,8 @@ fn start(_main: *const u8, i: isize, _: *const *const u8) -> isize { puts(ptr); } - NUM as isize + unsafe { + NUM = 43; + *NUM_REF as isize + } } diff --git a/src/constant.rs b/src/constant.rs index 06a06b0d450..7a88b78af05 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -1,6 +1,7 @@ use cranelift_module::*; use crate::prelude::*; -use rustc::mir::interpret::{read_target_uint, AllocId, ConstValue, GlobalId}; +use syntax::ast::Mutability as AstMutability; +use rustc::mir::interpret::{read_target_uint, AllocId, AllocType, ConstValue, GlobalId}; use rustc::ty::Const; use rustc_mir::interpret::{CompileTimeEvaluator, Memory}; @@ -191,12 +192,24 @@ fn define_all_allocs<'a, 'tcx: 'a, B: Backend + 'a>( data_ctx.define( alloc.bytes.to_vec().into_boxed_slice(), - Writability::Readonly, + match alloc.runtime_mutability { + AstMutability::Mutable => Writability::Writable, + AstMutability::Immutable => Writability::Readonly, + }, ); for &(offset, reloc) in alloc.relocations.iter() { - cx.todo.insert(TodoItem::Alloc(reloc)); - let data_id = data_id_for_alloc_id(module, reloc); + let data_id = match tcx.alloc_map.lock().get(reloc).unwrap() { + AllocType::Memory(_) => { + cx.todo.insert(TodoItem::Alloc(reloc)); + data_id_for_alloc_id(module, reloc) + } + AllocType::Function(_) => unimplemented!("function static reference"), + AllocType::Static(def_id) => { + cx.todo.insert(TodoItem::Static(def_id)); + data_id_for_static(tcx, module, def_id) + } + }; let reloc_offset = { let endianness = memory.endianness();