Closed
Description
Building on the changes in #39361, it'd be nice to suggest when the user ought to annotate a closure return type, as I think many users are not even aware this is possible. Example:
fn main() {
let _v = || [];
}
I'd like to suggest something like :
error[E0101]: cannot determine a type for this expression: unconstrained type
--> <anon>:2:17
|
2 | let _v = || [];
| -- ^^ cannot resolve type of expression
|
consider annotating the return type of this closure like so `|| -> TYPE { [] }`
Not sure the best way to phrase that yet. =)
cc @cengizio -- interested in following up on this? I can help mentor it.
cc @estebank @jonathandturner -- any thoughts on how to phrase the suggestion?
Activity
sophiajt commentedon Feb 21, 2017
I'd probably use a suggestion with a span, rather than trying to fit it. Or... maybe something like:
cengiz-io commentedon Feb 21, 2017
Hello @nikomatsakis
I'll be working on this.
Thanks for reporting and mentoring!
nikomatsakis commentedon Feb 21, 2017
@jonathandturner so actually I'm not sure that the main
^^
adds any value here. Maybe something like "cannot infer the return type for this closure" as the "main" error, combined with a "suggestion" for how to annotate:sophiajt commentedon Feb 21, 2017
Interesting, how about if the ^^ was the suggestion?
sophiajt commentedon Feb 21, 2017
I take it back, I think yours is better since the error should be about inference.
nikomatsakis commentedon Feb 21, 2017
@jonathandturner it does feel like saying the exact same thing (as i wrote it) is suboptimal...
sophiajt commentedon Feb 21, 2017
How about?
nikomatsakis commentedon Feb 22, 2017
Maybe this? Or do we try to avoid "labels" that assume you have read the "main" message?
sophiajt commentedon Feb 22, 2017
Generally, yeah. I did a quick survey when we were doing the error message redesign, and most people saw the label first. So I just assume people don't see the main message now, to be on the safe side.
nikomatsakis commentedon Feb 22, 2017
I am mildly worried that putting the tip 'in line' will also run into trouble because it will tend to wrap.
sophiajt commentedon Feb 23, 2017
It could go in attached note...
estebank commentedon Aug 13, 2019
#63507 (addressing #63506) will cause this case to suggest turning it into a boxed fn trait:
Suggesting annotating the closure itself would probably be better.
Edit: changed to be
Rollup merge of rust-lang#63507 - estebank:type-inference-error, r=Ce…