Skip to content

Commit 37af248

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 1c7c573 commit 37af248

File tree

5 files changed

+48
-44
lines changed

5 files changed

+48
-44
lines changed

compiler/rustc_ast/src/token.rs

+6-21
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

+7-7
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)
@@ -2191,7 +2191,7 @@ impl<'a> Parser<'a> {
21912191
fn parse_opt_meta_item_lit(&mut self) -> Option<MetaItemLit> {
21922192
self.recover_after_dot();
21932193
let span = self.token.span;
2194-
let uninterpolated_span = self.uninterpolated_token_span();
2194+
let uninterpolated_span = self.token_uninterpolated_span();
21952195
self.eat_token_lit().map(|token_lit| {
21962196
match MetaItemLit::from_token_lit(token_lit, span) {
21972197
Ok(lit) => lit,
@@ -2393,7 +2393,7 @@ impl<'a> Parser<'a> {
23932393
let movability =
23942394
if self.eat_keyword(exp!(Static)) { Movability::Static } else { Movability::Movable };
23952395

2396-
let coroutine_kind = if self.token.uninterpolated_span().at_least_rust_2018() {
2396+
let coroutine_kind = if self.token_uninterpolated_span().at_least_rust_2018() {
23972397
self.parse_coroutine_kind(Case::Sensitive)
23982398
} else {
23992399
None
@@ -2901,7 +2901,7 @@ impl<'a> Parser<'a> {
29012901
/// Parses `for await? <src_pat> in <src_expr> <src_loop_block>` (`for` token already eaten).
29022902
fn parse_expr_for(&mut self, opt_label: Option<Label>, lo: Span) -> PResult<'a, P<Expr>> {
29032903
let is_await =
2904-
self.token.uninterpolated_span().at_least_rust_2018() && self.eat_keyword(exp!(Await));
2904+
self.token_uninterpolated_span().at_least_rust_2018() && self.eat_keyword(exp!(Await));
29052905

29062906
if is_await {
29072907
self.psess.gated_spans.gate(sym::async_for_loop, self.prev_token.span);
@@ -3491,7 +3491,7 @@ impl<'a> Parser<'a> {
34913491
self.token.is_keyword(kw::Try)
34923492
&& self
34933493
.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block())
3494-
&& self.token.uninterpolated_span().at_least_rust_2018()
3494+
&& self.token_uninterpolated_span().at_least_rust_2018()
34953495
}
34963496

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

compiler/rustc_parse/src/parser/item.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ impl<'a> Parser<'a> {
597597
}
598598

599599
// Parse stray `impl async Trait`
600-
if (self.token.uninterpolated_span().at_least_rust_2018()
600+
if (self.token_uninterpolated_span().at_least_rust_2018()
601601
&& self.token.is_keyword(kw::Async))
602602
|| self.is_kw_followed_by_ident(kw::Async)
603603
{
@@ -884,7 +884,7 @@ impl<'a> Parser<'a> {
884884
&& self.look_ahead(1, |t| t.is_non_raw_ident_where(|i| i.name != kw::As))
885885
{
886886
self.bump(); // `default`
887-
Defaultness::Default(self.prev_token.uninterpolated_span())
887+
Defaultness::Default(self.prev_token_uninterpolated_span())
888888
} else {
889889
Defaultness::Final
890890
}
@@ -1211,7 +1211,7 @@ impl<'a> Parser<'a> {
12111211
attrs: &mut AttrVec,
12121212
mut safety: Safety,
12131213
) -> PResult<'a, ItemInfo> {
1214-
let extern_span = self.prev_token.uninterpolated_span();
1214+
let extern_span = self.prev_token_uninterpolated_span();
12151215
let abi = self.parse_abi(); // ABI?
12161216
// FIXME: This recovery should be tested better.
12171217
if safety == Safety::Default
@@ -2778,7 +2778,7 @@ impl<'a> Parser<'a> {
27782778
.expect("Span extracted directly from keyword should always work");
27792779

27802780
err.span_suggestion(
2781-
self.token.uninterpolated_span(),
2781+
self.token_uninterpolated_span(),
27822782
format!("`{original_kw}` already used earlier, remove this one"),
27832783
"",
27842784
Applicability::MachineApplicable,
@@ -2789,7 +2789,7 @@ impl<'a> Parser<'a> {
27892789
else if let Some(WrongKw::Misplaced(correct_pos_sp)) = wrong_kw {
27902790
let correct_pos_sp = correct_pos_sp.to(self.prev_token.span);
27912791
if let Ok(current_qual) = self.span_to_snippet(correct_pos_sp) {
2792-
let misplaced_qual_sp = self.token.uninterpolated_span();
2792+
let misplaced_qual_sp = self.token_uninterpolated_span();
27932793
let misplaced_qual = self.span_to_snippet(misplaced_qual_sp).unwrap();
27942794

27952795
err.span_suggestion(

compiler/rustc_parse/src/parser/mod.rs

+27-8
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

+3-3
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)