Skip to content

Commit

Permalink
Key AllocIntern's map by Tys as well as ConstAllocations
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanGlScott committed Jun 2, 2023
1 parent 861d52a commit a65ab67
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
15 changes: 10 additions & 5 deletions src/analyz/to_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,17 +286,22 @@ impl Default for ExportStyle {

#[derive(Default, Debug)]
pub struct AllocIntern<'tcx> {
map: HashMap<interpret::ConstAllocation<'tcx>, String>,
/// We key this map on both ConstAllocations and their Tys. Keying the map
/// on ConstAllocations alone is not sufficient, as two ConstAllocations
/// with different types can share the same underlying memory representation
/// (e.g., `0: u8` and `Ok(()) : Result<(), ()>`).
map: HashMap<(interpret::ConstAllocation<'tcx>, ty::Ty<'tcx>), String>,
new_vals: Vec<serde_json::Value>,
}

impl<'tcx> AllocIntern<'tcx> {
pub fn get(&self, alloc: interpret::ConstAllocation<'tcx>) -> Option<&str> {
self.map.get(&alloc).map(|x| x as &str)
pub fn get(&self, alloc: interpret::ConstAllocation<'tcx>, ty: ty::Ty<'tcx>) -> Option<&str> {
self.map.get(&(alloc, ty)).map(|x| x as &str)
}

pub fn insert(&mut self, tcx: TyCtxt<'tcx>,
alloc: interpret::ConstAllocation<'tcx>, mut static_def: serde_json::Value) -> String {
alloc: interpret::ConstAllocation<'tcx>, ty: ty::Ty<'tcx>,
mut static_def: serde_json::Value) -> String {
let crate_name = tcx.crate_name(LOCAL_CRATE);
let disambig = tcx.crate_hash(LOCAL_CRATE);
// NB: The use of :: here is important, as mir-json's dead
Expand All @@ -305,7 +310,7 @@ impl<'tcx> AllocIntern<'tcx> {
let id = format!("{}/{}::{{{{alloc}}}}[{}]", crate_name, disambig, self.map.len());
static_def["name"] = id.clone().into();
self.new_vals.push(static_def);
let old = self.map.insert(alloc, id.clone());
let old = self.map.insert((alloc, ty), id.clone());
assert!(old.is_none(), "duplicate insert for type {:?}", alloc);
id
}
Expand Down
5 changes: 3 additions & 2 deletions src/analyz/ty_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,8 @@ fn try_render_ref_opty<'mir, 'tcx>(
"def_id": def_id.to_json(mir),
})),
interpret::GlobalAlloc::Memory(ca) => {
let aid = match mir.allocs.get(ca) {
let ty = op_ty.layout.ty;
let aid = match mir.allocs.get(ca, ty) {
Some(alloc_id) => alloc_id.to_owned(),
None => {
let ma = tcx.create_memory_alloc(ca);
Expand All @@ -1256,7 +1257,7 @@ fn try_render_ref_opty<'mir, 'tcx>(
"rendered": rendered,
});

mir.allocs.insert(tcx, ca, static_ref)
mir.allocs.insert(tcx, ca, ty, static_ref)
}
};

Expand Down

0 comments on commit a65ab67

Please sign in to comment.