Skip to content

Commit

Permalink
Revert "Don't recover lifetimes/labels containing emojis as character…
Browse files Browse the repository at this point in the history
… literals"

Reverts PR rust-lang#108031
Fixes (doesnt close until beta backported) rust-lang#109746

This reverts commit e3f9db5.
This reverts commit 98b82ae.
This reverts commit 380fa26.
  • Loading branch information
compiler-errors committed Apr 10, 2023
1 parent 3c2e2dd commit a047064
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 178 deletions.
2 changes: 0 additions & 2 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,6 @@ pub enum StashKey {
/// When an invalid lifetime e.g. `'2` should be reinterpreted
/// as a char literal in the parser
LifetimeIsChar,
/// When an invalid lifetime e.g. `'🐱` contains emoji.
LifetimeContainsEmoji,
/// Maybe there was a typo where a comma was forgotten before
/// FRU syntax
MaybeFruTypo,
Expand Down
43 changes: 10 additions & 33 deletions compiler/rustc_lexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub enum TokenKind {
Literal { kind: LiteralKind, suffix_start: u32 },

/// "'a"
Lifetime { starts_with_number: bool, contains_emoji: bool },
Lifetime { starts_with_number: bool },

// One-char tokens:
/// ";"
Expand Down Expand Up @@ -632,13 +632,7 @@ impl Cursor<'_> {
// If the first symbol is valid for identifier, it can be a lifetime.
// Also check if it's a number for a better error reporting (so '0 will
// be reported as invalid lifetime and not as unterminated char literal).
// We also have to account for potential `'🐱` emojis to avoid reporting
// it as an unterminated char literal.
is_id_start(self.first())
|| self.first().is_digit(10)
// FIXME(#108019): `unic-emoji-char` seems to have data tables only up to Unicode
// 5.0, but Unicode is already newer than this.
|| unic_emoji_char::is_emoji(self.first())
is_id_start(self.first()) || self.first().is_digit(10)
};

if !can_be_a_lifetime {
Expand All @@ -651,33 +645,16 @@ impl Cursor<'_> {
return Literal { kind, suffix_start };
}

// Either a lifetime or a character literal.
// Either a lifetime or a character literal with
// length greater than 1.

let starts_with_number = self.first().is_digit(10);
let mut contains_emoji = false;

// FIXME(#108019): `unic-emoji-char` seems to have data tables only up to Unicode
// 5.0, but Unicode is already newer than this.
if unic_emoji_char::is_emoji(self.first()) {
contains_emoji = true;
} else {
// Skip the literal contents.
// First symbol can be a number (which isn't a valid identifier start),
// so skip it without any checks.
self.bump();
}
self.eat_while(|c| {
if is_id_continue(c) {
true
// FIXME(#108019): `unic-emoji-char` seems to have data tables only up to Unicode
// 5.0, but Unicode is already newer than this.
} else if unic_emoji_char::is_emoji(c) {
contains_emoji = true;
true
} else {
false
}
});
// Skip the literal contents.
// First symbol can be a number (which isn't a valid identifier start),
// so skip it without any checks.
self.bump();
self.eat_while(is_id_continue);

// Check if after skipping literal contents we've met a closing
// single quote (which means that user attempted to create a
Expand All @@ -687,7 +664,7 @@ impl Cursor<'_> {
let kind = Char { terminated: true };
Literal { kind, suffix_start: self.pos_within_token() }
} else {
Lifetime { starts_with_number, contains_emoji }
Lifetime { starts_with_number }
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lexer/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ fn lifetime() {
check_lexing(
"'abc",
expect![[r#"
Token { kind: Lifetime { starts_with_number: false, contains_emoji: false }, len: 4 }
Token { kind: Lifetime { starts_with_number: false }, len: 4 }
"#]],
);
}
Expand Down
9 changes: 2 additions & 7 deletions compiler/rustc_parse/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,21 +223,16 @@ impl<'a> StringReader<'a> {
};
token::Literal(token::Lit { kind, symbol, suffix })
}
rustc_lexer::TokenKind::Lifetime { starts_with_number, contains_emoji } => {
rustc_lexer::TokenKind::Lifetime { starts_with_number } => {
// Include the leading `'` in the real identifier, for macro
// expansion purposes. See #12512 for the gory details of why
// this is necessary.
let lifetime_name = self.str_from(start);
if starts_with_number {
let span = self.mk_sp(start, self.pos);
let mut diag = self.sess.struct_err("lifetimes or labels cannot start with a number");
let mut diag = self.sess.struct_err("lifetimes cannot start with a number");
diag.set_span(span);
diag.stash(span, StashKey::LifetimeIsChar);
} else if contains_emoji {
let span = self.mk_sp(start, self.pos);
let mut diag = self.sess.struct_err("lifetimes or labels cannot contain emojis");
diag.set_span(span);
diag.stash(span, StashKey::LifetimeContainsEmoji);
}
let ident = Symbol::intern(lifetime_name);
token::Lifetime(ident)
Expand Down
45 changes: 0 additions & 45 deletions tests/ui/lexer/issue-108019-bad-emoji-recovery.rs

This file was deleted.

86 changes: 0 additions & 86 deletions tests/ui/lexer/issue-108019-bad-emoji-recovery.stderr

This file was deleted.

4 changes: 2 additions & 2 deletions tests/ui/parser/numeric-lifetime.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
struct S<'1> { s: &'1 usize }
//~^ ERROR lifetimes or labels cannot start with a number
//~| ERROR lifetimes or labels cannot start with a number
//~^ ERROR lifetimes cannot start with a number
//~| ERROR lifetimes cannot start with a number
fn main() {
// verify that the parse error doesn't stop type checking
let x: usize = "";
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/parser/numeric-lifetime.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ LL | let x: usize = "";
| |
| expected due to this

error: lifetimes or labels cannot start with a number
error: lifetimes cannot start with a number
--> $DIR/numeric-lifetime.rs:1:10
|
LL | struct S<'1> { s: &'1 usize }
| ^^

error: lifetimes or labels cannot start with a number
error: lifetimes cannot start with a number
--> $DIR/numeric-lifetime.rs:1:20
|
LL | struct S<'1> { s: &'1 usize }
Expand Down

0 comments on commit a047064

Please sign in to comment.