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

Incorrect "consider slicing" suggestion with Vec inside a struct #120605

Closed
AaronC81 opened this issue Feb 3, 2024 · 1 comment · Fixed by #121236
Closed

Incorrect "consider slicing" suggestion with Vec inside a struct #120605

AaronC81 opened this issue Feb 3, 2024 · 1 comment · Fixed by #121236
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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

@AaronC81
Copy link
Contributor

AaronC81 commented Feb 3, 2024

Code

pub struct Struct {
    a: Vec<Struct>,
}

impl Struct {
    pub fn test(&self) {
        if let [Struct { a: [] }] = &self.a[..] {
            println!("matches!")   
        }
    }
}

Current output

error[E0529]: expected an array or slice, found `Vec<Struct>`
  --> src/main.rs:47:29
   |
47 |         if let [Struct { a: [] }] = &self.a[..] {
   |                             ^^      ----------- help: consider slicing here: `&self.a[..][..]`
   |                             |
   |                             pattern cannot match with input type `Vec<Struct>`

Desired output

error[E0529]: expected an array or slice, found `Vec<Struct>`
  --> src/main.rs:47:29
   |
47 |         if let [Struct { a: [] }] = &self.a[..] {
   |                             ^^
   |                             |
   |                             pattern cannot match with input type `Vec<Struct>`

Rationale and extra context

The suggestion to try &self.a[..][..] does not work, and should not be included. If you do try to follow this suggestion, the error then suggests &self.a[..][..][..], then &self.a[..][..][..][..], and so on.

Other cases

No response

Rust Version

rustc 1.77.0-nightly (88189a71e 2024-01-19)
binary: rustc
commit-hash: 88189a71e4e4376eea82ac61db6a539612eb200a
commit-date: 2024-01-19
host: aarch64-apple-darwin
release: 1.77.0-nightly
LLVM version: 17.0.6

(Also repros on latest nightly using the Playground)

Anything else?

No response

@AaronC81 AaronC81 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 Feb 3, 2024
@fmease fmease added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Feb 3, 2024
@long-long-float
Copy link
Contributor

@rustbot claim

@bors bors closed this as completed in 3ec2b7b Mar 17, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 17, 2024
Rollup merge of rust-lang#121236 - long-long-float:rust-fix-consider-slicing, r=Nadrieril

Don't show suggestion if slice pattern is not top-level

Close rust-lang#120605

Don't show suggestion to add slicing (`[..]`) if the slice pattern is enclosed by struct like `Struct { a: [] }`.

For example, current rustc makes a suggestion as a comment. However, the pattern `a: []` is wrong, not scrutinee `&self.a`.
In this case, the structure type `a: Vec<Struct>` and the pattern `a: []` are different so I think the pattern should be fixed, not the scrutinee.
If the parent of the pattern that was the target of the error is a structure, I made the compiler not show a suggestion.

```rs
pub struct Struct {
    a: Vec<Struct>,
}

impl Struct {
    pub fn test(&self) {
        if let [Struct { a: [] }] = &self.a {
//             ^^^^^^^^^^^^^^^^^^   ------- help: consider slicing here: `&self.a[..]`
            println!("matches!")
        }
    }
}
```

Note:

* ~~I created `PatInfo.history` to store parent-child relationships for patterns, but this may be inefficient.~~
  * I use two fields `parent_kind` and `current_kind` instead of vec. It may not performance issue.
* Currently only looking at direct parents, but may need to look at deeper ancestry.
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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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.

3 participants