-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Misleading error message originating from the fact that closure types are not inferred being polymorphic over lifetimes. (And related errors in need of improvement.) #71209
Comments
Regarding the last point, it is trying to suggest you to give |
Partly addresses rust-lang#71209.
On `FnDef` type annotation suggestion, use fn-pointer output Address the last point in rust-lang#71209.
No change to the first case. Current output for the second case:
The last case is
|
…ffected inference Mitigate part of rust-lang#71209. ``` error[E0308]: mismatched types --> $DIR/unboxed-closures-type-mismatch.rs:30:18 | LL | identity(1u16); | -------- ^^^^ expected `u8`, found `u16` | | | arguments to this function are incorrect | note: expected because the closure was earlier called with an argument of type `u8` --> $DIR/unboxed-closures-type-mismatch.rs:29:18 | LL | identity(1u8); | -------- ^^^ expected because this argument is of type `u8` | | | in this closure call note: closure parameter defined here --> $DIR/unboxed-closures-type-mismatch.rs:28:25 | LL | let identity = |x| x; | ^ help: change the type of the numeric literal from `u16` to `u8` | LL | identity(1u8); | ~~ ```
…ffected inference Mitigate part of rust-lang#71209. ``` error[E0308]: mismatched types --> $DIR/unboxed-closures-type-mismatch.rs:30:18 | LL | identity(1u16); | -------- ^^^^ expected `u8`, found `u16` | | | arguments to this function are incorrect | note: expected because the closure was earlier called with an argument of type `u8` --> $DIR/unboxed-closures-type-mismatch.rs:29:18 | LL | identity(1u8); | -------- ^^^ expected because this argument is of type `u8` | | | in this closure call note: closure parameter defined here --> $DIR/unboxed-closures-type-mismatch.rs:28:25 | LL | let identity = |x| x; | ^ help: change the type of the numeric literal from `u16` to `u8` | LL | identity(1u8); | ~~ ```
…n, r=petrochenkov On type error of closure call argument, point at earlier calls that affected inference Mitigate part of rust-lang#71209. When we encounter a type error on a specific argument of a closure call argument, where the closure's definition doesn't have a type specified, look for other calls of the closure to try and find the specific call that cased that argument to be inferred of the expected type. ``` error[E0308]: mismatched types --> $DIR/unboxed-closures-type-mismatch.rs:30:18 | LL | identity(1u16); | -------- ^^^^ expected `u8`, found `u16` | | | arguments to this function are incorrect | note: expected because the closure was earlier called with an argument of type `u8` --> $DIR/unboxed-closures-type-mismatch.rs:29:18 | LL | identity(1u8); | -------- ^^^ expected because this argument is of type `u8` | | | in this closure call note: closure parameter defined here --> $DIR/unboxed-closures-type-mismatch.rs:28:25 | LL | let identity = |x| x; | ^ help: change the type of the numeric literal from `u16` to `u8` | LL | identity(1u8); | ~~ ```
Rollup merge of rust-lang#116250 - estebank:closure-arg-inference-span, r=petrochenkov On type error of closure call argument, point at earlier calls that affected inference Mitigate part of rust-lang#71209. When we encounter a type error on a specific argument of a closure call argument, where the closure's definition doesn't have a type specified, look for other calls of the closure to try and find the specific call that cased that argument to be inferred of the expected type. ``` error[E0308]: mismatched types --> $DIR/unboxed-closures-type-mismatch.rs:30:18 | LL | identity(1u16); | -------- ^^^^ expected `u8`, found `u16` | | | arguments to this function are incorrect | note: expected because the closure was earlier called with an argument of type `u8` --> $DIR/unboxed-closures-type-mismatch.rs:29:18 | LL | identity(1u8); | -------- ^^^ expected because this argument is of type `u8` | | | in this closure call note: closure parameter defined here --> $DIR/unboxed-closures-type-mismatch.rs:28:25 | LL | let identity = |x| x; | ^ help: change the type of the numeric literal from `u16` to `u8` | LL | identity(1u8); | ~~ ```
The second case is now handled:
And as pointed above the third one is as well. I don't know how we might be able to address the first one at this time. |
The following code produces an error message that doesn’t at all explain the actual problem.
(Playground)
Errors:
In particular looking at this statement – “returns a value referencing data owned by the current function” – that’s just not what is going on here.
What really is going on here (as far as I can tell) is that
identity
is inferred as some closure implementingFn(A) -> A
forA
to be determined, then the first callidentity("result!")
tells the type inference thatA
is&'a str
for some'a
to be determined, and finallyidentity(&local_string)
already knows to apply unsized coercion and typechecks nailing'a
down to the lifetime oflocal_string
. This meansresult
is an&'a str
with that same lifetime.A bit less confusing of an error message but still suboptimal IMO is what you get when trying to use a closure truly polymorphically:
(Playground)
Errors:
I think this could be improved by a message explaining where the expected type
u8
comes from.Back to the original example.. in my opinion it would be nice if the example would not error at all. After all the following does work:
However, this again doesn’t work:
Finally, on the topic of trying to get more polymorphism out of closures than they support, I ran into this error message (which I could move into a separate issue if you guys think that it’s completely unrelated):
(Playground)
Errors:
Which is just weird, because: What is this
fn() -> A {f::<A>}
syntax supposed to mean?@rustbot modify labels to T-compiler, T-lang, A-diagnostics, D-incorrect, D-terse, D-papercut, C-bug, C-enhancement, A-closures, A-inference.
The text was updated successfully, but these errors were encountered: