Skip to content

Commit 43fcd7d

Browse files
committed
Create a convenience wrapper for get_global_alloc(id).unwrap()
1 parent 4572d32 commit 43fcd7d

File tree

10 files changed

+34
-35
lines changed

10 files changed

+34
-35
lines changed

src/librustc_codegen_llvm/common.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
244244
}
245245
}
246246
Scalar::Ptr(ptr) => {
247-
let base_addr = match self.tcx.get_global_alloc(ptr.alloc_id) {
248-
Some(GlobalAlloc::Memory(alloc)) => {
247+
let base_addr = match self.tcx.global_alloc(ptr.alloc_id) {
248+
GlobalAlloc::Memory(alloc) => {
249249
let init = const_alloc_to_llvm(self, alloc);
250250
let value = match alloc.mutability {
251251
Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None),
@@ -256,12 +256,11 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
256256
}
257257
value
258258
}
259-
Some(GlobalAlloc::Function(fn_instance)) => self.get_fn_addr(fn_instance),
260-
Some(GlobalAlloc::Static(def_id)) => {
259+
GlobalAlloc::Function(fn_instance) => self.get_fn_addr(fn_instance),
260+
GlobalAlloc::Static(def_id) => {
261261
assert!(self.tcx.is_static(def_id));
262262
self.get_static(def_id)
263263
}
264-
None => bug!("missing allocation {:?}", ptr.alloc_id),
265264
};
266265
let llval = unsafe {
267266
llvm::LLVMConstInBoundsGEP(

src/librustc_middle/mir/interpret/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ pub fn specialized_encode_alloc_id<'tcx, E: Encoder>(
197197
tcx: TyCtxt<'tcx>,
198198
alloc_id: AllocId,
199199
) -> Result<(), E::Error> {
200-
match tcx.get_global_alloc(alloc_id).expect("no value for given alloc ID") {
200+
match tcx.global_alloc(alloc_id) {
201201
GlobalAlloc::Memory(alloc) => {
202202
trace!("encoding {:?} with {:#?}", alloc_id, alloc);
203203
AllocDiscriminant::Alloc.encode(encoder)?;
@@ -513,6 +513,15 @@ impl<'tcx> TyCtxt<'tcx> {
513513
self.alloc_map.lock().alloc_map.get(&id).cloned()
514514
}
515515

516+
#[inline]
517+
#[track_caller]
518+
pub fn global_alloc(&self, id: AllocId) -> GlobalAlloc<'tcx> {
519+
match self.get_global_alloc(id) {
520+
Some(alloc) => alloc,
521+
None => bug!("could not find allocation for {}", id),
522+
}
523+
}
524+
516525
/// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. Trying to
517526
/// call this function twice, even with the same `Allocation` will ICE the compiler.
518527
pub fn set_alloc_id_memory(&self, id: AllocId, mem: &'tcx Allocation) {

src/librustc_middle/mir/mod.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -2410,13 +2410,9 @@ pub struct Constant<'tcx> {
24102410
impl Constant<'tcx> {
24112411
pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
24122412
match self.literal.val.try_to_scalar() {
2413-
Some(Scalar::Ptr(ptr)) => match tcx.get_global_alloc(ptr.alloc_id) {
2414-
Some(GlobalAlloc::Static(def_id)) => Some(def_id),
2415-
Some(_) => None,
2416-
None => {
2417-
tcx.sess.delay_span_bug(DUMMY_SP, "MIR cannot contain dangling const pointers");
2418-
None
2419-
}
2413+
Some(Scalar::Ptr(ptr)) => match tcx.global_alloc(ptr.alloc_id) {
2414+
GlobalAlloc::Static(def_id) => Some(def_id),
2415+
_ => None,
24202416
},
24212417
_ => None,
24222418
}

src/librustc_middle/ty/print/pretty.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -956,8 +956,7 @@ pub trait PrettyPrinter<'tcx>:
956956
) => {
957957
let byte_str = self
958958
.tcx()
959-
.get_global_alloc(ptr.alloc_id)
960-
.unwrap()
959+
.global_alloc(ptr.alloc_id)
961960
.unwrap_memory()
962961
.get_bytes(&self.tcx(), ptr, Size::from_bytes(*data))
963962
.unwrap();
@@ -1021,7 +1020,7 @@ pub trait PrettyPrinter<'tcx>:
10211020
)?;
10221021
}
10231022
(Scalar::Ptr(ptr), ty::FnPtr(_)) => {
1024-
let instance = self.tcx().get_global_alloc(ptr.alloc_id).unwrap().unwrap_fn();
1023+
let instance = self.tcx().global_alloc(ptr.alloc_id).unwrap_fn();
10251024
self = self.typed_value(
10261025
|this| this.print_value_path(instance.def_id(), instance.substs),
10271026
|this| this.print_type(ty),

src/librustc_middle/ty/relate.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,8 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
549549
if a_val == b_val {
550550
Ok(ConstValue::Scalar(a_val))
551551
} else if let ty::FnPtr(_) = a.ty.kind {
552-
let a_instance =
553-
tcx.get_global_alloc(a_val.assert_ptr().alloc_id).unwrap().unwrap_fn();
554-
let b_instance =
555-
tcx.get_global_alloc(b_val.assert_ptr().alloc_id).unwrap().unwrap_fn();
552+
let a_instance = tcx.global_alloc(a_val.assert_ptr().alloc_id).unwrap_fn();
553+
let b_instance = tcx.global_alloc(b_val.assert_ptr().alloc_id).unwrap_fn();
556554
if a_instance == b_instance {
557555
Ok(ConstValue::Scalar(a_val))
558556
} else {

src/librustc_mir/const_eval/eval_queries.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ pub(super) fn op_to_const<'tcx>(
130130

131131
let to_const_value = |mplace: MPlaceTy<'_>| match mplace.ptr {
132132
Scalar::Ptr(ptr) => {
133-
let alloc = ecx.tcx.get_global_alloc(ptr.alloc_id).unwrap().unwrap_memory();
133+
let alloc = ecx.tcx.global_alloc(ptr.alloc_id).unwrap_memory();
134134
ConstValue::ByRef { alloc, offset: ptr.offset }
135135
}
136136
Scalar::Raw { data, .. } => {
@@ -154,10 +154,9 @@ pub(super) fn op_to_const<'tcx>(
154154
},
155155
Immediate::ScalarPair(a, b) => {
156156
let (data, start) = match a.not_undef().unwrap() {
157-
Scalar::Ptr(ptr) => (
158-
ecx.tcx.get_global_alloc(ptr.alloc_id).unwrap().unwrap_memory(),
159-
ptr.offset.bytes(),
160-
),
157+
Scalar::Ptr(ptr) => {
158+
(ecx.tcx.global_alloc(ptr.alloc_id).unwrap_memory(), ptr.offset.bytes())
159+
}
161160
Scalar::Raw { .. } => (
162161
ecx.tcx
163162
.intern_const_alloc(Allocation::from_byte_aligned_bytes(b"" as &[u8])),
@@ -204,7 +203,7 @@ fn validate_and_turn_into_const<'tcx>(
204203
if is_static || cid.promoted.is_some() {
205204
let ptr = mplace.ptr.assert_ptr();
206205
Ok(ConstValue::ByRef {
207-
alloc: ecx.tcx.get_global_alloc(ptr.alloc_id).unwrap().unwrap_memory(),
206+
alloc: ecx.tcx.global_alloc(ptr.alloc_id).unwrap_memory(),
208207
offset: ptr.offset,
209208
})
210209
} else {

src/librustc_mir/interpret/memory.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
467467
})?;
468468
// Make sure we use the ID of the resolved memory, not the lazy one!
469469
let id = raw_const.alloc_id;
470-
let allocation = tcx.get_global_alloc(id).unwrap().unwrap_memory();
470+
let allocation = tcx.global_alloc(id).unwrap_memory();
471471

472472
(allocation, Some(def_id))
473473
}

src/librustc_mir/interpret/place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ where
11011101
raw: RawConst<'tcx>,
11021102
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
11031103
// This must be an allocation in `tcx`
1104-
assert!(self.tcx.get_global_alloc(raw.alloc_id).is_some());
1104+
let _ = self.tcx.global_alloc(raw.alloc_id);
11051105
let ptr = self.tag_global_base_pointer(Pointer::from(raw.alloc_id));
11061106
let layout = self.layout_of(raw.ty)?;
11071107
Ok(MPlaceTy::from_aligned_ptr(ptr, layout))

src/librustc_mir/monomorphize/collector.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1136,29 +1136,28 @@ fn create_mono_items_for_default_impls<'tcx>(
11361136

11371137
/// Scans the miri alloc in order to find function calls, closures, and drop-glue.
11381138
fn collect_miri<'tcx>(tcx: TyCtxt<'tcx>, alloc_id: AllocId, output: &mut Vec<MonoItem<'tcx>>) {
1139-
match tcx.get_global_alloc(alloc_id) {
1140-
Some(GlobalAlloc::Static(def_id)) => {
1139+
match tcx.global_alloc(alloc_id) {
1140+
GlobalAlloc::Static(def_id) => {
11411141
let instance = Instance::mono(tcx, def_id);
11421142
if should_monomorphize_locally(tcx, &instance) {
11431143
trace!("collecting static {:?}", def_id);
11441144
output.push(MonoItem::Static(def_id));
11451145
}
11461146
}
1147-
Some(GlobalAlloc::Memory(alloc)) => {
1147+
GlobalAlloc::Memory(alloc) => {
11481148
trace!("collecting {:?} with {:#?}", alloc_id, alloc);
11491149
for &((), inner) in alloc.relocations().values() {
11501150
rustc_data_structures::stack::ensure_sufficient_stack(|| {
11511151
collect_miri(tcx, inner, output);
11521152
});
11531153
}
11541154
}
1155-
Some(GlobalAlloc::Function(fn_instance)) => {
1155+
GlobalAlloc::Function(fn_instance) => {
11561156
if should_monomorphize_locally(tcx, &fn_instance) {
11571157
trace!("collecting {:?} with {:#?}", alloc_id, fn_instance);
11581158
output.push(create_fn_mono_item(fn_instance));
11591159
}
11601160
}
1161-
None => bug!("alloc id without corresponding allocation: {}", alloc_id),
11621161
}
11631162
}
11641163

src/librustc_mir_build/hair/pattern/_match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ impl<'tcx> LiteralExpander<'tcx> {
286286
(ConstValue::Scalar(p), x, y) if x == y => {
287287
match p {
288288
Scalar::Ptr(p) => {
289-
let alloc = self.tcx.get_global_alloc(p.alloc_id).unwrap().unwrap_memory();
289+
let alloc = self.tcx.global_alloc(p.alloc_id).unwrap_memory();
290290
ConstValue::ByRef { alloc, offset: p.offset }
291291
}
292292
Scalar::Raw { .. } => {
@@ -305,7 +305,7 @@ impl<'tcx> LiteralExpander<'tcx> {
305305
(ConstValue::Scalar(Scalar::Ptr(p)), ty::Array(t, n), ty::Slice(u)) => {
306306
assert_eq!(t, u);
307307
ConstValue::Slice {
308-
data: self.tcx.get_global_alloc(p.alloc_id).unwrap().unwrap_memory(),
308+
data: self.tcx.global_alloc(p.alloc_id).unwrap_memory(),
309309
start: p.offset.bytes().try_into().unwrap(),
310310
end: n.eval_usize(self.tcx, ty::ParamEnv::empty()).try_into().unwrap(),
311311
}

0 commit comments

Comments
 (0)