Skip to content

Commit 6189e6c

Browse files
committed
Clean up statement parsing without changing the semantics of parse_stmt.
1 parent 3265bd5 commit 6189e6c

File tree

3 files changed

+59
-112
lines changed

3 files changed

+59
-112
lines changed

src/libsyntax/ast.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,19 @@ pub struct Stmt {
804804
pub span: Span,
805805
}
806806

807+
impl Stmt {
808+
pub fn add_trailing_semicolon(mut self) -> Self {
809+
self.node = match self.node {
810+
StmtKind::Expr(expr) => StmtKind::Semi(expr),
811+
StmtKind::Mac(mac) => StmtKind::Mac(mac.map(|(mac, _style, attrs)| {
812+
(mac, MacStmtStyle::Semicolon, attrs)
813+
})),
814+
node @ _ => node,
815+
};
816+
self
817+
}
818+
}
819+
807820
impl fmt::Debug for Stmt {
808821
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
809822
write!(f, "stmt({}: {})", self.id.to_string(), pprust::stmt_to_string(self))

src/libsyntax/ext/expand.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -444,14 +444,7 @@ fn expand_stmt(stmt: Stmt, fld: &mut MacroExpander) -> SmallVector<Stmt> {
444444
// semicolon to the final statement produced by expansion.
445445
if style == MacStmtStyle::Semicolon {
446446
if let Some(stmt) = fully_expanded.pop() {
447-
fully_expanded.push(Stmt {
448-
id: stmt.id,
449-
node: match stmt.node {
450-
StmtKind::Expr(expr) => StmtKind::Semi(expr),
451-
_ => stmt.node /* might already have a semi */
452-
},
453-
span: stmt.span,
454-
});
447+
fully_expanded.push(stmt.add_trailing_semicolon());
455448
}
456449
}
457450

src/libsyntax/parse/parser.rs

Lines changed: 45 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -3789,7 +3789,13 @@ impl<'a> Parser<'a> {
37893789
self.span_err(self.last_span, message);
37903790
}
37913791

3792-
/// Parse a statement. may include decl.
3792+
/// Parse a statement. This stops just before trailing semicolons on everything but items.
3793+
/// e.g. a `StmtKind::Semi` parses to a `StmtKind::Expr`, leaving the trailing `;` unconsumed.
3794+
///
3795+
/// Also, if a macro begins an expression statement, this only parses the macro. For example,
3796+
/// ```rust
3797+
/// vec![1].into_iter(); //< `parse_stmt` only parses the "vec![1]"
3798+
/// ```
37933799
pub fn parse_stmt(&mut self) -> PResult<'a, Option<Stmt>> {
37943800
Ok(self.parse_stmt_())
37953801
}
@@ -4038,36 +4044,14 @@ impl<'a> Parser<'a> {
40384044
let mut stmts = vec![];
40394045

40404046
while !self.eat(&token::CloseDelim(token::Brace)) {
4041-
let Stmt {node, span, ..} = if let Some(s) = self.parse_stmt_() {
4042-
s
4047+
if let Some(stmt) = self.parse_stmt_() {
4048+
stmts.push(self.finish_parsing_statement(stmt)?);
40434049
} else if self.token == token::Eof {
40444050
break;
40454051
} else {
40464052
// Found only `;` or `}`.
40474053
continue;
40484054
};
4049-
4050-
match node {
4051-
StmtKind::Expr(e) => {
4052-
self.handle_expression_like_statement(e, span, &mut stmts)?;
4053-
}
4054-
StmtKind::Mac(mac) => {
4055-
self.handle_macro_in_block(mac.unwrap(), span, &mut stmts)?;
4056-
}
4057-
_ => { // all other kinds of statements:
4058-
let mut hi = span.hi;
4059-
if classify::stmt_ends_with_semi(&node) {
4060-
self.expect(&token::Semi)?;
4061-
hi = self.last_span.hi;
4062-
}
4063-
4064-
stmts.push(Stmt {
4065-
id: ast::DUMMY_NODE_ID,
4066-
node: node,
4067-
span: mk_sp(span.lo, hi)
4068-
});
4069-
}
4070-
}
40714055
}
40724056

40734057
Ok(P(ast::Block {
@@ -4078,93 +4062,50 @@ impl<'a> Parser<'a> {
40784062
}))
40794063
}
40804064

4081-
fn handle_macro_in_block(&mut self,
4082-
(mac, style, attrs): (ast::Mac, MacStmtStyle, ThinVec<Attribute>),
4083-
span: Span,
4084-
stmts: &mut Vec<Stmt>)
4085-
-> PResult<'a, ()> {
4086-
if style == MacStmtStyle::NoBraces {
4087-
// statement macro without braces; might be an
4088-
// expr depending on whether a semicolon follows
4089-
match self.token {
4090-
token::Semi => {
4091-
stmts.push(Stmt {
4092-
id: ast::DUMMY_NODE_ID,
4093-
node: StmtKind::Mac(P((mac, MacStmtStyle::Semicolon, attrs))),
4094-
span: mk_sp(span.lo, self.span.hi),
4095-
});
4096-
self.bump();
4097-
}
4098-
_ => {
4099-
let e = self.mk_mac_expr(span.lo, span.hi, mac.node, ThinVec::new());
4100-
let lo = e.span.lo;
4101-
let e = self.parse_dot_or_call_expr_with(e, lo, attrs)?;
4102-
let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?;
4103-
self.handle_expression_like_statement(e, span, stmts)?;
4104-
}
4105-
}
4106-
} else {
4107-
// statement macro; might be an expr
4108-
match self.token {
4109-
token::Semi => {
4110-
stmts.push(Stmt {
4111-
id: ast::DUMMY_NODE_ID,
4112-
node: StmtKind::Mac(P((mac, MacStmtStyle::Semicolon, attrs))),
4113-
span: mk_sp(span.lo, self.span.hi),
4114-
});
4115-
self.bump();
4116-
}
4117-
_ => {
4118-
stmts.push(Stmt {
4119-
id: ast::DUMMY_NODE_ID,
4120-
node: StmtKind::Mac(P((mac, style, attrs))),
4121-
span: span
4122-
});
4123-
}
4065+
/// Finish parsing expressions that start with macros and handle trailing semicolons
4066+
/// (or the lack thereof) -- c.f. `parse_stmt`.
4067+
fn finish_parsing_statement(&mut self, mut stmt: Stmt) -> PResult<'a, Stmt> {
4068+
if let StmtKind::Mac(mac) = stmt.node {
4069+
if mac.1 != MacStmtStyle::NoBraces || self.token == token::Semi {
4070+
stmt.node = StmtKind::Mac(mac);
4071+
} else {
4072+
let (mac, _style, attrs) = mac.unwrap();
4073+
let e = self.mk_mac_expr(stmt.span.lo, stmt.span.hi, mac.node, ThinVec::new());
4074+
let e = self.parse_dot_or_call_expr_with(e, stmt.span.lo, attrs)?;
4075+
let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?;
4076+
stmt.node = StmtKind::Expr(e);
41244077
}
41254078
}
4126-
Ok(())
4079+
4080+
self.handle_trailing_semicolon(stmt)
41274081
}
41284082

4129-
fn handle_expression_like_statement(&mut self,
4130-
e: P<Expr>,
4131-
span: Span,
4132-
stmts: &mut Vec<Stmt>)
4133-
-> PResult<'a, ()> {
4134-
// expression without semicolon
4135-
if classify::expr_requires_semi_to_be_stmt(&e) {
4136-
// Just check for errors and recover; do not eat semicolon yet.
4137-
if let Err(mut e) =
4138-
self.expect_one_of(&[], &[token::Semi, token::CloseDelim(token::Brace)])
4139-
{
4140-
e.emit();
4141-
self.recover_stmt();
4083+
fn handle_trailing_semicolon(&mut self, mut stmt: Stmt) -> PResult<'a, Stmt> {
4084+
match stmt.node {
4085+
StmtKind::Expr(ref expr) => {
4086+
// expression without semicolon
4087+
if classify::expr_requires_semi_to_be_stmt(expr) {
4088+
// Just check for errors and recover; do not eat semicolon yet.
4089+
if let Err(mut e) =
4090+
self.expect_one_of(&[], &[token::Semi, token::CloseDelim(token::Brace)])
4091+
{
4092+
e.emit();
4093+
self.recover_stmt();
4094+
}
4095+
}
41424096
}
4097+
StmtKind::Local(..) => {
4098+
self.expect_one_of(&[token::Semi], &[])?;
4099+
}
4100+
_ => {}
41434101
}
41444102

4145-
match self.token {
4146-
token::Semi => {
4147-
self.bump();
4148-
let span_with_semi = Span {
4149-
lo: span.lo,
4150-
hi: self.last_span.hi,
4151-
expn_id: span.expn_id,
4152-
};
4153-
stmts.push(Stmt {
4154-
id: ast::DUMMY_NODE_ID,
4155-
node: StmtKind::Semi(e),
4156-
span: span_with_semi,
4157-
});
4158-
}
4159-
_ => {
4160-
stmts.push(Stmt {
4161-
id: ast::DUMMY_NODE_ID,
4162-
node: StmtKind::Expr(e),
4163-
span: span
4164-
});
4165-
}
4103+
if self.eat(&token::Semi) {
4104+
stmt = stmt.add_trailing_semicolon();
41664105
}
4167-
Ok(())
4106+
4107+
stmt.span.hi = self.last_span.hi;
4108+
Ok(stmt)
41684109
}
41694110

41704111
// Parses a sequence of bounds if a `:` is found,

0 commit comments

Comments
 (0)