Skip to content

Commit

Permalink
Only error raw lifetime followed by \' in edition 2021+
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Dec 1, 2024
1 parent b87e935 commit 60d165a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
33 changes: 25 additions & 8 deletions compiler/rustc_parse/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,17 +300,35 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
let prefix_span = self.mk_sp(start, ident_start);

if prefix_span.at_least_rust_2021() {
// If the raw lifetime is followed by \' then treat it a normal
// lifetime followed by a \', which is to interpret it as a character
// literal. In this case, it's always an invalid character literal
// since the literal must necessarily have >3 characters (r#...) inside
// of it, which is invalid.
if self.cursor.as_str().starts_with('\'') {
let lit_span = self.mk_sp(start, self.pos + BytePos(1));
let contents = self.str_from_to(start + BytePos(1), self.pos);
emit_unescape_error(
self.dcx(),
contents,
lit_span,
lit_span,
Mode::Char,
0..contents.len(),
EscapeError::MoreThanOneChar,
)
.expect("expected error");
}

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
}
);
self.dcx().emit_err(errors::CannotBeRawLifetime {
span,
ident: lifetime_name_without_tick,
});
}

// Put the `'` back onto the lifetime name.
Expand Down Expand Up @@ -371,8 +389,7 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
rustc_lexer::TokenKind::Caret => token::BinOp(token::Caret),
rustc_lexer::TokenKind::Percent => token::BinOp(token::Percent),

rustc_lexer::TokenKind::Unknown
| rustc_lexer::TokenKind::InvalidIdent => {
rustc_lexer::TokenKind::Unknown | rustc_lexer::TokenKind::InvalidIdent => {
// Don't emit diagnostics for sequences of the same invalid token
if swallow_next_invalid > 0 {
swallow_next_invalid -= 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: character literal may only contain one codepoint
--> $DIR/immediately-followed-by-lt.rs:11:4
--> $DIR/immediately-followed-by-lt.rs:15:4
|
LL | w!('r#long'id);
| ^^^^^^^^
Expand Down
8 changes: 6 additions & 2 deletions tests/ui/lifetimes/raw/immediately-followed-by-lt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
//@ edition: 2021
//@ revisions: e2015 e2021

//@[e2021] edition: 2021
//@[e2015] edition: 2015
//@[e2015] check-pass

// Make sure we reject the case where a raw lifetime is immediately followed by another
// lifetime. This reserves a modest amount of space for changing lexing to, for example,
Expand All @@ -9,6 +13,6 @@ macro_rules! w {
}

w!('r#long'id);
//~^ ERROR character literal may only contain one codepoint
//[e2021]~^ ERROR character literal may only contain one codepoint

fn main() {}

0 comments on commit 60d165a

Please sign in to comment.