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

"Insert explicit type" suggestion produces invalid code for array #2922

Closed
jplatte opened this issue Jan 28, 2020 · 2 comments · Fixed by #8813
Closed

"Insert explicit type" suggestion produces invalid code for array #2922

jplatte opened this issue Jan 28, 2020 · 2 comments · Fixed by #8813
Labels
A-assists S-actionable Someone could pick this issue up and work on it right now

Comments

@jplatte
Copy link
Contributor

jplatte commented Jan 28, 2020

For arrays, ra produces an invalid suggestion to insert [T;_], where T is a concrete type, but _ is literally an underscore, which is not valid in that position.

Example:

let x = [0.0; 3];

produces a suggestion that when applied results in

let x: [f64;_] = [0.0; 3];

which leads to the error message

error: expected expression, found reserved identifier `_`
   --> src/file.rs:line:col
    |
col |             let x: [f64;_] = [0.0; 3];
    |                 -       ^ expected expression
    |                 |
    |                 while parsing the type for `x`
@lnicola
Copy link
Member

lnicola commented Jan 28, 2020

Not that I disagree that this is a bug, but wasn't there an RFC to allow underscores for array lengths? It seems like a nice feature.

EDIT: rust-lang/rfcs#2545

@jplatte
Copy link
Contributor Author

jplatte commented Jan 28, 2020

You were a bit faster than I was! ^^

But that RFC hasn't been merged yet, and apparently also hasn't been implemented, considering I get that error message on Nightly.

@lnicola lnicola added A-assists S-actionable Someone could pick this issue up and work on it right now labels Jan 25, 2021
lf- added a commit to lf-/rust-analyzer that referenced this issue May 14, 2021
Fix rust-lang#2922: add unknown length as a condition for a type having unknown.

Incorporate reviews by @flodiebold:

* Extract some of the const evaluation workings into functions
* Add fixmes on the hacks
* Add tests for impls on specific array lengths (these work!!! 😁)
* Add tests for const generics (indeed we don't support it yet)
lf- added a commit to lf-/rust-analyzer that referenced this issue May 14, 2021
Fix rust-lang#2922: add unknown length as a condition for a type having unknown.

Incorporate reviews:

* Extract some of the const evaluation workings into functions
* Add fixmes on the hacks
* Add tests for impls on specific array lengths (these work!!! 😁)
* Add tests for const generics (indeed we don't support it yet)
bors bot added a commit that referenced this issue May 16, 2021
8813: Get some more array lengths! r=lf- a=lf-

This is built on #8799 and thus contains its changes. I'll rebase it onto master when that one gets merged. It adds support for r-a understanding the length of:

* `let a: [u8; 2] = ...`
* `let a = b"aaa"`
* `let a = [0u8; 4]`

I have added support for getting the values of byte strings, which was not previously there. I am least confident in the correctness of this part and it probably needs some more tests, as we currently have only one test that exercised that part (!).

Fixes #2922.

Co-authored-by: Jade <[email protected]>
@bors bors bot closed this as completed in 78d6b88 May 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-assists S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants