Skip to content

Commit b817cf8

Browse files
committed
Replace the String in ParseResult::Failure with Token.
This lets us delay creation of failure messages until they are needed, which avoids ~1.6M allocations in html5ever.
1 parent e382267 commit b817cf8

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

src/libsyntax/ext/tt/macro_parser.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,22 @@ pub fn nameize(p_s: &ParseSess, ms: &[TokenTree], res: &[Rc<NamedMatch>])
251251

252252
pub enum ParseResult<T> {
253253
Success(T),
254-
/// Arm failed to match
255-
Failure(syntax_pos::Span, String),
254+
/// Arm failed to match. If the second parameter is `token::Eof`, it
255+
/// indicates an unexpected end of macro invocation. Otherwise, it
256+
/// indicates that no rules expected the given token.
257+
Failure(syntax_pos::Span, Token),
256258
/// Fatal error (malformed macro?). Abort compilation.
257259
Error(syntax_pos::Span, String)
258260
}
259261

262+
pub fn parse_failure_msg(tok: Token) -> String {
263+
match tok {
264+
token::Eof => "unexpected end of macro invocation".to_string(),
265+
_ => format!("no rules expected the token `{}`", pprust::token_to_string(&tok)),
266+
}
267+
}
268+
260269
pub type NamedParseResult = ParseResult<HashMap<Ident, Rc<NamedMatch>>>;
261-
pub type PositionalParseResult = ParseResult<Vec<Rc<NamedMatch>>>;
262270

263271
/// Perform a token equality check, ignoring syntax context (that is, an
264272
/// unhygienic comparison)
@@ -446,7 +454,7 @@ pub fn parse(sess: &ParseSess,
446454
} else if eof_eis.len() > 1 {
447455
return Error(sp, "ambiguity: multiple successful parses".to_string());
448456
} else {
449-
return Failure(sp, "unexpected end of macro invocation".to_string());
457+
return Failure(sp, token::Eof);
450458
}
451459
} else {
452460
if (!bb_eis.is_empty() && !next_eis.is_empty())
@@ -467,8 +475,7 @@ pub fn parse(sess: &ParseSess,
467475
}
468476
))
469477
} else if bb_eis.is_empty() && next_eis.is_empty() {
470-
return Failure(sp, format!("no rules expected the token `{}`",
471-
pprust::token_to_string(&tok)));
478+
return Failure(sp, tok);
472479
} else if !next_eis.is_empty() {
473480
/* Now process the next token */
474481
while !next_eis.is_empty() {

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use ext::expand::{Expansion, ExpansionKind};
1616
use ext::placeholders;
1717
use ext::tt::macro_parser::{Success, Error, Failure};
1818
use ext::tt::macro_parser::{MatchedSeq, MatchedNonterminal};
19-
use ext::tt::macro_parser::parse;
19+
use ext::tt::macro_parser::{parse, parse_failure_msg};
2020
use parse::ParseSess;
2121
use parse::lexer::new_tt_reader;
2222
use parse::parser::{Parser, Restrictions};
@@ -100,7 +100,7 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
100100

101101
// Which arm's failure should we report? (the one furthest along)
102102
let mut best_fail_spot = DUMMY_SP;
103-
let mut best_fail_msg = "internal error: ran no matchers".to_string();
103+
let mut best_fail_tok = None;
104104

105105
for (i, lhs) in lhses.iter().enumerate() { // try each arm's matchers
106106
let lhs_tt = match *lhs {
@@ -139,17 +139,18 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
139139
macro_ident: name
140140
})
141141
}
142-
Failure(sp, msg) => if sp.lo >= best_fail_spot.lo {
142+
Failure(sp, tok) => if sp.lo >= best_fail_spot.lo {
143143
best_fail_spot = sp;
144-
best_fail_msg = msg;
144+
best_fail_tok = Some(tok);
145145
},
146146
Error(err_sp, ref msg) => {
147147
cx.span_fatal(err_sp.substitute_dummy(sp), &msg[..])
148148
}
149149
}
150150
}
151151

152-
cx.span_fatal(best_fail_spot.substitute_dummy(sp), &best_fail_msg[..]);
152+
let best_fail_msg = parse_failure_msg(best_fail_tok.expect("ran no matchers"));
153+
cx.span_fatal(best_fail_spot.substitute_dummy(sp), &best_fail_msg);
153154
}
154155

155156
pub struct MacroRulesExpander;
@@ -227,8 +228,12 @@ pub fn compile(sess: &ParseSess, def: &ast::MacroDef) -> SyntaxExtension {
227228

228229
let argument_map = match parse(sess, &Vec::new(), arg_reader, &argument_gram) {
229230
Success(m) => m,
230-
Failure(sp, str) | Error(sp, str) => {
231-
panic!(sess.span_diagnostic.span_fatal(sp.substitute_dummy(def.span), &str));
231+
Failure(sp, tok) => {
232+
let s = parse_failure_msg(tok);
233+
panic!(sess.span_diagnostic.span_fatal(sp.substitute_dummy(def.span), &s));
234+
}
235+
Error(sp, s) => {
236+
panic!(sess.span_diagnostic.span_fatal(sp.substitute_dummy(def.span), &s));
232237
}
233238
};
234239

src/test/run-pass-fulldeps/auxiliary/procedural_mbe_matching.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager};
2525
use syntax::ext::build::AstBuilder;
2626
use syntax::ext::tt::macro_parser::{MatchedSeq, MatchedNonterminal};
2727
use syntax::ext::tt::macro_parser::{Success, Failure, Error};
28+
use syntax::ext::tt::macro_parser::parse_failure_msg;
2829
use syntax::ptr::P;
2930
use syntax_pos::Span;
3031
use rustc_plugin::Registry;
@@ -58,8 +59,11 @@ fn expand_mbe_matches(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree])
5859
_ => unreachable!()
5960
}
6061
}
61-
Failure(_, s) | Error(_, s) => {
62-
panic!("expected Success, but got Error/Failure: {}", s);
62+
Failure(_, tok) => {
63+
panic!("expected Success, but got Failure: {}", parse_failure_msg(tok));
64+
}
65+
Error(_, s) => {
66+
panic!("expected Success, but got Error: {}", s);
6367
}
6468
};
6569

0 commit comments

Comments
 (0)