Unclear errors with type annotations on functions instead of their traits #42226
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
A-suggestion-diagnostics
Area: Suggestions generated by the compiler applied by `cargo fix`
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
D-invalid-suggestion
Diagnostics: A structured suggestion resulting in incorrect code.
D-newcomer-roadblock
Diagnostics: Confusing error or lint; hard to understand for new users.
E-medium
Call for participation: Medium difficulty. Experience needed to fix: Intermediate.
P-medium
Medium priority
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Uh oh!
There was an error while loading. Please reload this page.
A common problem I run into is that I add type annotations to
into
where it should really be on theInto
trait instead. The error messages given for this are not helpful because they don't suggest adding type annotations to the Trait and then using UFCS instead of method syntax.So what happens is that I write my code thinking
rustc
can figure out the types, but it fails. A rough example is as follows (on play.rust-lang.org):So I think that
rustc
could infer all the types here, but it fails with:Okay, that's fine, I can add some type annotations then. Now I only frequently work with type annotations on functions, so forgetting that the type annotation for
Into
is on the trait and not the function, I replace thelet x...
line above withlet x = y.into::<u32>() * 512u32;
(on play.rust-lang.org) and get the following error:Another thing I end up trying is to explicitly specify the type of
x
by changing thelet x...
line tolet x: u32 = y.into() * 512u32;
, but you get the same initial error (E0283).So a correct solution to this problem is to add the type annotations to
Into
like solet x = Into::<u32>::into(y) * 512u32;
So my proposal here is twofold: 1) allow type inference to work for this example (which is quite reminiscent of code that I write) and 2) improve both E0283 and E0035.
For point 2, I think it would be possible to detect this exact situation and specify the correct solution.If the compiler sees type annotations on a function that doesn't have any, check the trait it's from and see if the trait itself takes type parameters. You could ignore the number of parameters specified to make this simpler and change E0035 to have help text at the bottom like:
Something similar can be done for E0283. I don't know if there's a way that the compiler could tell if a type annotation on the output would work or if it has to be an input type annotation on the right-hand side, but I think defaulting to specifying the type directly would be the right solution anyways. Again I think the heuristic should be if there is a function that doesn't have a type annotation but its trait does then it should add help text to the error similar to that for E0035:
Note: These examples were run on
rustc 1.17.0 (56124baa9 2017-04-24)
The text was updated successfully, but these errors were encountered: