Skip to content

Commit b2ce5a9

Browse files
committed
Make hir::Stmt a separate struct.
Benefits: - It lets us move the `NodeId` field out of every `hir::StmtKind` variant `NodeId` to a more sensible spot. - It eliminates sadness in `Stmt::fmt`. - It makes `hir::Stmt` match `ast::Stmt`.
1 parent e2f221c commit b2ce5a9

File tree

16 files changed

+92
-87
lines changed

16 files changed

+92
-87
lines changed

src/librustc/cfg/construct.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
9999
}
100100

101101
fn stmt(&mut self, stmt: &hir::Stmt, pred: CFGIndex) -> CFGIndex {
102-
let hir_id = self.tcx.hir().node_to_hir_id(stmt.node.id());
102+
let hir_id = self.tcx.hir().node_to_hir_id(stmt.id);
103103
match stmt.node {
104-
hir::StmtKind::Decl(ref decl, _) => {
104+
hir::StmtKind::Decl(ref decl) => {
105105
let exit = self.decl(&decl, pred);
106106
self.add_ast_node(hir_id.local_id, &[exit])
107107
}
108108

109-
hir::StmtKind::Expr(ref expr, _) |
110-
hir::StmtKind::Semi(ref expr, _) => {
109+
hir::StmtKind::Expr(ref expr) |
110+
hir::StmtKind::Semi(ref expr) => {
111111
let exit = self.expr(&expr, pred);
112112
self.add_ast_node(hir_id.local_id, &[exit])
113113
}

src/librustc/hir/check_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
298298

299299
fn check_stmt_attributes(&self, stmt: &hir::Stmt) {
300300
// When checking statements ignore expressions, they will be checked later
301-
if let hir::StmtKind::Decl(_, _) = stmt.node {
301+
if let hir::StmtKind::Decl(..) = stmt.node {
302302
for attr in stmt.node.attrs() {
303303
if attr.check_name("inline") {
304304
self.check_inline(attr, &stmt.span, Target::Statement);

src/librustc/hir/intravisit.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -953,14 +953,13 @@ pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
953953
}
954954

955955
pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
956+
visitor.visit_id(statement.id);
956957
match statement.node {
957-
StmtKind::Decl(ref declaration, id) => {
958-
visitor.visit_id(id);
958+
StmtKind::Decl(ref declaration) => {
959959
visitor.visit_decl(declaration)
960960
}
961-
StmtKind::Expr(ref expression, id) |
962-
StmtKind::Semi(ref expression, id) => {
963-
visitor.visit_id(id);
961+
StmtKind::Expr(ref expression) |
962+
StmtKind::Semi(ref expression) => {
964963
visitor.visit_expr(expression)
965964
}
966965
}

src/librustc/hir/lowering.rs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4331,10 +4331,11 @@ impl<'a> LoweringContext<'a> {
43314331
ThinVec::new(),
43324332
))
43334333
};
4334-
let match_stmt = respan(
4335-
head_sp,
4336-
hir::StmtKind::Expr(match_expr, self.next_id().node_id)
4337-
);
4334+
let match_stmt = hir::Stmt {
4335+
id: self.next_id().node_id,
4336+
node: hir::StmtKind::Expr(match_expr),
4337+
span: head_sp,
4338+
};
43384339

43394340
let next_expr = P(self.expr_ident(head_sp, next_ident, next_pat.id));
43404341

@@ -4357,10 +4358,11 @@ impl<'a> LoweringContext<'a> {
43574358

43584359
let body_block = self.with_loop_scope(e.id, |this| this.lower_block(body, false));
43594360
let body_expr = P(self.expr_block(body_block, ThinVec::new()));
4360-
let body_stmt = respan(
4361-
body.span,
4362-
hir::StmtKind::Expr(body_expr, self.next_id().node_id)
4363-
);
4361+
let body_stmt = hir::Stmt {
4362+
id: self.next_id().node_id,
4363+
node: hir::StmtKind::Expr(body_expr),
4364+
span: body.span,
4365+
};
43644366

43654367
let loop_block = P(self.block_all(
43664368
e.span,
@@ -4533,24 +4535,24 @@ impl<'a> LoweringContext<'a> {
45334535
let (l, item_ids) = self.lower_local(l);
45344536
let mut ids: SmallVec<[hir::Stmt; 1]> = item_ids
45354537
.into_iter()
4536-
.map(|item_id| Spanned {
4538+
.map(|item_id| hir::Stmt {
4539+
id: self.next_id().node_id,
45374540
node: hir::StmtKind::Decl(
45384541
P(Spanned {
45394542
node: hir::DeclKind::Item(item_id),
45404543
span: s.span,
45414544
}),
4542-
self.next_id().node_id,
45434545
),
45444546
span: s.span,
45454547
})
45464548
.collect();
4547-
ids.push(Spanned {
4549+
ids.push(hir::Stmt {
4550+
id: self.lower_node_id(s.id).node_id,
45484551
node: hir::StmtKind::Decl(
45494552
P(Spanned {
45504553
node: hir::DeclKind::Local(l),
45514554
span: s.span,
45524555
}),
4553-
self.lower_node_id(s.id).node_id,
45544556
),
45554557
span: s.span,
45564558
});
@@ -4561,26 +4563,28 @@ impl<'a> LoweringContext<'a> {
45614563
let mut id = Some(s.id);
45624564
return self.lower_item_id(it)
45634565
.into_iter()
4564-
.map(|item_id| Spanned {
4566+
.map(|item_id| hir::Stmt {
4567+
id: id.take()
4568+
.map(|id| self.lower_node_id(id).node_id)
4569+
.unwrap_or_else(|| self.next_id().node_id),
45654570
node: hir::StmtKind::Decl(
45664571
P(Spanned {
45674572
node: hir::DeclKind::Item(item_id),
45684573
span: s.span,
45694574
}),
4570-
id.take()
4571-
.map(|id| self.lower_node_id(id).node_id)
4572-
.unwrap_or_else(|| self.next_id().node_id),
45734575
),
45744576
span: s.span,
45754577
})
45764578
.collect();
45774579
}
4578-
StmtKind::Expr(ref e) => Spanned {
4579-
node: hir::StmtKind::Expr(P(self.lower_expr(e)), self.lower_node_id(s.id).node_id),
4580+
StmtKind::Expr(ref e) => hir::Stmt {
4581+
id: self.lower_node_id(s.id).node_id,
4582+
node: hir::StmtKind::Expr(P(self.lower_expr(e))),
45804583
span: s.span,
45814584
},
4582-
StmtKind::Semi(ref e) => Spanned {
4583-
node: hir::StmtKind::Semi(P(self.lower_expr(e)), self.lower_node_id(s.id).node_id),
4585+
StmtKind::Semi(ref e) => hir::Stmt {
4586+
id: self.lower_node_id(s.id).node_id,
4587+
node: hir::StmtKind::Semi(P(self.lower_expr(e))),
45844588
span: s.span,
45854589
},
45864590
StmtKind::Mac(..) => panic!("Shouldn't exist here"),
@@ -4806,7 +4810,11 @@ impl<'a> LoweringContext<'a> {
48064810
source,
48074811
});
48084812
let decl = respan(sp, hir::DeclKind::Local(local));
4809-
respan(sp, hir::StmtKind::Decl(P(decl), self.next_id().node_id))
4813+
hir::Stmt {
4814+
id: self.next_id().node_id,
4815+
node: hir::StmtKind::Decl(P(decl)),
4816+
span: sp
4817+
}
48104818
}
48114819

48124820
fn stmt_let(

src/librustc/hir/map/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
426426
}
427427

428428
fn visit_stmt(&mut self, stmt: &'hir Stmt) {
429-
let id = stmt.node.id();
429+
let id = stmt.id;
430430
self.insert(stmt.span, id, Node::Stmt(stmt));
431431

432432
self.with_parent(id, |this| {

src/librustc/hir/mod.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use util::nodemap::{NodeMap, FxHashSet};
1616
use mir::mono::Linkage;
1717

1818
use syntax_pos::{Span, DUMMY_SP, symbol::InternedString};
19-
use syntax::source_map::{self, Spanned};
19+
use syntax::source_map::Spanned;
2020
use rustc_target::spec::abi::Abi;
2121
use syntax::ast::{self, CrateSugar, Ident, Name, NodeId, DUMMY_NODE_ID, AsmDialect};
2222
use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy};
@@ -1144,45 +1144,38 @@ impl UnOp {
11441144
}
11451145

11461146
/// A statement
1147-
pub type Stmt = Spanned<StmtKind>;
1147+
#[derive(Clone, RustcEncodable, RustcDecodable)]
1148+
pub struct Stmt {
1149+
pub id: NodeId,
1150+
pub node: StmtKind,
1151+
pub span: Span,
1152+
}
11481153

1149-
impl fmt::Debug for StmtKind {
1154+
impl fmt::Debug for Stmt {
11501155
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1151-
// Sadness.
1152-
let spanned = source_map::dummy_spanned(self.clone());
1153-
write!(f,
1154-
"stmt({}: {})",
1155-
spanned.node.id(),
1156-
print::to_string(print::NO_ANN, |s| s.print_stmt(&spanned)))
1156+
write!(f, "stmt({}: {})", self.id,
1157+
print::to_string(print::NO_ANN, |s| s.print_stmt(self)))
11571158
}
11581159
}
11591160

11601161
#[derive(Clone, RustcEncodable, RustcDecodable)]
11611162
pub enum StmtKind {
11621163
/// Could be an item or a local (let) binding:
1163-
Decl(P<Decl>, NodeId),
1164+
Decl(P<Decl>),
11641165

11651166
/// Expr without trailing semi-colon (must have unit type):
1166-
Expr(P<Expr>, NodeId),
1167+
Expr(P<Expr>),
11671168

11681169
/// Expr with trailing semi-colon (may have any type):
1169-
Semi(P<Expr>, NodeId),
1170+
Semi(P<Expr>),
11701171
}
11711172

11721173
impl StmtKind {
11731174
pub fn attrs(&self) -> &[Attribute] {
11741175
match *self {
1175-
StmtKind::Decl(ref d, _) => d.node.attrs(),
1176-
StmtKind::Expr(ref e, _) |
1177-
StmtKind::Semi(ref e, _) => &e.attrs,
1178-
}
1179-
}
1180-
1181-
pub fn id(&self) -> NodeId {
1182-
match *self {
1183-
StmtKind::Decl(_, id) |
1184-
StmtKind::Expr(_, id) |
1185-
StmtKind::Semi(_, id) => id,
1176+
StmtKind::Decl(ref d) => d.node.attrs(),
1177+
StmtKind::Expr(ref e) |
1178+
StmtKind::Semi(ref e) => &e.attrs,
11861179
}
11871180
}
11881181
}

src/librustc/hir/print.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -992,14 +992,14 @@ impl<'a> State<'a> {
992992
pub fn print_stmt(&mut self, st: &hir::Stmt) -> io::Result<()> {
993993
self.maybe_print_comment(st.span.lo())?;
994994
match st.node {
995-
hir::StmtKind::Decl(ref decl, _) => {
995+
hir::StmtKind::Decl(ref decl) => {
996996
self.print_decl(&decl)?;
997997
}
998-
hir::StmtKind::Expr(ref expr, _) => {
998+
hir::StmtKind::Expr(ref expr) => {
999999
self.space_if_not_bol()?;
10001000
self.print_expr(&expr)?;
10011001
}
1002-
hir::StmtKind::Semi(ref expr, _) => {
1002+
hir::StmtKind::Semi(ref expr) => {
10031003
self.space_if_not_bol()?;
10041004
self.print_expr(&expr)?;
10051005
self.s.word(";")?;
@@ -2401,13 +2401,13 @@ fn expr_requires_semi_to_be_stmt(e: &hir::Expr) -> bool {
24012401
/// seen the semicolon, and thus don't need another.
24022402
fn stmt_ends_with_semi(stmt: &hir::StmtKind) -> bool {
24032403
match *stmt {
2404-
hir::StmtKind::Decl(ref d, _) => {
2404+
hir::StmtKind::Decl(ref d) => {
24052405
match d.node {
24062406
hir::DeclKind::Local(_) => true,
24072407
hir::DeclKind::Item(_) => false,
24082408
}
24092409
}
2410-
hir::StmtKind::Expr(ref e, _) => {
2410+
hir::StmtKind::Expr(ref e) => {
24112411
expr_requires_semi_to_be_stmt(&e)
24122412
}
24132413
hir::StmtKind::Semi(..) => {

src/librustc/ich/impls_hir.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,12 @@ impl_stable_hash_for!(enum hir::UnOp {
483483
UnNeg
484484
});
485485

486-
impl_stable_hash_for_spanned!(hir::StmtKind);
486+
impl_stable_hash_for!(struct hir::Stmt {
487+
id,
488+
node,
489+
span,
490+
});
491+
487492

488493
impl_stable_hash_for!(struct hir::Local {
489494
pat,
@@ -941,9 +946,9 @@ impl_stable_hash_for!(enum hir::ForeignItemKind {
941946
});
942947

943948
impl_stable_hash_for!(enum hir::StmtKind {
944-
Decl(decl, id),
945-
Expr(expr, id),
946-
Semi(expr, id)
949+
Decl(decl),
950+
Expr(expr),
951+
Semi(expr)
947952
});
948953

949954
impl_stable_hash_for!(struct hir::Arg {

src/librustc/middle/expr_use_visitor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
589589

590590
fn walk_stmt(&mut self, stmt: &hir::Stmt) {
591591
match stmt.node {
592-
hir::StmtKind::Decl(ref decl, _) => {
592+
hir::StmtKind::Decl(ref decl) => {
593593
match decl.node {
594594
hir::DeclKind::Local(ref local) => {
595595
self.walk_local(&local);
@@ -602,8 +602,8 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
602602
}
603603
}
604604

605-
hir::StmtKind::Expr(ref expr, _) |
606-
hir::StmtKind::Semi(ref expr, _) => {
605+
hir::StmtKind::Expr(ref expr) |
606+
hir::StmtKind::Semi(ref expr) => {
607607
self.consume_expr(&expr);
608608
}
609609
}

src/librustc/middle/liveness.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,11 +956,11 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
956956
fn propagate_through_stmt(&mut self, stmt: &hir::Stmt, succ: LiveNode)
957957
-> LiveNode {
958958
match stmt.node {
959-
hir::StmtKind::Decl(ref decl, _) => {
959+
hir::StmtKind::Decl(ref decl) => {
960960
self.propagate_through_decl(&decl, succ)
961961
}
962962

963-
hir::StmtKind::Expr(ref expr, _) | hir::StmtKind::Semi(ref expr, _) => {
963+
hir::StmtKind::Expr(ref expr) | hir::StmtKind::Semi(ref expr) => {
964964
self.propagate_through_expr(&expr, succ)
965965
}
966966
}

src/librustc/middle/region.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ fn resolve_pat<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, pat: &
835835
}
836836

837837
fn resolve_stmt<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, stmt: &'tcx hir::Stmt) {
838-
let stmt_id = visitor.tcx.hir().node_to_hir_id(stmt.node.id()).local_id;
838+
let stmt_id = visitor.tcx.hir().node_to_hir_id(stmt.id).local_id;
839839
debug!("resolve_stmt(stmt.id={:?})", stmt_id);
840840

841841
// Every statement will clean up the temporaries created during

src/librustc_lint/unused.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl LintPass for UnusedResults {
4141
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
4242
fn check_stmt(&mut self, cx: &LateContext, s: &hir::Stmt) {
4343
let expr = match s.node {
44-
hir::StmtKind::Semi(ref expr, _) => &**expr,
44+
hir::StmtKind::Semi(ref expr) => &**expr,
4545
_ => return,
4646
};
4747

@@ -205,7 +205,7 @@ impl LintPass for PathStatements {
205205

206206
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PathStatements {
207207
fn check_stmt(&mut self, cx: &LateContext, s: &hir::Stmt) {
208-
if let hir::StmtKind::Semi(ref expr, _) = s.node {
208+
if let hir::StmtKind::Semi(ref expr) = s.node {
209209
if let hir::ExprKind::Path(_) = expr.node {
210210
cx.span_lint(PATH_STATEMENTS, s.span, "path statement with no effect");
211211
}

src/librustc_mir/hair/cx/block.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ fn mirror_stmts<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
4646
-> Vec<StmtRef<'tcx>> {
4747
let mut result = vec![];
4848
for (index, stmt) in stmts.iter().enumerate() {
49-
let hir_id = cx.tcx.hir().node_to_hir_id(stmt.node.id());
49+
let hir_id = cx.tcx.hir().node_to_hir_id(stmt.id);
5050
let opt_dxn_ext = cx.region_scope_tree.opt_destruction_scope(hir_id.local_id);
51-
let stmt_span = StatementSpan(cx.tcx.hir().span(stmt.node.id()));
51+
let stmt_span = StatementSpan(cx.tcx.hir().span(stmt.id));
5252
match stmt.node {
53-
hir::StmtKind::Expr(ref expr, _) |
54-
hir::StmtKind::Semi(ref expr, _) => {
53+
hir::StmtKind::Expr(ref expr) |
54+
hir::StmtKind::Semi(ref expr) => {
5555
result.push(StmtRef::Mirror(Box::new(Stmt {
5656
kind: StmtKind::Expr {
5757
scope: region::Scope {
@@ -64,7 +64,7 @@ fn mirror_stmts<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
6464
span: stmt_span,
6565
})))
6666
}
67-
hir::StmtKind::Decl(ref decl, _) => {
67+
hir::StmtKind::Decl(ref decl) => {
6868
match decl.node {
6969
hir::DeclKind::Item(..) => {
7070
// ignore for purposes of the MIR

src/librustc_passes/hir_stats.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
144144
}
145145

146146
fn visit_stmt(&mut self, s: &'v hir::Stmt) {
147-
self.record("Stmt", Id::Node(s.node.id()), s);
147+
self.record("Stmt", Id::Node(s.id), s);
148148
hir_visit::walk_stmt(self, s)
149149
}
150150

0 commit comments

Comments
 (0)