Skip to content

Commit d6b136c

Browse files
Only error raw lifetime followed by \' in edition 2021+
1 parent 9639c5c commit d6b136c

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

compiler/rustc_parse/src/lexer/mod.rs

+29-5
Original file line numberDiff line numberDiff line change
@@ -294,15 +294,40 @@ 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+
// If the raw lifetime is followed by \' then treat it a normal
298+
// lifetime followed by a \', which is to interpret it as a character
299+
// literal. In this case, it's always an invalid character literal
300+
// since the literal must necessarily have >3 characters (r#...) inside
301+
// of it, which is invalid.
302+
if self.cursor.as_str().starts_with('\'') {
303+
let lit_span = self.mk_sp(start, self.pos + BytePos(1));
304+
let contents = self.str_from_to(start + BytePos(1), self.pos);
305+
emit_unescape_error(
306+
self.dcx(),
307+
contents,
308+
lit_span,
309+
lit_span,
310+
Mode::Char,
311+
0..contents.len(),
312+
EscapeError::MoreThanOneChar,
313+
)
314+
.expect("expected error");
315+
}
316+
297317
let span = self.mk_sp(start, self.pos);
298318

299-
let lifetime_name_without_tick = Symbol::intern(&self.str_from(ident_start));
319+
let lifetime_name_without_tick =
320+
Symbol::intern(&self.str_from(ident_start));
300321
if !lifetime_name_without_tick.can_be_raw() {
301-
self.dcx().emit_err(errors::CannotBeRawLifetime { span, ident: lifetime_name_without_tick });
322+
self.dcx().emit_err(errors::CannotBeRawLifetime {
323+
span,
324+
ident: lifetime_name_without_tick,
325+
});
302326
}
303327

304328
// Put the `'` back onto the lifetime name.
305-
let mut lifetime_name = String::with_capacity(lifetime_name_without_tick.as_str().len() + 1);
329+
let mut lifetime_name =
330+
String::with_capacity(lifetime_name_without_tick.as_str().len() + 1);
306331
lifetime_name.push('\'');
307332
lifetime_name += lifetime_name_without_tick.as_str();
308333
let sym = Symbol::intern(&lifetime_name);
@@ -358,8 +383,7 @@ impl<'psess, 'src> StringReader<'psess, 'src> {
358383
rustc_lexer::TokenKind::Caret => token::BinOp(token::Caret),
359384
rustc_lexer::TokenKind::Percent => token::BinOp(token::Percent),
360385

361-
rustc_lexer::TokenKind::Unknown
362-
| rustc_lexer::TokenKind::InvalidIdent => {
386+
rustc_lexer::TokenKind::Unknown | rustc_lexer::TokenKind::InvalidIdent => {
363387
// Don't emit diagnostics for sequences of the same invalid token
364388
if swallow_next_invalid > 0 {
365389
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)