Skip to content

Commit 02dd239

Browse files
authored
Rollup merge of rust-lang#55292 - estebank:macro-eof, r=pnkfelix
Macro diagnostics tweaks Fix rust-lang#30128, fix rust-lang#10951 by adding an appropriate span to the diagnostic. Fix rust-lang#26288 by suggesting adding semicolon to macro call.
2 parents b95d4d2 + f8818cb commit 02dd239

11 files changed

+123
-35
lines changed

src/libsyntax/ext/expand.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -1036,10 +1036,28 @@ impl<'a> Parser<'a> {
10361036
// Avoid emitting backtrace info twice.
10371037
let def_site_span = self.span.with_ctxt(SyntaxContext::empty());
10381038
let mut err = self.diagnostic().struct_span_err(def_site_span, &msg);
1039-
let msg = format!("caused by the macro expansion here; the usage \
1040-
of `{}!` is likely invalid in {} context",
1041-
macro_path, kind_name);
1042-
err.span_note(span, &msg).emit();
1039+
err.span_label(span, "caused by the macro expansion here");
1040+
let msg = format!(
1041+
"the usage of `{}!` is likely invalid in {} context",
1042+
macro_path,
1043+
kind_name,
1044+
);
1045+
err.note(&msg);
1046+
let semi_span = self.sess.source_map().next_point(span);
1047+
1048+
let semi_full_span = semi_span.to(self.sess.source_map().next_point(semi_span));
1049+
match self.sess.source_map().span_to_snippet(semi_full_span) {
1050+
Ok(ref snippet) if &snippet[..] != ";" && kind_name == "expression" => {
1051+
err.span_suggestion_with_applicability(
1052+
semi_span,
1053+
"you might be missing a semicolon here",
1054+
";".to_owned(),
1055+
Applicability::MaybeIncorrect,
1056+
);
1057+
}
1058+
_ => {}
1059+
}
1060+
err.emit();
10431061
}
10441062
}
10451063
}

src/libsyntax/ext/tt/macro_rules.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ pub struct ParserAnyMacro<'a> {
5050
impl<'a> ParserAnyMacro<'a> {
5151
pub fn make(mut self: Box<ParserAnyMacro<'a>>, kind: AstFragmentKind) -> AstFragment {
5252
let ParserAnyMacro { site_span, macro_ident, ref mut parser } = *self;
53-
let fragment = panictry!(parser.parse_ast_fragment(kind, true));
53+
let fragment = panictry!(parser.parse_ast_fragment(kind, true).map_err(|mut e| {
54+
if e.span.is_dummy() { // Get around lack of span in error (#30128)
55+
e.set_span(site_span);
56+
}
57+
e
58+
}));
5459

5560
// We allow semicolons at the end of expressions -- e.g. the semicolon in
5661
// `macro_rules! m { () => { panic!(); } }` isn't parsed by `.parse_expr()`,

src/libsyntax_pos/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,17 @@ impl MultiSpan {
612612
&self.primary_spans
613613
}
614614

615+
/// Returns `true` if this contains only a dummy primary span with any hygienic context.
616+
pub fn is_dummy(&self) -> bool {
617+
let mut is_dummy = true;
618+
for span in &self.primary_spans {
619+
if !span.is_dummy() {
620+
is_dummy = false;
621+
}
622+
}
623+
is_dummy
624+
}
625+
615626
/// Replaces all occurrences of one Span with another. Used to move Spans in areas that don't
616627
/// display well (like std macros). Returns true if replacements occurred.
617628
pub fn replace(&mut self, before: Span, after: Span) -> bool {

src/test/ui/issues/issue-30007.stderr

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ error: macro expansion ignores token `;` and any following
33
|
44
LL | () => ( String ; ); //~ ERROR macro expansion ignores token `;`
55
| ^
6-
|
7-
note: caused by the macro expansion here; the usage of `t!` is likely invalid in type context
8-
--> $DIR/issue-30007.rs:16:16
9-
|
6+
...
107
LL | let i: Vec<t!()>;
11-
| ^^^^
8+
| ---- caused by the macro expansion here
9+
|
10+
= note: the usage of `t!` is likely invalid in type context
1211

1312
error: aborting due to previous error
1413

src/test/ui/macros/macro-context.stderr

+12-15
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,33 @@ error: macro expansion ignores token `;` and any following
33
|
44
LL | () => ( i ; typeof ); //~ ERROR expected expression, found reserved keyword `typeof`
55
| ^
6-
|
7-
note: caused by the macro expansion here; the usage of `m!` is likely invalid in type context
8-
--> $DIR/macro-context.rs:20:12
9-
|
6+
...
107
LL | let a: m!();
11-
| ^^^^
8+
| ---- caused by the macro expansion here
9+
|
10+
= note: the usage of `m!` is likely invalid in type context
1211

1312
error: macro expansion ignores token `typeof` and any following
1413
--> $DIR/macro-context.rs:13:17
1514
|
1615
LL | () => ( i ; typeof ); //~ ERROR expected expression, found reserved keyword `typeof`
1716
| ^^^^^^
18-
|
19-
note: caused by the macro expansion here; the usage of `m!` is likely invalid in expression context
20-
--> $DIR/macro-context.rs:21:13
21-
|
17+
...
2218
LL | let i = m!();
23-
| ^^^^
19+
| ---- caused by the macro expansion here
20+
|
21+
= note: the usage of `m!` is likely invalid in expression context
2422

2523
error: macro expansion ignores token `;` and any following
2624
--> $DIR/macro-context.rs:13:15
2725
|
2826
LL | () => ( i ; typeof ); //~ ERROR expected expression, found reserved keyword `typeof`
2927
| ^
30-
|
31-
note: caused by the macro expansion here; the usage of `m!` is likely invalid in pattern context
32-
--> $DIR/macro-context.rs:23:9
33-
|
28+
...
3429
LL | m!() => {}
35-
| ^^^^
30+
| ---- caused by the macro expansion here
31+
|
32+
= note: the usage of `m!` is likely invalid in pattern context
3633

3734
error: expected expression, found reserved keyword `typeof`
3835
--> $DIR/macro-context.rs:13:17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
macro_rules! empty { () => () }
2+
3+
fn main() {
4+
match 42 {
5+
_ => { empty!() }
6+
};
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: expected expression, found `<eof>`
2+
--> $DIR/macro-in-expression-context-2.rs:5:16
3+
|
4+
LL | _ => { empty!() }
5+
| ^^^^^^^^
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// run-rustfix
2+
3+
macro_rules! foo {
4+
() => {
5+
assert_eq!("A", "A");
6+
assert_eq!("B", "B");
7+
}
8+
//~^^ ERROR macro expansion ignores token `assert_eq` and any following
9+
//~| NOTE the usage of `foo!` is likely invalid in expression context
10+
}
11+
12+
fn main() {
13+
foo!();
14+
//~^ NOTE caused by the macro expansion here
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// run-rustfix
2+
3+
macro_rules! foo {
4+
() => {
5+
assert_eq!("A", "A");
6+
assert_eq!("B", "B");
7+
}
8+
//~^^ ERROR macro expansion ignores token `assert_eq` and any following
9+
//~| NOTE the usage of `foo!` is likely invalid in expression context
10+
}
11+
12+
fn main() {
13+
foo!()
14+
//~^ NOTE caused by the macro expansion here
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: macro expansion ignores token `assert_eq` and any following
2+
--> $DIR/macro-in-expression-context.rs:6:9
3+
|
4+
LL | assert_eq!("B", "B");
5+
| ^^^^^^^^^
6+
...
7+
LL | foo!()
8+
| ------- help: you might be missing a semicolon here: `;`
9+
| |
10+
| caused by the macro expansion here
11+
|
12+
= note: the usage of `foo!` is likely invalid in expression context
13+
14+
error: aborting due to previous error
15+

src/test/ui/parser/macro/macro-incomplete-parse.stderr

+8-10
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ error: macro expansion ignores token `,` and any following
33
|
44
LL | , //~ ERROR macro expansion ignores token `,`
55
| ^
6-
|
7-
note: caused by the macro expansion here; the usage of `ignored_item!` is likely invalid in item context
8-
--> $DIR/macro-incomplete-parse.rs:31:1
9-
|
6+
...
107
LL | ignored_item!();
11-
| ^^^^^^^^^^^^^^^^
8+
| ---------------- caused by the macro expansion here
9+
|
10+
= note: the usage of `ignored_item!` is likely invalid in item context
1211

1312
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `,`
1413
--> $DIR/macro-incomplete-parse.rs:22:14
@@ -24,12 +23,11 @@ error: macro expansion ignores token `,` and any following
2423
|
2524
LL | () => ( 1, 2 ) //~ ERROR macro expansion ignores token `,`
2625
| ^
27-
|
28-
note: caused by the macro expansion here; the usage of `ignored_pat!` is likely invalid in pattern context
29-
--> $DIR/macro-incomplete-parse.rs:36:9
30-
|
26+
...
3127
LL | ignored_pat!() => (),
32-
| ^^^^^^^^^^^^^^
28+
| -------------- caused by the macro expansion here
29+
|
30+
= note: the usage of `ignored_pat!` is likely invalid in pattern context
3331

3432
error: aborting due to 3 previous errors
3533

0 commit comments

Comments
 (0)