Skip to content

Commit e909e53

Browse files
authored
Unrolled build for rust-lang#132363
Rollup merge of rust-lang#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 rust-lang/reference#1603 (comment) (well, specifically the `'r#_` case), but I don't see why we shouldn't just make it consistent with raw identifiers.
2 parents b026d85 + 9785c7c commit e909e53

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed

compiler/rustc_parse/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ parse_box_syntax_removed_suggestion = use `Box::new()` instead
7777
7878
parse_cannot_be_raw_ident = `{$ident}` cannot be a raw identifier
7979
80+
parse_cannot_be_raw_lifetime = `{$ident}` cannot be a raw lifetime
81+
8082
parse_catch_after_try = keyword `catch` cannot follow a `try` block
8183
.help = try using `match` on the result of the `try` block instead
8284

compiler/rustc_parse/src/errors.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2018,6 +2018,14 @@ pub(crate) struct CannotBeRawIdent {
20182018
pub ident: Symbol,
20192019
}
20202020

2021+
#[derive(Diagnostic)]
2022+
#[diag(parse_cannot_be_raw_lifetime)]
2023+
pub(crate) struct CannotBeRawLifetime {
2024+
#[primary_span]
2025+
pub span: Span,
2026+
pub ident: Symbol,
2027+
}
2028+
20212029
#[derive(Diagnostic)]
20222030
#[diag(parse_keyword_lifetime)]
20232031
pub(crate) struct KeywordLifetime {

compiler/rustc_parse/src/lexer/mod.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -294,15 +294,21 @@ impl<'psess, 'src> StringReader<'psess, 'src> {
294294
let prefix_span = self.mk_sp(start, ident_start);
295295

296296
if prefix_span.at_least_rust_2021() {
297-
let lifetime_name_without_tick = self.str_from(ident_start);
297+
let span = self.mk_sp(start, self.pos);
298+
299+
let lifetime_name_without_tick = Symbol::intern(&self.str_from(ident_start));
300+
if !lifetime_name_without_tick.can_be_raw() {
301+
self.dcx().emit_err(errors::CannotBeRawLifetime { span, ident: lifetime_name_without_tick });
302+
}
303+
298304
// Put the `'` back onto the lifetime name.
299-
let mut lifetime_name = String::with_capacity(lifetime_name_without_tick.len() + 1);
305+
let mut lifetime_name = String::with_capacity(lifetime_name_without_tick.as_str().len() + 1);
300306
lifetime_name.push('\'');
301-
lifetime_name += lifetime_name_without_tick;
307+
lifetime_name += lifetime_name_without_tick.as_str();
302308
let sym = Symbol::intern(&lifetime_name);
303309

304310
// Make sure we mark this as a raw identifier.
305-
self.psess.raw_identifier_spans.push(self.mk_sp(start, self.pos));
311+
self.psess.raw_identifier_spans.push(span);
306312

307313
token::Lifetime(sym, IdentIsRaw::Yes)
308314
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@ edition: 2021
2+
3+
// Reject raw lifetimes with identifier parts that wouldn't be valid raw identifiers.
4+
5+
macro_rules! w {
6+
($tt:tt) => {};
7+
}
8+
9+
w!('r#_);
10+
//~^ ERROR `_` cannot be a raw lifetime
11+
w!('r#self);
12+
//~^ ERROR `self` cannot be a raw lifetime
13+
w!('r#super);
14+
//~^ ERROR `super` cannot be a raw lifetime
15+
w!('r#Self);
16+
//~^ ERROR `Self` cannot be a raw lifetime
17+
w!('r#crate);
18+
//~^ ERROR `crate` cannot be a raw lifetime
19+
20+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error: `_` cannot be a raw lifetime
2+
--> $DIR/raw-lt-invalid-raw-id.rs:9:4
3+
|
4+
LL | w!('r#_);
5+
| ^^^^
6+
7+
error: `self` cannot be a raw lifetime
8+
--> $DIR/raw-lt-invalid-raw-id.rs:11:4
9+
|
10+
LL | w!('r#self);
11+
| ^^^^^^^
12+
13+
error: `super` cannot be a raw lifetime
14+
--> $DIR/raw-lt-invalid-raw-id.rs:13:4
15+
|
16+
LL | w!('r#super);
17+
| ^^^^^^^^
18+
19+
error: `Self` cannot be a raw lifetime
20+
--> $DIR/raw-lt-invalid-raw-id.rs:15:4
21+
|
22+
LL | w!('r#Self);
23+
| ^^^^^^^
24+
25+
error: `crate` cannot be a raw lifetime
26+
--> $DIR/raw-lt-invalid-raw-id.rs:17:4
27+
|
28+
LL | w!('r#crate);
29+
| ^^^^^^^^
30+
31+
error: aborting due to 5 previous errors
32+

0 commit comments

Comments
 (0)