Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce that raw lifetimes must be valid raw identifiers #132363

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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_lifetime = `{$ident}` cannot be a raw lifetime

parse_catch_after_try = keyword `catch` cannot follow a `try` block
.help = try using `match` on the result of the `try` block instead

Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2018,6 +2018,14 @@ pub(crate) struct CannotBeRawIdent {
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)]
#[diag(parse_keyword_lifetime)]
pub(crate) struct KeywordLifetime {
Expand Down
14 changes: 10 additions & 4 deletions compiler/rustc_parse/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,21 @@ impl<'psess, 'src> StringReader<'psess, 'src> {
let prefix_span = self.mk_sp(start, ident_start);

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.
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 += lifetime_name_without_tick;
lifetime_name += lifetime_name_without_tick.as_str();
let sym = Symbol::intern(&lifetime_name);

// 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)
} else {
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/lifetimes/raw/raw-lt-invalid-raw-id.rs
Original file line number Diff line number Diff line change
@@ -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() {}
32 changes: 32 additions & 0 deletions tests/ui/lifetimes/raw/raw-lt-invalid-raw-id.stderr
Original file line number Diff line number Diff line change
@@ -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

Loading