Skip to content

Commit 6eb3485

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

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

compiler/rustc_parse/src/lexer/mod.rs

+25-5
Original file line numberDiff line numberDiff line change
@@ -294,15 +294,36 @@ 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 just like we do with
298+
if self.cursor.as_str().starts_with('\'') {
299+
let lit_span = self.mk_sp(start, self.pos + BytePos(1));
300+
let contents = self.str_from_to(start + BytePos(1), self.pos);
301+
emit_unescape_error(
302+
self.dcx(),
303+
contents,
304+
lit_span,
305+
lit_span,
306+
Mode::Char,
307+
0..contents.len(),
308+
EscapeError::MoreThanOneChar,
309+
)
310+
.expect("expected error");
311+
}
312+
297313
let span = self.mk_sp(start, self.pos);
298314

299-
let lifetime_name_without_tick = Symbol::intern(&self.str_from(ident_start));
315+
let lifetime_name_without_tick =
316+
Symbol::intern(&self.str_from(ident_start));
300317
if !lifetime_name_without_tick.can_be_raw() {
301-
self.dcx().emit_err(errors::CannotBeRawLifetime { span, ident: lifetime_name_without_tick });
318+
self.dcx().emit_err(errors::CannotBeRawLifetime {
319+
span,
320+
ident: lifetime_name_without_tick,
321+
});
302322
}
303323

304324
// Put the `'` back onto the lifetime name.
305-
let mut lifetime_name = String::with_capacity(lifetime_name_without_tick.as_str().len() + 1);
325+
let mut lifetime_name =
326+
String::with_capacity(lifetime_name_without_tick.as_str().len() + 1);
306327
lifetime_name.push('\'');
307328
lifetime_name += lifetime_name_without_tick.as_str();
308329
let sym = Symbol::intern(&lifetime_name);
@@ -358,8 +379,7 @@ impl<'psess, 'src> StringReader<'psess, 'src> {
358379
rustc_lexer::TokenKind::Caret => token::BinOp(token::Caret),
359380
rustc_lexer::TokenKind::Percent => token::BinOp(token::Percent),
360381

361-
rustc_lexer::TokenKind::Unknown
362-
| rustc_lexer::TokenKind::InvalidIdent => {
382+
rustc_lexer::TokenKind::Unknown | rustc_lexer::TokenKind::InvalidIdent => {
363383
// Don't emit diagnostics for sequences of the same invalid token
364384
if swallow_next_invalid > 0 {
365385
swallow_next_invalid -= 1;

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() {}

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

-13
This file was deleted.

0 commit comments

Comments
 (0)