Skip to content

Improve diagnostics for character literals containing multiple codepoints #88684

Closed

Description

@eggyal

I tried to compile the following code (playground):

const SPADE: char = '♠️';

For the avoidance of doubt, ♠️ is U+2660. I expected the code to compile—but instead, the literal is rejected:

error: character literal may only contain one codepoint
 --> src/lib.rs:1:21
  |
1 | const SPADE: char = '♠️';
  |                     ^^^
  |
help: if you meant to write a `str` literal, use double quotes
  |
1 | const SPADE: char = "♠️";
  |                     ^^^

Notably, it is accepted if unicode-escaped:

const SPADE: char = '\u{2660}';

Meta

rustc --version --verbose:

rustc 1.57.0-nightly (e30b68353 2021-09-05)
binary: rustc
commit-hash: e30b68353fe22b00f40d021e7914eeb78473b3c1
commit-date: 2021-09-05
host: x86_64-apple-darwin
release: 1.57.0-nightly
LLVM version: 13.0.0

Activity

  1. added
    C-bug
    on Sep 6, 2021
  2. SNCPlay42 commented on Sep 6, 2021

    @SNCPlay42
    Contributor

    The spade "character" in your code is not just a single U+2660, it also has a unicode variation selector U+FE0F: playground

    On my machine at least, this makes a visible difference in appearance (which github tries to eliminate by adding a U+FE0F unless I "escape" with a code block):

    U+2660 U+FE0F: ♠️
    U+2660 alone: ♠
    
  3. eggyal commented on Sep 6, 2021

    @eggyal
    ContributorAuthor

    D'oh! Good spot, thanks @SNCPlay42. Perhaps some improved diagnostics could have helped here?

  4. SNCPlay42 commented on Sep 6, 2021

    @SNCPlay42
    Contributor

    We have similar diagnostics in other cases of misleading Unicode so that seems like a good idea.

  5. added
    A-diagnostics
    C-enhancement
    T-compiler
    and removed
    C-bug
    on Sep 6, 2021
  6. changed the title [-]Character literal containing only one codepoint rejected (but only if unescaped)[/-] [+]Improve diagnostics for character literals containing multiple codepoints[/+] on Sep 7, 2021
  7. added a commit that references this issue on Sep 21, 2021
    c2cdba4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsC-enhancementT-compiler

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions