From ede96cdb1b219c775eade1c0d3559693811e72df Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 10 Dec 2023 17:59:49 -0800 Subject: [PATCH 1/2] Add test reproducing unary None-delimited group precedence bug --- tests/test_expr.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_expr.rs b/tests/test_expr.rs index 9d0b44f9ba..ca5d270f3c 100644 --- a/tests/test_expr.rs +++ b/tests/test_expr.rs @@ -216,6 +216,30 @@ fn test_macro_variable_struct() { "###); } +#[test] +fn test_macro_variable_unary() { + // mimics the token stream corresponding to `$expr.method()` where expr is `&self` + let inner = Group::new(Delimiter::None, quote!(&self)); + let tokens = quote!(#inner.method()); + // FIXME + snapshot!(tokens as Expr, @r###" + Expr::Reference { + expr: Expr::MethodCall { + receiver: Expr::Path { + path: Path { + segments: [ + PathSegment { + ident: "self", + }, + ], + }, + }, + method: "method", + }, + } + "###); +} + #[test] fn test_macro_variable_match_arm() { // mimics the token stream corresponding to `match v { _ => $expr }` From 1ffbda912e6abb80a39775a1821579731f992c50 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 10 Dec 2023 18:00:51 -0800 Subject: [PATCH 2/2] Respect None-delimited group when parsing unary expr --- src/expr.rs | 4 ++++ tests/test_expr.rs | 23 ++++++++++++----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index f3e37d49c7..334caada90 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1368,6 +1368,10 @@ pub(crate) mod parsing { fn unary_expr(input: ParseStream, allow_struct: AllowStruct) -> Result { let begin = input.fork(); let attrs = input.call(expr_attrs)?; + if input.peek(token::Group) { + return trailer_expr(begin, attrs, input, allow_struct); + } + if input.peek(Token![&]) { let and_token: Token![&] = input.parse()?; let raw: Option = if input.peek(kw::raw) diff --git a/tests/test_expr.rs b/tests/test_expr.rs index ca5d270f3c..64cbea88f7 100644 --- a/tests/test_expr.rs +++ b/tests/test_expr.rs @@ -221,21 +221,22 @@ fn test_macro_variable_unary() { // mimics the token stream corresponding to `$expr.method()` where expr is `&self` let inner = Group::new(Delimiter::None, quote!(&self)); let tokens = quote!(#inner.method()); - // FIXME snapshot!(tokens as Expr, @r###" - Expr::Reference { - expr: Expr::MethodCall { - receiver: Expr::Path { - path: Path { - segments: [ - PathSegment { - ident: "self", - }, - ], + Expr::MethodCall { + receiver: Expr::Group { + expr: Expr::Reference { + expr: Expr::Path { + path: Path { + segments: [ + PathSegment { + ident: "self", + }, + ], + }, }, }, - method: "method", }, + method: "method", } "###); }