Skip to content

Commit

Permalink
Auto merge of #123752 - estebank:emoji-prefix, r=<try>
Browse files Browse the repository at this point in the history
Properly handle emojis as literal prefix in macros

Do not accept the following

```rust
macro_rules! lexes {($($_:tt)*) => {}}
lexes!(馃悰"foo");
```

Before, invalid emoji identifiers were gated during parsing instead of lexing in all cases, but this didn't account for macro expansion of literal prefixes.

Fix #123696.
  • Loading branch information
bors committed Apr 10, 2024
2 parents b3bd705 + 92debb1 commit d5ed922
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
6 changes: 5 additions & 1 deletion compiler/rustc_lexer/src/lib.rs
Expand Up @@ -88,6 +88,10 @@ pub enum TokenKind {
/// tokens.
UnknownPrefix,

/// Similar to the above, but *always* an error on every edition. This is used
/// for emoji identifier recovery, as those are not meant to be ever accepted.
InvalidPrefix,

/// Examples: `12u8`, `1.0e-40`, `b"123"`. Note that `_` is an invalid
/// suffix, but may be present here on string and float literals. Users of
/// this type will need to check for and reject that case.
Expand Down Expand Up @@ -528,7 +532,7 @@ impl Cursor<'_> {
// Known prefixes must have been handled earlier. So if
// we see a prefix here, it is definitely an unknown prefix.
match self.first() {
'#' | '"' | '\'' => UnknownPrefix,
'#' | '"' | '\'' => InvalidPrefix,
_ => InvalidIdent,
}
}
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_parse/src/lexer/mod.rs
Expand Up @@ -204,6 +204,7 @@ impl<'psess, 'src> StringReader<'psess, 'src> {
self.ident(start)
}
rustc_lexer::TokenKind::InvalidIdent
| rustc_lexer::TokenKind::InvalidPrefix
// Do not recover an identifier with emoji if the codepoint is a confusable
// with a recoverable substitution token, like `鉃朻.
if !UNICODE_ARRAY
Expand Down Expand Up @@ -301,7 +302,9 @@ impl<'psess, 'src> StringReader<'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
| rustc_lexer::TokenKind::InvalidPrefix => {
// Don't emit diagnostics for sequences of the same invalid token
if swallow_next_invalid > 0 {
swallow_next_invalid -= 1;
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/lexer/emoji-literal-prefix.rs
@@ -0,0 +1,8 @@
macro_rules! lexes {($($_:tt)*) => {}}

lexes!(馃悰#); //~ ERROR identifiers cannot contain emoji
lexes!(馃悰"foo");
lexes!(馃悰'q');
lexes!(馃悰'q);

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui/lexer/emoji-literal-prefix.stderr
@@ -0,0 +1,14 @@
error: identifiers cannot contain emoji: `馃悰`
--> $DIR/emoji-literal-prefix.rs:3:8
|
LL | lexes!(馃悰#);
| ^^
LL | lexes!(馃悰"foo");
| ^^
LL | lexes!(馃悰'q');
| ^^
LL | lexes!(馃悰'q);
| ^^

error: aborting due to 1 previous error

0 comments on commit d5ed922

Please sign in to comment.