Skip to content

Commit 8748cd9

Browse files
committed
Rollup merge of rust-lang#34316 - jseyfried:refactor_ast_stmt, r=eddyb
Refactor away `ast::Decl`, refactor `ast::Stmt`, and rename `ast::ExprKind::Again` to `ast::ExprKind::Continue`.
2 parents 33ea1e3 + f6fe5b6 commit 8748cd9

File tree

23 files changed

+330
-408
lines changed

23 files changed

+330
-408
lines changed

src/librustc/hir/lowering.rs

+18-23
Original file line numberDiff line numberDiff line change
@@ -238,19 +238,6 @@ impl<'a> LoweringContext<'a> {
238238
}
239239
}
240240

241-
fn lower_decl(&mut self, d: &Decl) -> P<hir::Decl> {
242-
match d.node {
243-
DeclKind::Local(ref l) => P(Spanned {
244-
node: hir::DeclLocal(self.lower_local(l)),
245-
span: d.span,
246-
}),
247-
DeclKind::Item(ref it) => P(Spanned {
248-
node: hir::DeclItem(self.lower_item_id(it)),
249-
span: d.span,
250-
}),
251-
}
252-
}
253-
254241
fn lower_ty_binding(&mut self, b: &TypeBinding) -> hir::TypeBinding {
255242
hir::TypeBinding {
256243
id: b.id,
@@ -1225,7 +1212,7 @@ impl<'a> LoweringContext<'a> {
12251212
hir::ExprPath(hir_qself, self.lower_path(path))
12261213
}
12271214
ExprKind::Break(opt_ident) => hir::ExprBreak(self.lower_opt_sp_ident(opt_ident)),
1228-
ExprKind::Again(opt_ident) => hir::ExprAgain(self.lower_opt_sp_ident(opt_ident)),
1215+
ExprKind::Continue(opt_ident) => hir::ExprAgain(self.lower_opt_sp_ident(opt_ident)),
12291216
ExprKind::Ret(ref e) => hir::ExprRet(e.as_ref().map(|x| self.lower_expr(x))),
12301217
ExprKind::InlineAsm(InlineAsm {
12311218
ref inputs,
@@ -1581,21 +1568,29 @@ impl<'a> LoweringContext<'a> {
15811568

15821569
fn lower_stmt(&mut self, s: &Stmt) -> hir::Stmt {
15831570
match s.node {
1584-
StmtKind::Decl(ref d, id) => {
1585-
Spanned {
1586-
node: hir::StmtDecl(self.lower_decl(d), id),
1571+
StmtKind::Local(ref l) => Spanned {
1572+
node: hir::StmtDecl(P(Spanned {
1573+
node: hir::DeclLocal(self.lower_local(l)),
15871574
span: s.span,
1588-
}
1589-
}
1590-
StmtKind::Expr(ref e, id) => {
1575+
}), s.id),
1576+
span: s.span,
1577+
},
1578+
StmtKind::Item(ref it) => Spanned {
1579+
node: hir::StmtDecl(P(Spanned {
1580+
node: hir::DeclItem(self.lower_item_id(it)),
1581+
span: s.span,
1582+
}), s.id),
1583+
span: s.span,
1584+
},
1585+
StmtKind::Expr(ref e) => {
15911586
Spanned {
1592-
node: hir::StmtExpr(self.lower_expr(e), id),
1587+
node: hir::StmtExpr(self.lower_expr(e), s.id),
15931588
span: s.span,
15941589
}
15951590
}
1596-
StmtKind::Semi(ref e, id) => {
1591+
StmtKind::Semi(ref e) => {
15971592
Spanned {
1598-
node: hir::StmtSemi(self.lower_expr(e), id),
1593+
node: hir::StmtSemi(self.lower_expr(e), s.id),
15991594
span: s.span,
16001595
}
16011596
}

src/librustc/lint/context.rs

-5
Original file line numberDiff line numberDiff line change
@@ -1005,11 +1005,6 @@ impl<'a> ast_visit::Visitor for EarlyContext<'a> {
10051005
ast_visit::walk_arm(self, a);
10061006
}
10071007

1008-
fn visit_decl(&mut self, d: &ast::Decl) {
1009-
run_lints!(self, check_decl, early_passes, d);
1010-
ast_visit::walk_decl(self, d);
1011-
}
1012-
10131008
fn visit_expr_post(&mut self, e: &ast::Expr) {
10141009
run_lints!(self, check_expr_post, early_passes, e);
10151010
}

src/librustc/lint/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ pub trait EarlyLintPass: LintPass {
195195
fn check_stmt(&mut self, _: &EarlyContext, _: &ast::Stmt) { }
196196
fn check_arm(&mut self, _: &EarlyContext, _: &ast::Arm) { }
197197
fn check_pat(&mut self, _: &EarlyContext, _: &ast::Pat) { }
198-
fn check_decl(&mut self, _: &EarlyContext, _: &ast::Decl) { }
199198
fn check_expr(&mut self, _: &EarlyContext, _: &ast::Expr) { }
200199
fn check_expr_post(&mut self, _: &EarlyContext, _: &ast::Expr) { }
201200
fn check_ty(&mut self, _: &EarlyContext, _: &ast::Ty) { }

src/librustc_lint/unused.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,9 @@ impl EarlyLintPass for UnusedParens {
365365

366366
fn check_stmt(&mut self, cx: &EarlyContext, s: &ast::Stmt) {
367367
let (value, msg) = match s.node {
368-
ast::StmtKind::Decl(ref decl, _) => match decl.node {
369-
ast::DeclKind::Local(ref local) => match local.init {
370-
Some(ref value) => (value, "assigned value"),
371-
None => return
372-
},
373-
_ => return
368+
ast::StmtKind::Local(ref local) => match local.init {
369+
Some(ref value) => (value, "assigned value"),
370+
None => return
374371
},
375372
_ => return
376373
};

src/librustc_passes/ast_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl<'a> Visitor for AstValidator<'a> {
7373
match expr.node {
7474
ExprKind::While(_, _, Some(ident)) | ExprKind::Loop(_, Some(ident)) |
7575
ExprKind::WhileLet(_, _, _, Some(ident)) | ExprKind::ForLoop(_, _, _, Some(ident)) |
76-
ExprKind::Break(Some(ident)) | ExprKind::Again(Some(ident)) => {
76+
ExprKind::Break(Some(ident)) | ExprKind::Continue(Some(ident)) => {
7777
self.check_label(ident.node, ident.span, expr.id);
7878
}
7979
_ => {}

src/librustc_resolve/build_reduced_graph.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ use syntax::ast::Name;
3030
use syntax::attr;
3131
use syntax::parse::token;
3232

33-
use syntax::ast::{Block, Crate, DeclKind};
33+
use syntax::ast::{Block, Crate};
3434
use syntax::ast::{ForeignItem, ForeignItemKind, Item, ItemKind};
3535
use syntax::ast::{Mutability, PathListItemKind};
36-
use syntax::ast::{Stmt, StmtKind, TraitItemKind};
36+
use syntax::ast::{StmtKind, TraitItemKind};
3737
use syntax::ast::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple};
3838
use syntax::visit::{self, Visitor};
3939

@@ -85,17 +85,11 @@ impl<'b> Resolver<'b> {
8585
}
8686

8787
fn block_needs_anonymous_module(&mut self, block: &Block) -> bool {
88-
fn is_item(statement: &Stmt) -> bool {
89-
if let StmtKind::Decl(ref declaration, _) = statement.node {
90-
if let DeclKind::Item(_) = declaration.node {
91-
return true;
92-
}
93-
}
94-
false
95-
}
96-
9788
// If any statements are items, we need to create an anonymous module
98-
block.stmts.iter().any(is_item)
89+
block.stmts.iter().any(|statement| match statement.node {
90+
StmtKind::Item(_) => true,
91+
_ => false,
92+
})
9993
}
10094

10195
/// Constructs the reduced graph for one item.

src/librustc_resolve/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2951,7 +2951,7 @@ impl<'a> Resolver<'a> {
29512951
})
29522952
}
29532953

2954-
ExprKind::Break(Some(label)) | ExprKind::Again(Some(label)) => {
2954+
ExprKind::Break(Some(label)) | ExprKind::Continue(Some(label)) => {
29552955
match self.search_label(mtwt::resolve(label.node)) {
29562956
None => {
29572957
self.record_def(expr.id, err_path_resolution());

src/librustc_save_analysis/dump_visitor.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1423,8 +1423,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
14231423
}
14241424

14251425
fn visit_stmt(&mut self, s: &ast::Stmt) {
1426-
let id = s.node.id();
1427-
self.process_macro_use(s.span, id.unwrap());
1426+
self.process_macro_use(s.span, s.id);
14281427
visit::walk_stmt(self, s)
14291428
}
14301429

src/libsyntax/ast.rs

+17-34
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use tokenstream::{TokenTree};
2727

2828
use std::fmt;
2929
use std::rc::Rc;
30-
use std::borrow::Cow;
3130
use std::hash::{Hash, Hasher};
3231
use serialize::{Encodable, Decodable, Encoder, Decoder};
3332

@@ -811,41 +810,35 @@ impl UnOp {
811810
}
812811

813812
/// A statement
814-
pub type Stmt = Spanned<StmtKind>;
813+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)]
814+
pub struct Stmt {
815+
pub id: NodeId,
816+
pub node: StmtKind,
817+
pub span: Span,
818+
}
815819

816820
impl fmt::Debug for Stmt {
817821
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
818-
write!(f, "stmt({}: {})",
819-
self.node.id()
820-
.map_or(Cow::Borrowed("<macro>"),|id|Cow::Owned(id.to_string())),
821-
pprust::stmt_to_string(self))
822+
write!(f, "stmt({}: {})", self.id.to_string(), pprust::stmt_to_string(self))
822823
}
823824
}
824825

825826

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

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

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

837-
Mac(P<Mac>, MacStmtStyle, ThinAttributes),
838-
}
838+
/// Expr with trailing semi-colon (may have any type).
839+
Semi(P<Expr>),
839840

840-
impl StmtKind {
841-
pub fn id(&self) -> Option<NodeId> {
842-
match *self {
843-
StmtKind::Decl(_, id) => Some(id),
844-
StmtKind::Expr(_, id) => Some(id),
845-
StmtKind::Semi(_, id) => Some(id),
846-
StmtKind::Mac(..) => None,
847-
}
848-
}
841+
Mac(P<(Mac, MacStmtStyle, ThinAttributes)>),
849842
}
850843

851844
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
@@ -875,16 +868,6 @@ pub struct Local {
875868
pub attrs: ThinAttributes,
876869
}
877870

878-
pub type Decl = Spanned<DeclKind>;
879-
880-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
881-
pub enum DeclKind {
882-
/// A local (let) binding:
883-
Local(P<Local>),
884-
/// An item binding:
885-
Item(P<Item>),
886-
}
887-
888871
/// An arm of a 'match'.
889872
///
890873
/// E.g. `0...10 => { println!("match!") }` as in
@@ -1053,7 +1036,7 @@ pub enum ExprKind {
10531036
/// A `break`, with an optional label to break
10541037
Break(Option<SpannedIdent>),
10551038
/// A `continue`, with an optional label
1056-
Again(Option<SpannedIdent>),
1039+
Continue(Option<SpannedIdent>),
10571040
/// A `return`, with an optional value to be returned
10581041
Ret(Option<P<Expr>>),
10591042

src/libsyntax/attr.rs

+16-27
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ pub use self::IntType::*;
1616

1717
use ast;
1818
use ast::{AttrId, Attribute, Attribute_, MetaItem, MetaItemKind};
19-
use ast::{Stmt, StmtKind, DeclKind};
20-
use ast::{Expr, Item, Local, Decl};
19+
use ast::{Expr, Item, Local, Stmt, StmtKind};
2120
use codemap::{spanned, dummy_spanned, Spanned};
2221
use syntax_pos::{Span, BytePos};
2322
use errors::Handler;
@@ -909,38 +908,28 @@ impl<T: HasAttrs + 'static> HasAttrs for P<T> {
909908
}
910909
}
911910

912-
impl HasAttrs for DeclKind {
913-
fn attrs(&self) -> &[Attribute] {
914-
match *self {
915-
DeclKind::Local(ref local) => local.attrs(),
916-
DeclKind::Item(ref item) => item.attrs(),
917-
}
918-
}
919-
920-
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
921-
match self {
922-
DeclKind::Local(local) => DeclKind::Local(local.map_attrs(f)),
923-
DeclKind::Item(item) => DeclKind::Item(item.map_attrs(f)),
924-
}
925-
}
926-
}
927-
928911
impl HasAttrs for StmtKind {
929912
fn attrs(&self) -> &[Attribute] {
930913
match *self {
931-
StmtKind::Decl(ref decl, _) => decl.attrs(),
932-
StmtKind::Expr(ref expr, _) | StmtKind::Semi(ref expr, _) => expr.attrs(),
933-
StmtKind::Mac(_, _, ref attrs) => attrs.attrs(),
914+
StmtKind::Local(ref local) => local.attrs(),
915+
StmtKind::Item(ref item) => item.attrs(),
916+
StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => expr.attrs(),
917+
StmtKind::Mac(ref mac) => {
918+
let (_, _, ref attrs) = **mac;
919+
attrs.attrs()
920+
}
934921
}
935922
}
936923

937924
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
938925
match self {
939-
StmtKind::Decl(decl, id) => StmtKind::Decl(decl.map_attrs(f), id),
940-
StmtKind::Expr(expr, id) => StmtKind::Expr(expr.map_attrs(f), id),
941-
StmtKind::Semi(expr, id) => StmtKind::Semi(expr.map_attrs(f), id),
942-
StmtKind::Mac(mac, style, attrs) =>
943-
StmtKind::Mac(mac, style, attrs.map_attrs(f)),
926+
StmtKind::Local(local) => StmtKind::Local(local.map_attrs(f)),
927+
StmtKind::Item(item) => StmtKind::Item(item.map_attrs(f)),
928+
StmtKind::Expr(expr) => StmtKind::Expr(expr.map_attrs(f)),
929+
StmtKind::Semi(expr) => StmtKind::Semi(expr.map_attrs(f)),
930+
StmtKind::Mac(mac) => StmtKind::Mac(mac.map(|(mac, style, attrs)| {
931+
(mac, style, attrs.map_attrs(f))
932+
})),
944933
}
945934
}
946935
}
@@ -967,4 +956,4 @@ derive_has_attrs_from_field! {
967956
Item, Expr, Local, ast::ForeignItem, ast::StructField, ast::ImplItem, ast::TraitItem, ast::Arm
968957
}
969958

970-
derive_has_attrs_from_field! { Decl: .node, Stmt: .node, ast::Variant: .node.attrs }
959+
derive_has_attrs_from_field! { Stmt: .node, ast::Variant: .node.attrs }

src/libsyntax/config.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,10 @@ impl<'a> fold::Folder for StripUnconfigured<'a> {
213213
}
214214

215215
fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVector<ast::Stmt> {
216-
let is_item = match stmt.node {
217-
ast::StmtKind::Decl(ref decl, _) => match decl.node {
218-
ast::DeclKind::Item(_) => true,
219-
_ => false,
220-
},
221-
_ => false,
222-
};
223-
224216
// avoid calling `visit_stmt_or_expr_attrs` on items
225-
if !is_item {
226-
self.visit_stmt_or_expr_attrs(stmt.attrs());
217+
match stmt.node {
218+
ast::StmtKind::Item(_) => {}
219+
_ => self.visit_stmt_or_expr_attrs(stmt.attrs()),
227220
}
228221

229222
self.configure(stmt).map(|stmt| fold::noop_fold_stmt(stmt, self))

src/libsyntax/ext/base.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,11 @@ impl<F> IdentMacroExpander for F
221221
// Use a macro because forwarding to a simple function has type system issues
222222
macro_rules! make_stmts_default {
223223
($me:expr) => {
224-
$me.make_expr().map(|e| {
225-
SmallVector::one(codemap::respan(
226-
e.span, ast::StmtKind::Expr(e, ast::DUMMY_NODE_ID)))
227-
})
224+
$me.make_expr().map(|e| SmallVector::one(ast::Stmt {
225+
id: ast::DUMMY_NODE_ID,
226+
span: e.span,
227+
node: ast::StmtKind::Expr(e),
228+
}))
228229
}
229230
}
230231

@@ -436,10 +437,11 @@ impl MacResult for DummyResult {
436437
}
437438

438439
fn make_stmts(self: Box<DummyResult>) -> Option<SmallVector<ast::Stmt>> {
439-
Some(SmallVector::one(
440-
codemap::respan(self.span,
441-
ast::StmtKind::Expr(DummyResult::raw_expr(self.span),
442-
ast::DUMMY_NODE_ID))))
440+
Some(SmallVector::one(ast::Stmt {
441+
id: ast::DUMMY_NODE_ID,
442+
node: ast::StmtKind::Expr(DummyResult::raw_expr(self.span)),
443+
span: self.span,
444+
}))
443445
}
444446
}
445447

0 commit comments

Comments
 (0)