From 3caf0bcdeb9546bd8a3ab58c0e33baabdcaac75a Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 6 Sep 2021 18:12:55 +0200 Subject: [PATCH 1/3] Accept `m!{ .. }.method()` and `m!{ .. }?` statements. --- compiler/rustc_parse/src/parser/stmt.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index 068bd36af5524..f1a91c41355ee 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -155,17 +155,20 @@ impl<'a> Parser<'a> { let mac = MacCall { path, args, prior_type_ascription: self.last_type_ascription }; - let kind = if delim == token::Brace || self.token == token::Semi || self.token == token::Eof - { - StmtKind::MacCall(P(MacCallStmt { mac, style, attrs, tokens: None })) - } else { - // Since none of the above applied, this is an expression statement macro. - let e = self.mk_expr(lo.to(hi), ExprKind::MacCall(mac), AttrVec::new()); - let e = self.maybe_recover_from_bad_qpath(e, true)?; - let e = self.parse_dot_or_call_expr_with(e, lo, attrs.into())?; - let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?; - StmtKind::Expr(e) - }; + let kind = + if (delim == token::Brace && self.token != token::Dot && self.token != token::Question) + || self.token == token::Semi + || self.token == token::Eof + { + StmtKind::MacCall(P(MacCallStmt { mac, style, attrs, tokens: None })) + } else { + // Since none of the above applied, this is an expression statement macro. + let e = self.mk_expr(lo.to(hi), ExprKind::MacCall(mac), AttrVec::new()); + let e = self.maybe_recover_from_bad_qpath(e, true)?; + let e = self.parse_dot_or_call_expr_with(e, lo, attrs.into())?; + let e = self.parse_assoc_expr_with(0, LhsExpr::AlreadyParsed(e))?; + StmtKind::Expr(e) + }; Ok(self.mk_stmt(lo.to(hi), kind)) } From ebf12529dfa62cf463de4fd5e1355e8ea4fe0559 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 6 Sep 2021 18:17:46 +0200 Subject: [PATCH 2/3] Add test for braced-macro followed by `.` or `?`. --- src/test/ui/parser/macro-braces-dot-question.rs | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/test/ui/parser/macro-braces-dot-question.rs diff --git a/src/test/ui/parser/macro-braces-dot-question.rs b/src/test/ui/parser/macro-braces-dot-question.rs new file mode 100644 index 0000000000000..05de5a845e958 --- /dev/null +++ b/src/test/ui/parser/macro-braces-dot-question.rs @@ -0,0 +1,9 @@ +// check-pass + +use std::io::Write; + +fn main() -> Result<(), std::io::Error> { + vec! { 1, 2, 3 }.len(); + write! { vec![], "" }?; + Ok(()) +} From 7d8d7a03412f6db1402f2713ee96a2040cfb24d2 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 13 Sep 2021 17:50:59 +0200 Subject: [PATCH 3/3] Add negative test in macro-braces-dot-question.rs. --- src/test/ui/parser/macro-braces-dot-question.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/ui/parser/macro-braces-dot-question.rs b/src/test/ui/parser/macro-braces-dot-question.rs index 05de5a845e958..016b434a6124a 100644 --- a/src/test/ui/parser/macro-braces-dot-question.rs +++ b/src/test/ui/parser/macro-braces-dot-question.rs @@ -5,5 +5,7 @@ use std::io::Write; fn main() -> Result<(), std::io::Error> { vec! { 1, 2, 3 }.len(); write! { vec![], "" }?; + println!{""} + [0]; // separate statement, not indexing into the result of println. Ok(()) }