Skip to content

Commit 76449d8

Browse files
committed
Point at macro arm when it doesn't expand to an expression
1 parent e5cd1ed commit 76449d8

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,18 @@ pub struct ParserAnyMacro<'a> {
4545
/// Span of the expansion site of the macro this parser is for
4646
site_span: Span,
4747
/// The ident of the macro we're parsing
48-
macro_ident: ast::Ident
48+
macro_ident: ast::Ident,
49+
arm_span: Span,
4950
}
5051

5152
impl<'a> ParserAnyMacro<'a> {
5253
pub fn make(mut self: Box<ParserAnyMacro<'a>>, kind: AstFragmentKind) -> AstFragment {
53-
let ParserAnyMacro { site_span, macro_ident, ref mut parser } = *self;
54+
let ParserAnyMacro { site_span, macro_ident, ref mut parser, arm_span } = *self;
5455
let fragment = panictry!(parser.parse_ast_fragment(kind, true).map_err(|mut e| {
5556
if e.span.is_dummy() { // Get around lack of span in error (#30128)
5657
e.set_span(site_span);
5758
e.span_label(site_span, "in this macro expansion");
59+
e.span_label(arm_span, "in this macro arm");
5860
} else if parser.token == token::Eof { // (#52866)
5961
e.set_span(parser.sess.source_map().next_point(parser.span));
6062
}
@@ -146,6 +148,7 @@ fn generic_extension<'cx>(cx: &'cx mut ExtCtxt,
146148
quoted::TokenTree::Delimited(_, ref delimed) => delimed.tts.clone(),
147149
_ => cx.span_bug(sp, "malformed macro rhs"),
148150
};
151+
let arm_span = rhses[i].span();
149152

150153
let rhs_spans = rhs.iter().map(|t| t.span()).collect::<Vec<_>>();
151154
// rhs has holes ( `$id` and `$(...)` that need filled)
@@ -184,7 +187,8 @@ fn generic_extension<'cx>(cx: &'cx mut ExtCtxt,
184187
// so we can print a useful error message if the parse of the expanded
185188
// macro leaves unparsed tokens.
186189
site_span: sp,
187-
macro_ident: name
190+
macro_ident: name,
191+
arm_span,
188192
})
189193
}
190194
Failure(sp, tok, t) => if sp.lo() >= best_fail_spot.lo() {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error: expected expression, found the end of the macro arm
22
--> $DIR/macro-in-expression-context-2.rs:5:16
33
|
4+
LL | macro_rules! empty { () => () }
5+
| -- in this macro arm
6+
...
47
LL | _ => { empty!() }
58
| ^^^^^^^^ in this macro expansion
69

0 commit comments

Comments
 (0)