Skip to content

Commit 60d165a

Browse files
Only error raw lifetime followed by \' in edition 2021+
1 parent b87e935 commit 60d165a

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

compiler/rustc_parse/src/lexer/mod.rs

+25-8
Original file line numberDiff line numberDiff line change
@@ -300,17 +300,35 @@ impl<'psess, 'src> Lexer<'psess, 'src> {
300300
let prefix_span = self.mk_sp(start, ident_start);
301301

302302
if prefix_span.at_least_rust_2021() {
303+
// If the raw lifetime is followed by \' then treat it a normal
304+
// lifetime followed by a \', which is to interpret it as a character
305+
// literal. In this case, it's always an invalid character literal
306+
// since the literal must necessarily have >3 characters (r#...) inside
307+
// of it, which is invalid.
308+
if self.cursor.as_str().starts_with('\'') {
309+
let lit_span = self.mk_sp(start, self.pos + BytePos(1));
310+
let contents = self.str_from_to(start + BytePos(1), self.pos);
311+
emit_unescape_error(
312+
self.dcx(),
313+
contents,
314+
lit_span,
315+
lit_span,
316+
Mode::Char,
317+
0..contents.len(),
318+
EscapeError::MoreThanOneChar,
319+
)
320+
.expect("expected error");
321+
}
322+
303323
let span = self.mk_sp(start, self.pos);
304324

305325
let lifetime_name_without_tick =
306326
Symbol::intern(&self.str_from(ident_start));
307327
if !lifetime_name_without_tick.can_be_raw() {
308-
self.dcx().emit_err(
309-
errors::CannotBeRawLifetime {
310-
span,
311-
ident: lifetime_name_without_tick
312-
}
313-
);
328+
self.dcx().emit_err(errors::CannotBeRawLifetime {
329+
span,
330+
ident: lifetime_name_without_tick,
331+
});
314332
}
315333

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

374-
rustc_lexer::TokenKind::Unknown
375-
| rustc_lexer::TokenKind::InvalidIdent => {
392+
rustc_lexer::TokenKind::Unknown | rustc_lexer::TokenKind::InvalidIdent => {
376393
// Don't emit diagnostics for sequences of the same invalid token
377394
if swallow_next_invalid > 0 {
378395
swallow_next_invalid -= 1;

tests/ui/lifetimes/raw/immediately-followed-by-lt.stderr renamed to tests/ui/lifetimes/raw/immediately-followed-by-lt.e2021.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: character literal may only contain one codepoint
2-
--> $DIR/immediately-followed-by-lt.rs:11:4
2+
--> $DIR/immediately-followed-by-lt.rs:15:4
33
|
44
LL | w!('r#long'id);
55
| ^^^^^^^^

tests/ui/lifetimes/raw/immediately-followed-by-lt.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
//@ edition: 2021
1+
//@ revisions: e2015 e2021
2+
3+
//@[e2021] edition: 2021
4+
//@[e2015] edition: 2015
5+
//@[e2015] check-pass
26

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

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

1418
fn main() {}

0 commit comments

Comments
 (0)