Fix error reporting for multibyte characters in byte string literal#139362
Fix error reporting for multibyte characters in byte string literal#139362tapanprakasht wants to merge 3 commits intorust-lang:mainfrom
Conversation
|
r? @Nadrieril rustbot has assigned @Nadrieril. Use |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| Applicability::MaybeIncorrect, | ||
| ); | ||
| } else if mode == Mode::Byte { | ||
| if mode == Mode::Byte { |
There was a problem hiding this comment.
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,
);
}
There was a problem hiding this comment.
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.
| Applicability::MaybeIncorrect, | ||
| ); | ||
| } else if mode == Mode::Byte { | ||
| if mode == Mode::Byte { |
There was a problem hiding this comment.
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
}
}|
Cf my comments I'm not yet confident this is implemented correctly, but I'm ok with the idea that we should only suggest utf8-encoded things for the sake of consistency. cc @davidtwco because you merged #87659 that introduced the previous behavior. |
|
@rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
|
@tapanprakasht |
Fixes #139226