Skip to content
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

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

Closed
aaay-aaay opened this issue Apr 7, 2023 · 0 comments · Fixed by #114322
Closed

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

aaay-aaay opened this issue Apr 7, 2023 · 0 comments · Fixed by #114322
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@aaay-aaay
Copy link

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

@aaay-aaay aaay-aaay added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 7, 2023
@jyn514 jyn514 added the D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. label Apr 17, 2023
@bors bors closed this as completed in 1778c58 Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants