Skip to content

Commit 9b50092

Browse files
authored
Rollup merge of #132341 - compiler-errors:raw-lt-prefix-id, r=chenyukang
Reject raw lifetime followed by `'`, like regular lifetimes do See comment. We want to reject cases like `'r#long'id`, which currently gets interpreted as a raw lifetime (`'r#long`) followed by a lifetime (`'id`). This could have alternative lexes, such as an overlong char literal (`'r#long'`) followed by an identifier (`id`). To avoid committing to this in any case, let's reject the whole thing. `@mattheww,` is this what you were looking for in rust-lang/reference#1603 (comment)? I'd say ignore the details about the specific error message (the fact that this gets reinterpreted as a char literal is 🤷), just that because this causes a lexer error we're effectively saving syntactical space like you wanted.
2 parents b73478b + 1990f15 commit 9b50092

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

compiler/rustc_lexer/src/lib.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,17 @@ impl Cursor<'_> {
715715
self.bump();
716716
self.bump();
717717
self.eat_while(is_id_continue);
718-
return RawLifetime;
718+
match self.first() {
719+
'\'' => {
720+
// Check if after skipping literal contents we've met a closing
721+
// single quote (which means that user attempted to create a
722+
// string with single quotes).
723+
self.bump();
724+
let kind = Char { terminated: true };
725+
return Literal { kind, suffix_start: self.pos_within_token() };
726+
}
727+
_ => return RawLifetime,
728+
}
719729
}
720730

721731
// Either a lifetime or a character literal with
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ edition: 2021
2+
3+
// Make sure we reject the case where a raw lifetime is immediately followed by another
4+
// lifetime. This reserves a modest amount of space for changing lexing to, for example,
5+
// delay rejection of overlong char literals like `'r#long'id`.
6+
7+
macro_rules! w {
8+
($($tt:tt)*) => {}
9+
}
10+
11+
w!('r#long'id);
12+
//~^ ERROR character literal may only contain one codepoint
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: character literal may only contain one codepoint
2+
--> $DIR/immediately-followed-by-lt.rs:11:4
3+
|
4+
LL | w!('r#long'id);
5+
| ^^^^^^^^
6+
|
7+
help: if you meant to write a string literal, use double quotes
8+
|
9+
LL | w!("r#long"id);
10+
| ~ ~
11+
12+
error: aborting due to 1 previous error
13+

0 commit comments

Comments
 (0)