mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Create a convenience wrapper for get_global_alloc(id).unwrap()
This commit is contained in:
parent
4572d32810
commit
43fcd7d55e
@ -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),
|
||||
@ -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(
|
||||
|
@ -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)?;
|
||||
@ -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) {
|
||||
|
@ -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,
|
||||
}
|
||||
|
@ -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();
|
||||
@ -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),
|
||||
|
@ -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 {
|
||||
|
@ -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, .. } => {
|
||||
@ -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])),
|
||||
@ -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 {
|
||||
|
@ -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))
|
||||
}
|
||||
|
@ -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))
|
||||
|
@ -1136,15 +1136,15 @@ 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(|| {
|
||||
@ -1152,13 +1152,12 @@ fn collect_miri<'tcx>(tcx: TyCtxt<'tcx>, alloc_id: AllocId, output: &mut Vec<Mon
|
||||
});
|
||||
}
|
||||
}
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 { .. } => {
|
||||
@ -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(),
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user