Description
Code
trait Test {}
impl Test for &[u8] {}
fn needs_test<T: Test>() -> T {
panic!()
}
fn example() {
needs_test::<[u8; 1]>();
let x: [u8; 1] = needs_test();
}
Current output
error[E0277]: the trait bound `[u8; 1]: Test` is not satisfied
--> src/lib.rs:9:18
|
9 | needs_test::<[u8; 1]>();
| ^^^^^^^ the trait `Test` is not implemented for `[u8; 1]`
|
= help: the trait `Test` is implemented for `&[u8]`
note: required by a bound in `needs_test`
--> src/lib.rs:4:18
|
4 | fn needs_test<T: Test>() -> T {
| ^^^^ required by this bound in `needs_test`
help: convert the array to a `&[u8]` slice instead
|
9 | needs_test::<&[u8; 1][..]>();
| + ++++
error[E0277]: the trait bound `[u8; 1]: Test` is not satisfied
--> src/lib.rs:10:22
|
10 | let x: [u8; 1] = needs_test();
| ^^^^^^^^^^ the trait `Test` is not implemented for `[u8; 1]`
|
= help: the trait `Test` is implemented for `&[u8]`
note: required by a bound in `needs_test`
--> src/lib.rs:4:18
|
4 | fn needs_test<T: Test>() -> T {
| ^^^^ required by this bound in `needs_test`
help: convert the array to a `&[u8]` slice instead
|
10 | let x: [u8; 1] = &needs_test[..]();
| + ++++
For more information about this error, try `rustc --explain E0277`.
Desired output
error[E0277]: the trait bound `[u8; 1]: Test` is not satisfied
--> src/lib.rs:9:18
|
9 | needs_test::<[u8; 1]>();
| ^^^^^^^ the trait `Test` is not implemented for `[u8; 1]`
|
= help: the trait `Test` is implemented for `&[u8]`
note: required by a bound in `needs_test`
--> src/lib.rs:4:18
|
4 | fn needs_test<T: Test>() -> T {
| ^^^^ required by this bound in `needs_test`
error[E0277]: the trait bound `[u8; 1]: Test` is not satisfied
--> src/lib.rs:10:22
|
10 | let x: [u8; 1] = needs_test();
| ^^^^^^^^^^ the trait `Test` is not implemented for `[u8; 1]`
|
= help: the trait `Test` is implemented for `&[u8]`
note: required by a bound in `needs_test`
--> src/lib.rs:4:18
|
4 | fn needs_test<T: Test>() -> T {
| ^^^^ required by this bound in `needs_test`
For more information about this error, try `rustc --explain E0277`.
Rationale and extra context
Both of these suggestions result in invalid code, and don't really make much sense. This suggestion should only be emitted when it's about an expression of type [T; N]
, and not a trait bound that arises some other way (like the type being explicitly written in the code).
Other cases
No response
Anything else?
This invalid suggestion also occurs in serde autoderived implementations of Deserialize, if a field of the struct/enum is an array with more than 32 elements. (This is the context where I initially discovered this, the MVE above is a reverse-engineering of that case).
This seems to only happen in nightly. I think this is just because this suggestion was added after 1.68.0 was released, or something like that.
The commit that introduced this suggestion (and also the bug) is 8ac7d0e.
Playground link: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=ec4aeb169fe3a41cfecf995c8cd66150