Skip to content

Commit ad144ac

Browse files
committed
Modify invalid macro in expression context diagnostic
1 parent 8544db0 commit ad144ac

7 files changed

+91
-34
lines changed

Diff for: src/libsyntax/ext/expand.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -1036,10 +1036,26 @@ 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+
match self.sess.source_map().span_to_snippet(semi_span) {
1048+
Ok(ref snippet) if &snippet[..] != ";" && kind_name == "expression" => {
1049+
err.span_suggestion_with_applicability(
1050+
semi_span,
1051+
"you might be missing a semicolon here",
1052+
";".to_owned(),
1053+
Applicability::MaybeIncorrect,
1054+
);
1055+
}
1056+
_ => {}
1057+
}
1058+
err.emit();
10431059
}
10441060
}
10451061
}

Diff for: 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

Diff for: src/test/ui/macros/macro-context.stderr

+14-15
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,35 @@ 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+
| ----- help: you might be missing a semicolon here: `;`
20+
| |
21+
| caused by the macro expansion here
22+
|
23+
= note: the usage of `m!` is likely invalid in expression context
2424

2525
error: macro expansion ignores token `;` and any following
2626
--> $DIR/macro-context.rs:13:15
2727
|
2828
LL | () => ( i ; typeof ); //~ ERROR expected expression, found reserved keyword `typeof`
2929
| ^
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-
|
30+
...
3431
LL | m!() => {}
35-
| ^^^^
32+
| ---- caused by the macro expansion here
33+
|
34+
= note: the usage of `m!` is likely invalid in pattern context
3635

3736
error: expected expression, found reserved keyword `typeof`
3837
--> $DIR/macro-context.rs:13:17

Diff for: src/test/ui/macros/macro-in-expression-context.fixed

+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+
}

Diff for: src/test/ui/macros/macro-in-expression-context.rs

+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+
}
+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+

Diff for: 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)