Skip to content

Commit 22d6204

Browse files
committed
use mir::Visitor when collecting alloc_ids in pretty printing
1 parent 8092b90 commit 22d6204

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

compiler/rustc_middle/src/mir/pretty.rs

+19-7
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ use rustc_middle::mir::interpret::{
1717
use rustc_middle::mir::visit::Visitor;
1818
use rustc_middle::mir::MirSource;
1919
use rustc_middle::mir::*;
20-
use rustc_middle::ty::{self, TyCtxt, TypeFoldable, TypeVisitor};
20+
use rustc_middle::ty::{self, TyCtxt};
2121
use rustc_target::abi::Size;
22-
use std::ops::ControlFlow;
2322

2423
const INDENT: &str = " ";
2524
/// Alignment for lining up comments following MIR statements
@@ -669,6 +668,7 @@ pub fn write_allocations<'tcx>(
669668
fn alloc_ids_from_alloc(alloc: &Allocation) -> impl DoubleEndedIterator<Item = AllocId> + '_ {
670669
alloc.relocations().values().map(|id| *id)
671670
}
671+
672672
fn alloc_ids_from_const(val: ConstValue<'_>) -> impl Iterator<Item = AllocId> + '_ {
673673
match val {
674674
ConstValue::Scalar(interpret::Scalar::Ptr(ptr, _size)) => {
@@ -682,17 +682,29 @@ pub fn write_allocations<'tcx>(
682682
}
683683
}
684684
}
685+
685686
struct CollectAllocIds(BTreeSet<AllocId>);
686-
impl<'tcx> TypeVisitor<'tcx> for CollectAllocIds {
687-
fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
688-
if let ty::ConstKind::Value(val) = c.val() {
687+
688+
impl<'tcx> Visitor<'tcx> for CollectAllocIds {
689+
fn visit_const(&mut self, c: &&'tcx ty::Const<'tcx>, _loc: Location) {
690+
if let ty::ConstKind::Value(val) = c.val {
689691
self.0.extend(alloc_ids_from_const(val));
690692
}
691-
c.super_visit_with(self)
693+
}
694+
695+
fn visit_constant(&mut self, c: &Constant<'tcx>, loc: Location) {
696+
match c.literal {
697+
ConstantKind::Ty(c) => self.visit_const(&c, loc),
698+
ConstantKind::Val(val, _) => {
699+
self.0.extend(alloc_ids_from_const(val));
700+
}
701+
}
692702
}
693703
}
704+
694705
let mut visitor = CollectAllocIds(Default::default());
695-
body.visit_with(&mut visitor);
706+
visitor.visit_body(body);
707+
696708
// `seen` contains all seen allocations, including the ones we have *not* printed yet.
697709
// The protocol is to first `insert` into `seen`, and only if that returns `true`
698710
// then push to `todo`.

0 commit comments

Comments
 (0)