Skip to content

Commit 6d1f006

Browse files
committed
Implement nested and mutable statics (fixes rust-lang#9)
1 parent 7f62fed commit 6d1f006

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

examples/mini_core.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ unsafe impl Sync for i16 {}
4444
unsafe impl Sync for i32 {}
4545
unsafe impl Sync for isize {}
4646
unsafe impl Sync for char {}
47+
unsafe impl<'a, T: ?Sized> Sync for &'a T {}
4748

4849
#[lang = "freeze"]
4950
trait Freeze {}

examples/mini_core_hello_world.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ extern "C" {
1616
fn puts(s: *const u8);
1717
}
1818

19-
static NUM: u8 = 6 * 7;
19+
static mut NUM: u8 = 6 * 7;
20+
static NUM_REF: &'static u8 = unsafe { &NUM };
2021

2122
#[lang = "start"]
2223
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 {
2526
puts(ptr);
2627
}
2728

28-
NUM as isize
29+
unsafe {
30+
NUM = 43;
31+
*NUM_REF as isize
32+
}
2933
}

src/constant.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use cranelift_module::*;
22
use crate::prelude::*;
3-
use rustc::mir::interpret::{read_target_uint, AllocId, ConstValue, GlobalId};
3+
use syntax::ast::Mutability as AstMutability;
4+
use rustc::mir::interpret::{read_target_uint, AllocId, AllocType, ConstValue, GlobalId};
45
use rustc::ty::Const;
56
use rustc_mir::interpret::{CompileTimeEvaluator, Memory};
67

@@ -191,12 +192,24 @@ fn define_all_allocs<'a, 'tcx: 'a, B: Backend + 'a>(
191192

192193
data_ctx.define(
193194
alloc.bytes.to_vec().into_boxed_slice(),
194-
Writability::Readonly,
195+
match alloc.runtime_mutability {
196+
AstMutability::Mutable => Writability::Writable,
197+
AstMutability::Immutable => Writability::Readonly,
198+
},
195199
);
196200

197201
for &(offset, reloc) in alloc.relocations.iter() {
198-
cx.todo.insert(TodoItem::Alloc(reloc));
199-
let data_id = data_id_for_alloc_id(module, reloc);
202+
let data_id = match tcx.alloc_map.lock().get(reloc).unwrap() {
203+
AllocType::Memory(_) => {
204+
cx.todo.insert(TodoItem::Alloc(reloc));
205+
data_id_for_alloc_id(module, reloc)
206+
}
207+
AllocType::Function(_) => unimplemented!("function static reference"),
208+
AllocType::Static(def_id) => {
209+
cx.todo.insert(TodoItem::Static(def_id));
210+
data_id_for_static(tcx, module, def_id)
211+
}
212+
};
200213

201214
let reloc_offset = {
202215
let endianness = memory.endianness();

0 commit comments

Comments
 (0)