-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Bad advice given for arr[0i32.into()] #107292
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
Comments
This comment was marked as resolved.
This comment was marked as resolved.
|
Oof. I actually already knew that, since I have a bunch of However, that's not the whole explanation. fn main() {
let arr: [u32; 3] = [0, 1, 2];
let idx = 0u8;
let val: u32 = arr[idx.into()];
println!("{}", val);
}
(Also, for some reason |
It seems like this issue is already fixed on nightly:
|
Not completely, that's just a different rearrangement of syntax that doesn't compile: Compiling playground v0.0.1 (/playground)
error[[E0412]](https://doc.rust-lang.org/nightly/error-index.html#E0412): cannot find type `T` in this scope
--> src/main.rs:4:37
|
4 | println!("{}", arr[<i32 as Into<T>>::into(idx)]);
| ^ not found in this scope
For more information about this error, try `rustc --explain E0412`.
error: could not compile `playground` due to previous error |
@d0sboots the compiler is giving you the syntax that you can use to specify what you're turning into, but it can't guess which type you want to convert to. Until #114811 we weren't showing you the full list because it was too long, but after it the output is:
You need to substitute The original report with the mentioned PR adds the list of impls to the output:
Only outstanding work is to have rustc understand |
So, there's one thing I still don't understand with this. Array indexing is always a Because of this, the real code change here (for the non-working code sample) IMO is to assign to a temporary variable, so that the type inference works better. |
Correct, the indexing operation doesn't (today) influence inference in the way that we would need for this to work. |
Code
Current output
Desired output
I'm too new to Rust to say exactly, but:
idx.into()
expression, not the outer expression it is highlighting::<T>
, which is not syntactically valid hereRationale and extra context
Naively, one one expect that the requirement of array access being
usize
means aninto()
would work here. Clearly, that's not the case but I'm still not sure why.Other cases
still gives bad advice, although less catastrophically:
Only when you take that bad advice, but with an explicit type instead of _, do you get a useful message:
Anything else?
No response
The text was updated successfully, but these errors were encountered: