Skip to content

Commit c45871b

Browse files
committed
Keep label on moved spans and point at macro invocation on parse error
1 parent 76449d8 commit c45871b

File tree

7 files changed

+40
-15
lines changed

7 files changed

+40
-15
lines changed

src/librustc_errors/diagnostic.rs

+11
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ impl Diagnostic {
139139
self
140140
}
141141

142+
pub fn replace_span_with(&mut self, after: Span) -> &mut Self {
143+
let before = self.span.clone();
144+
self.set_span(after);
145+
for span_label in before.span_labels() {
146+
if let Some(label) = span_label.label {
147+
self.span_label(after, label);
148+
}
149+
}
150+
self
151+
}
152+
142153
pub fn note_expected_found(&mut self,
143154
label: &dyn fmt::Display,
144155
expected: DiagnosticStyledString,

src/libsyntax/ext/tt/macro_rules.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,24 @@ impl<'a> ParserAnyMacro<'a> {
5353
pub fn make(mut self: Box<ParserAnyMacro<'a>>, kind: AstFragmentKind) -> AstFragment {
5454
let ParserAnyMacro { site_span, macro_ident, ref mut parser, arm_span } = *self;
5555
let fragment = panictry!(parser.parse_ast_fragment(kind, true).map_err(|mut e| {
56-
if e.span.is_dummy() { // Get around lack of span in error (#30128)
57-
e.set_span(site_span);
58-
e.span_label(site_span, "in this macro expansion");
59-
e.span_label(arm_span, "in this macro arm");
60-
} else if parser.token == token::Eof { // (#52866)
61-
e.set_span(parser.sess.source_map().next_point(parser.span));
62-
}
63-
if parser.token == token::Eof {
56+
if parser.token == token::Eof && e.message().ends_with(", found `<eof>`") {
57+
if !e.span.is_dummy() { // early end of macro arm (#52866)
58+
e.replace_span_with(parser.sess.source_map().next_point(parser.span));
59+
}
6460
let msg = &e.message[0];
6561
e.message[0] = (
6662
msg.0.replace(", found `<eof>`", ", found the end of the macro arm"),
6763
msg.1,
6864
);
6965
}
66+
if e.span.is_dummy() { // Get around lack of span in error (#30128)
67+
e.replace_span_with(site_span);
68+
if parser.sess.source_map().span_to_filename(arm_span).is_real() {
69+
e.span_label(arm_span, "in this macro arm");
70+
}
71+
} else if !parser.sess.source_map().span_to_filename(parser.span).is_real() {
72+
e.span_label(site_span, "in this macro invocation");
73+
}
7074
e
7175
}));
7276

src/test/ui/directory_ownership/macro-expanded-mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
macro_rules! mod_decl {
1414
($i:ident) => { mod $i; }
15-
//~^ ERROR Cannot declare a non-inline module inside a block
1615
}
1716

1817
mod macro_expanded_mod_helper {
@@ -21,4 +20,5 @@ mod macro_expanded_mod_helper {
2120

2221
fn main() {
2322
mod_decl!(foo);
23+
//~^ ERROR Cannot declare a non-inline module inside a block
2424
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: Cannot declare a non-inline module inside a block unless it has a path attribute
2-
--> $DIR/macro-expanded-mod.rs:14:28
2+
--> $DIR/macro-expanded-mod.rs:22:15
33
|
4-
LL | ($i:ident) => { mod $i; }
5-
| ^
4+
LL | mod_decl!(foo);
5+
| ^^^
66

77
error: aborting due to previous error
88

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ error: expected one of `move`, `|`, or `||`, found the end of the macro arm
2626
--> <::edition_kw_macro_2015::passes_ident macros>:1:25
2727
|
2828
LL | ( $ i : ident ) => ( $ i )
29-
| ^
29+
| ^ expected one of `move`, `|`, or `||` here
30+
|
31+
::: $DIR/edition-keywords-2018-2015-parsing.rs:26:8
32+
|
33+
LL | if passes_ident!(async) == 1 {}
34+
| -------------------- in this macro invocation
3035

3136
error: aborting due to 5 previous errors
3237

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ error: expected one of `move`, `|`, or `||`, found the end of the macro arm
2626
--> <::edition_kw_macro_2018::passes_ident macros>:1:25
2727
|
2828
LL | ( $ i : ident ) => ( $ i )
29-
| ^
29+
| ^ expected one of `move`, `|`, or `||` here
30+
|
31+
::: $DIR/edition-keywords-2018-2018-parsing.rs:26:8
32+
|
33+
LL | if passes_ident!(async) == 1 {}
34+
| -------------------- in this macro invocation
3035

3136
error: aborting due to 5 previous errors
3237

src/test/ui/macros/macro-in-expression-context-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | macro_rules! empty { () => () }
55
| -- in this macro arm
66
...
77
LL | _ => { empty!() }
8-
| ^^^^^^^^ in this macro expansion
8+
| ^^^^^^^^ expected expression
99

1010
error: aborting due to previous error
1111

0 commit comments

Comments
 (0)