Skip to content

Commit d59b17c

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 49ed25b commit d59b17c

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)
@@ -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,
@@ -2390,7 +2390,7 @@ impl<'a> Parser<'a> {
23902390
let movability =
23912391
if self.eat_keyword(exp!(Static)) { Movability::Static } else { Movability::Movable };
23922392

2393-
let coroutine_kind = if self.token.uninterpolated_span().at_least_rust_2018() {
2393+
let coroutine_kind = if self.token_uninterpolated_span().at_least_rust_2018() {
23942394
self.parse_coroutine_kind(Case::Sensitive)
23952395
} else {
23962396
None
@@ -2939,7 +2939,7 @@ impl<'a> Parser<'a> {
29392939
/// Parses `for await? <src_pat> in <src_expr> <src_loop_block>` (`for` token already eaten).
29402940
fn parse_expr_for(&mut self, opt_label: Option<Label>, lo: Span) -> PResult<'a, P<Expr>> {
29412941
let is_await =
2942-
self.token.uninterpolated_span().at_least_rust_2018() && self.eat_keyword(exp!(Await));
2942+
self.token_uninterpolated_span().at_least_rust_2018() && self.eat_keyword(exp!(Await));
29432943

29442944
if is_await {
29452945
self.psess.gated_spans.gate(sym::async_for_loop, self.prev_token.span);
@@ -3529,7 +3529,7 @@ impl<'a> Parser<'a> {
35293529
self.token.is_keyword(kw::Try)
35303530
&& self
35313531
.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block())
3532-
&& self.token.uninterpolated_span().at_least_rust_2018()
3532+
&& self.token_uninterpolated_span().at_least_rust_2018()
35333533
}
35343534

35353535
/// 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
@@ -591,7 +591,7 @@ impl<'a> Parser<'a> {
591591
}
592592

593593
// Parse stray `impl async Trait`
594-
if (self.token.uninterpolated_span().at_least_rust_2018()
594+
if (self.token_uninterpolated_span().at_least_rust_2018()
595595
&& self.token.is_keyword(kw::Async))
596596
|| self.is_kw_followed_by_ident(kw::Async)
597597
{
@@ -877,7 +877,7 @@ impl<'a> Parser<'a> {
877877
&& self.look_ahead(1, |t| t.is_non_raw_ident_where(|i| i.name != kw::As))
878878
{
879879
self.bump(); // `default`
880-
Defaultness::Default(self.prev_token.uninterpolated_span())
880+
Defaultness::Default(self.prev_token_uninterpolated_span())
881881
} else {
882882
Defaultness::Final
883883
}
@@ -1208,7 +1208,7 @@ impl<'a> Parser<'a> {
12081208
attrs: &mut AttrVec,
12091209
mut safety: Safety,
12101210
) -> PResult<'a, ItemKind> {
1211-
let extern_span = self.prev_token.uninterpolated_span();
1211+
let extern_span = self.prev_token_uninterpolated_span();
12121212
let abi = self.parse_abi(); // ABI?
12131213
// FIXME: This recovery should be tested better.
12141214
if safety == Safety::Default
@@ -2781,7 +2781,7 @@ impl<'a> Parser<'a> {
27812781
.expect("Span extracted directly from keyword should always work");
27822782

27832783
err.span_suggestion(
2784-
self.token.uninterpolated_span(),
2784+
self.token_uninterpolated_span(),
27852785
format!("`{original_kw}` already used earlier, remove this one"),
27862786
"",
27872787
Applicability::MachineApplicable,
@@ -2792,7 +2792,7 @@ impl<'a> Parser<'a> {
27922792
else if let Some(WrongKw::Misplaced(correct_pos_sp)) = wrong_kw {
27932793
let correct_pos_sp = correct_pos_sp.to(self.prev_token.span);
27942794
if let Ok(current_qual) = self.span_to_snippet(correct_pos_sp) {
2795-
let misplaced_qual_sp = self.token.uninterpolated_span();
2795+
let misplaced_qual_sp = self.token_uninterpolated_span();
27962796
let misplaced_qual = self.span_to_snippet(misplaced_qual_sp).unwrap();
27972797

27982798
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
}
@@ -1723,15 +1723,34 @@ impl<'a> Parser<'a> {
17231723
self.num_bump_calls
17241724
}
17251725

1726-
pub fn uninterpolated_token_span(&self) -> Span {
1726+
/// For interpolated `self.token`, returns a span of the fragment to which
1727+
/// the interpolated token refers. For all other tokens this is just a
1728+
/// regular span. It is particularly important to use this for identifiers
1729+
/// and lifetimes for which spans affect name resolution and edition
1730+
/// checks. Note that keywords are also identifiers, so they should use
1731+
/// this if they keep spans or perform edition checks.
1732+
pub fn token_uninterpolated_span(&self) -> Span {
17271733
match &self.token.kind {
1734+
token::NtIdent(ident, _) | token::NtLifetime(ident, _) => ident.span,
17281735
token::Interpolated(nt) => nt.use_span(),
17291736
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(_))) => {
17301737
self.look_ahead(1, |t| t.span)
17311738
}
17321739
_ => self.token.span,
17331740
}
17341741
}
1742+
1743+
/// Like `token_uninterpolated_span`, but works on `self.prev_token`.
1744+
pub fn prev_token_uninterpolated_span(&self) -> Span {
1745+
match &self.prev_token.kind {
1746+
token::NtIdent(ident, _) | token::NtLifetime(ident, _) => ident.span,
1747+
token::Interpolated(nt) => nt.use_span(),
1748+
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(_))) => {
1749+
self.look_ahead(0, |t| t.span)
1750+
}
1751+
_ => self.prev_token.span,
1752+
}
1753+
}
17351754
}
17361755

17371756
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)