Skip to content

Commit 758834d

Browse files
committed
Only eat semicolons for statements that need them
When parsing a statement (e.g. inside a function body), we now consider `struct Foo {};` and `$stmt;` to each consist of two statements: `struct Foo {}` and `;`, and `$stmt` and `;`. As a result, an attribute macro invoke as `fn foo() { #[attr] struct Bar{}; }` will see `struct Bar{}` as its input. Additionally, the 'unused semicolon' lint now fires in more places.
1 parent de88bf1 commit 758834d

File tree

2 files changed

+2
-8
lines changed

2 files changed

+2
-8
lines changed

compiler/rustc_parse/src/parser/stmt.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,7 @@ impl<'a> Parser<'a> {
473473
// so capture it
474474
add_semi_token(local.tokens.as_mut());
475475
}
476-
StmtKind::Empty => eat_semi = false,
477-
_ => {}
476+
StmtKind::Empty | StmtKind::Item(_) | StmtKind::Semi(_) => eat_semi = false,
478477
}
479478

480479
if eat_semi && self.eat(&token::Semi) {

src/test/ui/proc-macro/allowed-attr-stmt-expr.stdout

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
261261
span: $DIR/allowed-attr-stmt-expr.rs:53:54: 53:56 (#0),
262262
},
263263
]
264-
PRINT-ATTR INPUT (DISPLAY): #[rustc_dummy] struct Other { } ;
264+
PRINT-ATTR INPUT (DISPLAY): #[rustc_dummy] struct Other { }
265265
PRINT-ATTR INPUT (DEBUG): TokenStream [
266266
Punct {
267267
ch: '#',
@@ -291,9 +291,4 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
291291
stream: TokenStream [],
292292
span: $DIR/allowed-attr-stmt-expr.rs:57:18: 57:20 (#0),
293293
},
294-
Punct {
295-
ch: ';',
296-
spacing: Alone,
297-
span: $DIR/allowed-attr-stmt-expr.rs:57:20: 57:21 (#0),
298-
},
299294
]

0 commit comments

Comments
 (0)