Skip to content

\xHH escaping suggestions are wrong for characters that are too big  #87397

Closed
@SkiFire13

Description

@SkiFire13

Consider this code:

fn main() {
    b'字';
    b"字";
}

playground

The current output is:

error: non-ASCII character in byte constant
 --> src/main.rs:2:7
  |
2 |     b'字';
  |       ^^
  |       |
  |       byte constant must be ASCII
  |       help: use a \xHH escape for a non-ASCII byte: `\x5B57`

error: non-ASCII character in byte constant
 --> src/main.rs:3:7
  |
3 |     b"字";
  |       ^^
  |       |
  |       byte constant must be ASCII
  |       help: use a \xHH escape for a non-ASCII byte: `\x5B57`

error: could not compile `playground` due to 2 previous errors

The suggestions are incorrect: \x5B57 is not a valid escaped character, only the \x5B part is interpreted as an escaped character while the 57 part is interpreted as two normal characters.

In the case of byte characters the suggestion leads to a byte character literal with more than one character, thus in another compile error. However in the case of byte strings this will silently compile, even though that probably wasn't what the user wanted nor expected.

This happens in both the current stable and nightly compilers.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions