Skip to content

Refactor ast::Stmt and rename ExprKind::Again to Continue #34316

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 4 commits into from
Jun 28, 2016
Merged
Show file tree
Hide file tree
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
41 changes: 18 additions & 23 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,6 @@ impl<'a> LoweringContext<'a> {
}
}

fn lower_decl(&mut self, d: &Decl) -> P<hir::Decl> {
match d.node {
DeclKind::Local(ref l) => P(Spanned {
node: hir::DeclLocal(self.lower_local(l)),
span: d.span,
}),
DeclKind::Item(ref it) => P(Spanned {
node: hir::DeclItem(self.lower_item_id(it)),
span: d.span,
}),
}
}

fn lower_ty_binding(&mut self, b: &TypeBinding) -> hir::TypeBinding {
hir::TypeBinding {
id: b.id,
Expand Down Expand Up @@ -1223,7 +1210,7 @@ impl<'a> LoweringContext<'a> {
hir::ExprPath(hir_qself, self.lower_path(path))
}
ExprKind::Break(opt_ident) => hir::ExprBreak(self.lower_opt_sp_ident(opt_ident)),
ExprKind::Again(opt_ident) => hir::ExprAgain(self.lower_opt_sp_ident(opt_ident)),
ExprKind::Continue(opt_ident) => hir::ExprAgain(self.lower_opt_sp_ident(opt_ident)),
ExprKind::Ret(ref e) => hir::ExprRet(e.as_ref().map(|x| self.lower_expr(x))),
ExprKind::InlineAsm(InlineAsm {
ref inputs,
Expand Down Expand Up @@ -1579,21 +1566,29 @@ impl<'a> LoweringContext<'a> {

fn lower_stmt(&mut self, s: &Stmt) -> hir::Stmt {
match s.node {
StmtKind::Decl(ref d, id) => {
Spanned {
node: hir::StmtDecl(self.lower_decl(d), id),
StmtKind::Local(ref l) => Spanned {
node: hir::StmtDecl(P(Spanned {
node: hir::DeclLocal(self.lower_local(l)),
span: s.span,
}
}
StmtKind::Expr(ref e, id) => {
}), s.id),
span: s.span,
},
StmtKind::Item(ref it) => Spanned {
node: hir::StmtDecl(P(Spanned {
node: hir::DeclItem(self.lower_item_id(it)),
span: s.span,
}), s.id),
span: s.span,
},
StmtKind::Expr(ref e) => {
Spanned {
node: hir::StmtExpr(self.lower_expr(e), id),
node: hir::StmtExpr(self.lower_expr(e), s.id),
span: s.span,
}
}
StmtKind::Semi(ref e, id) => {
StmtKind::Semi(ref e) => {
Spanned {
node: hir::StmtSemi(self.lower_expr(e), id),
node: hir::StmtSemi(self.lower_expr(e), s.id),
span: s.span,
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1005,11 +1005,6 @@ impl<'a, 'v> ast_visit::Visitor<'v> for EarlyContext<'a> {
ast_visit::walk_arm(self, a);
}

fn visit_decl(&mut self, d: &ast::Decl) {
run_lints!(self, check_decl, early_passes, d);
ast_visit::walk_decl(self, d);
}

fn visit_expr_post(&mut self, e: &ast::Expr) {
run_lints!(self, check_expr_post, early_passes, e);
}
Expand Down
1 change: 0 additions & 1 deletion src/librustc/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ pub trait EarlyLintPass: LintPass {
fn check_stmt(&mut self, _: &EarlyContext, _: &ast::Stmt) { }
fn check_arm(&mut self, _: &EarlyContext, _: &ast::Arm) { }
fn check_pat(&mut self, _: &EarlyContext, _: &ast::Pat) { }
fn check_decl(&mut self, _: &EarlyContext, _: &ast::Decl) { }
fn check_expr(&mut self, _: &EarlyContext, _: &ast::Expr) { }
fn check_expr_post(&mut self, _: &EarlyContext, _: &ast::Expr) { }
fn check_ty(&mut self, _: &EarlyContext, _: &ast::Ty) { }
Expand Down
9 changes: 3 additions & 6 deletions src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,9 @@ impl EarlyLintPass for UnusedParens {

fn check_stmt(&mut self, cx: &EarlyContext, s: &ast::Stmt) {
let (value, msg) = match s.node {
ast::StmtKind::Decl(ref decl, _) => match decl.node {
ast::DeclKind::Local(ref local) => match local.init {
Some(ref value) => (value, "assigned value"),
None => return
},
_ => return
ast::StmtKind::Local(ref local) => match local.init {
Some(ref value) => (value, "assigned value"),
None => return
},
_ => return
};
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_passes/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl<'a, 'v> Visitor<'v> for AstValidator<'a> {
match expr.node {
ExprKind::While(_, _, Some(ident)) | ExprKind::Loop(_, Some(ident)) |
ExprKind::WhileLet(_, _, _, Some(ident)) | ExprKind::ForLoop(_, _, _, Some(ident)) |
ExprKind::Break(Some(ident)) | ExprKind::Again(Some(ident)) => {
ExprKind::Break(Some(ident)) | ExprKind::Continue(Some(ident)) => {
self.check_label(ident.node, ident.span, expr.id);
}
_ => {}
Expand Down
18 changes: 6 additions & 12 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ use syntax::attr;
use syntax::parse::token;
use syntax::codemap::{Span, DUMMY_SP};

use syntax::ast::{Block, Crate, DeclKind};
use syntax::ast::{Block, Crate};
use syntax::ast::{ForeignItem, ForeignItemKind, Item, ItemKind};
use syntax::ast::{Mutability, PathListItemKind};
use syntax::ast::{Stmt, StmtKind, TraitItemKind};
use syntax::ast::{StmtKind, TraitItemKind};
use syntax::ast::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple};
use syntax::visit::{self, Visitor};

Expand Down Expand Up @@ -84,17 +84,11 @@ impl<'b> Resolver<'b> {
}

fn block_needs_anonymous_module(&mut self, block: &Block) -> bool {
fn is_item(statement: &Stmt) -> bool {
if let StmtKind::Decl(ref declaration, _) = statement.node {
if let DeclKind::Item(_) = declaration.node {
return true;
}
}
false
}

// If any statements are items, we need to create an anonymous module
block.stmts.iter().any(is_item)
block.stmts.iter().any(|statement| match statement.node {
StmtKind::Item(_) => true,
_ => false,
})
}

/// Constructs the reduced graph for one item.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2988,7 +2988,7 @@ impl<'a> Resolver<'a> {
})
}

ExprKind::Break(Some(label)) | ExprKind::Again(Some(label)) => {
ExprKind::Break(Some(label)) | ExprKind::Continue(Some(label)) => {
match self.search_label(mtwt::resolve(label.node)) {
None => {
self.record_def(expr.id, err_path_resolution());
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_save_analysis/dump_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1421,8 +1421,7 @@ impl<'v, 'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'v> for DumpVisitor<'l, 'tcx,
}

fn visit_stmt(&mut self, s: &ast::Stmt) {
let id = s.node.id();
self.process_macro_use(s.span, id.unwrap());
self.process_macro_use(s.span, s.id);
visit::walk_stmt(self, s)
}

Expand Down
57 changes: 18 additions & 39 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use ptr::P;

use std::fmt;
use std::rc::Rc;
use std::borrow::Cow;
use std::hash::{Hash, Hasher};
use serialize::{Encodable, Decodable, Encoder, Decoder};

Expand Down Expand Up @@ -786,42 +785,38 @@ impl UnOp {
}

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

impl fmt::Debug for Stmt {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "stmt({}: {})",
self.node.id()
.map_or(Cow::Borrowed("<macro>"),|id|Cow::Owned(id.to_string())),
pprust::stmt_to_string(self))
write!(f, "stmt({}: {})", self.id.to_string(), pprust::stmt_to_string(self))
}
}


#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
pub enum StmtKind {
/// Could be an item or a local (let) binding:
Decl(P<Decl>, NodeId),
/// A local (let) binding.
Local(P<Local>),

/// Expr without trailing semi-colon (must have unit type):
Expr(P<Expr>, NodeId),
/// An item definition.
Item(P<Item>),

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

Mac(P<Mac>, MacStmtStyle, ThinAttributes),
/// Expr with trailing semi-colon (may have any type).
Semi(P<Expr>),

Mac(P<(Mac, MacStmtStyle, ThinAttributes)>),
}

impl StmtKind {
pub fn id(&self) -> Option<NodeId> {
match *self {
StmtKind::Decl(_, id) => Some(id),
StmtKind::Expr(_, id) => Some(id),
StmtKind::Semi(_, id) => Some(id),
StmtKind::Mac(..) => None,
}
}

pub fn attrs(&self) -> &[Attribute] {
HasAttrs::attrs(self)
}
Expand Down Expand Up @@ -860,22 +855,6 @@ impl Local {
}
}

pub type Decl = Spanned<DeclKind>;

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

impl Decl {
pub fn attrs(&self) -> &[Attribute] {
HasAttrs::attrs(self)
}
}

/// represents one arm of a 'match'
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct Arm {
Expand Down Expand Up @@ -1041,7 +1020,7 @@ pub enum ExprKind {
/// A `break`, with an optional label to break
Break(Option<SpannedIdent>),
/// A `continue`, with an optional label
Again(Option<SpannedIdent>),
Continue(Option<SpannedIdent>),
/// A `return`, with an optional value to be returned
Ret(Option<P<Expr>>),

Expand Down
44 changes: 17 additions & 27 deletions src/libsyntax/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub use self::IntType::*;

use ast;
use ast::{AttrId, Attribute, Attribute_, MetaItem, MetaItemKind};
use ast::{Stmt, StmtKind, DeclKind};
use ast::{Expr, Item, Local, Decl};
use ast::{Stmt, StmtKind};
use ast::{Expr, Item, Local};
use codemap::{Span, Spanned, spanned, dummy_spanned};
use codemap::BytePos;
use errors::Handler;
Expand Down Expand Up @@ -924,38 +924,28 @@ impl<T: HasAttrs + 'static> HasAttrs for P<T> {
}
}

impl HasAttrs for DeclKind {
fn attrs(&self) -> &[Attribute] {
match *self {
DeclKind::Local(ref local) => local.attrs(),
DeclKind::Item(ref item) => item.attrs(),
}
}

fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
match self {
DeclKind::Local(local) => DeclKind::Local(local.map_attrs(f)),
DeclKind::Item(item) => DeclKind::Item(item.map_attrs(f)),
}
}
}

impl HasAttrs for StmtKind {
fn attrs(&self) -> &[Attribute] {
match *self {
StmtKind::Decl(ref decl, _) => decl.attrs(),
StmtKind::Expr(ref expr, _) | StmtKind::Semi(ref expr, _) => expr.attrs(),
StmtKind::Mac(_, _, ref attrs) => attrs.attrs(),
StmtKind::Local(ref local) => local.attrs(),
StmtKind::Item(ref item) => item.attrs(),
StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => expr.attrs(),
StmtKind::Mac(ref mac) => {
let (_, _, ref attrs) = **mac;
attrs.attrs()
}
}
}

fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
match self {
StmtKind::Decl(decl, id) => StmtKind::Decl(decl.map_attrs(f), id),
StmtKind::Expr(expr, id) => StmtKind::Expr(expr.map_attrs(f), id),
StmtKind::Semi(expr, id) => StmtKind::Semi(expr.map_attrs(f), id),
StmtKind::Mac(mac, style, attrs) =>
StmtKind::Mac(mac, style, attrs.map_attrs(f)),
StmtKind::Local(local) => StmtKind::Local(local.map_attrs(f)),
StmtKind::Item(item) => StmtKind::Item(item.map_attrs(f)),
StmtKind::Expr(expr) => StmtKind::Expr(expr.map_attrs(f)),
StmtKind::Semi(expr) => StmtKind::Semi(expr.map_attrs(f)),
StmtKind::Mac(mac) => StmtKind::Mac(mac.map(|(mac, style, attrs)| {
(mac, style, attrs.map_attrs(f))
})),
}
}
}
Expand All @@ -982,4 +972,4 @@ derive_has_attrs_from_field! {
Item, Expr, Local, ast::ForeignItem, ast::StructField, ast::ImplItem, ast::TraitItem, ast::Arm
}

derive_has_attrs_from_field! { Decl: .node, Stmt: .node, ast::Variant: .node.attrs }
derive_has_attrs_from_field! { Stmt: .node, ast::Variant: .node.attrs }
13 changes: 3 additions & 10 deletions src/libsyntax/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,10 @@ impl<'a> fold::Folder for StripUnconfigured<'a> {
}

fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVector<ast::Stmt> {
let is_item = match stmt.node {
ast::StmtKind::Decl(ref decl, _) => match decl.node {
ast::DeclKind::Item(_) => true,
_ => false,
},
_ => false,
};

// avoid calling `visit_stmt_or_expr_attrs` on items
if !is_item {
self.visit_stmt_or_expr_attrs(stmt.attrs());
match stmt.node {
ast::StmtKind::Item(_) => {}
_ => self.visit_stmt_or_expr_attrs(stmt.attrs()),
}

self.configure(stmt).map(|stmt| fold::noop_fold_stmt(stmt, self))
Expand Down
18 changes: 10 additions & 8 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,11 @@ impl<F> IdentMacroExpander for F
// Use a macro because forwarding to a simple function has type system issues
macro_rules! make_stmts_default {
($me:expr) => {
$me.make_expr().map(|e| {
SmallVector::one(codemap::respan(
e.span, ast::StmtKind::Expr(e, ast::DUMMY_NODE_ID)))
})
$me.make_expr().map(|e| SmallVector::one(ast::Stmt {
id: ast::DUMMY_NODE_ID,
span: e.span,
node: ast::StmtKind::Expr(e),
}))
}
}

Expand Down Expand Up @@ -399,10 +400,11 @@ impl MacResult for DummyResult {
}

fn make_stmts(self: Box<DummyResult>) -> Option<SmallVector<ast::Stmt>> {
Some(SmallVector::one(
codemap::respan(self.span,
ast::StmtKind::Expr(DummyResult::raw_expr(self.span),
ast::DUMMY_NODE_ID))))
Some(SmallVector::one(ast::Stmt {
id: ast::DUMMY_NODE_ID,
node: ast::StmtKind::Expr(DummyResult::raw_expr(self.span)),
span: self.span,
}))
}
}

Expand Down
Loading