From d562848268e02d6cf342598551bc663fcd4ff341 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Sat, 28 Aug 2021 17:28:39 -0700 Subject: [PATCH 1/2] fix(rustc_parse): incorrect span information for macro block expr Old error output: = note: this warning originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info) help: wrap this expression in parentheses | 4 | break '_l $f(;) | ^ ^ New error output: = note: this warning originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info) help: wrap this expression in parentheses | 4 | break '_l ($f); | ^ ^ --- compiler/rustc_parse/src/parser/expr.rs | 2 +- src/test/ui/parser/issue-87812.rs | 13 +++++++++++++ src/test/ui/parser/issue-87812.stderr | 22 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/parser/issue-87812.rs create mode 100644 src/test/ui/parser/issue-87812.stderr diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 326c8f81ffbf9..bb7e44b18d262 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -50,7 +50,7 @@ macro_rules! maybe_whole_expr { let block = block.clone(); $p.bump(); return Ok($p.mk_expr( - $p.token.span, + $p.prev_token.span, ExprKind::Block(block, None), AttrVec::new(), )); diff --git a/src/test/ui/parser/issue-87812.rs b/src/test/ui/parser/issue-87812.rs new file mode 100644 index 0000000000000..0ba87b995443f --- /dev/null +++ b/src/test/ui/parser/issue-87812.rs @@ -0,0 +1,13 @@ +#![deny(break_with_label_and_loop)] + +macro_rules! foo { + ( $f:block ) => { + '_l: loop { + break '_l $f; //~ERROR + } + }; +} + +fn main() { + let x = foo!({ 3 }); +} diff --git a/src/test/ui/parser/issue-87812.stderr b/src/test/ui/parser/issue-87812.stderr new file mode 100644 index 0000000000000..d61ee23a50b5b --- /dev/null +++ b/src/test/ui/parser/issue-87812.stderr @@ -0,0 +1,22 @@ +error: this labeled break expression is easy to confuse with an unlabeled break with a labeled value expression + --> $DIR/issue-87812.rs:6:13 + | +LL | break '_l $f; + | ^^^^^^^^^^^^ +... +LL | let x = foo!({ 3 }); + | ----------- in this macro invocation + | +note: the lint level is defined here + --> $DIR/issue-87812.rs:1:9 + | +LL | #![deny(break_with_label_and_loop)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info) +help: wrap this expression in parentheses + | +LL | break '_l ($f); + | + + + +error: aborting due to previous error + From f7c0566b1209fc7c29d9621cb482acdf58dabd65 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Sat, 28 Aug 2021 17:35:39 -0700 Subject: [PATCH 2/2] fix(rustc_parse): incorrect span information for macro path expr Old error output: 3 | let _: usize = $f; | ----- ^ expected `usize`, found struct `Baz` | | | expected due to this New error output: 3 | let _: usize = $f; | ----- ^^ expected `usize`, found struct `Baz` | | | expected due to this --- compiler/rustc_parse/src/parser/expr.rs | 2 +- src/test/ui/parser/issue-87812-path.rs | 11 +++++++++++ src/test/ui/parser/issue-87812-path.stderr | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/parser/issue-87812-path.rs create mode 100644 src/test/ui/parser/issue-87812-path.stderr diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index bb7e44b18d262..eea6c482cf18d 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -41,7 +41,7 @@ macro_rules! maybe_whole_expr { let path = path.clone(); $p.bump(); return Ok($p.mk_expr( - $p.token.span, + $p.prev_token.span, ExprKind::Path(None, path), AttrVec::new(), )); diff --git a/src/test/ui/parser/issue-87812-path.rs b/src/test/ui/parser/issue-87812-path.rs new file mode 100644 index 0000000000000..b88780876db06 --- /dev/null +++ b/src/test/ui/parser/issue-87812-path.rs @@ -0,0 +1,11 @@ +macro_rules! foo { + ( $f:path ) => {{ + let _: usize = $f; //~ERROR + }}; +} + +struct Baz; + +fn main() { + foo!(Baz); +} diff --git a/src/test/ui/parser/issue-87812-path.stderr b/src/test/ui/parser/issue-87812-path.stderr new file mode 100644 index 0000000000000..0c8e6fdd3076a --- /dev/null +++ b/src/test/ui/parser/issue-87812-path.stderr @@ -0,0 +1,16 @@ +error[E0308]: mismatched types + --> $DIR/issue-87812-path.rs:3:24 + | +LL | let _: usize = $f; + | ----- ^^ expected `usize`, found struct `Baz` + | | + | expected due to this +... +LL | foo!(Baz); + | ---------- in this macro invocation + | + = note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`.