Skip to content

Commit

Permalink
Merge pull request rust-lang#340 from solson/babysteps
Browse files Browse the repository at this point in the history
Remove `#[linkage(foo)]` statics from core miri
  • Loading branch information
oli-obk authored Sep 15, 2017
2 parents e8ea7da + 8cbfbf7 commit f13455a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
28 changes: 28 additions & 0 deletions miri/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,32 @@ impl<'tcx> Machine<'tcx> for Evaluator {
.map(PrimVal::Ptr)
}
}

fn global_item_with_linkage<'a>(
ecx: &mut EvalContext<'a, 'tcx, Self>,
instance: ty::Instance<'tcx>,
mutability: Mutability,
) -> EvalResult<'tcx> {
// FIXME: check that it's `#[linkage = "extern_weak"]`
trace!("Initializing an extern global with NULL");
let ptr_size = ecx.memory.pointer_size();
let ptr = ecx.memory.allocate(
ptr_size,
ptr_size,
MemoryKind::UninitializedStatic,
)?;
ecx.memory.write_ptr_sized_unsigned(ptr, PrimVal::Bytes(0))?;
ecx.memory.mark_static_initalized(ptr.alloc_id, mutability)?;
ecx.globals.insert(
GlobalId {
instance,
promoted: None,
},
PtrAndAlign {
ptr: ptr.into(),
aligned: true,
},
);
Ok(())
}
}
10 changes: 10 additions & 0 deletions src/librustc_mir/interpret/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,14 @@ impl<'tcx> super::Machine<'tcx> for CompileTimeFunctionEvaluator {
ConstEvalError::NeedsRfc("Heap allocations via `box` keyword".to_string()).into(),
)
}

fn global_item_with_linkage<'a>(
_ecx: &mut EvalContext<'a, 'tcx, Self>,
_instance: ty::Instance<'tcx>,
_mutability: Mutability,
) -> EvalResult<'tcx> {
Err(
ConstEvalError::NotConst("statics with `linkage` attribute".to_string()).into(),
)
}
}
8 changes: 8 additions & 0 deletions src/librustc_mir/interpret/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::{EvalResult, EvalContext, Lvalue, PrimVal, ValTy};

use rustc::{mir, ty};
use syntax::codemap::Span;
use syntax::ast::Mutability;

/// Methods of this trait signifies a point where CTFE evaluation would fail
/// and some use case dependent behaviour can instead be applied
Expand Down Expand Up @@ -70,4 +71,11 @@ pub trait Machine<'tcx>: Sized {
ecx: &mut EvalContext<'a, 'tcx, Self>,
ty: ty::Ty<'tcx>,
) -> EvalResult<'tcx, PrimVal>;

/// Called when trying to access a global declared with a `linkage` attribute
fn global_item_with_linkage<'a>(
ecx: &mut EvalContext<'a, 'tcx, Self>,
instance: ty::Instance<'tcx>,
mutability: Mutability,
) -> EvalResult<'tcx>;
}
18 changes: 1 addition & 17 deletions src/librustc_mir/interpret/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,23 +205,7 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
return Ok(false);
}
if self.tcx.has_attr(def_id, "linkage") {
// FIXME: check that it's `#[linkage = "extern_weak"]`
trace!("Initializing an extern global with NULL");
let ptr_size = self.memory.pointer_size();
let ptr = self.memory.allocate(
ptr_size,
ptr_size,
MemoryKind::UninitializedStatic,
)?;
self.memory.write_ptr_sized_unsigned(ptr, PrimVal::Bytes(0))?;
self.memory.mark_static_initalized(ptr.alloc_id, mutability)?;
self.globals.insert(
cid,
PtrAndAlign {
ptr: ptr.into(),
aligned: true,
},
);
M::global_item_with_linkage(self, cid.instance, mutability)?;
return Ok(false);
}
let mir = self.load_mir(instance.def)?;
Expand Down

0 comments on commit f13455a

Please sign in to comment.