Skip to content

Commit 9ab2ed2

Browse files
committed
Attempt to print liveness results for yield expressions. Currently fails because this creates a type checking cycle.
1 parent f7276ca commit 9ab2ed2

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl<'hir> Map<'hir> {
179179
pub fn local_def_id(&self, hir_id: HirId) -> LocalDefId {
180180
self.opt_local_def_id(hir_id).unwrap_or_else(|| {
181181
bug!(
182-
"local_def_id: no entry for `{:?}`, which has a map of `{:?}`",
182+
"local_def_id: no entry for `{:#?}`, which has a map of `{:#?}`",
183183
hir_id,
184184
self.find(hir_id)
185185
)

compiler/rustc_passes/src/liveness.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ enum VarKind {
192192
pub struct IrMaps<'tcx> {
193193
tcx: TyCtxt<'tcx>,
194194
live_node_map: HirIdMap<LiveNode>,
195-
variable_map: HirIdMap<Variable>,
195+
pub variable_map: HirIdMap<Variable>,
196196
capture_info_map: HirIdMap<Rc<Vec<CaptureInfo>>>,
197197
var_kinds: IndexVec<Variable, VarKind>,
198198
lnks: IndexVec<LiveNode, LiveNodeKind>,
@@ -248,7 +248,7 @@ impl IrMaps<'tcx> {
248248
}
249249
}
250250

251-
fn variable_name(&self, var: Variable) -> Symbol {
251+
pub fn variable_name(&self, var: Variable) -> Symbol {
252252
match self.var_kinds[var] {
253253
Local(LocalInfo { name, .. }) | Param(_, name) | Upvar(_, name) => name,
254254
}
@@ -481,7 +481,7 @@ pub fn compute_body_liveness(
481481
body_id: hir::BodyId,
482482
) -> Liveness<'a, 'tcx> {
483483
let body = maps.tcx.hir().body(body_id);
484-
let local_def_id = maps.tcx.hir().local_def_id(body_id.hir_id);
484+
let local_def_id = maps.tcx.hir().local_def_id(maps.tcx.hir().body_owner(body_id));
485485

486486
// gather up the various local variables, significant expressions,
487487
// and so forth:
@@ -505,7 +505,7 @@ const ACC_WRITE: u32 = 2;
505505
const ACC_USE: u32 = 4;
506506

507507
pub struct Liveness<'a, 'tcx> {
508-
ir: &'a mut IrMaps<'tcx>,
508+
pub ir: &'a mut IrMaps<'tcx>,
509509
typeck_results: &'a ty::TypeckResults<'tcx>,
510510
param_env: ty::ParamEnv<'tcx>,
511511
closure_min_captures: Option<&'tcx RootVariableMinCaptureList<'tcx>>,
@@ -552,7 +552,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
552552
}
553553
}
554554

555-
fn live_node(&self, hir_id: HirId, span: Span) -> LiveNode {
555+
pub fn live_node(&self, hir_id: HirId, span: Span) -> LiveNode {
556556
match self.ir.live_node_map.get(&hir_id) {
557557
Some(&ln) => ln,
558558
None => {
@@ -583,7 +583,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
583583
succ
584584
}
585585

586-
fn live_on_entry(&self, ln: LiveNode, var: Variable) -> bool {
586+
pub fn live_on_entry(&self, ln: LiveNode, var: Variable) -> bool {
587587
self.rwu_table.get_reader(ln, var)
588588
}
589589

compiler/rustc_typeck/src/check/generator_interior.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
1313
use rustc_hir::{Arm, Expr, ExprKind, Guard, HirId, Pat, PatKind};
1414
use rustc_middle::middle::region::{self, YieldData};
1515
use rustc_middle::ty::{self, Ty};
16-
use rustc_passes::liveness::{compute_body_liveness, IrMaps};
16+
use rustc_passes::liveness::{compute_body_liveness, IrMaps, Liveness};
1717
use rustc_span::Span;
1818
use smallvec::SmallVec;
1919

@@ -31,6 +31,7 @@ struct InteriorVisitor<'a, 'tcx> {
3131
/// that they may succeed the said yield point in the post-order.
3232
guard_bindings: SmallVec<[SmallVec<[HirId; 4]>; 1]>,
3333
guard_bindings_set: HirIdSet,
34+
liveness: Liveness<'a, 'tcx>,
3435
}
3536

3637
impl<'a, 'tcx> InteriorVisitor<'a, 'tcx> {
@@ -156,7 +157,7 @@ pub fn resolve_interior<'a, 'tcx>(
156157
) {
157158
let mut ir_maps = IrMaps::new(fcx.tcx);
158159
// FIXME: use this to inform capture information
159-
let _liveness = compute_body_liveness(&mut ir_maps, body_id);
160+
let liveness = compute_body_liveness(&mut ir_maps, body_id);
160161
let body = fcx.tcx.hir().body(body_id);
161162
let mut visitor = InteriorVisitor {
162163
fcx,
@@ -167,6 +168,7 @@ pub fn resolve_interior<'a, 'tcx>(
167168
prev_unresolved_span: None,
168169
guard_bindings: <_>::default(),
169170
guard_bindings_set: <_>::default(),
171+
liveness,
170172
};
171173
intravisit::walk_body(&mut visitor, body);
172174

@@ -336,6 +338,21 @@ impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> {
336338
_ => {}
337339
}
338340
}
341+
ExprKind::Yield(..) => {
342+
let live_node = self.liveness.live_node(expr.hir_id, expr.span);
343+
344+
for (_, &var) in self.liveness.ir.variable_map.iter() {
345+
if self.liveness.live_on_entry(live_node, var) {
346+
debug!(
347+
"Variable {:?} is live on entry at {:?}",
348+
self.liveness.ir.variable_name(var),
349+
expr.span
350+
);
351+
}
352+
}
353+
354+
intravisit::walk_expr(self, expr)
355+
}
339356
_ => intravisit::walk_expr(self, expr),
340357
}
341358

0 commit comments

Comments
 (0)