Skip to content

Fix error reporting for multibyte characters in byte string literal #139362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions compiler/rustc_parse/src/lexer/unescape_error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,7 @@ pub(crate) fn emit_unescape_error(
err.span_label(span, format!("must be ASCII{postfix}"));
// Note: the \\xHH suggestions are not given for raw byte string
// literals, because they are araw and so cannot use any escapes.
if (c as u32) <= 0xFF && mode != Mode::RawByteStr {
err.span_suggestion(
span,
format!(
"if you meant to use the unicode code point for {c:?}, use a \\xHH escape"
),
format!("\\x{:X}", c as u32),
Applicability::MaybeIncorrect,
);
} else if mode == Mode::Byte {
if mode == Mode::Byte {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there may also need to be consideration of the situation in the origin code, where it's possible that users may want to use single bytes, so we can explicitly state that the suggested value is a single byte value, not a UTF-8 encoding.
So, we could add this suggestion above if mode == Mode::Byte { such as below.

          if (c as u32) <= 0xFF && mode != Mode::RawByteStr {
              err.span_suggestion(
                  span,
                  format!(
                      "if you meant to use the byte with hex value {:#04X} (note: this is not the Unicode code point for {c:?})", 
                      c as u32
                  ),
                  format!("\\x{:02X}", c as u32),
                  Applicability::MaybeIncorrect,
              );
          }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that if a user explicitly specifies a character using a byte literal, their intention is likely to use the actual byte encoding. Otherwise, they would have directly provided the correct hex value.
Additionally, when dealing with a single byte value, it suggests a Unicode code point in hex even though that's not the full encoding, while for a multi-byte value, it throws a different error. That's more of an inconsistent behaviour.

Copy link
Member

@Nadrieril Nadrieril Apr 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know enough about typical uses of byte chars. We should at the very least explicitly mention that what we're suggesting is not an utf8 encoding? For reference, this behavior was introduced in #87659 to fix #87397.

Copy link
Member

@Nadrieril Nadrieril Apr 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it true that we can only reach this branch for multibyte characters? My limited knowledge of utf8 would suggest we do the obviously-correct thing:

if mode != Mode::RawByteStr {
	let mut utf8 = String::new();
	utf8.push(c);
	if mode == Mode::Byte && utf8.as_bytes().count() > 1 {
	    // error "this is multibyte"
	} else {
	    // suggest \xHH
	}
}

err.span_label(span, "this multibyte character does not fit into a single byte");
} else if mode != Mode::RawByteStr {
let mut utf8 = String::new();
Expand Down
11 changes: 4 additions & 7 deletions tests/ui/parser/byte-literals.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,10 @@ error: non-ASCII character in byte literal
--> $DIR/byte-literals.rs:10:7
|
LL | b'é';
| ^ must be ASCII
|
help: if you meant to use the unicode code point for 'é', use a \xHH escape
|
LL - b'é';
LL + b'\xE9';
|
| ^
| |
| must be ASCII
| this multibyte character does not fit into a single byte

error[E0763]: unterminated byte constant
--> $DIR/byte-literals.rs:11:6
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/parser/byte-string-literals.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ error: non-ASCII character in byte string literal
LL | b"é";
| ^ must be ASCII
|
help: if you meant to use the unicode code point for 'é', use a \xHH escape
help: if you meant to use the UTF-8 encoding of 'é', use \xHH escapes
|
LL - b"é";
LL + b"\xE9";
LL + b"\xC3\xA9";
|

error: non-ASCII character in raw byte string literal
Expand Down
12 changes: 11 additions & 1 deletion tests/ui/suggestions/multibyte-escapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
fn main() {
b'µ';
//~^ ERROR: non-ASCII character in byte literal
//~| HELP: if you meant to use the unicode code point for 'µ', use a \xHH escape
//~| NOTE: this multibyte character does not fit into a single byte
//~| NOTE: must be ASCII

b'字';
Expand All @@ -15,4 +15,14 @@ fn main() {
//~^ ERROR: non-ASCII character in byte string literal
//~| HELP: if you meant to use the UTF-8 encoding of '字', use \xHH escapes
//~| NOTE: must be ASCII

b"µ";
//~^ ERROR: non-ASCII character in byte string literal
//~| HELP: if you meant to use the UTF-8 encoding of 'µ', use \xHH escapes
//~| NOTE: must be ASCII

b"ñ";
//~^ ERROR: non-ASCII character in byte string literal
//~| HELP: if you meant to use the UTF-8 encoding of 'ñ', use \xHH escapes
//~| NOTE: must be ASCII
}
37 changes: 29 additions & 8 deletions tests/ui/suggestions/multibyte-escapes.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ error: non-ASCII character in byte literal
--> $DIR/multibyte-escapes.rs:4:7
|
LL | b'µ';
| ^ must be ASCII
|
help: if you meant to use the unicode code point for 'µ', use a \xHH escape
|
LL - b'µ';
LL + b'\xB5';
|
| ^
| |
| must be ASCII
| this multibyte character does not fit into a single byte

error: non-ASCII character in byte literal
--> $DIR/multibyte-escapes.rs:9:7
Expand All @@ -31,5 +28,29 @@ LL - b"字";
LL + b"\xE5\xAD\x97";
|

error: aborting due to 3 previous errors
error: non-ASCII character in byte string literal
--> $DIR/multibyte-escapes.rs:19:7
|
LL | b"µ";
| ^ must be ASCII
|
help: if you meant to use the UTF-8 encoding of 'µ', use \xHH escapes
|
LL - b"µ";
LL + b"\xC2\xB5";
|

error: non-ASCII character in byte string literal
--> $DIR/multibyte-escapes.rs:24:7
|
LL | b"ñ";
| ^ must be ASCII
|
help: if you meant to use the UTF-8 encoding of 'ñ', use \xHH escapes
|
LL - b"ñ";
LL + b"\xC3\xB1";
|

error: aborting due to 5 previous errors

Loading