Skip to content

Commit

Permalink
Create a convenience wrapper for get_global_alloc(id).unwrap()
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed May 8, 2020
1 parent 4572d32 commit 43fcd7d
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 35 deletions.
9 changes: 4 additions & 5 deletions src/librustc_codegen_llvm/common.rs
Expand Up @@ -244,8 +244,8 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
}
}
Scalar::Ptr(ptr) => {
let base_addr = match self.tcx.get_global_alloc(ptr.alloc_id) {
Some(GlobalAlloc::Memory(alloc)) => {
let base_addr = match self.tcx.global_alloc(ptr.alloc_id) {
GlobalAlloc::Memory(alloc) => {
let init = const_alloc_to_llvm(self, alloc);
let value = match alloc.mutability {
Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None),
Expand All @@ -256,12 +256,11 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
}
value
}
Some(GlobalAlloc::Function(fn_instance)) => self.get_fn_addr(fn_instance),
Some(GlobalAlloc::Static(def_id)) => {
GlobalAlloc::Function(fn_instance) => self.get_fn_addr(fn_instance),
GlobalAlloc::Static(def_id) => {
assert!(self.tcx.is_static(def_id));
self.get_static(def_id)
}
None => bug!("missing allocation {:?}", ptr.alloc_id),
};
let llval = unsafe {
llvm::LLVMConstInBoundsGEP(
Expand Down
11 changes: 10 additions & 1 deletion src/librustc_middle/mir/interpret/mod.rs
Expand Up @@ -197,7 +197,7 @@ pub fn specialized_encode_alloc_id<'tcx, E: Encoder>(
tcx: TyCtxt<'tcx>,
alloc_id: AllocId,
) -> Result<(), E::Error> {
match tcx.get_global_alloc(alloc_id).expect("no value for given alloc ID") {
match tcx.global_alloc(alloc_id) {
GlobalAlloc::Memory(alloc) => {
trace!("encoding {:?} with {:#?}", alloc_id, alloc);
AllocDiscriminant::Alloc.encode(encoder)?;
Expand Down Expand Up @@ -513,6 +513,15 @@ impl<'tcx> TyCtxt<'tcx> {
self.alloc_map.lock().alloc_map.get(&id).cloned()
}

#[inline]
#[track_caller]
pub fn global_alloc(&self, id: AllocId) -> GlobalAlloc<'tcx> {
match self.get_global_alloc(id) {
Some(alloc) => alloc,
None => bug!("could not find allocation for {}", id),
}
}

/// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. Trying to
/// call this function twice, even with the same `Allocation` will ICE the compiler.
pub fn set_alloc_id_memory(&self, id: AllocId, mem: &'tcx Allocation) {
Expand Down
10 changes: 3 additions & 7 deletions src/librustc_middle/mir/mod.rs
Expand Up @@ -2410,13 +2410,9 @@ pub struct Constant<'tcx> {
impl Constant<'tcx> {
pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
match self.literal.val.try_to_scalar() {
Some(Scalar::Ptr(ptr)) => match tcx.get_global_alloc(ptr.alloc_id) {
Some(GlobalAlloc::Static(def_id)) => Some(def_id),
Some(_) => None,
None => {
tcx.sess.delay_span_bug(DUMMY_SP, "MIR cannot contain dangling const pointers");
None
}
Some(Scalar::Ptr(ptr)) => match tcx.global_alloc(ptr.alloc_id) {
GlobalAlloc::Static(def_id) => Some(def_id),
_ => None,
},
_ => None,
}
Expand Down
5 changes: 2 additions & 3 deletions src/librustc_middle/ty/print/pretty.rs
Expand Up @@ -956,8 +956,7 @@ pub trait PrettyPrinter<'tcx>:
) => {
let byte_str = self
.tcx()
.get_global_alloc(ptr.alloc_id)
.unwrap()
.global_alloc(ptr.alloc_id)
.unwrap_memory()
.get_bytes(&self.tcx(), ptr, Size::from_bytes(*data))
.unwrap();
Expand Down Expand Up @@ -1021,7 +1020,7 @@ pub trait PrettyPrinter<'tcx>:
)?;
}
(Scalar::Ptr(ptr), ty::FnPtr(_)) => {
let instance = self.tcx().get_global_alloc(ptr.alloc_id).unwrap().unwrap_fn();
let instance = self.tcx().global_alloc(ptr.alloc_id).unwrap_fn();
self = self.typed_value(
|this| this.print_value_path(instance.def_id(), instance.substs),
|this| this.print_type(ty),
Expand Down
6 changes: 2 additions & 4 deletions src/librustc_middle/ty/relate.rs
Expand Up @@ -549,10 +549,8 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
if a_val == b_val {
Ok(ConstValue::Scalar(a_val))
} else if let ty::FnPtr(_) = a.ty.kind {
let a_instance =
tcx.get_global_alloc(a_val.assert_ptr().alloc_id).unwrap().unwrap_fn();
let b_instance =
tcx.get_global_alloc(b_val.assert_ptr().alloc_id).unwrap().unwrap_fn();
let a_instance = tcx.global_alloc(a_val.assert_ptr().alloc_id).unwrap_fn();
let b_instance = tcx.global_alloc(b_val.assert_ptr().alloc_id).unwrap_fn();
if a_instance == b_instance {
Ok(ConstValue::Scalar(a_val))
} else {
Expand Down
11 changes: 5 additions & 6 deletions src/librustc_mir/const_eval/eval_queries.rs
Expand Up @@ -130,7 +130,7 @@ pub(super) fn op_to_const<'tcx>(

let to_const_value = |mplace: MPlaceTy<'_>| match mplace.ptr {
Scalar::Ptr(ptr) => {
let alloc = ecx.tcx.get_global_alloc(ptr.alloc_id).unwrap().unwrap_memory();
let alloc = ecx.tcx.global_alloc(ptr.alloc_id).unwrap_memory();
ConstValue::ByRef { alloc, offset: ptr.offset }
}
Scalar::Raw { data, .. } => {
Expand All @@ -154,10 +154,9 @@ pub(super) fn op_to_const<'tcx>(
},
Immediate::ScalarPair(a, b) => {
let (data, start) = match a.not_undef().unwrap() {
Scalar::Ptr(ptr) => (
ecx.tcx.get_global_alloc(ptr.alloc_id).unwrap().unwrap_memory(),
ptr.offset.bytes(),
),
Scalar::Ptr(ptr) => {
(ecx.tcx.global_alloc(ptr.alloc_id).unwrap_memory(), ptr.offset.bytes())
}
Scalar::Raw { .. } => (
ecx.tcx
.intern_const_alloc(Allocation::from_byte_aligned_bytes(b"" as &[u8])),
Expand Down Expand Up @@ -204,7 +203,7 @@ fn validate_and_turn_into_const<'tcx>(
if is_static || cid.promoted.is_some() {
let ptr = mplace.ptr.assert_ptr();
Ok(ConstValue::ByRef {
alloc: ecx.tcx.get_global_alloc(ptr.alloc_id).unwrap().unwrap_memory(),
alloc: ecx.tcx.global_alloc(ptr.alloc_id).unwrap_memory(),
offset: ptr.offset,
})
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/interpret/memory.rs
Expand Up @@ -467,7 +467,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
})?;
// Make sure we use the ID of the resolved memory, not the lazy one!
let id = raw_const.alloc_id;
let allocation = tcx.get_global_alloc(id).unwrap().unwrap_memory();
let allocation = tcx.global_alloc(id).unwrap_memory();

(allocation, Some(def_id))
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/interpret/place.rs
Expand Up @@ -1101,7 +1101,7 @@ where
raw: RawConst<'tcx>,
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
// This must be an allocation in `tcx`
assert!(self.tcx.get_global_alloc(raw.alloc_id).is_some());
let _ = self.tcx.global_alloc(raw.alloc_id);
let ptr = self.tag_global_base_pointer(Pointer::from(raw.alloc_id));
let layout = self.layout_of(raw.ty)?;
Ok(MPlaceTy::from_aligned_ptr(ptr, layout))
Expand Down
9 changes: 4 additions & 5 deletions src/librustc_mir/monomorphize/collector.rs
Expand Up @@ -1136,29 +1136,28 @@ fn create_mono_items_for_default_impls<'tcx>(

/// Scans the miri alloc in order to find function calls, closures, and drop-glue.
fn collect_miri<'tcx>(tcx: TyCtxt<'tcx>, alloc_id: AllocId, output: &mut Vec<MonoItem<'tcx>>) {
match tcx.get_global_alloc(alloc_id) {
Some(GlobalAlloc::Static(def_id)) => {
match tcx.global_alloc(alloc_id) {
GlobalAlloc::Static(def_id) => {
let instance = Instance::mono(tcx, def_id);
if should_monomorphize_locally(tcx, &instance) {
trace!("collecting static {:?}", def_id);
output.push(MonoItem::Static(def_id));
}
}
Some(GlobalAlloc::Memory(alloc)) => {
GlobalAlloc::Memory(alloc) => {
trace!("collecting {:?} with {:#?}", alloc_id, alloc);
for &((), inner) in alloc.relocations().values() {
rustc_data_structures::stack::ensure_sufficient_stack(|| {
collect_miri(tcx, inner, output);
});
}
}
Some(GlobalAlloc::Function(fn_instance)) => {
GlobalAlloc::Function(fn_instance) => {
if should_monomorphize_locally(tcx, &fn_instance) {
trace!("collecting {:?} with {:#?}", alloc_id, fn_instance);
output.push(create_fn_mono_item(fn_instance));
}
}
None => bug!("alloc id without corresponding allocation: {}", alloc_id),
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir_build/hair/pattern/_match.rs
Expand Up @@ -286,7 +286,7 @@ impl<'tcx> LiteralExpander<'tcx> {
(ConstValue::Scalar(p), x, y) if x == y => {
match p {
Scalar::Ptr(p) => {
let alloc = self.tcx.get_global_alloc(p.alloc_id).unwrap().unwrap_memory();
let alloc = self.tcx.global_alloc(p.alloc_id).unwrap_memory();
ConstValue::ByRef { alloc, offset: p.offset }
}
Scalar::Raw { .. } => {
Expand All @@ -305,7 +305,7 @@ impl<'tcx> LiteralExpander<'tcx> {
(ConstValue::Scalar(Scalar::Ptr(p)), ty::Array(t, n), ty::Slice(u)) => {
assert_eq!(t, u);
ConstValue::Slice {
data: self.tcx.get_global_alloc(p.alloc_id).unwrap().unwrap_memory(),
data: self.tcx.global_alloc(p.alloc_id).unwrap_memory(),
start: p.offset.bytes().try_into().unwrap(),
end: n.eval_usize(self.tcx, ty::ParamEnv::empty()).try_into().unwrap(),
}
Expand Down

0 comments on commit 43fcd7d

Please sign in to comment.