Skip to content

unicode code point suggestion for non-ASCII character in byte string literal is incorrect #139226

@mozkeeler

Description

@mozkeeler

If you try to compile this:

fn main() {
    let bytes = b"ñ";
}

You'll get:

error: non-ASCII character in byte string literal
 --> src/main.rs:2:19
  |
2 |     let bytes = b"ñ";
  |                   ^ must be ASCII
  |
help: if you meant to use the unicode code point for 'ñ', use a \xHH escape
  |
2 |     let bytes = b"\xF1";
  |                   ~~~~

This is an unhelpful suggestion, because that's not a correct unicode encoding for 'ñ' (in utf-8, it would be "\xC3\xB1", and in utf-16 it would be "\x00\xF1").

Meta

rustc --version --verbose: This reproduces on https://play.rust-lang.org with both stable and nightly (although the output is a bit different on nightly)

Backtrace

Metadata

Metadata

Assignees

Labels

A-UnicodeArea: UnicodeA-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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