Skip to content

Commit 770013d

Browse files
committed
fix: Overlapping spans in delimited meta-vars
1 parent 503e129 commit 770013d

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

compiler/rustc_expand/src/mbe/macro_rules.rs

+7
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@ fn expand_macro<'cx>(
236236
target_sp.open = source_sp.open.with_ctxt(ctxt);
237237
target_sp.close = source_sp.close.with_ctxt(ctxt);
238238
}
239+
(
240+
TokenTree::Delimited(target_sp, ..),
241+
mbe::TokenTree::MetaVar(source_sp, ..),
242+
) => {
243+
target_sp.open = source_sp.with_ctxt(ctxt);
244+
target_sp.close = source_sp.with_ctxt(ctxt).shrink_to_hi();
245+
}
239246
_ => {
240247
let sp = rhs_tt.span().with_ctxt(ctxt);
241248
tt.set_span(sp);

tests/ui/macros/issue-118786.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// compile-flags: --crate-type lib -O -C debug-assertions=yes
2+
3+
// Regression test for issue 118786
4+
5+
macro_rules! make_macro {
6+
($macro_name:tt) => {
7+
macro_rules! $macro_name {
8+
//~^ ERROR macros that expand to items must be delimited with braces or followed by a semicolon
9+
//~| ERROR macro expansion ignores token `{` and any following
10+
//~| ERROR cannot find macro `macro_rules` in this scope
11+
() => {}
12+
}
13+
}
14+
}
15+
16+
make_macro!((meow));

tests/ui/macros/issue-118786.stderr

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
error: macros that expand to items must be delimited with braces or followed by a semicolon
2+
--> $DIR/issue-118786.rs:7:22
3+
|
4+
LL | macro_rules! $macro_name {
5+
| ^^^^^^^^^^^
6+
|
7+
help: change the delimiters to curly braces
8+
|
9+
LL | macro_rules! {} {
10+
| ~ +
11+
help: add a semicolon
12+
|
13+
LL | macro_rules! $macro_name; {
14+
| +
15+
16+
error: macro expansion ignores token `{` and any following
17+
--> $DIR/issue-118786.rs:7:34
18+
|
19+
LL | macro_rules! $macro_name {
20+
| ^
21+
...
22+
LL | make_macro!((meow));
23+
| ------------------- caused by the macro expansion here
24+
|
25+
= note: the usage of `make_macro!` is likely invalid in item context
26+
27+
error: cannot find macro `macro_rules` in this scope
28+
--> $DIR/issue-118786.rs:7:9
29+
|
30+
LL | macro_rules! $macro_name {
31+
| ^^^^^^^^^^^
32+
...
33+
LL | make_macro!((meow));
34+
| ------------------- in this macro invocation
35+
|
36+
note: maybe you have forgotten to define a name for this `macro_rules!`
37+
--> $DIR/issue-118786.rs:7:9
38+
|
39+
LL | macro_rules! $macro_name {
40+
| ^^^^^^^^^^^
41+
...
42+
LL | make_macro!((meow));
43+
| ------------------- in this macro invocation
44+
= note: this error originates in the macro `make_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
45+
46+
error: aborting due to 3 previous errors
47+

0 commit comments

Comments
 (0)