Rollup merge of #107709 - tialaramex:master, r=compiler-errors

Fix problem noticed in PR106859 with char -> u8 suggestion

HN reader `@ayosec` noticed that my #106859 a few weeks back, malfunctions if you have a Unicode escape, the code suggested b'\u{0}' if you tried to use '\u{0}' where a byte should be, when of course b'\u{0}' is not a byte literal, regardless of the codepoint you can't write Unicode escapes in a byte literal at all.

My proposed fix here just checks that the "character" you wrote is fewer than 5 bytes, thus allowing \x7F and similar escapes but conveniently forbidding even the smallest Unicode escape \u{0} before offering the suggestion as before.

I have provided an updated test which includes examples which do and don't work because of this additional rule.
This commit is contained in:
Matthias Krüger 2023-02-07 17:57:17 +01:00 committed by GitHub
commit 0e3af6af47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 3 deletions

View file

@ -1922,7 +1922,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
(ty::Uint(ty::UintTy::U8), ty::Char) => {
if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span)
&& let Some(code) = code.strip_prefix('\'').and_then(|s| s.strip_suffix('\''))
&& code.chars().next().map_or(false, |c| c.is_ascii())
&& !code.starts_with("\\u") // forbid all Unicode escapes
&& code.chars().next().map_or(false, |c| c.is_ascii()) // forbids literal Unicode characters beyond ASCII
{
err.span_suggestion(
span,

View file

@ -12,7 +12,19 @@ fn main() {
//~^ ERROR: mismatched types [E0308]
//~| HELP: if you meant to write a byte literal, prefix with `b`
let _a: u8 = '\x20';
//~^ ERROR: mismatched types [E0308]
//~| HELP: if you meant to write a byte literal, prefix with `b`
// Do not issue the suggestion if the char literal is a Unicode escape
foo('\u{0080}');
//~^ ERROR: mismatched types [E0308]
// Do not issue the suggestion if the char literal isn't ASCII
let _t: u8 = '€';
//~^ ERROR: mismatched types [E0308]
// Do not issue the suggestion if the char literal isn't ASCII
foo('\u{1f980}');
//~^ ERROR: mismatched types [E0308]
}

View file

@ -30,13 +30,54 @@ LL | foo(b'#');
| ~~~~
error[E0308]: mismatched types
--> $DIR/type-mismatch-byte-literal.rs:16:18
--> $DIR/type-mismatch-byte-literal.rs:15:18
|
LL | let _a: u8 = '\x20';
| -- ^^^^^^ expected `u8`, found `char`
| |
| expected due to this
|
help: if you meant to write a byte literal, prefix with `b`
|
LL | let _a: u8 = b'\x20';
| ~~~~~~~
error[E0308]: mismatched types
--> $DIR/type-mismatch-byte-literal.rs:20:9
|
LL | foo('\u{0080}');
| --- ^^^^^^^^^^ expected `u8`, found `char`
| |
| arguments to this function are incorrect
|
note: function defined here
--> $DIR/type-mismatch-byte-literal.rs:4:4
|
LL | fn foo(_t: u8) {}
| ^^^ ------
error[E0308]: mismatched types
--> $DIR/type-mismatch-byte-literal.rs:24:18
|
LL | let _t: u8 = '€';
| -- ^^^ expected `u8`, found `char`
| |
| expected due to this
error: aborting due to 3 previous errors
error[E0308]: mismatched types
--> $DIR/type-mismatch-byte-literal.rs:28:9
|
LL | foo('\u{1f980}');
| --- ^^^^^^^^^^^ expected `u8`, found `char`
| |
| arguments to this function are incorrect
|
note: function defined here
--> $DIR/type-mismatch-byte-literal.rs:4:4
|
LL | fn foo(_t: u8) {}
| ^^^ ------
error: aborting due to 6 previous errors
For more information about this error, try `rustc --explain E0308`.