Rollup merge of #132363 - compiler-errors:raw-lt-id-valid, r=wesleywiser

Enforce that raw lifetimes must be valid raw identifiers

Make sure that the identifier part of a raw lifetime is a valid raw identifier. This precludes `'r#_` and all module segment paths for now.

I don't believe this is compelling to support. This was raised by `@ehuss` in https://github.com/rust-lang/reference/pull/1603#discussion_r1822726753 (well, specifically the `'r#_` case), but I don't see why we shouldn't just make it consistent with raw identifiers.
This commit is contained in:
Matthias Krüger 2024-11-09 19:16:43 +01:00 committed by GitHub
commit 910ac7eabf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 72 additions and 4 deletions

View File

@ -77,6 +77,8 @@ parse_box_syntax_removed_suggestion = use `Box::new()` instead
parse_cannot_be_raw_ident = `{$ident}` cannot be a raw identifier parse_cannot_be_raw_ident = `{$ident}` cannot be a raw identifier
parse_cannot_be_raw_lifetime = `{$ident}` cannot be a raw lifetime
parse_catch_after_try = keyword `catch` cannot follow a `try` block parse_catch_after_try = keyword `catch` cannot follow a `try` block
.help = try using `match` on the result of the `try` block instead .help = try using `match` on the result of the `try` block instead

View File

@ -2018,6 +2018,14 @@ pub(crate) struct CannotBeRawIdent {
pub ident: Symbol, pub ident: Symbol,
} }
#[derive(Diagnostic)]
#[diag(parse_cannot_be_raw_lifetime)]
pub(crate) struct CannotBeRawLifetime {
#[primary_span]
pub span: Span,
pub ident: Symbol,
}
#[derive(Diagnostic)] #[derive(Diagnostic)]
#[diag(parse_keyword_lifetime)] #[diag(parse_keyword_lifetime)]
pub(crate) struct KeywordLifetime { pub(crate) struct KeywordLifetime {

View File

@ -294,15 +294,21 @@ fn next_token(&mut self) -> (Token, bool) {
let prefix_span = self.mk_sp(start, ident_start); let prefix_span = self.mk_sp(start, ident_start);
if prefix_span.at_least_rust_2021() { if prefix_span.at_least_rust_2021() {
let lifetime_name_without_tick = self.str_from(ident_start); let span = self.mk_sp(start, self.pos);
let lifetime_name_without_tick = Symbol::intern(&self.str_from(ident_start));
if !lifetime_name_without_tick.can_be_raw() {
self.dcx().emit_err(errors::CannotBeRawLifetime { span, ident: lifetime_name_without_tick });
}
// Put the `'` back onto the lifetime name. // Put the `'` back onto the lifetime name.
let mut lifetime_name = String::with_capacity(lifetime_name_without_tick.len() + 1); let mut lifetime_name = String::with_capacity(lifetime_name_without_tick.as_str().len() + 1);
lifetime_name.push('\''); lifetime_name.push('\'');
lifetime_name += lifetime_name_without_tick; lifetime_name += lifetime_name_without_tick.as_str();
let sym = Symbol::intern(&lifetime_name); let sym = Symbol::intern(&lifetime_name);
// Make sure we mark this as a raw identifier. // Make sure we mark this as a raw identifier.
self.psess.raw_identifier_spans.push(self.mk_sp(start, self.pos)); self.psess.raw_identifier_spans.push(span);
token::Lifetime(sym, IdentIsRaw::Yes) token::Lifetime(sym, IdentIsRaw::Yes)
} else { } else {

View File

@ -0,0 +1,20 @@
//@ edition: 2021
// Reject raw lifetimes with identifier parts that wouldn't be valid raw identifiers.
macro_rules! w {
($tt:tt) => {};
}
w!('r#_);
//~^ ERROR `_` cannot be a raw lifetime
w!('r#self);
//~^ ERROR `self` cannot be a raw lifetime
w!('r#super);
//~^ ERROR `super` cannot be a raw lifetime
w!('r#Self);
//~^ ERROR `Self` cannot be a raw lifetime
w!('r#crate);
//~^ ERROR `crate` cannot be a raw lifetime
fn main() {}

View File

@ -0,0 +1,32 @@
error: `_` cannot be a raw lifetime
--> $DIR/raw-lt-invalid-raw-id.rs:9:4
|
LL | w!('r#_);
| ^^^^
error: `self` cannot be a raw lifetime
--> $DIR/raw-lt-invalid-raw-id.rs:11:4
|
LL | w!('r#self);
| ^^^^^^^
error: `super` cannot be a raw lifetime
--> $DIR/raw-lt-invalid-raw-id.rs:13:4
|
LL | w!('r#super);
| ^^^^^^^^
error: `Self` cannot be a raw lifetime
--> $DIR/raw-lt-invalid-raw-id.rs:15:4
|
LL | w!('r#Self);
| ^^^^^^^
error: `crate` cannot be a raw lifetime
--> $DIR/raw-lt-invalid-raw-id.rs:17:4
|
LL | w!('r#crate);
| ^^^^^^^^
error: aborting due to 5 previous errors