Skip to content

Redo hir::Stmt #57689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions src/librustc/cfg/construct.rs
Original file line number Diff line number Diff line change
@@ -99,30 +99,21 @@ 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.node.id());
match stmt.node {
hir::StmtKind::Decl(ref decl, _) => {
let exit = self.decl(&decl, pred);
self.add_ast_node(hir_id.local_id, &[exit])
}

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])
}
}
}

fn decl(&mut self, decl: &hir::Decl, pred: CFGIndex) -> CFGIndex {
match decl.node {
hir::DeclKind::Local(ref local) => {
let hir_id = self.tcx.hir().node_to_hir_id(stmt.id);
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::DeclKind::Item(_) => pred,
}
hir::StmtKind::Item(_) => {
pred
}
hir::StmtKind::Expr(ref expr) |
hir::StmtKind::Semi(ref expr) => {
self.expr(&expr, pred)
}
};
self.add_ast_node(hir_id.local_id, &[exit])
}

fn pat(&mut self, pat: &hir::Pat, pred: CFGIndex) -> CFGIndex {
4 changes: 2 additions & 2 deletions src/librustc/hir/check_attr.rs
Original file line number Diff line number Diff line change
@@ -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);
}
22 changes: 5 additions & 17 deletions src/librustc/hir/intravisit.rs
Original file line number Diff line number Diff line change
@@ -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)
}
@@ -953,26 +950,17 @@ 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, id) => {
visitor.visit_id(id);
visitor.visit_decl(declaration)
}
StmtKind::Expr(ref expression, id) |
StmtKind::Semi(ref expression, id) => {
visitor.visit_id(id);
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);
78 changes: 35 additions & 43 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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
@@ -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 {
@@ -4331,10 +4331,11 @@ impl<'a> LoweringContext<'a> {
ThinVec::new(),
))
};
let match_stmt = respan(
head_sp,
hir::StmtKind::Expr(match_expr, self.next_id().node_id)
);
let match_stmt = hir::Stmt {
id: self.next_id().node_id,
node: hir::StmtKind::Expr(match_expr),
span: head_sp,
};

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

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

let body_block = self.with_loop_scope(e.id, |this| this.lower_block(body, false));
let body_expr = P(self.expr_block(body_block, ThinVec::new()));
let body_stmt = respan(
body.span,
hir::StmtKind::Expr(body_expr, self.next_id().node_id)
);
let body_stmt = hir::Stmt {
id: self.next_id().node_id,
node: hir::StmtKind::Expr(body_expr),
span: body.span,
};

let loop_block = P(self.block_all(
e.span,
@@ -4533,25 +4535,15 @@ impl<'a> LoweringContext<'a> {
let (l, item_ids) = self.lower_local(l);
let mut ids: SmallVec<[hir::Stmt; 1]> = item_ids
.into_iter()
.map(|item_id| Spanned {
node: hir::StmtKind::Decl(
P(Spanned {
node: hir::DeclKind::Item(item_id),
span: s.span,
}),
self.next_id().node_id,
),
.map(|item_id| hir::Stmt {
id: self.next_id().node_id,
node: hir::StmtKind::Item(P(item_id)),
span: s.span,
})
.collect();
ids.push(Spanned {
node: hir::StmtKind::Decl(
P(Spanned {
node: hir::DeclKind::Local(l),
span: s.span,
}),
self.lower_node_id(s.id).node_id,
),
ids.push(hir::Stmt {
id: self.lower_node_id(s.id).node_id,
node: hir::StmtKind::Local(P(l)),
span: s.span,
});
return ids;
@@ -4561,26 +4553,23 @@ impl<'a> LoweringContext<'a> {
let mut id = Some(s.id);
return self.lower_item_id(it)
.into_iter()
.map(|item_id| Spanned {
node: hir::StmtKind::Decl(
P(Spanned {
node: hir::DeclKind::Item(item_id),
span: s.span,
}),
id.take()
.map(|item_id| hir::Stmt {
id: id.take()
.map(|id| self.lower_node_id(id).node_id)
.unwrap_or_else(|| self.next_id().node_id),
),
node: hir::StmtKind::Item(P(item_id)),
span: s.span,
})
.collect();
}
StmtKind::Expr(ref e) => Spanned {
node: hir::StmtKind::Expr(P(self.lower_expr(e)), self.lower_node_id(s.id).node_id),
StmtKind::Expr(ref e) => hir::Stmt {
id: self.lower_node_id(s.id).node_id,
node: hir::StmtKind::Expr(P(self.lower_expr(e))),
span: s.span,
},
StmtKind::Semi(ref e) => Spanned {
node: hir::StmtKind::Semi(P(self.lower_expr(e)), self.lower_node_id(s.id).node_id),
StmtKind::Semi(ref e) => hir::Stmt {
id: self.lower_node_id(s.id).node_id,
node: hir::StmtKind::Semi(P(self.lower_expr(e))),
span: s.span,
},
StmtKind::Mac(..) => panic!("Shouldn't exist here"),
@@ -4795,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,
@@ -4804,9 +4793,12 @@ impl<'a> LoweringContext<'a> {
span: sp,
attrs: ThinVec::new(),
source,
});
let decl = respan(sp, hir::DeclKind::Local(local));
respan(sp, hir::StmtKind::Decl(P(decl), self.next_id().node_id))
};
hir::Stmt {
id: self.next_id().node_id,
node: hir::StmtKind::Local(P(local)),
span: sp
}
}

fn stmt_let(
2 changes: 1 addition & 1 deletion src/librustc/hir/map/collector.rs
Original file line number Diff line number Diff line change
@@ -426,7 +426,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
}

fn visit_stmt(&mut self, stmt: &'hir Stmt) {
let id = stmt.node.id();
let id = stmt.id;
self.insert(stmt.span, id, Node::Stmt(stmt));

self.with_parent(id, |this| {
70 changes: 20 additions & 50 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ use util::nodemap::{NodeMap, FxHashSet};
use mir::mono::Linkage;

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

/// A statement
pub type Stmt = Spanned<StmtKind>;
#[derive(Clone, RustcEncodable, RustcDecodable)]
pub struct Stmt {
pub id: NodeId,
pub node: StmtKind,
pub span: Span,
}

impl fmt::Debug for StmtKind {
impl fmt::Debug for Stmt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Sadness.
let spanned = source_map::dummy_spanned(self.clone());
write!(f,
"stmt({}: {})",
spanned.node.id(),
print::to_string(print::NO_ANN, |s| s.print_stmt(&spanned)))
write!(f, "stmt({}: {})", self.id,
print::to_string(print::NO_ANN, |s| s.print_stmt(self)))
}
}

#[derive(Clone, RustcEncodable, RustcDecodable)]
pub enum StmtKind {
/// Could be an item or a local (let) binding:
Decl(P<Decl>, NodeId),
/// 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>, NodeId),
Expr(P<Expr>),

/// Expr with trailing semi-colon (may have any type):
Semi(P<Expr>, NodeId),
Semi(P<Expr>),
}

impl StmtKind {
pub fn attrs(&self) -> &[Attribute] {
match *self {
StmtKind::Decl(ref d, _) => d.node.attrs(),
StmtKind::Expr(ref e, _) |
StmtKind::Semi(ref e, _) => &e.attrs,
}
}

pub fn id(&self) -> NodeId {
match *self {
StmtKind::Decl(_, id) |
StmtKind::Expr(_, id) |
StmtKind::Semi(_, id) => id,
StmtKind::Local(ref l) => &l.attrs,
StmtKind::Item(_) => &[],
StmtKind::Expr(ref e) |
StmtKind::Semi(ref e) => &e.attrs,
}
}
}
@@ -1201,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 {
Loading