Open spikespaz opened 2 years ago
Hi, @spikespaz I want to contribute to this repo. Can you please me in which file this code is?
@ahmadjubair33 This is a MCVE, the original problematic code can be found here: https://github.com/spikespaz/sorbet-rs/blob/0124efd4a3e1695d47db9c8ccf1c1b6112f4073e/sorbet-color/src/css.rs#L91
This is the only conversation I could find on this topic so far, but that's surprising to me:
https://internals.rust-lang.org/t/improved-type-inference-for-operators/5393/4
I guess it might be a challenge if there are multiple implementations of the required trait (like Div<f64>
in this case).
Also, I don't think Rust would infer this type anyway, but it gives a clearer error message without the try.
fn main() {
let _: f64 = "5".parse::<_>().unwrap() / 5.0_f64;
}
Compiling playground v0.0.1 (/playground)
error[E0282]: type annotations needed
--> src/main.rs:2:30
|
2 | let _: f64 = "5".parse::<_>().unwrap() / 5.0_f64;
| ^ cannot infer type
For more information about this error, try `rustc --explain E0282`.
Here's a standalone reproducer:
use std::any::type_name;
fn generic<T>() -> Option<T>
{
println!("{}", type_name::<T>());
panic!()
}
fn concrete() -> Option<usize> {
generic()?;
Some(0)
}
fn main() {
concrete().unwrap();
}
Here, T
is inferred to be ()
for no obvious reason. I expected this to produce a compile error along the lines of "type annotations needed".
I can also reproduce this all the way back to Rust 1.13, so this is not due to try_trait_v2
as I had initially assumed.
no obvious reason
fn main()
is really fn main() -> ()
, so the call to unwrap
must (unambiguously!) return ()
, meaning that T
must be ()
Do it differently
fn main() {
let _ = concrete().unwrap();
}
and you'll get an ambiguity and the expected error.
UPD: I realize I was incorrect, because the ;
after unwrap
already disconnects the return type of the main
from the result of unwrap
Given the following code:
Playground
The current output is:
Ideally the output should look like:
If a type is provided to
str::parse
via the turbofish, rather than leaving it anonymous as_
, the code compiles well.