Skip to content

Commit b90cedd

Browse files
committed
Refactor ensure_complete_parse.
1 parent 4a8467b commit b90cedd

File tree

3 files changed

+31
-45
lines changed

3 files changed

+31
-45
lines changed

src/libsyntax/ext/expand.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use ast::{Block, Crate, Ident, Mac_, PatKind};
12-
use ast::{MacStmtStyle, StmtKind, ItemKind};
12+
use ast::{Name, MacStmtStyle, StmtKind, ItemKind};
1313
use ast;
1414
use ext::hygiene::Mark;
1515
use ext::placeholders::{placeholder, PlaceholderExpander};
@@ -299,10 +299,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
299299
};
300300

301301
attr::mark_used(&attr);
302+
let name = intern(&attr.name());
302303
self.cx.bt_push(ExpnInfo {
303304
call_site: attr.span,
304305
callee: NameAndSpan {
305-
format: MacroAttribute(intern(&attr.name())),
306+
format: MacroAttribute(name),
306307
span: Some(attr.span),
307308
allow_internal_unstable: false,
308309
}
@@ -325,7 +326,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
325326
let item_toks = TokenStream::from_tts(tts_for_item(&item, &self.cx.parse_sess));
326327

327328
let tok_result = mac.expand(self.cx, attr.span, attr_toks, item_toks);
328-
self.parse_expansion(tok_result, kind, attr.span)
329+
self.parse_expansion(tok_result, kind, name, attr.span)
329330
}
330331
_ => unreachable!(),
331332
}
@@ -424,7 +425,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
424425

425426
let toks = TokenStream::from_tts(marked_tts);
426427
let tok_result = expandfun.expand(self.cx, span, toks);
427-
Some(self.parse_expansion(tok_result, kind, span))
428+
Some(self.parse_expansion(tok_result, kind, extname, span))
428429
}
429430
};
430431

@@ -443,7 +444,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
443444
})
444445
}
445446

446-
fn parse_expansion(&mut self, toks: TokenStream, kind: ExpansionKind, span: Span) -> Expansion {
447+
fn parse_expansion(&mut self, toks: TokenStream, kind: ExpansionKind, name: Name, span: Span)
448+
-> Expansion {
447449
let mut parser = self.cx.new_parser_from_tts(&toks.to_tts());
448450
let expansion = match parser.parse_expansion(kind, false) {
449451
Ok(expansion) => expansion,
@@ -452,13 +454,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
452454
return kind.dummy(span);
453455
}
454456
};
455-
parser.ensure_complete_parse(kind == ExpansionKind::Expr, |parser| {
456-
let msg = format!("macro expansion ignores token `{}` and any following",
457-
parser.this_token_to_string());
458-
parser.diagnostic().struct_span_err(parser.span, &msg)
459-
.span_note(span, "caused by the macro expansion here")
460-
.emit();
461-
});
457+
parser.ensure_complete_parse(name, kind.name(), span);
462458
// FIXME better span info
463459
expansion.fold_with(&mut ChangeSpan { span: span })
464460
}

src/libsyntax/ext/tt/macro_rules.rs

+11-24
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,19 @@ pub struct ParserAnyMacro<'a> {
3939
}
4040

4141
impl<'a> ParserAnyMacro<'a> {
42-
/// Make sure we don't have any tokens left to parse, so we don't
43-
/// silently drop anything. `allow_semi` is so that "optional"
44-
/// semicolons at the end of normal expressions aren't complained
45-
/// about e.g. the semicolon in `macro_rules! kapow { () => {
46-
/// panic!(); } }` doesn't get picked up by .parse_expr(), but it's
47-
/// allowed to be there.
48-
fn ensure_complete_parse(&mut self, allow_semi: bool, context: &str) {
42+
pub fn make(mut self: Box<ParserAnyMacro<'a>>, kind: ExpansionKind) -> Expansion {
4943
let ParserAnyMacro { site_span, macro_ident, ref mut parser } = *self;
50-
parser.ensure_complete_parse(allow_semi, |parser| {
51-
let token_str = parser.this_token_to_string();
52-
let msg = format!("macro expansion ignores token `{}` and any \
53-
following",
54-
token_str);
55-
let span = parser.span;
56-
let mut err = parser.diagnostic().struct_span_err(span, &msg);
57-
let msg = format!("caused by the macro expansion here; the usage \
58-
of `{}!` is likely invalid in {} context",
59-
macro_ident, context);
60-
err.span_note(site_span, &msg)
61-
.emit();
62-
});
63-
}
44+
let expansion = panictry!(parser.parse_expansion(kind, true));
6445

65-
pub fn make(mut self: Box<ParserAnyMacro<'a>>, kind: ExpansionKind) -> Expansion {
66-
let expansion = panictry!(self.parser.parse_expansion(kind, true));
67-
self.ensure_complete_parse(kind == ExpansionKind::Expr, kind.name());
46+
// We allow semicolons at the end of expressions -- e.g. the semicolon in
47+
// `macro_rules! m { () => { panic!(); } }` isn't parsed by `.parse_expr()`,
48+
// but `m!()` is allowed in expression positions (c.f. issue #34706).
49+
if kind == ExpansionKind::Expr && parser.token == token::Semi {
50+
parser.bump();
51+
}
52+
53+
// Make sure we don't have any tokens left to parse so we don't silently drop anything.
54+
parser.ensure_complete_parse(macro_ident.name, kind.name(), site_span);
6855
expansion
6956
}
7057
}

src/libsyntax/parse/parser.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -6172,14 +6172,17 @@ impl<'a> Parser<'a> {
61726172
}
61736173
}
61746174

6175-
pub fn ensure_complete_parse<F>(&mut self, allow_semi: bool, on_err: F)
6176-
where F: FnOnce(&Parser)
6177-
{
6178-
if allow_semi && self.token == token::Semi {
6179-
self.bump();
6180-
}
6181-
if self.token != token::Eof {
6182-
on_err(self);
6183-
}
6175+
pub fn ensure_complete_parse(&mut self, macro_name: ast::Name, kind_name: &str, span: Span) {
6176+
if self.token == token::Eof {
6177+
return
6178+
}
6179+
6180+
let msg = format!("macro expansion ignores token `{}` and any following",
6181+
self.this_token_to_string());
6182+
let mut err = self.diagnostic().struct_span_err(self.span, &msg);
6183+
let msg = format!("caused by the macro expansion here; the usage \
6184+
of `{}!` is likely invalid in {} context",
6185+
macro_name, kind_name);
6186+
err.span_note(span, &msg).emit();
61846187
}
61856188
}

0 commit comments

Comments
 (0)