Skip to content

Commit ff4caa4

Browse files
committed
Remove Token::uninterpolated_span.
In favour of the similar method on `Parser`, which works on things other than identifiers and lifetimes.
1 parent a15999b commit ff4caa4

File tree

5 files changed

+48
-44
lines changed

5 files changed

+48
-44
lines changed

compiler/rustc_ast/src/token.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,9 @@ pub enum TokenKind {
448448

449449
/// Identifier token.
450450
/// Do not forget about `NtIdent` when you want to match on identifiers.
451-
/// It's recommended to use `Token::(ident,uninterpolate,uninterpolated_span)` to
452-
/// treat regular and interpolated identifiers in the same way.
451+
/// It's recommended to use `Token::{ident,uninterpolate}` and
452+
/// `Parser::token_uninterpolated_span` to treat regular and interpolated
453+
/// identifiers in the same way.
453454
Ident(Symbol, IdentIsRaw),
454455
/// This identifier (and its span) is the identifier passed to the
455456
/// declarative macro. The span in the surrounding `Token` is the span of
@@ -458,8 +459,9 @@ pub enum TokenKind {
458459

459460
/// Lifetime identifier token.
460461
/// Do not forget about `NtLifetime` when you want to match on lifetime identifiers.
461-
/// It's recommended to use `Token::(lifetime,uninterpolate,uninterpolated_span)` to
462-
/// treat regular and interpolated lifetime identifiers in the same way.
462+
/// It's recommended to use `Token::{ident,uninterpolate}` and
463+
/// `Parser::token_uninterpolated_span` to treat regular and interpolated
464+
/// identifiers in the same way.
463465
Lifetime(Symbol, IdentIsRaw),
464466
/// This identifier (and its span) is the lifetime passed to the
465467
/// declarative macro. The span in the surrounding `Token` is the span of
@@ -585,23 +587,6 @@ impl Token {
585587
Token::new(Ident(ident.name, ident.is_raw_guess().into()), ident.span)
586588
}
587589

588-
/// For interpolated tokens, returns a span of the fragment to which the interpolated
589-
/// token refers. For all other tokens this is just a regular span.
590-
/// It is particularly important to use this for identifiers and lifetimes
591-
/// for which spans affect name resolution and edition checks.
592-
/// Note that keywords are also identifiers, so they should use this
593-
/// if they keep spans or perform edition checks.
594-
//
595-
// Note: `Parser::uninterpolated_token_span` may give better information
596-
// than this method does.
597-
pub fn uninterpolated_span(&self) -> Span {
598-
match self.kind {
599-
NtIdent(ident, _) | NtLifetime(ident, _) => ident.span,
600-
Interpolated(ref nt) => nt.use_span(),
601-
_ => self.span,
602-
}
603-
}
604-
605590
pub fn is_range_separator(&self) -> bool {
606591
[DotDot, DotDotDot, DotDotEq].contains(&self.kind)
607592
}

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,7 @@ impl<'a> Parser<'a> {
13181318

13191319
/// Assuming we have just parsed `.`, continue parsing into an expression.
13201320
fn parse_dot_suffix(&mut self, self_arg: P<Expr>, lo: Span) -> PResult<'a, P<Expr>> {
1321-
if self.token.uninterpolated_span().at_least_rust_2018() && self.eat_keyword(exp!(Await)) {
1321+
if self.token_uninterpolated_span().at_least_rust_2018() && self.eat_keyword(exp!(Await)) {
13221322
return Ok(self.mk_await_expr(self_arg, lo));
13231323
}
13241324

@@ -1509,9 +1509,9 @@ impl<'a> Parser<'a> {
15091509
this.parse_expr_let(restrictions)
15101510
} else if this.eat_keyword(exp!(Underscore)) {
15111511
Ok(this.mk_expr(this.prev_token.span, ExprKind::Underscore))
1512-
} else if this.token.uninterpolated_span().at_least_rust_2018() {
1512+
} else if this.token_uninterpolated_span().at_least_rust_2018() {
15131513
// `Span::at_least_rust_2018()` is somewhat expensive; don't get it repeatedly.
1514-
if this.token.uninterpolated_span().at_least_rust_2024()
1514+
if this.token_uninterpolated_span().at_least_rust_2024()
15151515
// check for `gen {}` and `gen move {}`
15161516
// or `async gen {}` and `async gen move {}`
15171517
&& (this.is_gen_block(kw::Gen, 0)
@@ -2186,7 +2186,7 @@ impl<'a> Parser<'a> {
21862186
fn parse_opt_meta_item_lit(&mut self) -> Option<MetaItemLit> {
21872187
self.recover_after_dot();
21882188
let span = self.token.span;
2189-
let uninterpolated_span = self.uninterpolated_token_span();
2189+
let uninterpolated_span = self.token_uninterpolated_span();
21902190
self.eat_token_lit().map(|token_lit| {
21912191
match MetaItemLit::from_token_lit(token_lit, span) {
21922192
Ok(lit) => lit,
@@ -2388,7 +2388,7 @@ impl<'a> Parser<'a> {
23882388
let movability =
23892389
if self.eat_keyword(exp!(Static)) { Movability::Static } else { Movability::Movable };
23902390

2391-
let coroutine_kind = if self.token.uninterpolated_span().at_least_rust_2018() {
2391+
let coroutine_kind = if self.token_uninterpolated_span().at_least_rust_2018() {
23922392
self.parse_coroutine_kind(Case::Sensitive)
23932393
} else {
23942394
None
@@ -2896,7 +2896,7 @@ impl<'a> Parser<'a> {
28962896
/// Parses `for await? <src_pat> in <src_expr> <src_loop_block>` (`for` token already eaten).
28972897
fn parse_expr_for(&mut self, opt_label: Option<Label>, lo: Span) -> PResult<'a, P<Expr>> {
28982898
let is_await =
2899-
self.token.uninterpolated_span().at_least_rust_2018() && self.eat_keyword(exp!(Await));
2899+
self.token_uninterpolated_span().at_least_rust_2018() && self.eat_keyword(exp!(Await));
29002900

29012901
if is_await {
29022902
self.psess.gated_spans.gate(sym::async_for_loop, self.prev_token.span);
@@ -3486,7 +3486,7 @@ impl<'a> Parser<'a> {
34863486
self.token.is_keyword(kw::Try)
34873487
&& self
34883488
.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block())
3489-
&& self.token.uninterpolated_span().at_least_rust_2018()
3489+
&& self.token_uninterpolated_span().at_least_rust_2018()
34903490
}
34913491

34923492
/// Parses an `async move? {...}` or `gen move? {...}` expression.

compiler/rustc_parse/src/parser/item.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ impl<'a> Parser<'a> {
598598
}
599599

600600
// Parse stray `impl async Trait`
601-
if (self.token.uninterpolated_span().at_least_rust_2018()
601+
if (self.token_uninterpolated_span().at_least_rust_2018()
602602
&& self.token.is_keyword(kw::Async))
603603
|| self.is_kw_followed_by_ident(kw::Async)
604604
{
@@ -885,7 +885,7 @@ impl<'a> Parser<'a> {
885885
&& self.look_ahead(1, |t| t.is_non_raw_ident_where(|i| i.name != kw::As))
886886
{
887887
self.bump(); // `default`
888-
Defaultness::Default(self.prev_token.uninterpolated_span())
888+
Defaultness::Default(self.prev_token_uninterpolated_span())
889889
} else {
890890
Defaultness::Final
891891
}
@@ -1219,7 +1219,7 @@ impl<'a> Parser<'a> {
12191219
attrs: &mut AttrVec,
12201220
mut safety: Safety,
12211221
) -> PResult<'a, ItemInfo> {
1222-
let extern_span = self.prev_token.uninterpolated_span();
1222+
let extern_span = self.prev_token_uninterpolated_span();
12231223
let abi = self.parse_abi(); // ABI?
12241224
// FIXME: This recovery should be tested better.
12251225
if safety == Safety::Default
@@ -2787,7 +2787,7 @@ impl<'a> Parser<'a> {
27872787
.expect("Span extracted directly from keyword should always work");
27882788

27892789
err.span_suggestion(
2790-
self.token.uninterpolated_span(),
2790+
self.token_uninterpolated_span(),
27912791
format!("`{original_kw}` already used earlier, remove this one"),
27922792
"",
27932793
Applicability::MachineApplicable,
@@ -2798,7 +2798,7 @@ impl<'a> Parser<'a> {
27982798
else if let Some(WrongKw::Misplaced(correct_pos_sp)) = wrong_kw {
27992799
let correct_pos_sp = correct_pos_sp.to(self.prev_token.span);
28002800
if let Ok(current_qual) = self.span_to_snippet(correct_pos_sp) {
2801-
let misplaced_qual_sp = self.token.uninterpolated_span();
2801+
let misplaced_qual_sp = self.token_uninterpolated_span();
28022802
let misplaced_qual = self.span_to_snippet(misplaced_qual_sp).unwrap();
28032803

28042804
err.span_suggestion(

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,14 +1313,14 @@ impl<'a> Parser<'a> {
13131313

13141314
/// Parses asyncness: `async` or nothing.
13151315
fn parse_coroutine_kind(&mut self, case: Case) -> Option<CoroutineKind> {
1316-
let span = self.token.uninterpolated_span();
1316+
let span = self.token_uninterpolated_span();
13171317
if self.eat_keyword_case(exp!(Async), case) {
13181318
// FIXME(gen_blocks): Do we want to unconditionally parse `gen` and then
13191319
// error if edition <= 2024, like we do with async and edition <= 2018?
1320-
if self.token.uninterpolated_span().at_least_rust_2024()
1320+
if self.token_uninterpolated_span().at_least_rust_2024()
13211321
&& self.eat_keyword_case(exp!(Gen), case)
13221322
{
1323-
let gen_span = self.prev_token.uninterpolated_span();
1323+
let gen_span = self.prev_token_uninterpolated_span();
13241324
Some(CoroutineKind::AsyncGen {
13251325
span: span.to(gen_span),
13261326
closure_id: DUMMY_NODE_ID,
@@ -1333,7 +1333,7 @@ impl<'a> Parser<'a> {
13331333
return_impl_trait_id: DUMMY_NODE_ID,
13341334
})
13351335
}
1336-
} else if self.token.uninterpolated_span().at_least_rust_2024()
1336+
} else if self.token_uninterpolated_span().at_least_rust_2024()
13371337
&& self.eat_keyword_case(exp!(Gen), case)
13381338
{
13391339
Some(CoroutineKind::Gen {
@@ -1349,9 +1349,9 @@ impl<'a> Parser<'a> {
13491349
/// Parses fn unsafety: `unsafe`, `safe` or nothing.
13501350
fn parse_safety(&mut self, case: Case) -> Safety {
13511351
if self.eat_keyword_case(exp!(Unsafe), case) {
1352-
Safety::Unsafe(self.prev_token.uninterpolated_span())
1352+
Safety::Unsafe(self.prev_token_uninterpolated_span())
13531353
} else if self.eat_keyword_case(exp!(Safe), case) {
1354-
Safety::Safe(self.prev_token.uninterpolated_span())
1354+
Safety::Safe(self.prev_token_uninterpolated_span())
13551355
} else {
13561356
Safety::Default
13571357
}
@@ -1378,7 +1378,7 @@ impl<'a> Parser<'a> {
13781378
.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block())
13791379
&& self.eat_keyword_case(exp!(Const), case)
13801380
{
1381-
Const::Yes(self.prev_token.uninterpolated_span())
1381+
Const::Yes(self.prev_token_uninterpolated_span())
13821382
} else {
13831383
Const::No
13841384
}
@@ -1716,15 +1716,34 @@ impl<'a> Parser<'a> {
17161716
self.num_bump_calls
17171717
}
17181718

1719-
pub fn uninterpolated_token_span(&self) -> Span {
1719+
/// For interpolated `self.token`, returns a span of the fragment to which
1720+
/// the interpolated token refers. For all other tokens this is just a
1721+
/// regular span. It is particularly important to use this for identifiers
1722+
/// and lifetimes for which spans affect name resolution and edition
1723+
/// checks. Note that keywords are also identifiers, so they should use
1724+
/// this if they keep spans or perform edition checks.
1725+
pub fn token_uninterpolated_span(&self) -> Span {
17201726
match &self.token.kind {
1727+
token::NtIdent(ident, _) | token::NtLifetime(ident, _) => ident.span,
17211728
token::Interpolated(nt) => nt.use_span(),
17221729
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(_))) => {
17231730
self.look_ahead(1, |t| t.span)
17241731
}
17251732
_ => self.token.span,
17261733
}
17271734
}
1735+
1736+
/// Like `token_uninterpolated_span`, but works on `self.prev_token`.
1737+
pub fn prev_token_uninterpolated_span(&self) -> Span {
1738+
match &self.prev_token.kind {
1739+
token::NtIdent(ident, _) | token::NtLifetime(ident, _) => ident.span,
1740+
token::Interpolated(nt) => nt.use_span(),
1741+
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(_))) => {
1742+
self.look_ahead(0, |t| t.span)
1743+
}
1744+
_ => self.prev_token.span,
1745+
}
1746+
}
17281747
}
17291748

17301749
pub(crate) fn make_unclosed_delims_error(

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ impl<'a> Parser<'a> {
775775
/// Is a `dyn B0 + ... + Bn` type allowed here?
776776
fn is_explicit_dyn_type(&mut self) -> bool {
777777
self.check_keyword(exp!(Dyn))
778-
&& (self.token.uninterpolated_span().at_least_rust_2018()
778+
&& (self.token_uninterpolated_span().at_least_rust_2018()
779779
|| self.look_ahead(1, |t| {
780780
(can_begin_dyn_bound_in_edition_2015(t) || *t == TokenKind::Star)
781781
&& !can_continue_type_after_non_fn_ident(t)
@@ -998,13 +998,13 @@ impl<'a> Parser<'a> {
998998
BoundConstness::Never
999999
};
10001000

1001-
let asyncness = if self.token.uninterpolated_span().at_least_rust_2018()
1001+
let asyncness = if self.token_uninterpolated_span().at_least_rust_2018()
10021002
&& self.eat_keyword(exp!(Async))
10031003
{
10041004
self.psess.gated_spans.gate(sym::async_trait_bounds, self.prev_token.span);
10051005
BoundAsyncness::Async(self.prev_token.span)
10061006
} else if self.may_recover()
1007-
&& self.token.uninterpolated_span().is_rust_2015()
1007+
&& self.token_uninterpolated_span().is_rust_2015()
10081008
&& self.is_kw_followed_by_ident(kw::Async)
10091009
{
10101010
self.bump(); // eat `async`

0 commit comments

Comments
 (0)