Skip to content

Commit dadf5a5

Browse files
authored
Rollup merge of rust-lang#55298 - estebank:macro-def, r=pnkfelix
Point at macro definition when no rules expect token Fix rust-lang#35150.
2 parents 02dd239 + 1ab45ec commit dadf5a5

21 files changed

+152
-50
lines changed

src/libsyntax/ext/base.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,13 @@ impl<F> AttrProcMacro for F
247247

248248
/// Represents a thing that maps token trees to Macro Results
249249
pub trait TTMacroExpander {
250-
fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, span: Span, input: TokenStream)
251-
-> Box<dyn MacResult+'cx>;
250+
fn expand<'cx>(
251+
&self,
252+
ecx: &'cx mut ExtCtxt,
253+
span: Span,
254+
input: TokenStream,
255+
def_span: Option<Span>,
256+
) -> Box<dyn MacResult+'cx>;
252257
}
253258

254259
pub type MacroExpanderFn =
@@ -259,8 +264,13 @@ impl<F> TTMacroExpander for F
259264
where F: for<'cx> Fn(&'cx mut ExtCtxt, Span, &[tokenstream::TokenTree])
260265
-> Box<dyn MacResult+'cx>
261266
{
262-
fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, span: Span, input: TokenStream)
263-
-> Box<dyn MacResult+'cx> {
267+
fn expand<'cx>(
268+
&self,
269+
ecx: &'cx mut ExtCtxt,
270+
span: Span,
271+
input: TokenStream,
272+
_def_span: Option<Span>,
273+
) -> Box<dyn MacResult+'cx> {
264274
struct AvoidInterpolatedIdents;
265275

266276
impl Folder for AvoidInterpolatedIdents {

src/libsyntax/ext/expand.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
764764
edition) {
765765
dummy_span
766766
} else {
767-
kind.make_from(expander.expand(self.cx, span, mac.node.stream()))
767+
kind.make_from(expander.expand(self.cx, span, mac.node.stream(), None))
768768
}
769769
}
770770

@@ -785,7 +785,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
785785
edition) {
786786
dummy_span
787787
} else {
788-
kind.make_from(expander.expand(self.cx, span, mac.node.stream()))
788+
kind.make_from(expander.expand(
789+
self.cx,
790+
span,
791+
mac.node.stream(),
792+
def_info.map(|(_, s)| s),
793+
))
789794
}
790795
}
791796

src/libsyntax/ext/tt/macro_rules.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,19 @@ struct MacroRulesMacroExpander {
7979
}
8080

8181
impl TTMacroExpander for MacroRulesMacroExpander {
82-
fn expand<'cx>(&self,
83-
cx: &'cx mut ExtCtxt,
84-
sp: Span,
85-
input: TokenStream)
86-
-> Box<dyn MacResult+'cx> {
82+
fn expand<'cx>(
83+
&self,
84+
cx: &'cx mut ExtCtxt,
85+
sp: Span,
86+
input: TokenStream,
87+
def_span: Option<Span>,
88+
) -> Box<dyn MacResult+'cx> {
8789
if !self.valid {
8890
return DummyResult::any(sp);
8991
}
9092
generic_extension(cx,
9193
sp,
94+
def_span,
9295
self.name,
9396
input,
9497
&self.lhses,
@@ -104,6 +107,7 @@ fn trace_macros_note(cx: &mut ExtCtxt, sp: Span, message: String) {
104107
/// Given `lhses` and `rhses`, this is the new macro we create
105108
fn generic_extension<'cx>(cx: &'cx mut ExtCtxt,
106109
sp: Span,
110+
def_span: Option<Span>,
107111
name: ast::Ident,
108112
arg: TokenStream,
109113
lhses: &[quoted::TokenTree],
@@ -183,7 +187,14 @@ fn generic_extension<'cx>(cx: &'cx mut ExtCtxt,
183187
}
184188

185189
let best_fail_msg = parse_failure_msg(best_fail_tok.expect("ran no matchers"));
186-
let mut err = cx.struct_span_err(best_fail_spot.substitute_dummy(sp), &best_fail_msg);
190+
let span = best_fail_spot.substitute_dummy(sp);
191+
let mut err = cx.struct_span_err(span, &best_fail_msg);
192+
err.span_label(span, best_fail_msg);
193+
if let Some(sp) = def_span {
194+
if cx.source_map().span_to_filename(sp).is_real() && !sp.is_dummy() {
195+
err.span_label(cx.source_map().def_span(sp), "when calling this macro");
196+
}
197+
}
187198

188199
// Check whether there's a missing comma in this macro call, like `println!("{}" a);`
189200
if let Some((arg, comma_span)) = arg.add_comma() {
@@ -194,7 +205,7 @@ fn generic_extension<'cx>(cx: &'cx mut ExtCtxt,
194205
};
195206
match TokenTree::parse(cx, lhs_tt, arg.clone()) {
196207
Success(_) => {
197-
if comma_span == DUMMY_SP {
208+
if comma_span.is_dummy() {
198209
err.note("you might be missing a comma");
199210
} else {
200211
err.span_suggestion_short_with_applicability(

src/test/run-pass-fulldeps/auxiliary/plugin_args.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ impl TTMacroExpander for Expander {
3838
fn expand<'cx>(&self,
3939
ecx: &'cx mut ExtCtxt,
4040
sp: Span,
41-
_: TokenStream) -> Box<MacResult+'cx> {
41+
_: TokenStream,
42+
_: Option<Span>) -> Box<MacResult+'cx> {
4243
let args = self.args.iter().map(|i| pprust::meta_list_item_to_string(i))
4344
.collect::<Vec<_>>().join(", ");
4445
MacEager::expr(ecx.expr_str(sp, Symbol::intern(&args)))

src/test/ui/editions/edition-keywords-2015-2015-parsing.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ error: no rules expected the token `r#async`
22
--> $DIR/edition-keywords-2015-2015-parsing.rs:22:31
33
|
44
LL | r#async = consumes_async!(r#async); //~ ERROR no rules expected the token `r#async`
5-
| ^^^^^^^
5+
| ^^^^^^^ no rules expected the token `r#async`
66

77
error: no rules expected the token `async`
88
--> $DIR/edition-keywords-2015-2015-parsing.rs:23:35
99
|
1010
LL | r#async = consumes_async_raw!(async); //~ ERROR no rules expected the token `async`
11-
| ^^^^^
11+
| ^^^^^ no rules expected the token `async`
1212

1313
error: aborting due to 2 previous errors
1414

src/test/ui/editions/edition-keywords-2015-2018-parsing.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ error: no rules expected the token `r#async`
22
--> $DIR/edition-keywords-2015-2018-parsing.rs:22:31
33
|
44
LL | r#async = consumes_async!(r#async); //~ ERROR no rules expected the token `r#async`
5-
| ^^^^^^^
5+
| ^^^^^^^ no rules expected the token `r#async`
66

77
error: no rules expected the token `async`
88
--> $DIR/edition-keywords-2015-2018-parsing.rs:23:35
99
|
1010
LL | r#async = consumes_async_raw!(async); //~ ERROR no rules expected the token `async`
11-
| ^^^^^
11+
| ^^^^^ no rules expected the token `async`
1212

1313
error: aborting due to 2 previous errors
1414

src/test/ui/editions/edition-keywords-2018-2015-parsing.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ error: no rules expected the token `r#async`
1414
--> $DIR/edition-keywords-2018-2015-parsing.rs:22:31
1515
|
1616
LL | r#async = consumes_async!(r#async); //~ ERROR no rules expected the token `r#async`
17-
| ^^^^^^^
17+
| ^^^^^^^ no rules expected the token `r#async`
1818

1919
error: no rules expected the token `async`
2020
--> $DIR/edition-keywords-2018-2015-parsing.rs:23:35
2121
|
2222
LL | r#async = consumes_async_raw!(async); //~ ERROR no rules expected the token `async`
23-
| ^^^^^
23+
| ^^^^^ no rules expected the token `async`
2424

2525
error: expected one of `move`, `|`, or `||`, found `<eof>`
2626
--> <::edition_kw_macro_2015::passes_ident macros>:1:22

src/test/ui/editions/edition-keywords-2018-2018-parsing.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ error: no rules expected the token `r#async`
1414
--> $DIR/edition-keywords-2018-2018-parsing.rs:22:31
1515
|
1616
LL | r#async = consumes_async!(r#async); //~ ERROR no rules expected the token `r#async`
17-
| ^^^^^^^
17+
| ^^^^^^^ no rules expected the token `r#async`
1818

1919
error: no rules expected the token `async`
2020
--> $DIR/edition-keywords-2018-2018-parsing.rs:23:35
2121
|
2222
LL | r#async = consumes_async_raw!(async); //~ ERROR no rules expected the token `async`
23-
| ^^^^^
23+
| ^^^^^ no rules expected the token `async`
2424

2525
error: expected one of `move`, `|`, or `||`, found `<eof>`
2626
--> <::edition_kw_macro_2018::passes_ident macros>:1:22
+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
error: unexpected end of macro invocation
22
--> $DIR/empty-comment.rs:20:5
33
|
4+
LL | macro_rules! one_arg_macro {
5+
| -------------------------- when calling this macro
6+
...
47
LL | one_arg_macro!(/**/); //~ ERROR unexpected end
5-
| ^^^^^^^^^^^^^^^^^^^^^
8+
| ^^^^^^^^^^^^^^^^^^^^^ unexpected end of macro invocation
69

710
error: aborting due to previous error
811

src/test/ui/fail-simple.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: no rules expected the token `@`
22
--> $DIR/fail-simple.rs:12:12
33
|
44
LL | panic!(@); //~ ERROR no rules expected the token `@`
5-
| ^
5+
| ^ no rules expected the token `@`
66

77
error: aborting due to previous error
88

src/test/ui/issues/issue-7970a.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
error: unexpected end of macro invocation
22
--> $DIR/issue-7970a.rs:16:5
33
|
4+
LL | macro_rules! one_arg_macro {
5+
| -------------------------- when calling this macro
6+
...
47
LL | one_arg_macro!();
5-
| ^^^^^^^^^^^^^^^^^
8+
| ^^^^^^^^^^^^^^^^^ unexpected end of macro invocation
69

710
error: aborting due to previous error
811

src/test/ui/macros/macro-at-most-once-rep-2018-feature-gate.stderr

+12-3
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,29 @@ LL | ($(a)?*) => {}
5151
error: no rules expected the token `?`
5252
--> $DIR/macro-at-most-once-rep-2018-feature-gate.rs:41:11
5353
|
54+
LL | macro_rules! foo {
55+
| ---------------- when calling this macro
56+
...
5457
LL | foo!(a?); //~ ERROR no rules expected the token `?`
55-
| ^
58+
| ^ no rules expected the token `?`
5659

5760
error: no rules expected the token `?`
5861
--> $DIR/macro-at-most-once-rep-2018-feature-gate.rs:42:11
5962
|
63+
LL | macro_rules! foo {
64+
| ---------------- when calling this macro
65+
...
6066
LL | foo!(a?a); //~ ERROR no rules expected the token `?`
61-
| ^
67+
| ^ no rules expected the token `?`
6268

6369
error: no rules expected the token `?`
6470
--> $DIR/macro-at-most-once-rep-2018-feature-gate.rs:43:11
6571
|
72+
LL | macro_rules! foo {
73+
| ---------------- when calling this macro
74+
...
6675
LL | foo!(a?a?a); //~ ERROR no rules expected the token `?`
67-
| ^
76+
| ^ no rules expected the token `?`
6877

6978
error: aborting due to 10 previous errors
7079

src/test/ui/macros/macro-at-most-once-rep-2018.stderr

+44-11
Original file line numberDiff line numberDiff line change
@@ -7,68 +7,101 @@ LL | ($(a),?) => {} //~ERROR the `?` macro repetition operator
77
error: no rules expected the token `?`
88
--> $DIR/macro-at-most-once-rep-2018.rs:36:11
99
|
10+
LL | macro_rules! foo {
11+
| ---------------- when calling this macro
12+
...
1013
LL | foo!(a?); //~ ERROR no rules expected the token `?`
11-
| ^
14+
| ^ no rules expected the token `?`
1215

1316
error: no rules expected the token `?`
1417
--> $DIR/macro-at-most-once-rep-2018.rs:37:11
1518
|
19+
LL | macro_rules! foo {
20+
| ---------------- when calling this macro
21+
...
1622
LL | foo!(a?a); //~ ERROR no rules expected the token `?`
17-
| ^
23+
| ^ no rules expected the token `?`
1824

1925
error: no rules expected the token `?`
2026
--> $DIR/macro-at-most-once-rep-2018.rs:38:11
2127
|
28+
LL | macro_rules! foo {
29+
| ---------------- when calling this macro
30+
...
2231
LL | foo!(a?a?a); //~ ERROR no rules expected the token `?`
23-
| ^
32+
| ^ no rules expected the token `?`
2433

2534
error: unexpected end of macro invocation
2635
--> $DIR/macro-at-most-once-rep-2018.rs:40:5
2736
|
37+
LL | macro_rules! barplus {
38+
| -------------------- when calling this macro
39+
...
2840
LL | barplus!(); //~ERROR unexpected end of macro invocation
29-
| ^^^^^^^^^^^
41+
| ^^^^^^^^^^^ unexpected end of macro invocation
3042

3143
error: unexpected end of macro invocation
3244
--> $DIR/macro-at-most-once-rep-2018.rs:41:14
3345
|
46+
LL | macro_rules! barplus {
47+
| -------------------- when calling this macro
48+
...
3449
LL | barplus!(a); //~ERROR unexpected end of macro invocation
35-
| ^
50+
| ^ unexpected end of macro invocation
3651

3752
error: no rules expected the token `?`
3853
--> $DIR/macro-at-most-once-rep-2018.rs:42:15
3954
|
55+
LL | macro_rules! barplus {
56+
| -------------------- when calling this macro
57+
...
4058
LL | barplus!(a?); //~ ERROR no rules expected the token `?`
41-
| ^
59+
| ^ no rules expected the token `?`
4260

4361
error: no rules expected the token `?`
4462
--> $DIR/macro-at-most-once-rep-2018.rs:43:15
4563
|
64+
LL | macro_rules! barplus {
65+
| -------------------- when calling this macro
66+
...
4667
LL | barplus!(a?a); //~ ERROR no rules expected the token `?`
47-
| ^
68+
| ^ no rules expected the token `?`
4869

4970
error: unexpected end of macro invocation
5071
--> $DIR/macro-at-most-once-rep-2018.rs:47:5
5172
|
73+
LL | macro_rules! barstar {
74+
| -------------------- when calling this macro
75+
...
5276
LL | barstar!(); //~ERROR unexpected end of macro invocation
53-
| ^^^^^^^^^^^
77+
| ^^^^^^^^^^^ unexpected end of macro invocation
5478

5579
error: unexpected end of macro invocation
5680
--> $DIR/macro-at-most-once-rep-2018.rs:48:14
5781
|
82+
LL | macro_rules! barstar {
83+
| -------------------- when calling this macro
84+
...
5885
LL | barstar!(a); //~ERROR unexpected end of macro invocation
59-
| ^
86+
| ^ unexpected end of macro invocation
6087

6188
error: no rules expected the token `?`
6289
--> $DIR/macro-at-most-once-rep-2018.rs:49:15
6390
|
91+
LL | macro_rules! barstar {
92+
| -------------------- when calling this macro
93+
...
6494
LL | barstar!(a?); //~ ERROR no rules expected the token `?`
65-
| ^
95+
| ^ no rules expected the token `?`
6696

6797
error: no rules expected the token `?`
6898
--> $DIR/macro-at-most-once-rep-2018.rs:50:15
6999
|
100+
LL | macro_rules! barstar {
101+
| -------------------- when calling this macro
102+
...
70103
LL | barstar!(a?a); //~ ERROR no rules expected the token `?`
71-
| ^
104+
| ^ no rules expected the token `?`
72105

73106
error: aborting due to 12 previous errors
74107

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
error: no rules expected the token `a`
22
--> $DIR/macro-non-lifetime.rs:18:8
33
|
4+
LL | macro_rules! m { ($x:lifetime) => { } }
5+
| -------------- when calling this macro
6+
...
47
LL | m!(a);
5-
| ^
8+
| ^ no rules expected the token `a`
69

710
error: aborting due to previous error
811

0 commit comments

Comments
 (0)