Skip to content

Commit f13455a

Browse files
authored
Merge pull request rust-lang#340 from solson/babysteps
Remove `#[linkage(foo)]` statics from core miri
2 parents e8ea7da + 8cbfbf7 commit f13455a

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

miri/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,32 @@ impl<'tcx> Machine<'tcx> for Evaluator {
239239
.map(PrimVal::Ptr)
240240
}
241241
}
242+
243+
fn global_item_with_linkage<'a>(
244+
ecx: &mut EvalContext<'a, 'tcx, Self>,
245+
instance: ty::Instance<'tcx>,
246+
mutability: Mutability,
247+
) -> EvalResult<'tcx> {
248+
// FIXME: check that it's `#[linkage = "extern_weak"]`
249+
trace!("Initializing an extern global with NULL");
250+
let ptr_size = ecx.memory.pointer_size();
251+
let ptr = ecx.memory.allocate(
252+
ptr_size,
253+
ptr_size,
254+
MemoryKind::UninitializedStatic,
255+
)?;
256+
ecx.memory.write_ptr_sized_unsigned(ptr, PrimVal::Bytes(0))?;
257+
ecx.memory.mark_static_initalized(ptr.alloc_id, mutability)?;
258+
ecx.globals.insert(
259+
GlobalId {
260+
instance,
261+
promoted: None,
262+
},
263+
PtrAndAlign {
264+
ptr: ptr.into(),
265+
aligned: true,
266+
},
267+
);
268+
Ok(())
269+
}
242270
}

src/librustc_mir/interpret/const_eval.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,14 @@ impl<'tcx> super::Machine<'tcx> for CompileTimeFunctionEvaluator {
245245
ConstEvalError::NeedsRfc("Heap allocations via `box` keyword".to_string()).into(),
246246
)
247247
}
248+
249+
fn global_item_with_linkage<'a>(
250+
_ecx: &mut EvalContext<'a, 'tcx, Self>,
251+
_instance: ty::Instance<'tcx>,
252+
_mutability: Mutability,
253+
) -> EvalResult<'tcx> {
254+
Err(
255+
ConstEvalError::NotConst("statics with `linkage` attribute".to_string()).into(),
256+
)
257+
}
248258
}

src/librustc_mir/interpret/machine.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use super::{EvalResult, EvalContext, Lvalue, PrimVal, ValTy};
66

77
use rustc::{mir, ty};
88
use syntax::codemap::Span;
9+
use syntax::ast::Mutability;
910

1011
/// Methods of this trait signifies a point where CTFE evaluation would fail
1112
/// and some use case dependent behaviour can instead be applied
@@ -70,4 +71,11 @@ pub trait Machine<'tcx>: Sized {
7071
ecx: &mut EvalContext<'a, 'tcx, Self>,
7172
ty: ty::Ty<'tcx>,
7273
) -> EvalResult<'tcx, PrimVal>;
74+
75+
/// Called when trying to access a global declared with a `linkage` attribute
76+
fn global_item_with_linkage<'a>(
77+
ecx: &mut EvalContext<'a, 'tcx, Self>,
78+
instance: ty::Instance<'tcx>,
79+
mutability: Mutability,
80+
) -> EvalResult<'tcx>;
7381
}

src/librustc_mir/interpret/step.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -205,23 +205,7 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
205205
return Ok(false);
206206
}
207207
if self.tcx.has_attr(def_id, "linkage") {
208-
// FIXME: check that it's `#[linkage = "extern_weak"]`
209-
trace!("Initializing an extern global with NULL");
210-
let ptr_size = self.memory.pointer_size();
211-
let ptr = self.memory.allocate(
212-
ptr_size,
213-
ptr_size,
214-
MemoryKind::UninitializedStatic,
215-
)?;
216-
self.memory.write_ptr_sized_unsigned(ptr, PrimVal::Bytes(0))?;
217-
self.memory.mark_static_initalized(ptr.alloc_id, mutability)?;
218-
self.globals.insert(
219-
cid,
220-
PtrAndAlign {
221-
ptr: ptr.into(),
222-
aligned: true,
223-
},
224-
);
208+
M::global_item_with_linkage(self, cid.instance, mutability)?;
225209
return Ok(false);
226210
}
227211
let mir = self.load_mir(instance.def)?;

0 commit comments

Comments
 (0)