Skip to content

Commit afbd004

Browse files
committed
Remove hir::StmtKind::Decl.
It's a level of indirection that hurts far more than it helps. The code is simpler without it. (This commit cuts more than 120 lines of code.) In particular, this commit removes some unnecessary `Span`s within `DeclKind` that were always identical to those in the enclosing `Stmt`, and some unnecessary allocations via `P`.
1 parent b2ce5a9 commit afbd004

File tree

16 files changed

+150
-272
lines changed

16 files changed

+150
-272
lines changed

src/librustc/cfg/construct.rs

+10-19
Original file line numberDiff line numberDiff line change
@@ -100,29 +100,20 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
100100

101101
fn stmt(&mut self, stmt: &hir::Stmt, pred: CFGIndex) -> CFGIndex {
102102
let hir_id = self.tcx.hir().node_to_hir_id(stmt.id);
103-
match stmt.node {
104-
hir::StmtKind::Decl(ref decl) => {
105-
let exit = self.decl(&decl, pred);
106-
self.add_ast_node(hir_id.local_id, &[exit])
103+
let exit = match stmt.node {
104+
hir::StmtKind::Local(ref local) => {
105+
let init_exit = self.opt_expr(&local.init, pred);
106+
self.pat(&local.pat, init_exit)
107+
}
108+
hir::StmtKind::Item(_) => {
109+
pred
107110
}
108-
109111
hir::StmtKind::Expr(ref expr) |
110112
hir::StmtKind::Semi(ref expr) => {
111-
let exit = self.expr(&expr, pred);
112-
self.add_ast_node(hir_id.local_id, &[exit])
113+
self.expr(&expr, pred)
113114
}
114-
}
115-
}
116-
117-
fn decl(&mut self, decl: &hir::Decl, pred: CFGIndex) -> CFGIndex {
118-
match decl.node {
119-
hir::DeclKind::Local(ref local) => {
120-
let init_exit = self.opt_expr(&local.init, pred);
121-
self.pat(&local.pat, init_exit)
122-
}
123-
124-
hir::DeclKind::Item(_) => pred,
125-
}
115+
};
116+
self.add_ast_node(hir_id.local_id, &[exit])
126117
}
127118

128119
fn pat(&mut self, pat: &hir::Pat, pred: CFGIndex) -> CFGIndex {

src/librustc/hir/check_attr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ 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 {
302-
for attr in stmt.node.attrs() {
301+
if let hir::StmtKind::Local(ref l) = stmt.node {
302+
for attr in l.attrs.iter() {
303303
if attr.check_name("inline") {
304304
self.check_inline(attr, &stmt.span, Target::Statement);
305305
}

src/librustc/hir/intravisit.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,6 @@ pub trait Visitor<'v> : Sized {
260260
fn visit_pat(&mut self, p: &'v Pat) {
261261
walk_pat(self, p)
262262
}
263-
fn visit_decl(&mut self, d: &'v Decl) {
264-
walk_decl(self, d)
265-
}
266263
fn visit_anon_const(&mut self, c: &'v AnonConst) {
267264
walk_anon_const(self, c)
268265
}
@@ -955,23 +952,15 @@ pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
955952
pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
956953
visitor.visit_id(statement.id);
957954
match statement.node {
958-
StmtKind::Decl(ref declaration) => {
959-
visitor.visit_decl(declaration)
960-
}
955+
StmtKind::Local(ref local) => visitor.visit_local(local),
956+
StmtKind::Item(ref item) => visitor.visit_nested_item(**item),
961957
StmtKind::Expr(ref expression) |
962958
StmtKind::Semi(ref expression) => {
963959
visitor.visit_expr(expression)
964960
}
965961
}
966962
}
967963

968-
pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
969-
match declaration.node {
970-
DeclKind::Local(ref local) => visitor.visit_local(local),
971-
DeclKind::Item(item) => visitor.visit_nested_item(item),
972-
}
973-
}
974-
975964
pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) {
976965
visitor.visit_id(constant.id);
977966
visitor.visit_nested_body(constant.body);

src/librustc/hir/lowering.rs

+9-25
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,7 @@ impl<'a> LoweringContext<'a> {
19571957
)
19581958
}
19591959

1960-
fn lower_local(&mut self, l: &Local) -> (P<hir::Local>, SmallVec<[hir::ItemId; 1]>) {
1960+
fn lower_local(&mut self, l: &Local) -> (hir::Local, SmallVec<[hir::ItemId; 1]>) {
19611961
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(l.id);
19621962
let mut ids = SmallVec::<[hir::ItemId; 1]>::new();
19631963
if self.sess.features_untracked().impl_trait_in_bindings {
@@ -1967,7 +1967,7 @@ impl<'a> LoweringContext<'a> {
19671967
}
19681968
}
19691969
let parent_def_id = DefId::local(self.current_hir_id_owner.last().unwrap().0);
1970-
(P(hir::Local {
1970+
(hir::Local {
19711971
id: node_id,
19721972
hir_id,
19731973
ty: l.ty
@@ -1984,7 +1984,7 @@ impl<'a> LoweringContext<'a> {
19841984
span: l.span,
19851985
attrs: l.attrs.clone(),
19861986
source: hir::LocalSource::Normal,
1987-
}), ids)
1987+
}, ids)
19881988
}
19891989

19901990
fn lower_mutability(&mut self, m: Mutability) -> hir::Mutability {
@@ -4537,23 +4537,13 @@ impl<'a> LoweringContext<'a> {
45374537
.into_iter()
45384538
.map(|item_id| hir::Stmt {
45394539
id: self.next_id().node_id,
4540-
node: hir::StmtKind::Decl(
4541-
P(Spanned {
4542-
node: hir::DeclKind::Item(item_id),
4543-
span: s.span,
4544-
}),
4545-
),
4540+
node: hir::StmtKind::Item(P(item_id)),
45464541
span: s.span,
45474542
})
45484543
.collect();
45494544
ids.push(hir::Stmt {
45504545
id: self.lower_node_id(s.id).node_id,
4551-
node: hir::StmtKind::Decl(
4552-
P(Spanned {
4553-
node: hir::DeclKind::Local(l),
4554-
span: s.span,
4555-
}),
4556-
),
4546+
node: hir::StmtKind::Local(P(l)),
45574547
span: s.span,
45584548
});
45594549
return ids;
@@ -4567,12 +4557,7 @@ impl<'a> LoweringContext<'a> {
45674557
id: id.take()
45684558
.map(|id| self.lower_node_id(id).node_id)
45694559
.unwrap_or_else(|| self.next_id().node_id),
4570-
node: hir::StmtKind::Decl(
4571-
P(Spanned {
4572-
node: hir::DeclKind::Item(item_id),
4573-
span: s.span,
4574-
}),
4575-
),
4560+
node: hir::StmtKind::Item(P(item_id)),
45764561
span: s.span,
45774562
})
45784563
.collect();
@@ -4799,7 +4784,7 @@ impl<'a> LoweringContext<'a> {
47994784
) -> hir::Stmt {
48004785
let LoweredNodeId { node_id, hir_id } = self.next_id();
48014786

4802-
let local = P(hir::Local {
4787+
let local = hir::Local {
48034788
pat,
48044789
ty: None,
48054790
init: ex,
@@ -4808,11 +4793,10 @@ impl<'a> LoweringContext<'a> {
48084793
span: sp,
48094794
attrs: ThinVec::new(),
48104795
source,
4811-
});
4812-
let decl = respan(sp, hir::DeclKind::Local(local));
4796+
};
48134797
hir::Stmt {
48144798
id: self.next_id().node_id,
4815-
node: hir::StmtKind::Decl(P(decl)),
4799+
node: hir::StmtKind::Local(P(local)),
48164800
span: sp
48174801
}
48184802
}

src/librustc/hir/mod.rs

+6-29
Original file line numberDiff line numberDiff line change
@@ -1160,8 +1160,10 @@ impl fmt::Debug for Stmt {
11601160

11611161
#[derive(Clone, RustcEncodable, RustcDecodable)]
11621162
pub enum StmtKind {
1163-
/// Could be an item or a local (let) binding:
1164-
Decl(P<Decl>),
1163+
/// A local (let) binding:
1164+
Local(P<Local>),
1165+
/// An item binding:
1166+
Item(P<ItemId>),
11651167

11661168
/// Expr without trailing semi-colon (must have unit type):
11671169
Expr(P<Expr>),
@@ -1173,7 +1175,8 @@ pub enum StmtKind {
11731175
impl StmtKind {
11741176
pub fn attrs(&self) -> &[Attribute] {
11751177
match *self {
1176-
StmtKind::Decl(ref d) => d.node.attrs(),
1178+
StmtKind::Local(ref l) => &l.attrs,
1179+
StmtKind::Item(_) => &[],
11771180
StmtKind::Expr(ref e) |
11781181
StmtKind::Semi(ref e) => &e.attrs,
11791182
}
@@ -1194,32 +1197,6 @@ pub struct Local {
11941197
pub source: LocalSource,
11951198
}
11961199

1197-
pub type Decl = Spanned<DeclKind>;
1198-
1199-
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1200-
pub enum DeclKind {
1201-
/// A local (let) binding:
1202-
Local(P<Local>),
1203-
/// An item binding:
1204-
Item(ItemId),
1205-
}
1206-
1207-
impl DeclKind {
1208-
pub fn attrs(&self) -> &[Attribute] {
1209-
match *self {
1210-
DeclKind::Local(ref l) => &l.attrs,
1211-
DeclKind::Item(_) => &[]
1212-
}
1213-
}
1214-
1215-
pub fn is_local(&self) -> bool {
1216-
match *self {
1217-
DeclKind::Local(_) => true,
1218-
_ => false,
1219-
}
1220-
}
1221-
}
1222-
12231200
/// represents one arm of a 'match'
12241201
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
12251202
pub struct Arm {

src/librustc/hir/print.rs

+21-38
Original file line numberDiff line numberDiff line change
@@ -992,8 +992,23 @@ 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) => {
996-
self.print_decl(&decl)?;
995+
hir::StmtKind::Local(ref loc) => {
996+
self.space_if_not_bol()?;
997+
self.ibox(indent_unit)?;
998+
self.word_nbsp("let")?;
999+
1000+
self.ibox(indent_unit)?;
1001+
self.print_local_decl(&loc)?;
1002+
self.end()?;
1003+
if let Some(ref init) = loc.init {
1004+
self.nbsp()?;
1005+
self.word_space("=")?;
1006+
self.print_expr(&init)?;
1007+
}
1008+
self.end()?
1009+
}
1010+
hir::StmtKind::Item(ref item) => {
1011+
self.ann.nested(self, Nested::Item(**item))?
9971012
}
9981013
hir::StmtKind::Expr(ref expr) => {
9991014
self.space_if_not_bol()?;
@@ -1562,30 +1577,6 @@ impl<'a> State<'a> {
15621577
Ok(())
15631578
}
15641579

1565-
pub fn print_decl(&mut self, decl: &hir::Decl) -> io::Result<()> {
1566-
self.maybe_print_comment(decl.span.lo())?;
1567-
match decl.node {
1568-
hir::DeclKind::Local(ref loc) => {
1569-
self.space_if_not_bol()?;
1570-
self.ibox(indent_unit)?;
1571-
self.word_nbsp("let")?;
1572-
1573-
self.ibox(indent_unit)?;
1574-
self.print_local_decl(&loc)?;
1575-
self.end()?;
1576-
if let Some(ref init) = loc.init {
1577-
self.nbsp()?;
1578-
self.word_space("=")?;
1579-
self.print_expr(&init)?;
1580-
}
1581-
self.end()
1582-
}
1583-
hir::DeclKind::Item(item) => {
1584-
self.ann.nested(self, Nested::Item(item))
1585-
}
1586-
}
1587-
}
1588-
15891580
pub fn print_usize(&mut self, i: usize) -> io::Result<()> {
15901581
self.s.word(i.to_string())
15911582
}
@@ -2401,18 +2392,10 @@ fn expr_requires_semi_to_be_stmt(e: &hir::Expr) -> bool {
24012392
/// seen the semicolon, and thus don't need another.
24022393
fn stmt_ends_with_semi(stmt: &hir::StmtKind) -> bool {
24032394
match *stmt {
2404-
hir::StmtKind::Decl(ref d) => {
2405-
match d.node {
2406-
hir::DeclKind::Local(_) => true,
2407-
hir::DeclKind::Item(_) => false,
2408-
}
2409-
}
2410-
hir::StmtKind::Expr(ref e) => {
2411-
expr_requires_semi_to_be_stmt(&e)
2412-
}
2413-
hir::StmtKind::Semi(..) => {
2414-
false
2415-
}
2395+
hir::StmtKind::Local(_) => true,
2396+
hir::StmtKind::Item(_) => false,
2397+
hir::StmtKind::Expr(ref e) => expr_requires_semi_to_be_stmt(&e),
2398+
hir::StmtKind::Semi(..) => false,
24162399
}
24172400
}
24182401

src/librustc/ich/impls_hir.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -501,12 +501,6 @@ impl_stable_hash_for!(struct hir::Local {
501501
source
502502
});
503503

504-
impl_stable_hash_for_spanned!(hir::DeclKind);
505-
impl_stable_hash_for!(enum hir::DeclKind {
506-
Local(local),
507-
Item(item_id)
508-
});
509-
510504
impl_stable_hash_for!(struct hir::Arm {
511505
attrs,
512506
pats,
@@ -946,7 +940,8 @@ impl_stable_hash_for!(enum hir::ForeignItemKind {
946940
});
947941

948942
impl_stable_hash_for!(enum hir::StmtKind {
949-
Decl(decl),
943+
Local(local),
944+
Item(item_id),
950945
Expr(expr),
951946
Semi(expr)
952947
});

src/librustc/lint/context.rs

-5
Original file line numberDiff line numberDiff line change
@@ -941,11 +941,6 @@ impl<'a, 'tcx> hir_visit::Visitor<'tcx> for LateContext<'a, 'tcx> {
941941
hir_visit::walk_arm(self, a);
942942
}
943943

944-
fn visit_decl(&mut self, d: &'tcx hir::Decl) {
945-
run_lints!(self, check_decl, d);
946-
hir_visit::walk_decl(self, d);
947-
}
948-
949944
fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam) {
950945
run_lints!(self, check_generic_param, p);
951946
hir_visit::walk_generic_param(self, p);

src/librustc/lint/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ macro_rules! late_lint_methods {
191191
fn check_stmt(a: &$hir hir::Stmt);
192192
fn check_arm(a: &$hir hir::Arm);
193193
fn check_pat(a: &$hir hir::Pat);
194-
fn check_decl(a: &$hir hir::Decl);
195194
fn check_expr(a: &$hir hir::Expr);
196195
fn check_expr_post(a: &$hir hir::Expr);
197196
fn check_ty(a: &$hir hir::Ty);

src/librustc/middle/expr_use_visitor.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -589,17 +589,13 @@ 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) => {
593-
match decl.node {
594-
hir::DeclKind::Local(ref local) => {
595-
self.walk_local(&local);
596-
}
592+
hir::StmtKind::Local(ref local) => {
593+
self.walk_local(&local);
594+
}
597595

598-
hir::DeclKind::Item(_) => {
599-
// we don't visit nested items in this visitor,
600-
// only the fn body we were given.
601-
}
602-
}
596+
hir::StmtKind::Item(_) => {
597+
// we don't visit nested items in this visitor,
598+
// only the fn body we were given.
603599
}
604600

605601
hir::StmtKind::Expr(ref expr) |

0 commit comments

Comments
 (0)