From 758834d3e2b2589f73b62df386780c8096ed7ae1 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Wed, 25 Nov 2020 16:52:24 -0500 Subject: [PATCH] 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. --- compiler/rustc_parse/src/parser/stmt.rs | 3 +-- src/test/ui/proc-macro/allowed-attr-stmt-expr.stdout | 7 +------ 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index b41aba9b627..e974556f43a 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -473,8 +473,7 @@ impl<'a> Parser<'a> { // so capture it add_semi_token(local.tokens.as_mut()); } - StmtKind::Empty => eat_semi = false, - _ => {} + StmtKind::Empty | StmtKind::Item(_) | StmtKind::Semi(_) => eat_semi = false, } if eat_semi && self.eat(&token::Semi) { diff --git a/src/test/ui/proc-macro/allowed-attr-stmt-expr.stdout b/src/test/ui/proc-macro/allowed-attr-stmt-expr.stdout index 189d2383b7b..0b9ce8cdfdb 100644 --- a/src/test/ui/proc-macro/allowed-attr-stmt-expr.stdout +++ b/src/test/ui/proc-macro/allowed-attr-stmt-expr.stdout @@ -261,7 +261,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [ span: $DIR/allowed-attr-stmt-expr.rs:53:54: 53:56 (#0), }, ] -PRINT-ATTR INPUT (DISPLAY): #[rustc_dummy] struct Other { } ; +PRINT-ATTR INPUT (DISPLAY): #[rustc_dummy] struct Other { } PRINT-ATTR INPUT (DEBUG): TokenStream [ Punct { ch: '#', @@ -291,9 +291,4 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [ stream: TokenStream [], span: $DIR/allowed-attr-stmt-expr.rs:57:18: 57:20 (#0), }, - Punct { - ch: ';', - spacing: Alone, - span: $DIR/allowed-attr-stmt-expr.rs:57:20: 57:21 (#0), - }, ]