Skip to content

Commit 343889b

Browse files
committed
Auto merge of #96683 - nnethercote:speed-up-Token-ident-lifetime, r=petrochenkov
Speed up `Token::{ident,lifetime}` Some speed and cleanliness improvements. r? `@petrochenkov`
2 parents 364bf39 + 1d2e172 commit 343889b

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

compiler/rustc_ast/src/token.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -475,19 +475,29 @@ impl Token {
475475
}
476476

477477
/// Returns an identifier if this token is an identifier.
478+
#[inline]
478479
pub fn ident(&self) -> Option<(Ident, /* is_raw */ bool)> {
479-
let token = self.uninterpolate();
480-
match token.kind {
481-
Ident(name, is_raw) => Some((Ident::new(name, token.span), is_raw)),
480+
// We avoid using `Token::uninterpolate` here because it's slow.
481+
match &self.kind {
482+
&Ident(name, is_raw) => Some((Ident::new(name, self.span), is_raw)),
483+
Interpolated(nt) => match **nt {
484+
NtIdent(ident, is_raw) => Some((ident, is_raw)),
485+
_ => None,
486+
},
482487
_ => None,
483488
}
484489
}
485490

486491
/// Returns a lifetime identifier if this token is a lifetime.
492+
#[inline]
487493
pub fn lifetime(&self) -> Option<Ident> {
488-
let token = self.uninterpolate();
489-
match token.kind {
490-
Lifetime(name) => Some(Ident::new(name, token.span)),
494+
// We avoid using `Token::uninterpolate` here because it's slow.
495+
match &self.kind {
496+
&Lifetime(name) => Some(Ident::new(name, self.span)),
497+
Interpolated(nt) => match **nt {
498+
NtLifetime(ident) => Some(ident),
499+
_ => None,
500+
},
491501
_ => None,
492502
}
493503
}
@@ -521,7 +531,7 @@ impl Token {
521531
/// (which happens while parsing the result of macro expansion)?
522532
pub fn is_whole_expr(&self) -> bool {
523533
if let Interpolated(ref nt) = self.kind
524-
&& let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtIdent(..) | NtBlock(_) = **nt
534+
&& let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = **nt
525535
{
526536
return true;
527537
}

compiler/rustc_expand/src/mbe/transcribe.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@ pub(super) fn transcribe<'a>(
217217
}
218218

219219
// Replace the meta-var with the matched token tree from the invocation.
220-
mbe::TokenTree::MetaVar(mut sp, mut orignal_ident) => {
220+
mbe::TokenTree::MetaVar(mut sp, mut original_ident) => {
221221
// Find the matched nonterminal from the macro invocation, and use it to replace
222222
// the meta-var.
223-
let ident = MacroRulesNormalizedIdent::new(orignal_ident);
223+
let ident = MacroRulesNormalizedIdent::new(original_ident);
224224
if let Some(cur_matched) = lookup_cur_matched(ident, interp, &repeats) {
225225
match cur_matched {
226226
MatchedTokenTree(ref tt) => {
@@ -249,9 +249,9 @@ pub(super) fn transcribe<'a>(
249249
// If we aren't able to match the meta-var, we push it back into the result but
250250
// with modified syntax context. (I believe this supports nested macros).
251251
marker.visit_span(&mut sp);
252-
marker.visit_ident(&mut orignal_ident);
252+
marker.visit_ident(&mut original_ident);
253253
result.push(TokenTree::token(token::Dollar, sp).into());
254-
result.push(TokenTree::Token(Token::from_ast_ident(orignal_ident)).into());
254+
result.push(TokenTree::Token(Token::from_ast_ident(original_ident)).into());
255255
}
256256
}
257257

0 commit comments

Comments
 (0)