Skip to content

Commit 8544db0

Browse files
committed
Add macro call span when lacking any other span in diagnostic
1 parent a66dc8a commit 8544db0

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 6 additions & 1 deletion
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

Lines changed: 11 additions & 0 deletions
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 {
Lines changed: 7 additions & 0 deletions
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+
}
Lines changed: 8 additions & 0 deletions
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+

0 commit comments

Comments
 (0)