Skip to content

Commit

Permalink
Remove hir::StmtKind::Decl.
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
nnethercote committed Jan 17, 2019
1 parent b2ce5a9 commit afbd004
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 272 deletions.
29 changes: 10 additions & 19 deletions src/librustc/cfg/construct.rs
Expand Up @@ -100,29 +100,20 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {

fn stmt(&mut self, stmt: &hir::Stmt, pred: CFGIndex) -> CFGIndex {
let hir_id = self.tcx.hir().node_to_hir_id(stmt.id);
match stmt.node {
hir::StmtKind::Decl(ref decl) => {
let exit = self.decl(&decl, pred);
self.add_ast_node(hir_id.local_id, &[exit])
let exit = match stmt.node {
hir::StmtKind::Local(ref local) => {
let init_exit = self.opt_expr(&local.init, pred);
self.pat(&local.pat, init_exit)
}
hir::StmtKind::Item(_) => {
pred
}

hir::StmtKind::Expr(ref expr) |
hir::StmtKind::Semi(ref expr) => {
let exit = self.expr(&expr, pred);
self.add_ast_node(hir_id.local_id, &[exit])
self.expr(&expr, pred)
}
}
}

fn decl(&mut self, decl: &hir::Decl, pred: CFGIndex) -> CFGIndex {
match decl.node {
hir::DeclKind::Local(ref local) => {
let init_exit = self.opt_expr(&local.init, pred);
self.pat(&local.pat, init_exit)
}

hir::DeclKind::Item(_) => pred,
}
};
self.add_ast_node(hir_id.local_id, &[exit])
}

fn pat(&mut self, pat: &hir::Pat, pred: CFGIndex) -> CFGIndex {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/check_attr.rs
Expand Up @@ -298,8 +298,8 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {

fn check_stmt_attributes(&self, stmt: &hir::Stmt) {
// When checking statements ignore expressions, they will be checked later
if let hir::StmtKind::Decl(..) = stmt.node {
for attr in stmt.node.attrs() {
if let hir::StmtKind::Local(ref l) = stmt.node {
for attr in l.attrs.iter() {
if attr.check_name("inline") {
self.check_inline(attr, &stmt.span, Target::Statement);
}
Expand Down
15 changes: 2 additions & 13 deletions src/librustc/hir/intravisit.rs
Expand Up @@ -260,9 +260,6 @@ pub trait Visitor<'v> : Sized {
fn visit_pat(&mut self, p: &'v Pat) {
walk_pat(self, p)
}
fn visit_decl(&mut self, d: &'v Decl) {
walk_decl(self, d)
}
fn visit_anon_const(&mut self, c: &'v AnonConst) {
walk_anon_const(self, c)
}
Expand Down Expand Up @@ -955,23 +952,15 @@ pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
visitor.visit_id(statement.id);
match statement.node {
StmtKind::Decl(ref declaration) => {
visitor.visit_decl(declaration)
}
StmtKind::Local(ref local) => visitor.visit_local(local),
StmtKind::Item(ref item) => visitor.visit_nested_item(**item),
StmtKind::Expr(ref expression) |
StmtKind::Semi(ref expression) => {
visitor.visit_expr(expression)
}
}
}

pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
match declaration.node {
DeclKind::Local(ref local) => visitor.visit_local(local),
DeclKind::Item(item) => visitor.visit_nested_item(item),
}
}

pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) {
visitor.visit_id(constant.id);
visitor.visit_nested_body(constant.body);
Expand Down
34 changes: 9 additions & 25 deletions src/librustc/hir/lowering.rs
Expand Up @@ -1957,7 +1957,7 @@ impl<'a> LoweringContext<'a> {
)
}

fn lower_local(&mut self, l: &Local) -> (P<hir::Local>, SmallVec<[hir::ItemId; 1]>) {
fn lower_local(&mut self, l: &Local) -> (hir::Local, SmallVec<[hir::ItemId; 1]>) {
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(l.id);
let mut ids = SmallVec::<[hir::ItemId; 1]>::new();
if self.sess.features_untracked().impl_trait_in_bindings {
Expand All @@ -1967,7 +1967,7 @@ impl<'a> LoweringContext<'a> {
}
}
let parent_def_id = DefId::local(self.current_hir_id_owner.last().unwrap().0);
(P(hir::Local {
(hir::Local {
id: node_id,
hir_id,
ty: l.ty
Expand All @@ -1984,7 +1984,7 @@ impl<'a> LoweringContext<'a> {
span: l.span,
attrs: l.attrs.clone(),
source: hir::LocalSource::Normal,
}), ids)
}, ids)
}

fn lower_mutability(&mut self, m: Mutability) -> hir::Mutability {
Expand Down Expand Up @@ -4537,23 +4537,13 @@ impl<'a> LoweringContext<'a> {
.into_iter()
.map(|item_id| hir::Stmt {
id: self.next_id().node_id,
node: hir::StmtKind::Decl(
P(Spanned {
node: hir::DeclKind::Item(item_id),
span: s.span,
}),
),
node: hir::StmtKind::Item(P(item_id)),
span: s.span,
})
.collect();
ids.push(hir::Stmt {
id: self.lower_node_id(s.id).node_id,
node: hir::StmtKind::Decl(
P(Spanned {
node: hir::DeclKind::Local(l),
span: s.span,
}),
),
node: hir::StmtKind::Local(P(l)),
span: s.span,
});
return ids;
Expand All @@ -4567,12 +4557,7 @@ impl<'a> LoweringContext<'a> {
id: id.take()
.map(|id| self.lower_node_id(id).node_id)
.unwrap_or_else(|| self.next_id().node_id),
node: hir::StmtKind::Decl(
P(Spanned {
node: hir::DeclKind::Item(item_id),
span: s.span,
}),
),
node: hir::StmtKind::Item(P(item_id)),
span: s.span,
})
.collect();
Expand Down Expand Up @@ -4799,7 +4784,7 @@ impl<'a> LoweringContext<'a> {
) -> hir::Stmt {
let LoweredNodeId { node_id, hir_id } = self.next_id();

let local = P(hir::Local {
let local = hir::Local {
pat,
ty: None,
init: ex,
Expand All @@ -4808,11 +4793,10 @@ impl<'a> LoweringContext<'a> {
span: sp,
attrs: ThinVec::new(),
source,
});
let decl = respan(sp, hir::DeclKind::Local(local));
};
hir::Stmt {
id: self.next_id().node_id,
node: hir::StmtKind::Decl(P(decl)),
node: hir::StmtKind::Local(P(local)),
span: sp
}
}
Expand Down
35 changes: 6 additions & 29 deletions src/librustc/hir/mod.rs
Expand Up @@ -1160,8 +1160,10 @@ impl fmt::Debug for Stmt {

#[derive(Clone, RustcEncodable, RustcDecodable)]
pub enum StmtKind {
/// Could be an item or a local (let) binding:
Decl(P<Decl>),
/// A local (let) binding:
Local(P<Local>),
/// An item binding:
Item(P<ItemId>),

/// Expr without trailing semi-colon (must have unit type):
Expr(P<Expr>),
Expand All @@ -1173,7 +1175,8 @@ pub enum StmtKind {
impl StmtKind {
pub fn attrs(&self) -> &[Attribute] {
match *self {
StmtKind::Decl(ref d) => d.node.attrs(),
StmtKind::Local(ref l) => &l.attrs,
StmtKind::Item(_) => &[],
StmtKind::Expr(ref e) |
StmtKind::Semi(ref e) => &e.attrs,
}
Expand All @@ -1194,32 +1197,6 @@ pub struct Local {
pub source: LocalSource,
}

pub type Decl = Spanned<DeclKind>;

#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub enum DeclKind {
/// A local (let) binding:
Local(P<Local>),
/// An item binding:
Item(ItemId),
}

impl DeclKind {
pub fn attrs(&self) -> &[Attribute] {
match *self {
DeclKind::Local(ref l) => &l.attrs,
DeclKind::Item(_) => &[]
}
}

pub fn is_local(&self) -> bool {
match *self {
DeclKind::Local(_) => true,
_ => false,
}
}
}

/// represents one arm of a 'match'
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub struct Arm {
Expand Down
59 changes: 21 additions & 38 deletions src/librustc/hir/print.rs
Expand Up @@ -992,8 +992,23 @@ impl<'a> State<'a> {
pub fn print_stmt(&mut self, st: &hir::Stmt) -> io::Result<()> {
self.maybe_print_comment(st.span.lo())?;
match st.node {
hir::StmtKind::Decl(ref decl) => {
self.print_decl(&decl)?;
hir::StmtKind::Local(ref loc) => {
self.space_if_not_bol()?;
self.ibox(indent_unit)?;
self.word_nbsp("let")?;

self.ibox(indent_unit)?;
self.print_local_decl(&loc)?;
self.end()?;
if let Some(ref init) = loc.init {
self.nbsp()?;
self.word_space("=")?;
self.print_expr(&init)?;
}
self.end()?
}
hir::StmtKind::Item(ref item) => {
self.ann.nested(self, Nested::Item(**item))?
}
hir::StmtKind::Expr(ref expr) => {
self.space_if_not_bol()?;
Expand Down Expand Up @@ -1562,30 +1577,6 @@ impl<'a> State<'a> {
Ok(())
}

pub fn print_decl(&mut self, decl: &hir::Decl) -> io::Result<()> {
self.maybe_print_comment(decl.span.lo())?;
match decl.node {
hir::DeclKind::Local(ref loc) => {
self.space_if_not_bol()?;
self.ibox(indent_unit)?;
self.word_nbsp("let")?;

self.ibox(indent_unit)?;
self.print_local_decl(&loc)?;
self.end()?;
if let Some(ref init) = loc.init {
self.nbsp()?;
self.word_space("=")?;
self.print_expr(&init)?;
}
self.end()
}
hir::DeclKind::Item(item) => {
self.ann.nested(self, Nested::Item(item))
}
}
}

pub fn print_usize(&mut self, i: usize) -> io::Result<()> {
self.s.word(i.to_string())
}
Expand Down Expand Up @@ -2401,18 +2392,10 @@ fn expr_requires_semi_to_be_stmt(e: &hir::Expr) -> bool {
/// seen the semicolon, and thus don't need another.
fn stmt_ends_with_semi(stmt: &hir::StmtKind) -> bool {
match *stmt {
hir::StmtKind::Decl(ref d) => {
match d.node {
hir::DeclKind::Local(_) => true,
hir::DeclKind::Item(_) => false,
}
}
hir::StmtKind::Expr(ref e) => {
expr_requires_semi_to_be_stmt(&e)
}
hir::StmtKind::Semi(..) => {
false
}
hir::StmtKind::Local(_) => true,
hir::StmtKind::Item(_) => false,
hir::StmtKind::Expr(ref e) => expr_requires_semi_to_be_stmt(&e),
hir::StmtKind::Semi(..) => false,
}
}

Expand Down
9 changes: 2 additions & 7 deletions src/librustc/ich/impls_hir.rs
Expand Up @@ -501,12 +501,6 @@ impl_stable_hash_for!(struct hir::Local {
source
});

impl_stable_hash_for_spanned!(hir::DeclKind);
impl_stable_hash_for!(enum hir::DeclKind {
Local(local),
Item(item_id)
});

impl_stable_hash_for!(struct hir::Arm {
attrs,
pats,
Expand Down Expand Up @@ -946,7 +940,8 @@ impl_stable_hash_for!(enum hir::ForeignItemKind {
});

impl_stable_hash_for!(enum hir::StmtKind {
Decl(decl),
Local(local),
Item(item_id),
Expr(expr),
Semi(expr)
});
Expand Down
5 changes: 0 additions & 5 deletions src/librustc/lint/context.rs
Expand Up @@ -941,11 +941,6 @@ impl<'a, 'tcx> hir_visit::Visitor<'tcx> for LateContext<'a, 'tcx> {
hir_visit::walk_arm(self, a);
}

fn visit_decl(&mut self, d: &'tcx hir::Decl) {
run_lints!(self, check_decl, d);
hir_visit::walk_decl(self, d);
}

fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam) {
run_lints!(self, check_generic_param, p);
hir_visit::walk_generic_param(self, p);
Expand Down
1 change: 0 additions & 1 deletion src/librustc/lint/mod.rs
Expand Up @@ -191,7 +191,6 @@ macro_rules! late_lint_methods {
fn check_stmt(a: &$hir hir::Stmt);
fn check_arm(a: &$hir hir::Arm);
fn check_pat(a: &$hir hir::Pat);
fn check_decl(a: &$hir hir::Decl);
fn check_expr(a: &$hir hir::Expr);
fn check_expr_post(a: &$hir hir::Expr);
fn check_ty(a: &$hir hir::Ty);
Expand Down
16 changes: 6 additions & 10 deletions src/librustc/middle/expr_use_visitor.rs
Expand Up @@ -589,17 +589,13 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {

fn walk_stmt(&mut self, stmt: &hir::Stmt) {
match stmt.node {
hir::StmtKind::Decl(ref decl) => {
match decl.node {
hir::DeclKind::Local(ref local) => {
self.walk_local(&local);
}
hir::StmtKind::Local(ref local) => {
self.walk_local(&local);
}

hir::DeclKind::Item(_) => {
// we don't visit nested items in this visitor,
// only the fn body we were given.
}
}
hir::StmtKind::Item(_) => {
// we don't visit nested items in this visitor,
// only the fn body we were given.
}

hir::StmtKind::Expr(ref expr) |
Expand Down

0 comments on commit afbd004

Please sign in to comment.