Navigation Menu

Skip to content

Commit

Permalink
Add macro call span when lacking any other span in diagnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Oct 23, 2018
1 parent a66dc8a commit 8544db0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/libsyntax/ext/tt/macro_rules.rs
Expand Up @@ -50,7 +50,12 @@ pub struct ParserAnyMacro<'a> {
impl<'a> ParserAnyMacro<'a> {
pub fn make(mut self: Box<ParserAnyMacro<'a>>, kind: AstFragmentKind) -> AstFragment {
let ParserAnyMacro { site_span, macro_ident, ref mut parser } = *self;
let fragment = panictry!(parser.parse_ast_fragment(kind, true));
let fragment = panictry!(parser.parse_ast_fragment(kind, true).map_err(|mut e| {
if e.span.is_dummy() { // Get around lack of span in error (#30128)
e.set_span(site_span);
}
e
}));

// We allow semicolons at the end of expressions -- e.g. the semicolon in
// `macro_rules! m { () => { panic!(); } }` isn't parsed by `.parse_expr()`,
Expand Down
11 changes: 11 additions & 0 deletions src/libsyntax_pos/lib.rs
Expand Up @@ -612,6 +612,17 @@ impl MultiSpan {
&self.primary_spans
}

/// Returns `true` if this contains only a dummy primary span with any hygienic context.
pub fn is_dummy(&self) -> bool {
let mut is_dummy = true;
for span in &self.primary_spans {
if !span.is_dummy() {
is_dummy = false;
}
}
is_dummy
}

/// Replaces all occurrences of one Span with another. Used to move Spans in areas that don't
/// display well (like std macros). Returns true if replacements occurred.
pub fn replace(&mut self, before: Span, after: Span) -> bool {
Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/macros/macro-in-expression-context-2.rs
@@ -0,0 +1,7 @@
macro_rules! empty { () => () }

fn main() {
match 42 {
_ => { empty!() }
};
}
8 changes: 8 additions & 0 deletions src/test/ui/macros/macro-in-expression-context-2.stderr
@@ -0,0 +1,8 @@
error: expected expression, found `<eof>`
--> $DIR/macro-in-expression-context-2.rs:5:16
|
LL | _ => { empty!() }
| ^^^^^^^^

error: aborting due to previous error

0 comments on commit 8544db0

Please sign in to comment.