Skip to content

Commit 1037f40

Browse files
committed
Move unwrap_fn and unwrap_memory to GlobalAlloc
1 parent 38ae8f3 commit 1037f40

File tree

7 files changed

+43
-26
lines changed

7 files changed

+43
-26
lines changed

src/librustc_middle/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#![feature(or_patterns)]
4343
#![feature(range_is_empty)]
4444
#![feature(specialization)]
45+
#![feature(track_caller)]
4546
#![feature(trusted_len)]
4647
#![feature(vec_remove_item)]
4748
#![feature(stmt_expr_attributes)]

src/librustc_middle/mir/interpret/mod.rs

+22-16
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,28 @@ pub enum GlobalAlloc<'tcx> {
379379
Memory(&'tcx Allocation),
380380
}
381381

382+
impl GlobalAlloc<'tcx> {
383+
/// Panics if the `GlobalAlloc` does not refer to an `GlobalAlloc::Memory`
384+
#[track_caller]
385+
#[inline]
386+
pub fn unwrap_memory(&self) -> &'tcx Allocation {
387+
match *self {
388+
GlobalAlloc::Memory(mem) => mem,
389+
_ => bug!("expected memory, got {:?}", self),
390+
}
391+
}
392+
393+
/// Panics if the `GlobalAlloc` is not `GlobalAlloc::Function`
394+
#[track_caller]
395+
#[inline]
396+
pub fn unwrap_fn(&self) -> Instance<'tcx> {
397+
match *self {
398+
GlobalAlloc::Function(instance) => instance,
399+
_ => bug!("expected function, got {:?}", self),
400+
}
401+
}
402+
}
403+
382404
pub struct AllocMap<'tcx> {
383405
/// Maps `AllocId`s to their corresponding allocations.
384406
alloc_map: FxHashMap<AllocId, GlobalAlloc<'tcx>>,
@@ -491,22 +513,6 @@ impl<'tcx> TyCtxt<'tcx> {
491513
self.alloc_map.lock().alloc_map.get(&id).cloned()
492514
}
493515

494-
/// Panics if the `AllocId` does not refer to an `Allocation`
495-
pub fn unwrap_memory(&self, id: AllocId) -> &'tcx Allocation {
496-
match self.get_global_alloc(id) {
497-
Some(GlobalAlloc::Memory(mem)) => mem,
498-
_ => bug!("expected allocation ID {} to point to memory", id),
499-
}
500-
}
501-
502-
/// Panics if the `AllocId` does not refer to a function
503-
pub fn unwrap_fn(&self, id: AllocId) -> Instance<'tcx> {
504-
match self.get_global_alloc(id) {
505-
Some(GlobalAlloc::Function(instance)) => instance,
506-
_ => bug!("expected allocation ID {} to point to a function", id),
507-
}
508-
}
509-
510516
/// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. Trying to
511517
/// call this function twice, even with the same `Allocation` will ICE the compiler.
512518
pub fn set_alloc_id_memory(&self, id: AllocId, mem: &'tcx Allocation) {

src/librustc_middle/ty/print/pretty.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,9 @@ pub trait PrettyPrinter<'tcx>:
956956
) => {
957957
let byte_str = self
958958
.tcx()
959-
.unwrap_memory(ptr.alloc_id)
959+
.get_global_alloc(ptr.alloc_id)
960+
.unwrap()
961+
.unwrap_memory()
960962
.get_bytes(&self.tcx(), ptr, Size::from_bytes(*data))
961963
.unwrap();
962964
p!(pretty_print_byte_str(byte_str));
@@ -1019,7 +1021,7 @@ pub trait PrettyPrinter<'tcx>:
10191021
)?;
10201022
}
10211023
(Scalar::Ptr(ptr), ty::FnPtr(_)) => {
1022-
let instance = self.tcx().unwrap_fn(ptr.alloc_id);
1024+
let instance = self.tcx().get_global_alloc(ptr.alloc_id).unwrap().unwrap_fn();
10231025
self = self.typed_value(
10241026
|this| this.print_value_path(instance.def_id(), instance.substs),
10251027
|this| this.print_type(ty),

src/librustc_middle/ty/relate.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,10 @@ 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 = tcx.unwrap_fn(a_val.assert_ptr().alloc_id);
553-
let b_instance = tcx.unwrap_fn(b_val.assert_ptr().alloc_id);
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();
554556
if a_instance == b_instance {
555557
Ok(ConstValue::Scalar(a_val))
556558
} else {

src/librustc_mir/const_eval/eval_queries.rs

+9-3
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.unwrap_memory(ptr.alloc_id);
133+
let alloc = ecx.tcx.get_global_alloc(ptr.alloc_id).unwrap().unwrap_memory();
134134
ConstValue::ByRef { alloc, offset: ptr.offset }
135135
}
136136
Scalar::Raw { data, .. } => {
@@ -154,7 +154,10 @@ 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) => (ecx.tcx.unwrap_memory(ptr.alloc_id), ptr.offset.bytes()),
157+
Scalar::Ptr(ptr) => (
158+
ecx.tcx.get_global_alloc(ptr.alloc_id).unwrap().unwrap_memory(),
159+
ptr.offset.bytes(),
160+
),
158161
Scalar::Raw { .. } => (
159162
ecx.tcx
160163
.intern_const_alloc(Allocation::from_byte_aligned_bytes(b"" as &[u8])),
@@ -200,7 +203,10 @@ fn validate_and_turn_into_const<'tcx>(
200203
// whether they become immediates.
201204
if is_static || cid.promoted.is_some() {
202205
let ptr = mplace.ptr.assert_ptr();
203-
Ok(ConstValue::ByRef { alloc: ecx.tcx.unwrap_memory(ptr.alloc_id), offset: ptr.offset })
206+
Ok(ConstValue::ByRef {
207+
alloc: ecx.tcx.get_global_alloc(ptr.alloc_id).unwrap().unwrap_memory(),
208+
offset: ptr.offset,
209+
})
204210
} else {
205211
Ok(op_to_const(&ecx, mplace.into()))
206212
}

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.unwrap_memory(id);
470+
let allocation = tcx.get_global_alloc(id).unwrap().unwrap_memory();
471471

472472
(allocation, Some(def_id))
473473
}

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.unwrap_memory(p.alloc_id);
289+
let alloc = self.tcx.get_global_alloc(p.alloc_id).unwrap().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.unwrap_memory(p.alloc_id),
308+
data: self.tcx.get_global_alloc(p.alloc_id).unwrap().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)