1
Fork 0

rename get_global_alloc to try_get_global_alloc

This commit is contained in:
Ralf Jung 2022-07-17 11:40:34 -04:00
parent da5e4d73f1
commit a10d8e4581
10 changed files with 32 additions and 22 deletions

View file

@ -312,7 +312,9 @@ impl<'s> AllocDecodingSession<'s> {
State::InProgress(TinyList::new_single(self.session_id), alloc_id);
Some(alloc_id)
}
AllocDiscriminant::Fn | AllocDiscriminant::Static | AllocDiscriminant::Vtable => {
AllocDiscriminant::Fn
| AllocDiscriminant::Static
| AllocDiscriminant::Vtable => {
// Fns and statics cannot be cyclic, and their `AllocId`
// is determined later by interning.
*entry =
@ -366,7 +368,8 @@ impl<'s> AllocDecodingSession<'s> {
assert!(alloc_id.is_none());
trace!("creating static alloc ID");
let ty = <Ty<'_> as Decodable<D>>::decode(decoder);
let poly_trait_ref = <Option<ty::PolyExistentialTraitRef<'_>> as Decodable<D>>::decode(decoder);
let poly_trait_ref =
<Option<ty::PolyExistentialTraitRef<'_>> as Decodable<D>>::decode(decoder);
trace!("decoded vtable alloc instance: {ty:?}, {poly_trait_ref:?}");
let alloc_id = decoder.interner().create_vtable_alloc(ty, poly_trait_ref);
alloc_id
@ -533,7 +536,11 @@ impl<'tcx> TyCtxt<'tcx> {
}
/// Generates an `AllocId` for a (symbolic, not-reified) vtable. Will get deduplicated.
pub fn create_vtable_alloc(self, ty: Ty<'tcx>, poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>) -> AllocId {
pub fn create_vtable_alloc(
self,
ty: Ty<'tcx>,
poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
) -> AllocId {
self.reserve_and_set_dedup(GlobalAlloc::Vtable(ty, poly_trait_ref))
}
@ -554,7 +561,7 @@ impl<'tcx> TyCtxt<'tcx> {
/// This function exists to allow const eval to detect the difference between evaluation-
/// local dangling pointers and allocations in constants/statics.
#[inline]
pub fn get_global_alloc(self, id: AllocId) -> Option<GlobalAlloc<'tcx>> {
pub fn try_get_global_alloc(self, id: AllocId) -> Option<GlobalAlloc<'tcx>> {
self.alloc_map.lock().alloc_map.get(&id).cloned()
}
@ -565,7 +572,7 @@ impl<'tcx> TyCtxt<'tcx> {
/// ids), this function is frequently used throughout rustc, but should not be used within
/// the miri engine.
pub fn global_alloc(self, id: AllocId) -> GlobalAlloc<'tcx> {
match self.get_global_alloc(id) {
match self.try_get_global_alloc(id) {
Some(alloc) => alloc,
None => bug!("could not find allocation for {id:?}"),
}

View file

@ -720,12 +720,14 @@ pub fn write_allocations<'tcx>(
write!(w, "{}", display_allocation(tcx, alloc.inner()))
};
write!(w, "\n{id:?}")?;
match tcx.get_global_alloc(id) {
match tcx.try_get_global_alloc(id) {
// This can't really happen unless there are bugs, but it doesn't cost us anything to
// gracefully handle it and allow buggy rustc to be debugged via allocation printing.
None => write!(w, " (deallocated)")?,
Some(GlobalAlloc::Function(inst)) => write!(w, " (fn: {inst})")?,
Some(GlobalAlloc::Vtable(ty, Some(trait_ref))) => write!(w, " (vtable: impl {trait_ref} for {ty})")?,
Some(GlobalAlloc::Vtable(ty, Some(trait_ref))) => {
write!(w, " (vtable: impl {trait_ref} for {ty})")?
}
Some(GlobalAlloc::Vtable(ty, None)) => write!(w, " (vtable: impl ? for {ty})")?,
Some(GlobalAlloc::Static(did)) if !tcx.is_foreign_item(did) => {
match tcx.eval_static_initializer(did) {

View file

@ -147,7 +147,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for mir::interpret::AllocId {
ty::tls::with_opt(|tcx| {
trace!("hashing {:?}", *self);
let tcx = tcx.expect("can't hash AllocIds during hir lowering");
tcx.get_global_alloc(*self).hash_stable(hcx, hasher);
tcx.try_get_global_alloc(*self).hash_stable(hcx, hasher);
});
}
}

View file

@ -1269,7 +1269,7 @@ pub trait PrettyPrinter<'tcx>:
if let ty::Array(elem, len) = inner.kind() {
if let ty::Uint(ty::UintTy::U8) = elem.kind() {
if let ty::ConstKind::Value(ty::ValTree::Leaf(int)) = len.kind() {
match self.tcx().get_global_alloc(alloc_id) {
match self.tcx().try_get_global_alloc(alloc_id) {
Some(GlobalAlloc::Memory(alloc)) => {
let len = int.assert_bits(self.tcx().data_layout.pointer_size);
let range =
@ -1298,7 +1298,8 @@ pub trait PrettyPrinter<'tcx>:
ty::FnPtr(_) => {
// FIXME: We should probably have a helper method to share code with the "Byte strings"
// printing above (which also has to handle pointers to all sorts of things).
if let Some(GlobalAlloc::Function(instance)) = self.tcx().get_global_alloc(alloc_id)
if let Some(GlobalAlloc::Function(instance)) =
self.tcx().try_get_global_alloc(alloc_id)
{
self = self.typed_value(
|this| this.print_value_path(instance.def_id(), instance.substs),