Skip to content

Invalid "convert the array to a slice" suggestion #110063

Closed
@aaay-aaay

Description

@aaay-aaay

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-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