Skip to content

Commit 28ddd7a

Browse files
committed
rustc: use hir::ItemLocalId instead of ast::NodeId in CFG.
1 parent 45d31e7 commit 28ddd7a

File tree

13 files changed

+249
-216
lines changed

13 files changed

+249
-216
lines changed

Diff for: src/librustc/cfg/construct.rs

+44-45
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use rustc_data_structures::graph;
1212
use cfg::*;
1313
use middle::region::CodeExtent;
1414
use ty::{self, TyCtxt};
15-
use syntax::ast;
1615
use syntax::ptr::P;
1716

1817
use hir::{self, PatKind};
@@ -30,13 +29,13 @@ struct CFGBuilder<'a, 'tcx: 'a> {
3029

3130
#[derive(Copy, Clone)]
3231
struct BlockScope {
33-
block_expr_id: ast::NodeId, // id of breakable block expr node
32+
block_expr_id: hir::ItemLocalId, // id of breakable block expr node
3433
break_index: CFGIndex, // where to go on `break`
3534
}
3635

3736
#[derive(Copy, Clone)]
3837
struct LoopScope {
39-
loop_id: ast::NodeId, // id of loop/while node
38+
loop_id: hir::ItemLocalId, // id of loop/while node
4039
continue_index: CFGIndex, // where to go on a `loop`
4140
break_index: CFGIndex, // where to go on a `break`
4241
}
@@ -70,6 +69,7 @@ pub fn construct<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
7069
cfg_builder.add_contained_edge(body_exit, fn_exit);
7170
let CFGBuilder { graph, .. } = cfg_builder;
7271
CFG {
72+
owner_def_id,
7373
graph,
7474
entry,
7575
exit: fn_exit,
@@ -79,10 +79,10 @@ pub fn construct<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
7979
impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
8080
fn block(&mut self, blk: &hir::Block, pred: CFGIndex) -> CFGIndex {
8181
if blk.targeted_by_break {
82-
let expr_exit = self.add_ast_node(blk.id, &[]);
82+
let expr_exit = self.add_ast_node(blk.hir_id.local_id, &[]);
8383

8484
self.breakable_block_scopes.push(BlockScope {
85-
block_expr_id: blk.id,
85+
block_expr_id: blk.hir_id.local_id,
8686
break_index: expr_exit,
8787
});
8888

@@ -104,21 +104,22 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
104104

105105
let expr_exit = self.opt_expr(&blk.expr, stmts_exit);
106106

107-
self.add_ast_node(blk.id, &[expr_exit])
107+
self.add_ast_node(blk.hir_id.local_id, &[expr_exit])
108108
}
109109
}
110110

111111
fn stmt(&mut self, stmt: &hir::Stmt, pred: CFGIndex) -> CFGIndex {
112+
let hir_id = self.tcx.hir.node_to_hir_id(stmt.node.id());
112113
match stmt.node {
113-
hir::StmtDecl(ref decl, id) => {
114+
hir::StmtDecl(ref decl, _) => {
114115
let exit = self.decl(&decl, pred);
115-
self.add_ast_node(id, &[exit])
116+
self.add_ast_node(hir_id.local_id, &[exit])
116117
}
117118

118-
hir::StmtExpr(ref expr, id) |
119-
hir::StmtSemi(ref expr, id) => {
119+
hir::StmtExpr(ref expr, _) |
120+
hir::StmtSemi(ref expr, _) => {
120121
let exit = self.expr(&expr, pred);
121-
self.add_ast_node(id, &[exit])
122+
self.add_ast_node(hir_id.local_id, &[exit])
122123
}
123124
}
124125
}
@@ -140,31 +141,31 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
140141
PatKind::Path(_) |
141142
PatKind::Lit(..) |
142143
PatKind::Range(..) |
143-
PatKind::Wild => self.add_ast_node(pat.id, &[pred]),
144+
PatKind::Wild => self.add_ast_node(pat.hir_id.local_id, &[pred]),
144145

145146
PatKind::Box(ref subpat) |
146147
PatKind::Ref(ref subpat, _) |
147148
PatKind::Binding(.., Some(ref subpat)) => {
148149
let subpat_exit = self.pat(&subpat, pred);
149-
self.add_ast_node(pat.id, &[subpat_exit])
150+
self.add_ast_node(pat.hir_id.local_id, &[subpat_exit])
150151
}
151152

152153
PatKind::TupleStruct(_, ref subpats, _) |
153154
PatKind::Tuple(ref subpats, _) => {
154155
let pats_exit = self.pats_all(subpats.iter(), pred);
155-
self.add_ast_node(pat.id, &[pats_exit])
156+
self.add_ast_node(pat.hir_id.local_id, &[pats_exit])
156157
}
157158

158159
PatKind::Struct(_, ref subpats, _) => {
159160
let pats_exit = self.pats_all(subpats.iter().map(|f| &f.node.pat), pred);
160-
self.add_ast_node(pat.id, &[pats_exit])
161+
self.add_ast_node(pat.hir_id.local_id, &[pats_exit])
161162
}
162163

163164
PatKind::Slice(ref pre, ref vec, ref post) => {
164165
let pre_exit = self.pats_all(pre.iter(), pred);
165166
let vec_exit = self.pats_all(vec.iter(), pre_exit);
166167
let post_exit = self.pats_all(post.iter(), vec_exit);
167-
self.add_ast_node(pat.id, &[post_exit])
168+
self.add_ast_node(pat.hir_id.local_id, &[post_exit])
168169
}
169170
}
170171
}
@@ -180,7 +181,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
180181
match expr.node {
181182
hir::ExprBlock(ref blk) => {
182183
let blk_exit = self.block(&blk, pred);
183-
self.add_ast_node(expr.id, &[blk_exit])
184+
self.add_ast_node(expr.hir_id.local_id, &[blk_exit])
184185
}
185186

186187
hir::ExprIf(ref cond, ref then, None) => {
@@ -200,7 +201,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
200201
//
201202
let cond_exit = self.expr(&cond, pred); // 1
202203
let then_exit = self.expr(&then, cond_exit); // 2
203-
self.add_ast_node(expr.id, &[cond_exit, then_exit]) // 3,4
204+
self.add_ast_node(expr.hir_id.local_id, &[cond_exit, then_exit]) // 3,4
204205
}
205206

206207
hir::ExprIf(ref cond, ref then, Some(ref otherwise)) => {
@@ -221,7 +222,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
221222
let cond_exit = self.expr(&cond, pred); // 1
222223
let then_exit = self.expr(&then, cond_exit); // 2
223224
let else_exit = self.expr(&otherwise, cond_exit); // 3
224-
self.add_ast_node(expr.id, &[then_exit, else_exit]) // 4, 5
225+
self.add_ast_node(expr.hir_id.local_id, &[then_exit, else_exit]) // 4, 5
225226
}
226227

227228
hir::ExprWhile(ref cond, ref body, _) => {
@@ -245,12 +246,12 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
245246
let loopback = self.add_dummy_node(&[pred]); // 1
246247

247248
// Create expr_exit without pred (cond_exit)
248-
let expr_exit = self.add_ast_node(expr.id, &[]); // 3
249+
let expr_exit = self.add_ast_node(expr.hir_id.local_id, &[]); // 3
249250

250251
// The LoopScope needs to be on the loop_scopes stack while evaluating the
251252
// condition and the body of the loop (both can break out of the loop)
252253
self.loop_scopes.push(LoopScope {
253-
loop_id: expr.id,
254+
loop_id: expr.hir_id.local_id,
254255
continue_index: loopback,
255256
break_index: expr_exit
256257
});
@@ -282,9 +283,9 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
282283
// may cause additional edges.
283284

284285
let loopback = self.add_dummy_node(&[pred]); // 1
285-
let expr_exit = self.add_ast_node(expr.id, &[]); // 2
286+
let expr_exit = self.add_ast_node(expr.hir_id.local_id, &[]); // 2
286287
self.loop_scopes.push(LoopScope {
287-
loop_id: expr.id,
288+
loop_id: expr.hir_id.local_id,
288289
continue_index: loopback,
289290
break_index: expr_exit,
290291
});
@@ -295,7 +296,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
295296
}
296297

297298
hir::ExprMatch(ref discr, ref arms, _) => {
298-
self.match_(expr.id, &discr, &arms, pred)
299+
self.match_(expr.hir_id.local_id, &discr, &arms, pred)
299300
}
300301

301302
hir::ExprBinary(op, ref l, ref r) if op.node.is_lazy() => {
@@ -315,30 +316,30 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
315316
//
316317
let l_exit = self.expr(&l, pred); // 1
317318
let r_exit = self.expr(&r, l_exit); // 2
318-
self.add_ast_node(expr.id, &[l_exit, r_exit]) // 3,4
319+
self.add_ast_node(expr.hir_id.local_id, &[l_exit, r_exit]) // 3,4
319320
}
320321

321322
hir::ExprRet(ref v) => {
322323
let v_exit = self.opt_expr(v, pred);
323-
let b = self.add_ast_node(expr.id, &[v_exit]);
324+
let b = self.add_ast_node(expr.hir_id.local_id, &[v_exit]);
324325
self.add_returning_edge(expr, b);
325326
self.add_unreachable_node()
326327
}
327328

328329
hir::ExprBreak(destination, ref opt_expr) => {
329330
let v = self.opt_expr(opt_expr, pred);
330-
let (scope_id, break_dest) =
331+
let (target_scope, break_dest) =
331332
self.find_scope_edge(expr, destination, ScopeCfKind::Break);
332-
let b = self.add_ast_node(expr.id, &[v]);
333-
self.add_exiting_edge(expr, b, scope_id, break_dest);
333+
let b = self.add_ast_node(expr.hir_id.local_id, &[v]);
334+
self.add_exiting_edge(expr, b, target_scope, break_dest);
334335
self.add_unreachable_node()
335336
}
336337

337338
hir::ExprAgain(destination) => {
338-
let (scope_id, cont_dest) =
339+
let (target_scope, cont_dest) =
339340
self.find_scope_edge(expr, destination, ScopeCfKind::Continue);
340-
let a = self.add_ast_node(expr.id, &[pred]);
341-
self.add_exiting_edge(expr, a, scope_id, cont_dest);
341+
let a = self.add_ast_node(expr.hir_id.local_id, &[pred]);
342+
self.add_exiting_edge(expr, a, target_scope, cont_dest);
342343
self.add_unreachable_node()
343344
}
344345

@@ -397,7 +398,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
397398
hir::ExprInlineAsm(_, ref outputs, ref inputs) => {
398399
let post_outputs = self.exprs(outputs.iter().map(|e| &*e), pred);
399400
let post_inputs = self.exprs(inputs.iter().map(|e| &*e), post_outputs);
400-
self.add_ast_node(expr.id, &[post_inputs])
401+
self.add_ast_node(expr.hir_id.local_id, &[post_inputs])
401402
}
402403

403404
hir::ExprClosure(..) |
@@ -444,10 +445,10 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
444445
//! Handles case of an expression that evaluates `subexprs` in order
445446
446447
let subexprs_exit = self.exprs(subexprs, pred);
447-
self.add_ast_node(expr.id, &[subexprs_exit])
448+
self.add_ast_node(expr.hir_id.local_id, &[subexprs_exit])
448449
}
449450

450-
fn match_(&mut self, id: ast::NodeId, discr: &hir::Expr,
451+
fn match_(&mut self, id: hir::ItemLocalId, discr: &hir::Expr,
451452
arms: &[hir::Arm], pred: CFGIndex) -> CFGIndex {
452453
// The CFG for match expression is quite complex, so no ASCII
453454
// art for it (yet).
@@ -552,8 +553,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
552553
self.add_node(CFGNodeData::Dummy, preds)
553554
}
554555

555-
fn add_ast_node(&mut self, id: ast::NodeId, preds: &[CFGIndex]) -> CFGIndex {
556-
assert!(id != ast::DUMMY_NODE_ID);
556+
fn add_ast_node(&mut self, id: hir::ItemLocalId, preds: &[CFGIndex]) -> CFGIndex {
557557
self.add_node(CFGNodeData::AST(id), preds)
558558
}
559559

@@ -579,14 +579,13 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
579579
fn add_exiting_edge(&mut self,
580580
from_expr: &hir::Expr,
581581
from_index: CFGIndex,
582-
scope_id: ast::NodeId,
582+
target_scope: CodeExtent,
583583
to_index: CFGIndex) {
584584
let mut data = CFGEdgeData { exiting_scopes: vec![] };
585585
let mut scope = CodeExtent::Misc(from_expr.id);
586-
let target_scope = CodeExtent::Misc(scope_id);
587586
let region_maps = self.tcx.region_maps(self.owner_def_id);
588587
while scope != target_scope {
589-
data.exiting_scopes.push(scope.node_id());
588+
data.exiting_scopes.push(self.tcx.hir.node_to_hir_id(scope.node_id()).local_id);
590589
scope = region_maps.encl_scope(scope);
591590
}
592591
self.graph.add_edge(from_index, to_index, data);
@@ -607,13 +606,13 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
607606
fn find_scope_edge(&self,
608607
expr: &hir::Expr,
609608
destination: hir::Destination,
610-
scope_cf_kind: ScopeCfKind) -> (ast::NodeId, CFGIndex) {
609+
scope_cf_kind: ScopeCfKind) -> (CodeExtent, CFGIndex) {
611610

612611
match destination.target_id {
613612
hir::ScopeTarget::Block(block_expr_id) => {
614613
for b in &self.breakable_block_scopes {
615-
if b.block_expr_id == block_expr_id {
616-
return (block_expr_id, match scope_cf_kind {
614+
if b.block_expr_id == self.tcx.hir.node_to_hir_id(block_expr_id).local_id {
615+
return (CodeExtent::Misc(block_expr_id), match scope_cf_kind {
617616
ScopeCfKind::Break => b.break_index,
618617
ScopeCfKind::Continue => bug!("can't continue to block"),
619618
});
@@ -623,8 +622,8 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
623622
}
624623
hir::ScopeTarget::Loop(hir::LoopIdResult::Ok(loop_id)) => {
625624
for l in &self.loop_scopes {
626-
if l.loop_id == loop_id {
627-
return (loop_id, match scope_cf_kind {
625+
if l.loop_id == self.tcx.hir.node_to_hir_id(loop_id).local_id {
626+
return (CodeExtent::Misc(loop_id), match scope_cf_kind {
628627
ScopeCfKind::Break => l.break_index,
629628
ScopeCfKind::Continue => l.continue_index,
630629
});

Diff for: src/librustc/cfg/graphviz.rs

+33-30
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,47 @@
1515
use graphviz as dot;
1616
use graphviz::IntoCow;
1717

18-
use syntax::ast;
19-
20-
use hir::map as hir_map;
2118
use cfg;
19+
use hir;
20+
use ty::TyCtxt;
2221

2322
pub type Node<'a> = (cfg::CFGIndex, &'a cfg::CFGNode);
2423
pub type Edge<'a> = &'a cfg::CFGEdge;
2524

26-
pub struct LabelledCFG<'a, 'hir: 'a> {
27-
pub hir_map: &'a hir_map::Map<'hir>,
25+
pub struct LabelledCFG<'a, 'tcx: 'a> {
26+
pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
2827
pub cfg: &'a cfg::CFG,
2928
pub name: String,
3029
/// `labelled_edges` controls whether we emit labels on the edges
3130
pub labelled_edges: bool,
3231
}
3332

34-
fn replace_newline_with_backslash_l(s: String) -> String {
35-
// Replacing newlines with \\l causes each line to be left-aligned,
36-
// improving presentation of (long) pretty-printed expressions.
37-
if s.contains("\n") {
38-
let mut s = s.replace("\n", "\\l");
39-
// Apparently left-alignment applies to the line that precedes
40-
// \l, not the line that follows; so, add \l at end of string
41-
// if not already present, ensuring last line gets left-aligned
42-
// as well.
43-
let mut last_two: Vec<_> =
44-
s.chars().rev().take(2).collect();
45-
last_two.reverse();
46-
if last_two != ['\\', 'l'] {
47-
s.push_str("\\l");
33+
impl<'a, 'tcx> LabelledCFG<'a, 'tcx> {
34+
fn local_id_to_string(&self, local_id: hir::ItemLocalId) -> String {
35+
let node_id = self.tcx.hir.hir_to_node_id(hir::HirId {
36+
owner: self.tcx.closure_base_def_id(self.cfg.owner_def_id).index,
37+
local_id
38+
});
39+
let s = self.tcx.hir.node_to_string(node_id);
40+
41+
// Replacing newlines with \\l causes each line to be left-aligned,
42+
// improving presentation of (long) pretty-printed expressions.
43+
if s.contains("\n") {
44+
let mut s = s.replace("\n", "\\l");
45+
// Apparently left-alignment applies to the line that precedes
46+
// \l, not the line that follows; so, add \l at end of string
47+
// if not already present, ensuring last line gets left-aligned
48+
// as well.
49+
let mut last_two: Vec<_> =
50+
s.chars().rev().take(2).collect();
51+
last_two.reverse();
52+
if last_two != ['\\', 'l'] {
53+
s.push_str("\\l");
54+
}
55+
s
56+
} else {
57+
s
4858
}
49-
s
50-
} else {
51-
s
5259
}
5360
}
5461

@@ -66,12 +73,10 @@ impl<'a, 'hir> dot::Labeller<'a> for LabelledCFG<'a, 'hir> {
6673
dot::LabelText::LabelStr("entry".into_cow())
6774
} else if i == self.cfg.exit {
6875
dot::LabelText::LabelStr("exit".into_cow())
69-
} else if n.data.id() == ast::DUMMY_NODE_ID {
76+
} else if n.data.id() == hir::DUMMY_ITEM_LOCAL_ID {
7077
dot::LabelText::LabelStr("(dummy_node)".into_cow())
7178
} else {
72-
let s = self.hir_map.node_to_string(n.data.id());
73-
// left-aligns the lines
74-
let s = replace_newline_with_backslash_l(s);
79+
let s = self.local_id_to_string(n.data.id());
7580
dot::LabelText::EscStr(s.into_cow())
7681
}
7782
}
@@ -82,15 +87,13 @@ impl<'a, 'hir> dot::Labeller<'a> for LabelledCFG<'a, 'hir> {
8287
return dot::LabelText::EscStr(label.into_cow());
8388
}
8489
let mut put_one = false;
85-
for (i, &node_id) in e.data.exiting_scopes.iter().enumerate() {
90+
for (i, &id) in e.data.exiting_scopes.iter().enumerate() {
8691
if put_one {
8792
label.push_str(",\\l");
8893
} else {
8994
put_one = true;
9095
}
91-
let s = self.hir_map.node_to_string(node_id);
92-
// left-aligns the lines
93-
let s = replace_newline_with_backslash_l(s);
96+
let s = self.local_id_to_string(id);
9497
label.push_str(&format!("exiting scope_{} {}",
9598
i,
9699
&s[..]));

0 commit comments

Comments
 (0)