Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a10d8e4

Browse files
committed
rename get_global_alloc to try_get_global_alloc
1 parent da5e4d7 commit a10d8e4

File tree

10 files changed

+32
-22
lines changed

10 files changed

+32
-22
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
362362
let (data_id, alloc, section_name) = match todo_item {
363363
TodoItem::Alloc(alloc_id) => {
364364
//println!("alloc_id {}", alloc_id);
365-
let alloc = match tcx.get_global_alloc(alloc_id).unwrap() {
365+
let alloc = match tcx.global_alloc(alloc_id) {
366366
GlobalAlloc::Memory(alloc) => alloc,
367367
GlobalAlloc::Function(_) | GlobalAlloc::Static(_) | GlobalAlloc::Vtable(..) => {
368368
unreachable!()

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
369369
// we don't deallocate it.
370370
let (alloc_id, _, _) = ecx.ptr_get_alloc_id(ptr)?;
371371
let is_allocated_in_another_const = matches!(
372-
ecx.tcx.get_global_alloc(alloc_id),
372+
ecx.tcx.try_get_global_alloc(alloc_id),
373373
Some(interpret::GlobalAlloc::Memory(_))
374374
);
375375

compiler/rustc_const_eval/src/const_eval/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pub(crate) fn deref_mir_constant<'tcx>(
138138
let mplace = ecx.deref_operand(&op).unwrap();
139139
if let Some(alloc_id) = mplace.ptr.provenance {
140140
assert_eq!(
141-
tcx.get_global_alloc(alloc_id).unwrap().unwrap_memory().0.0.mutability,
141+
tcx.global_alloc(alloc_id).unwrap_memory().0.0.mutability,
142142
Mutability::Not,
143143
"deref_mir_constant cannot be used with mutable allocations as \
144144
that could allow pattern matching to observe mutable statics",

compiler/rustc_const_eval/src/interpret/intern.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn intern_shallow<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx, const_eval:
9494
// to validation to error -- it has the much better error messages, pointing out where
9595
// in the value the dangling reference lies.
9696
// The `delay_span_bug` ensures that we don't forget such a check in validation.
97-
if tcx.get_global_alloc(alloc_id).is_none() {
97+
if tcx.try_get_global_alloc(alloc_id).is_none() {
9898
tcx.sess.delay_span_bug(ecx.tcx.span, "tried to intern dangling pointer");
9999
}
100100
// treat dangling pointers like other statics
@@ -454,7 +454,7 @@ pub fn intern_const_alloc_recursive<
454454
.sess
455455
.span_err(ecx.tcx.span, "encountered dangling pointer in final constant");
456456
return Err(reported);
457-
} else if ecx.tcx.get_global_alloc(alloc_id).is_none() {
457+
} else if ecx.tcx.try_get_global_alloc(alloc_id).is_none() {
458458
// We have hit an `AllocId` that is neither in local or global memory and isn't
459459
// marked as dangling by local memory. That should be impossible.
460460
span_bug!(ecx.tcx.span, "encountered unknown alloc id {:?}", alloc_id);

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
161161
) -> InterpResult<'tcx, Pointer<M::Provenance>> {
162162
let alloc_id = ptr.provenance;
163163
// We need to handle `extern static`.
164-
match self.tcx.get_global_alloc(alloc_id) {
164+
match self.tcx.try_get_global_alloc(alloc_id) {
165165
Some(GlobalAlloc::Static(def_id)) if self.tcx.is_thread_local_static(def_id) => {
166166
bug!("global memory cannot point to thread-local static")
167167
}
@@ -289,7 +289,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
289289

290290
let Some((alloc_kind, mut alloc)) = self.memory.alloc_map.remove(&alloc_id) else {
291291
// Deallocating global memory -- always an error
292-
return Err(match self.tcx.get_global_alloc(alloc_id) {
292+
return Err(match self.tcx.try_get_global_alloc(alloc_id) {
293293
Some(GlobalAlloc::Function(..)) => {
294294
err_ub_format!("deallocating {alloc_id:?}, which is a function")
295295
}
@@ -478,7 +478,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
478478
id: AllocId,
479479
is_write: bool,
480480
) -> InterpResult<'tcx, Cow<'tcx, Allocation<M::Provenance, M::AllocExtra>>> {
481-
let (alloc, def_id) = match self.tcx.get_global_alloc(id) {
481+
let (alloc, def_id) = match self.tcx.try_get_global_alloc(id) {
482482
Some(GlobalAlloc::Memory(mem)) => {
483483
// Memory of a constant or promoted or anonymous memory referenced by a static.
484484
(mem, None)
@@ -669,7 +669,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
669669
// # Statics
670670
// Can't do this in the match argument, we may get cycle errors since the lock would
671671
// be held throughout the match.
672-
match self.tcx.get_global_alloc(id) {
672+
match self.tcx.try_get_global_alloc(id) {
673673
Some(GlobalAlloc::Static(did)) => {
674674
assert!(!self.tcx.is_thread_local_static(did));
675675
// Use size and align of the type.
@@ -715,7 +715,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
715715
if let Some(extra) = self.memory.extra_fn_ptr_map.get(&id) {
716716
Some(FnVal::Other(*extra))
717717
} else {
718-
match self.tcx.get_global_alloc(id) {
718+
match self.tcx.try_get_global_alloc(id) {
719719
Some(GlobalAlloc::Function(instance)) => Some(FnVal::Instance(instance)),
720720
_ => None,
721721
}
@@ -839,7 +839,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> std::fmt::Debug for DumpAllocs<'a,
839839
}
840840
None => {
841841
// global alloc
842-
match self.ecx.tcx.get_global_alloc(id) {
842+
match self.ecx.tcx.try_get_global_alloc(id) {
843843
Some(GlobalAlloc::Memory(alloc)) => {
844844
write!(fmt, " (unchanged global, ")?;
845845
write_allocation_track_relocs(

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
447447
// `!` is a ZST and we want to validate it.
448448
if let Ok((alloc_id, _offset, _prov)) = self.ecx.ptr_try_get_alloc_id(place.ptr) {
449449
// Special handling for pointers to statics (irrespective of their type).
450-
let alloc_kind = self.ecx.tcx.get_global_alloc(alloc_id);
450+
let alloc_kind = self.ecx.tcx.try_get_global_alloc(alloc_id);
451451
if let Some(GlobalAlloc::Static(did)) = alloc_kind {
452452
assert!(!self.ecx.tcx.is_thread_local_static(did));
453453
assert!(self.ecx.tcx.is_static(did));

compiler/rustc_middle/src/mir/interpret/mod.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,9 @@ impl<'s> AllocDecodingSession<'s> {
312312
State::InProgress(TinyList::new_single(self.session_id), alloc_id);
313313
Some(alloc_id)
314314
}
315-
AllocDiscriminant::Fn | AllocDiscriminant::Static | AllocDiscriminant::Vtable => {
315+
AllocDiscriminant::Fn
316+
| AllocDiscriminant::Static
317+
| AllocDiscriminant::Vtable => {
316318
// Fns and statics cannot be cyclic, and their `AllocId`
317319
// is determined later by interning.
318320
*entry =
@@ -366,7 +368,8 @@ impl<'s> AllocDecodingSession<'s> {
366368
assert!(alloc_id.is_none());
367369
trace!("creating static alloc ID");
368370
let ty = <Ty<'_> as Decodable<D>>::decode(decoder);
369-
let poly_trait_ref = <Option<ty::PolyExistentialTraitRef<'_>> as Decodable<D>>::decode(decoder);
371+
let poly_trait_ref =
372+
<Option<ty::PolyExistentialTraitRef<'_>> as Decodable<D>>::decode(decoder);
370373
trace!("decoded vtable alloc instance: {ty:?}, {poly_trait_ref:?}");
371374
let alloc_id = decoder.interner().create_vtable_alloc(ty, poly_trait_ref);
372375
alloc_id
@@ -533,7 +536,11 @@ impl<'tcx> TyCtxt<'tcx> {
533536
}
534537

535538
/// Generates an `AllocId` for a (symbolic, not-reified) vtable. Will get deduplicated.
536-
pub fn create_vtable_alloc(self, ty: Ty<'tcx>, poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>) -> AllocId {
539+
pub fn create_vtable_alloc(
540+
self,
541+
ty: Ty<'tcx>,
542+
poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
543+
) -> AllocId {
537544
self.reserve_and_set_dedup(GlobalAlloc::Vtable(ty, poly_trait_ref))
538545
}
539546

@@ -554,7 +561,7 @@ impl<'tcx> TyCtxt<'tcx> {
554561
/// This function exists to allow const eval to detect the difference between evaluation-
555562
/// local dangling pointers and allocations in constants/statics.
556563
#[inline]
557-
pub fn get_global_alloc(self, id: AllocId) -> Option<GlobalAlloc<'tcx>> {
564+
pub fn try_get_global_alloc(self, id: AllocId) -> Option<GlobalAlloc<'tcx>> {
558565
self.alloc_map.lock().alloc_map.get(&id).cloned()
559566
}
560567

@@ -565,7 +572,7 @@ impl<'tcx> TyCtxt<'tcx> {
565572
/// ids), this function is frequently used throughout rustc, but should not be used within
566573
/// the miri engine.
567574
pub fn global_alloc(self, id: AllocId) -> GlobalAlloc<'tcx> {
568-
match self.get_global_alloc(id) {
575+
match self.try_get_global_alloc(id) {
569576
Some(alloc) => alloc,
570577
None => bug!("could not find allocation for {id:?}"),
571578
}

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,12 +720,14 @@ pub fn write_allocations<'tcx>(
720720
write!(w, "{}", display_allocation(tcx, alloc.inner()))
721721
};
722722
write!(w, "\n{id:?}")?;
723-
match tcx.get_global_alloc(id) {
723+
match tcx.try_get_global_alloc(id) {
724724
// This can't really happen unless there are bugs, but it doesn't cost us anything to
725725
// gracefully handle it and allow buggy rustc to be debugged via allocation printing.
726726
None => write!(w, " (deallocated)")?,
727727
Some(GlobalAlloc::Function(inst)) => write!(w, " (fn: {inst})")?,
728-
Some(GlobalAlloc::Vtable(ty, Some(trait_ref))) => write!(w, " (vtable: impl {trait_ref} for {ty})")?,
728+
Some(GlobalAlloc::Vtable(ty, Some(trait_ref))) => {
729+
write!(w, " (vtable: impl {trait_ref} for {ty})")?
730+
}
729731
Some(GlobalAlloc::Vtable(ty, None)) => write!(w, " (vtable: impl ? for {ty})")?,
730732
Some(GlobalAlloc::Static(did)) if !tcx.is_foreign_item(did) => {
731733
match tcx.eval_static_initializer(did) {

compiler/rustc_middle/src/ty/impls_ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for mir::interpret::AllocId {
147147
ty::tls::with_opt(|tcx| {
148148
trace!("hashing {:?}", *self);
149149
let tcx = tcx.expect("can't hash AllocIds during hir lowering");
150-
tcx.get_global_alloc(*self).hash_stable(hcx, hasher);
150+
tcx.try_get_global_alloc(*self).hash_stable(hcx, hasher);
151151
});
152152
}
153153
}

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,7 @@ pub trait PrettyPrinter<'tcx>:
12691269
if let ty::Array(elem, len) = inner.kind() {
12701270
if let ty::Uint(ty::UintTy::U8) = elem.kind() {
12711271
if let ty::ConstKind::Value(ty::ValTree::Leaf(int)) = len.kind() {
1272-
match self.tcx().get_global_alloc(alloc_id) {
1272+
match self.tcx().try_get_global_alloc(alloc_id) {
12731273
Some(GlobalAlloc::Memory(alloc)) => {
12741274
let len = int.assert_bits(self.tcx().data_layout.pointer_size);
12751275
let range =
@@ -1298,7 +1298,8 @@ pub trait PrettyPrinter<'tcx>:
12981298
ty::FnPtr(_) => {
12991299
// FIXME: We should probably have a helper method to share code with the "Byte strings"
13001300
// printing above (which also has to handle pointers to all sorts of things).
1301-
if let Some(GlobalAlloc::Function(instance)) = self.tcx().get_global_alloc(alloc_id)
1301+
if let Some(GlobalAlloc::Function(instance)) =
1302+
self.tcx().try_get_global_alloc(alloc_id)
13021303
{
13031304
self = self.typed_value(
13041305
|this| this.print_value_path(instance.def_id(), instance.substs),

0 commit comments

Comments
 (0)