Skip to content

Commit ec67311

Browse files
authored
Unrolled build for rust-lang#126452
Rollup merge of rust-lang#126452 - compiler-errors:raw-lifetimes, r=spastorino Implement raw lifetimes and labels (`'r#ident`) This PR does two things: 1. Reserve lifetime prefixes, e.g. `'prefix#lt` in edition 2021. 2. Implements raw lifetimes, e.g. `'r#async` in edition 2021. This PR additionally extends the `keyword_idents_2024` lint to also check lifetimes. cc `@traviscross` r? parser
2 parents 12b26c1 + b6a86be commit ec67311

40 files changed

+373
-85
lines changed

compiler/rustc_ast/src/mut_visit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ fn visit_lazy_tts<T: MutVisitor>(vis: &mut T, lazy_tts: &mut Option<LazyAttrToke
752752
pub fn visit_token<T: MutVisitor>(vis: &mut T, t: &mut Token) {
753753
let Token { kind, span } = t;
754754
match kind {
755-
token::Ident(name, _ /*raw*/) | token::Lifetime(name) => {
755+
token::Ident(name, _is_raw) | token::Lifetime(name, _is_raw) => {
756756
let mut ident = Ident::new(*name, *span);
757757
vis.visit_ident(&mut ident);
758758
*name = ident.name;
@@ -762,7 +762,7 @@ pub fn visit_token<T: MutVisitor>(vis: &mut T, t: &mut Token) {
762762
token::NtIdent(ident, _is_raw) => {
763763
vis.visit_ident(ident);
764764
}
765-
token::NtLifetime(ident) => {
765+
token::NtLifetime(ident, _is_raw) => {
766766
vis.visit_ident(ident);
767767
}
768768
token::Interpolated(nt) => {

compiler/rustc_ast/src/token.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,11 @@ pub enum TokenKind {
331331
/// Do not forget about `NtLifetime` when you want to match on lifetime identifiers.
332332
/// It's recommended to use `Token::(lifetime,uninterpolate,uninterpolated_span)` to
333333
/// treat regular and interpolated lifetime identifiers in the same way.
334-
Lifetime(Symbol),
334+
Lifetime(Symbol, IdentIsRaw),
335335
/// This identifier (and its span) is the lifetime passed to the
336336
/// declarative macro. The span in the surrounding `Token` is the span of
337337
/// the `lifetime` metavariable in the macro's RHS.
338-
NtLifetime(Ident),
338+
NtLifetime(Ident, IdentIsRaw),
339339

340340
/// An embedded AST node, as produced by a macro. This only exists for
341341
/// historical reasons. We'd like to get rid of it, for multiple reasons.
@@ -458,7 +458,7 @@ impl Token {
458458
/// if they keep spans or perform edition checks.
459459
pub fn uninterpolated_span(&self) -> Span {
460460
match self.kind {
461-
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
461+
NtIdent(ident, _) | NtLifetime(ident, _) => ident.span,
462462
Interpolated(ref nt) => nt.use_span(),
463463
_ => self.span,
464464
}
@@ -661,7 +661,9 @@ impl Token {
661661
pub fn uninterpolate(&self) -> Cow<'_, Token> {
662662
match self.kind {
663663
NtIdent(ident, is_raw) => Cow::Owned(Token::new(Ident(ident.name, is_raw), ident.span)),
664-
NtLifetime(ident) => Cow::Owned(Token::new(Lifetime(ident.name), ident.span)),
664+
NtLifetime(ident, is_raw) => {
665+
Cow::Owned(Token::new(Lifetime(ident.name, is_raw), ident.span))
666+
}
665667
_ => Cow::Borrowed(self),
666668
}
667669
}
@@ -679,11 +681,11 @@ impl Token {
679681

680682
/// Returns a lifetime identifier if this token is a lifetime.
681683
#[inline]
682-
pub fn lifetime(&self) -> Option<Ident> {
684+
pub fn lifetime(&self) -> Option<(Ident, IdentIsRaw)> {
683685
// We avoid using `Token::uninterpolate` here because it's slow.
684686
match self.kind {
685-
Lifetime(name) => Some(Ident::new(name, self.span)),
686-
NtLifetime(ident) => Some(ident),
687+
Lifetime(name, is_raw) => Some((Ident::new(name, self.span), is_raw)),
688+
NtLifetime(ident, is_raw) => Some((ident, is_raw)),
687689
_ => None,
688690
}
689691
}
@@ -865,7 +867,7 @@ impl Token {
865867
_ => return None,
866868
},
867869
SingleQuote => match joint.kind {
868-
Ident(name, IdentIsRaw::No) => Lifetime(Symbol::intern(&format!("'{name}"))),
870+
Ident(name, is_raw) => Lifetime(Symbol::intern(&format!("'{name}")), is_raw),
869871
_ => return None,
870872
},
871873

compiler/rustc_ast/src/tokenstream.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -482,11 +482,11 @@ impl TokenStream {
482482
token::NtIdent(ident, is_raw) => {
483483
TokenTree::Token(Token::new(token::Ident(ident.name, is_raw), ident.span), spacing)
484484
}
485-
token::NtLifetime(ident) => TokenTree::Delimited(
485+
token::NtLifetime(ident, is_raw) => TokenTree::Delimited(
486486
DelimSpan::from_single(token.span),
487487
DelimSpacing::new(Spacing::JointHidden, spacing),
488488
Delimiter::Invisible,
489-
TokenStream::token_alone(token::Lifetime(ident.name), ident.span),
489+
TokenStream::token_alone(token::Lifetime(ident.name, is_raw), ident.span),
490490
),
491491
token::Interpolated(ref nt) => TokenTree::Delimited(
492492
DelimSpan::from_single(token.span),

compiler/rustc_ast_pretty/src/pprust/state.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use std::borrow::Cow;
1111
use ast::TraitBoundModifiers;
1212
use rustc_ast::attr::AttrIdGenerator;
1313
use rustc_ast::ptr::P;
14-
use rustc_ast::token::{self, BinOpToken, CommentKind, Delimiter, Nonterminal, Token, TokenKind};
14+
use rustc_ast::token::{
15+
self, BinOpToken, CommentKind, Delimiter, IdentIsRaw, Nonterminal, Token, TokenKind,
16+
};
1517
use rustc_ast::tokenstream::{Spacing, TokenStream, TokenTree};
1618
use rustc_ast::util::classify;
1719
use rustc_ast::util::comments::{Comment, CommentStyle};
@@ -947,8 +949,13 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
947949
token::NtIdent(ident, is_raw) => {
948950
IdentPrinter::for_ast_ident(ident, is_raw.into()).to_string().into()
949951
}
950-
token::Lifetime(name) => name.to_string().into(),
951-
token::NtLifetime(ident) => ident.name.to_string().into(),
952+
953+
token::Lifetime(name, IdentIsRaw::No)
954+
| token::NtLifetime(Ident { name, .. }, IdentIsRaw::No) => name.to_string().into(),
955+
token::Lifetime(name, IdentIsRaw::Yes)
956+
| token::NtLifetime(Ident { name, .. }, IdentIsRaw::Yes) => {
957+
format!("'r#{}", &name.as_str()[1..]).into()
958+
}
952959

953960
/* Other */
954961
token::DocComment(comment_kind, attr_style, data) => {

compiler/rustc_expand/src/mbe/macro_parser.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,10 @@ pub(crate) enum NamedMatch {
398398
fn token_name_eq(t1: &Token, t2: &Token) -> bool {
399399
if let (Some((ident1, is_raw1)), Some((ident2, is_raw2))) = (t1.ident(), t2.ident()) {
400400
ident1.name == ident2.name && is_raw1 == is_raw2
401-
} else if let (Some(ident1), Some(ident2)) = (t1.lifetime(), t2.lifetime()) {
402-
ident1.name == ident2.name
401+
} else if let (Some((ident1, is_raw1)), Some((ident2, is_raw2))) =
402+
(t1.lifetime(), t2.lifetime())
403+
{
404+
ident1.name == ident2.name && is_raw1 == is_raw2
403405
} else {
404406
t1.kind == t2.kind
405407
}

compiler/rustc_expand/src/mbe/transcribe.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,9 @@ pub(super) fn transcribe<'a>(
283283
let kind = token::NtIdent(*ident, *is_raw);
284284
TokenTree::token_alone(kind, sp)
285285
}
286-
MatchedSingle(ParseNtResult::Lifetime(ident)) => {
286+
MatchedSingle(ParseNtResult::Lifetime(ident, is_raw)) => {
287287
marker.visit_span(&mut sp);
288-
let kind = token::NtLifetime(*ident);
288+
let kind = token::NtLifetime(*ident, *is_raw);
289289
TokenTree::token_alone(kind, sp)
290290
}
291291
MatchedSingle(ParseNtResult::Nt(nt)) => {

compiler/rustc_expand/src/proc_macro_server.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,16 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> for Vec<TokenTree<TokenStre
229229
span: ident.span,
230230
})),
231231

232-
Lifetime(name) => {
232+
Lifetime(name, is_raw) => {
233233
let ident = symbol::Ident::new(name, span).without_first_quote();
234234
trees.extend([
235235
TokenTree::Punct(Punct { ch: b'\'', joint: true, span }),
236-
TokenTree::Ident(Ident { sym: ident.name, is_raw: false, span }),
236+
TokenTree::Ident(Ident { sym: ident.name, is_raw: is_raw.into(), span }),
237237
]);
238238
}
239-
NtLifetime(ident) => {
240-
let stream = TokenStream::token_alone(token::Lifetime(ident.name), ident.span);
239+
NtLifetime(ident, is_raw) => {
240+
let stream =
241+
TokenStream::token_alone(token::Lifetime(ident.name, is_raw), ident.span);
241242
trees.push(TokenTree::Group(Group {
242243
delimiter: pm::Delimiter::None,
243244
stream: Some(stream),

compiler/rustc_lexer/src/lib.rs

+29-10
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ pub enum TokenKind {
9191
/// tokens.
9292
UnknownPrefix,
9393

94+
/// An unknown prefix in a lifetime, like `'foo#`.
95+
///
96+
/// Note that like above, only the `'` and prefix are included in the token
97+
/// and not the separator.
98+
UnknownPrefixLifetime,
99+
100+
/// `'r#lt`, which in edition < 2021 is split into several tokens: `'r # lt`.
101+
RawLifetime,
102+
94103
/// Similar to the above, but *always* an error on every edition. This is used
95104
/// for emoji identifier recovery, as those are not meant to be ever accepted.
96105
InvalidPrefix,
@@ -677,9 +686,17 @@ impl Cursor<'_> {
677686
return Literal { kind, suffix_start };
678687
}
679688

689+
if self.first() == 'r' && self.second() == '#' && is_id_start(self.third()) {
690+
// Eat "r" and `#`, and identifier start characters.
691+
self.bump();
692+
self.bump();
693+
self.bump();
694+
self.eat_while(is_id_continue);
695+
return RawLifetime;
696+
}
697+
680698
// Either a lifetime or a character literal with
681699
// length greater than 1.
682-
683700
let starts_with_number = self.first().is_ascii_digit();
684701

685702
// Skip the literal contents.
@@ -688,15 +705,17 @@ impl Cursor<'_> {
688705
self.bump();
689706
self.eat_while(is_id_continue);
690707

691-
// Check if after skipping literal contents we've met a closing
692-
// single quote (which means that user attempted to create a
693-
// string with single quotes).
694-
if self.first() == '\'' {
695-
self.bump();
696-
let kind = Char { terminated: true };
697-
Literal { kind, suffix_start: self.pos_within_token() }
698-
} else {
699-
Lifetime { starts_with_number }
708+
match self.first() {
709+
// Check if after skipping literal contents we've met a closing
710+
// single quote (which means that user attempted to create a
711+
// string with single quotes).
712+
'\'' => {
713+
self.bump();
714+
let kind = Char { terminated: true };
715+
Literal { kind, suffix_start: self.pos_within_token() }
716+
}
717+
'#' if !starts_with_number => UnknownPrefixLifetime,
718+
_ => Lifetime { starts_with_number },
700719
}
701720
}
702721

compiler/rustc_lint/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,10 @@ lint_range_endpoint_out_of_range = range endpoint is out of range for `{$ty}`
708708
lint_range_use_inclusive_range = use an inclusive range instead
709709
710710
711+
lint_raw_prefix = prefix `'r` is reserved
712+
.label = reserved prefix
713+
.suggestion = insert whitespace here to avoid this being parsed as a prefix in Rust 2021
714+
711715
lint_reason_must_be_string_literal = reason must be a string literal
712716
713717
lint_reason_must_come_last = reason in lint attribute must come last

compiler/rustc_lint/src/builtin.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -1851,9 +1851,16 @@ impl KeywordIdents {
18511851
TokenTree::Token(token, _) => {
18521852
if let Some((ident, token::IdentIsRaw::No)) = token.ident() {
18531853
if !prev_dollar {
1854-
self.check_ident_token(cx, UnderMacro(true), ident);
1854+
self.check_ident_token(cx, UnderMacro(true), ident, "");
18551855
}
1856-
} else if *token == TokenKind::Dollar {
1856+
} else if let Some((ident, token::IdentIsRaw::No)) = token.lifetime() {
1857+
self.check_ident_token(
1858+
cx,
1859+
UnderMacro(true),
1860+
ident.without_first_quote(),
1861+
"'",
1862+
);
1863+
} else if token.kind == TokenKind::Dollar {
18571864
prev_dollar = true;
18581865
continue;
18591866
}
@@ -1869,6 +1876,7 @@ impl KeywordIdents {
18691876
cx: &EarlyContext<'_>,
18701877
UnderMacro(under_macro): UnderMacro,
18711878
ident: Ident,
1879+
prefix: &'static str,
18721880
) {
18731881
let (lint, edition) = match ident.name {
18741882
kw::Async | kw::Await | kw::Try => (KEYWORD_IDENTS_2018, Edition::Edition2018),
@@ -1902,7 +1910,7 @@ impl KeywordIdents {
19021910
cx.emit_span_lint(
19031911
lint,
19041912
ident.span,
1905-
BuiltinKeywordIdents { kw: ident, next: edition, suggestion: ident.span },
1913+
BuiltinKeywordIdents { kw: ident, next: edition, suggestion: ident.span, prefix },
19061914
);
19071915
}
19081916
}
@@ -1915,7 +1923,11 @@ impl EarlyLintPass for KeywordIdents {
19151923
self.check_tokens(cx, &mac.args.tokens);
19161924
}
19171925
fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: Ident) {
1918-
self.check_ident_token(cx, UnderMacro(false), ident);
1926+
if ident.name.as_str().starts_with('\'') {
1927+
self.check_ident_token(cx, UnderMacro(false), ident.without_first_quote(), "'");
1928+
} else {
1929+
self.check_ident_token(cx, UnderMacro(false), ident, "");
1930+
}
19191931
}
19201932
}
19211933

compiler/rustc_lint/src/context/diagnostics.rs

+4
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
172172
}
173173
.decorate_lint(diag);
174174
}
175+
BuiltinLintDiag::RawPrefix(label_span) => {
176+
lints::RawPrefix { label: label_span, suggestion: label_span.shrink_to_hi() }
177+
.decorate_lint(diag);
178+
}
175179
BuiltinLintDiag::UnusedBuiltinAttribute { attr_name, macro_name, invoc_span } => {
176180
lints::UnusedBuiltinAttribute { invoc_span, attr_name, macro_name }.decorate_lint(diag);
177181
}

compiler/rustc_lint/src/lints.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,9 @@ pub(crate) enum BuiltinEllipsisInclusiveRangePatternsLint {
363363
pub(crate) struct BuiltinKeywordIdents {
364364
pub kw: Ident,
365365
pub next: Edition,
366-
#[suggestion(code = "r#{kw}", applicability = "machine-applicable")]
366+
#[suggestion(code = "{prefix}r#{kw}", applicability = "machine-applicable")]
367367
pub suggestion: Span,
368+
pub prefix: &'static str,
368369
}
369370

370371
#[derive(LintDiagnostic)]
@@ -2814,6 +2815,15 @@ pub(crate) struct ReservedPrefix {
28142815
pub prefix: String,
28152816
}
28162817

2818+
#[derive(LintDiagnostic)]
2819+
#[diag(lint_raw_prefix)]
2820+
pub(crate) struct RawPrefix {
2821+
#[label]
2822+
pub label: Span,
2823+
#[suggestion(code = " ", applicability = "machine-applicable")]
2824+
pub suggestion: Span,
2825+
}
2826+
28172827
#[derive(LintDiagnostic)]
28182828
#[diag(lint_unused_builtin_attribute)]
28192829
pub(crate) struct UnusedBuiltinAttribute {

compiler/rustc_lint_defs/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,8 @@ pub enum BuiltinLintDiag {
612612
LegacyDeriveHelpers(Span),
613613
OrPatternsBackCompat(Span, String),
614614
ReservedPrefix(Span, String),
615+
/// `'r#` in edition < 2021.
616+
RawPrefix(Span),
615617
TrailingMacro(bool, Ident),
616618
BreakWithLabelAndLoop(Span),
617619
UnicodeTextFlow(Span, String),

0 commit comments

Comments
 (0)