Skip to content

Commit 3164c2a

Browse files
committed
Make logging for drop-tracking easier to read.
Some of these are a little questionable because the output is so much longer, but I would really love to keep the bit that adds the pretty-printed expression to the generated CFG .dot file. Before: ``` DEBUG rustc_typeck::check::generator_interior::drop_ranges::record_consumed_borrow consume PlaceWithHirId { hir_id: HirId { owner: DefId(0:7 ~ default_struct_update[79f9]::foo), local_id: 15 }, place: Place { base_ty: impl std::future::Future<Output = ()>, base: Rvalue, projections: [] } }; diag_expr_id=HirId { owner: DefId(0:7 ~ default_struct_update[79f9]::foo), local_id: 15 }, using parent expr HirId { owner: DefId(0:7 ~ default_struct_update[79f9]::foo), local_id: 49 } ``` After: ``` DEBUG rustc_typeck::check::generator_interior::drop_ranges::record_consumed_borrow consume PlaceWithHirId { hir_id: HirId { owner: DefId(0:7 ~ default_struct_update[79f9]::foo), local_id: 15 }, place: Place { base_ty: impl std::future::Future<Output = ()>, base: Rvalue, projections: [] } }; diag_expr_id=expr from_config(Config { nickname: None, ..Default::default() }) (hir_id=HirId { owner: DefId(0:7 ~ default_struct_update[79f9]::foo), local_id: 15 }), using parent expr .await (hir_id=HirId { owner: DefId(0:7 ~ default_struct_update[79f9]::foo), local_id: 49 }) ```
1 parent 483ee1f commit 3164c2a

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

compiler/rustc_typeck/src/check/generator_interior/drop_ranges.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ rustc_index::newtype_index! {
109109
}
110110

111111
/// Identifies a value whose drop state we need to track.
112-
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
112+
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
113113
enum TrackedValue {
114114
/// Represents a named variable, such as a let binding, parameter, or upvar.
115115
///
@@ -121,6 +121,21 @@ enum TrackedValue {
121121
Temporary(HirId),
122122
}
123123

124+
impl Debug for TrackedValue {
125+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
126+
ty::tls::with_opt(|opt_tcx| {
127+
if let Some(tcx) = opt_tcx {
128+
write!(f, "{}", tcx.hir().node_to_string(self.hir_id()))
129+
} else {
130+
match self {
131+
Self::Variable(hir_id) => write!(f, "Variable({:?})", hir_id),
132+
Self::Temporary(hir_id) => write!(f, "Temporary({:?})", hir_id),
133+
}
134+
}
135+
})
136+
}
137+
}
138+
124139
impl TrackedValue {
125140
fn hir_id(&self) -> HirId {
126141
match self {
@@ -148,7 +163,7 @@ enum TrackedValueConversionError {
148163
/// Place projects are not currently supported.
149164
///
150165
/// The reasoning around these is kind of subtle, so we choose to be more
151-
/// conservative around these for now. There is not reason in theory we
166+
/// conservative around these for now. There is no reason in theory we
152167
/// cannot support these, we just have not implemented it yet.
153168
PlaceProjectionsNotSupported,
154169
}

compiler/rustc_typeck/src/check/generator_interior/drop_ranges/cfg_build.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,14 @@ impl<'a, 'tcx> DropRangeVisitor<'a, 'tcx> {
129129
/// ExprUseVisitor's consume callback doesn't go deep enough for our purposes in all
130130
/// expressions. This method consumes a little deeper into the expression when needed.
131131
fn consume_expr(&mut self, expr: &hir::Expr<'_>) {
132-
debug!("consuming expr {:?}, count={:?}", expr.hir_id, self.expr_index);
132+
debug!("consuming expr {:?}, count={:?}", expr.kind, self.expr_index);
133133
let places = self
134134
.places
135135
.consumed
136136
.get(&expr.hir_id)
137137
.map_or(vec![], |places| places.iter().cloned().collect());
138138
for place in places {
139+
trace!(?place, "consuming place");
139140
for_each_consumable(self.hir, place, |value| self.record_drop(value));
140141
}
141142
}

compiler/rustc_typeck/src/check/generator_interior/drop_ranges/cfg_visualize.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,18 @@ impl<'a> dot::Labeller<'a> for DropRangesGraph<'_, '_> {
7474

7575
fn node_label(&'a self, n: &Self::Node) -> dot::LabelText<'a> {
7676
dot::LabelText::LabelStr(
77-
self.drop_ranges
78-
.post_order_map
79-
.iter()
80-
.find(|(_hir_id, &post_order_id)| post_order_id == *n)
81-
.map_or("<unknown>".into(), |(hir_id, _)| self.tcx.hir().node_to_string(*hir_id))
82-
.into(),
77+
format!(
78+
"{n:?}: {}",
79+
self.drop_ranges
80+
.post_order_map
81+
.iter()
82+
.find(|(_hir_id, &post_order_id)| post_order_id == *n)
83+
.map_or("<unknown>".into(), |(hir_id, _)| self
84+
.tcx
85+
.hir()
86+
.node_to_string(*hir_id))
87+
)
88+
.into(),
8389
)
8490
}
8591
}

compiler/rustc_typeck/src/check/generator_interior/drop_ranges/record_consumed_borrow.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ impl<'tcx> ExprUseDelegate<'tcx> {
7575
if !self.places.consumed.contains_key(&consumer) {
7676
self.places.consumed.insert(consumer, <_>::default());
7777
}
78+
debug!(?consumer, ?target, "mark_consumed");
7879
self.places.consumed.get_mut(&consumer).map(|places| places.insert(target));
7980
}
8081

@@ -136,13 +137,16 @@ impl<'tcx> expr_use_visitor::Delegate<'tcx> for ExprUseDelegate<'tcx> {
136137
place_with_id: &expr_use_visitor::PlaceWithHirId<'tcx>,
137138
diag_expr_id: HirId,
138139
) {
139-
let parent = match self.tcx.hir().find_parent_node(place_with_id.hir_id) {
140+
let hir = self.tcx.hir();
141+
let parent = match hir.find_parent_node(place_with_id.hir_id) {
140142
Some(parent) => parent,
141143
None => place_with_id.hir_id,
142144
};
143145
debug!(
144-
"consume {:?}; diag_expr_id={:?}, using parent {:?}",
145-
place_with_id, diag_expr_id, parent
146+
"consume {:?}; diag_expr_id={}, using parent {}",
147+
place_with_id,
148+
hir.node_to_string(diag_expr_id),
149+
hir.node_to_string(parent)
146150
);
147151
place_with_id
148152
.try_into()

0 commit comments

Comments
 (0)