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

Suggest the correct array length on mismatch #107173

Merged
merged 1 commit into from
Feb 15, 2023

Conversation

clubby789
Copy link
Contributor

Fixes #107156

I wasn't able to find a way to get the Span for the actual array size unfortunately, so this suggestion can't be applied automatically.

@rustbot label +A-diagnostics

@rustbot
Copy link
Collaborator

rustbot commented Jan 21, 2023

r? @estebank

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-diagnostics Area: Messages for errors, warnings, and lints labels Jan 21, 2023
@clubby789
Copy link
Contributor Author

Actually, looking at the changes in tests, it might not be best to do this so generally 🤔 . It only really applies when the array is being bound and the type doesn't match. I'm not sure if the information is available here to only lint on those cases.

@compiler-errors
Copy link
Member

Hmmm... yeah, I would look at check_decl, but I agree that this help right now does not really add much right now.

@rustbot author
r? @compiler-errors

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 22, 2023
@rustbot rustbot assigned compiler-errors and unassigned estebank Jan 22, 2023
@clubby789 clubby789 force-pushed the suggest-array-length branch 2 times, most recently from cff21bb to 804c4a0 Compare January 22, 2023 22:59
@clubby789
Copy link
Contributor Author

Reusing some code from the original array length suggestion mentioned https://github.com/rust-lang/rust/blob/master/compiler/rustc_hir_typeck/src/expr.rs#L1339, this now suggests the correct type for constants. It would be nice to have this also work for let arr: [u8; 2] = [1, 2, 3] which I'll work on

@clubby789
Copy link
Contributor Author

clubby789 commented Feb 12, 2023

This now handles let as well as const mismatches.
Some issues:

LL |     let wrong: [u8; 3] = returns_arr();
   |                -------   ^^^^^^^^^^^^^ expected an array with a fixed size of 3 elements, found one with 2 elements
   |                |    |
   |                |    help: consider specifying the actual array length: `2`
   |                expected due to this

'expected due to this' is emitted much further along the line and overlaps which isn't hugely readable, but IDEs should handle it fine which is the requested use case.

for let foo: [u8; 3] = [1, 2], I search for a Local Stmt where there is an initializer with a span matching the error cause, and an explicit type. This does mean that if the initializer is seperate from the let statement it won't work, but it means we don't need to worry about cases like this

    let blah: [u8; 4];
    if true {
        blah = [1];
    } else {
        blah = [3, 4];
    }

(also, I notice that the expected due to this annotation is missing if the let and assignment are seperate.)

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 12, 2023
@compiler-errors
Copy link
Member

Hmmm... I guess this is fine. Maybe we should add an ObligationCauseCode for a let with ascribed type so we don't need to do that HIR visiting.

@bors r+

@bors
Copy link
Contributor

bors commented Feb 13, 2023

📌 Commit 903ca87 has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 13, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 14, 2023
…compiler-errors

Suggest the correct array length on mismatch

Fixes rust-lang#107156

I wasn't able to find a way to get the `Span` for the actual array size unfortunately, so this suggestion can't be applied automatically.

`@rustbot` label +A-diagnostics
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 15, 2023
Rollup of 7 pull requests

Successful merges:

 - rust-lang#105300 (rework min_choice algorithm of member constraints)
 - rust-lang#107163 (Remove some superfluous type parameters from layout.rs.)
 - rust-lang#107173 (Suggest the correct array length on mismatch)
 - rust-lang#107411 (Handle discriminant in DataflowConstProp)
 - rust-lang#107968 (Enable `#[thread_local]` on armv6k-nintendo-3ds)
 - rust-lang#108032 (Un📦ing the Resolver)
 - rust-lang#108060 (Revert to using `RtlGenRandom` as a fallback)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit a110cf5 into rust-lang:master Feb 15, 2023
@rustbot rustbot added this to the 1.69.0 milestone Feb 15, 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 S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

E0308 type mismatch for arrays doens't offer to fix the length (unlike E0658)
5 participants