Skip to content

Commit 4c5d773

Browse files
committed
syntax: Remove duplicate span from token::Lifetime
1 parent 5e69353 commit 4c5d773

File tree

7 files changed

+39
-36
lines changed

7 files changed

+39
-36
lines changed

src/librustc/ich/impls_syntax.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for token::TokenKind {
357357
ident.name.hash_stable(hcx, hasher);
358358
is_raw.hash_stable(hcx, hasher);
359359
}
360-
token::Lifetime(ident) => ident.name.hash_stable(hcx, hasher),
360+
token::Lifetime(name) => name.hash_stable(hcx, hasher),
361361

362362
token::Interpolated(_) => {
363363
bug!("interpolated tokens should not be present in the HIR")

src/libsyntax/ext/tt/macro_parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,8 @@ pub fn parse_failure_msg(tok: TokenKind) -> String {
431431
fn token_name_eq(t1: &TokenKind, t2: &TokenKind) -> bool {
432432
if let (Some((id1, is_raw1)), Some((id2, is_raw2))) = (t1.ident(), t2.ident()) {
433433
id1.name == id2.name && is_raw1 == is_raw2
434-
} else if let (Some(id1), Some(id2)) = (t1.lifetime(), t2.lifetime()) {
435-
id1.name == id2.name
434+
} else if let (Some(name1), Some(name2)) = (t1.lifetime_name(), t2.lifetime_name()) {
435+
name1 == name2
436436
} else {
437437
*t1 == *t2
438438
}

src/libsyntax/mut_visit.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,6 @@ pub fn noop_visit_tts<T: MutVisitor>(TokenStream(tts): &mut TokenStream, vis: &m
599599
pub fn noop_visit_token<T: MutVisitor>(t: &mut TokenKind, vis: &mut T) {
600600
match t {
601601
token::Ident(id, _is_raw) => vis.visit_ident(id),
602-
token::Lifetime(id) => vis.visit_ident(id),
603602
token::Interpolated(nt) => {
604603
let mut nt = Lrc::make_mut(nt);
605604
vis.visit_interpolated(&mut nt);

src/libsyntax/parse/lexer/mod.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,13 +1041,6 @@ impl<'a> StringReader<'a> {
10411041
return Ok(TokenKind::lit(token::Char, symbol, None));
10421042
}
10431043

1044-
// Include the leading `'` in the real identifier, for macro
1045-
// expansion purposes. See #12512 for the gory details of why
1046-
// this is necessary.
1047-
let ident = self.with_str_from(start_with_quote, |lifetime_name| {
1048-
self.mk_ident(lifetime_name)
1049-
});
1050-
10511044
if starts_with_number {
10521045
// this is a recovered lifetime written `'1`, error but accept it
10531046
self.err_span_(
@@ -1057,7 +1050,10 @@ impl<'a> StringReader<'a> {
10571050
);
10581051
}
10591052

1060-
return Ok(token::Lifetime(ident));
1053+
// Include the leading `'` in the real identifier, for macro
1054+
// expansion purposes. See #12512 for the gory details of why
1055+
// this is necessary.
1056+
return Ok(token::Lifetime(self.name_from(start_with_quote)));
10611057
}
10621058
let msg = "unterminated character literal";
10631059
let symbol = self.scan_single_quoted_string(start_with_quote, msg);
@@ -1690,7 +1686,7 @@ mod tests {
16901686
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
16911687
let sh = mk_sess(sm.clone());
16921688
assert_eq!(setup(&sm, &sh, "'abc".to_string()).next_token(),
1693-
token::Lifetime(Ident::from_str("'abc")));
1689+
token::Lifetime(Symbol::intern("'abc")));
16941690
})
16951691
}
16961692

src/libsyntax/parse/parser.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,7 +2652,7 @@ impl<'a> Parser<'a> {
26522652
// and lifetime tokens, so the former are never encountered during normal parsing.
26532653
match **nt {
26542654
token::NtIdent(ident, is_raw) => Token::new(token::Ident(ident, is_raw), ident.span),
2655-
token::NtLifetime(ident) => Token::new(token::Lifetime(ident), ident.span),
2655+
token::NtLifetime(ident) => Token::new(token::Lifetime(ident.name), ident.span),
26562656
_ => return,
26572657
}
26582658
}
@@ -3922,9 +3922,8 @@ impl<'a> Parser<'a> {
39223922
// Parse &pat / &mut pat
39233923
self.expect_and()?;
39243924
let mutbl = self.parse_mutability();
3925-
if let token::Lifetime(ident) = self.token.kind {
3926-
let mut err = self.fatal(&format!("unexpected lifetime `{}` in pattern",
3927-
ident));
3925+
if let token::Lifetime(name) = self.token.kind {
3926+
let mut err = self.fatal(&format!("unexpected lifetime `{}` in pattern", name));
39283927
err.span_label(self.span, "unexpected lifetime");
39293928
return Err(err);
39303929
}

src/libsyntax/parse/token.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::symbol::kw;
1212
use crate::syntax::parse::parse_stream_from_source_str;
1313
use crate::tokenstream::{self, DelimSpan, TokenStream, TokenTree};
1414

15-
use syntax_pos::symbol::{self, Symbol};
15+
use syntax_pos::symbol::Symbol;
1616
use syntax_pos::{self, Span, FileName, DUMMY_SP};
1717
use log::info;
1818

@@ -211,7 +211,7 @@ pub enum TokenKind {
211211

212212
/* Name components */
213213
Ident(ast::Ident, /* is_raw */ bool),
214-
Lifetime(ast::Ident),
214+
Lifetime(ast::Name),
215215

216216
Interpolated(Lrc<Nonterminal>),
217217

@@ -364,7 +364,23 @@ impl TokenKind {
364364
_ => false,
365365
}
366366
}
367+
}
368+
369+
impl Token {
370+
/// Returns a lifetime identifier if this token is a lifetime.
371+
pub fn lifetime(&self) -> Option<ast::Ident> {
372+
match self.kind {
373+
Lifetime(name) => Some(ast::Ident::new(name, self.span)),
374+
Interpolated(ref nt) => match **nt {
375+
NtLifetime(ident) => Some(ident),
376+
_ => None,
377+
},
378+
_ => None,
379+
}
380+
}
381+
}
367382

383+
impl TokenKind {
368384
/// Returns an identifier if this token is an identifier.
369385
pub fn ident(&self) -> Option<(ast::Ident, /* is_raw */ bool)> {
370386
match *self {
@@ -376,12 +392,12 @@ impl TokenKind {
376392
_ => None,
377393
}
378394
}
379-
/// Returns a lifetime identifier if this token is a lifetime.
380-
pub fn lifetime(&self) -> Option<ast::Ident> {
395+
/// Returns a lifetime name if this token is a lifetime.
396+
pub fn lifetime_name(&self) -> Option<ast::Name> {
381397
match *self {
382-
Lifetime(ident) => Some(ident),
398+
Lifetime(name) => Some(name),
383399
Interpolated(ref nt) => match **nt {
384-
NtLifetime(ident) => Some(ident),
400+
NtLifetime(ident) => Some(ident.name),
385401
_ => None,
386402
},
387403
_ => None,
@@ -393,7 +409,7 @@ impl TokenKind {
393409
}
394410
/// Returns `true` if the token is a lifetime.
395411
crate fn is_lifetime(&self) -> bool {
396-
self.lifetime().is_some()
412+
self.lifetime_name().is_some()
397413
}
398414

399415
/// Returns `true` if the token is a identifier whose name is the given
@@ -521,13 +537,7 @@ impl TokenKind {
521537
_ => return None,
522538
},
523539
SingleQuote => match joint {
524-
Ident(ident, false) => {
525-
let name = Symbol::intern(&format!("'{}", ident));
526-
Lifetime(symbol::Ident {
527-
name,
528-
span: ident.span,
529-
})
530-
}
540+
Ident(ident, false) => Lifetime(Symbol::intern(&format!("'{}", ident))),
531541
_ => return None,
532542
},
533543

@@ -597,7 +607,7 @@ impl TokenKind {
597607

598608
(&Literal(a), &Literal(b)) => a == b,
599609

600-
(&Lifetime(a), &Lifetime(b)) => a.name == b.name,
610+
(&Lifetime(a), &Lifetime(b)) => a == b,
601611
(&Ident(a, b), &Ident(c, d)) => b == d && (a.name == c.name ||
602612
a.name == kw::DollarCrate ||
603613
c.name == kw::DollarCrate),
@@ -732,8 +742,7 @@ impl Nonterminal {
732742
Some(TokenTree::token(ident.span, token).into())
733743
}
734744
Nonterminal::NtLifetime(ident) => {
735-
let token = Lifetime(ident);
736-
Some(TokenTree::token(ident.span, token).into())
745+
Some(TokenTree::token(ident.span, Lifetime(ident.name)).into())
737746
}
738747
Nonterminal::NtTT(ref tt) => {
739748
Some(tt.clone().into())

src/libsyntax_ext/proc_macro_server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ impl FromInternal<(TreeAndJoint, &'_ ParseSess, &'_ mut Vec<Self>)>
145145
Ident(ident, false) if ident.name == kw::DollarCrate =>
146146
tt!(Ident::dollar_crate()),
147147
Ident(ident, is_raw) => tt!(Ident::new(ident.name, is_raw)),
148-
Lifetime(ident) => {
149-
let ident = ident.without_first_quote();
148+
Lifetime(name) => {
149+
let ident = ast::Ident::new(name, span).without_first_quote();
150150
stack.push(tt!(Ident::new(ident.name, false)));
151151
tt!(Punct::new('\'', true))
152152
}

0 commit comments

Comments
 (0)