Skip to content

detect when unconstrained type parameters could be resolved by a closure return type #40014

Closed
@nikomatsakis

Description

@nikomatsakis
Contributor

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

sophiajt commented on Feb 21, 2017

@sophiajt
Contributor

I'd probably use a suggestion with a span, rather than trying to fit it. Or... maybe something like:

error[E0101]: cannot determine a type for this expression: unconstrained type
 --> <anon>:2:17
  |
2 |     let _v = || [];
  |              -- ^^ cannot resolve type of expression
                 |
                 needs return type, for example: `|| -> TYPE { [] }`
cengiz-io

cengiz-io commented on Feb 21, 2017

@cengiz-io
Contributor

Hello @nikomatsakis

I'll be working on this.

Thanks for reporting and mentoring!

nikomatsakis

nikomatsakis commented on Feb 21, 2017

@nikomatsakis
ContributorAuthor

@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:

error[E0101]: cannot infer the return type of this closure
 --> <anon>:2:17
  |
2 |     let _v = || [];
  |                 ^^ cannot infer the return type of this closure
  suggestion: add an explicit annotation like `|| -> [XXX; 0] { [] }`
sophiajt

sophiajt commented on Feb 21, 2017

@sophiajt
Contributor

Interesting, how about if the ^^ was the suggestion?

error[E0101]: cannot infer the return type of this closure
 --> <anon>:2:17
  |
2 |     let _v = || [];
  |              ^^ needs return type, for example: `|| -> TYPE { [] }`
sophiajt

sophiajt commented on Feb 21, 2017

@sophiajt
Contributor

I take it back, I think yours is better since the error should be about inference.

nikomatsakis

nikomatsakis commented on Feb 21, 2017

@nikomatsakis
ContributorAuthor

@jonathandturner it does feel like saying the exact same thing (as i wrote it) is suboptimal...

sophiajt

sophiajt commented on Feb 21, 2017

@sophiajt
Contributor

How about?

error[E0101]: cannot infer the return type of this closure
 --> <anon>:2:17
  |
2 |     let _v = || [];
  |              ^^ can't infer return type, use: `|| -> TYPE { [] }`
nikomatsakis

nikomatsakis commented on Feb 22, 2017

@nikomatsakis
ContributorAuthor

Maybe this? Or do we try to avoid "labels" that assume you have read the "main" message?

error[E0101]: cannot infer the return type of this closure
 --> <anon>:2:17
  |
2 |     let _v = || [];
  |              ^^ to specify return type, use: `|| -> TYPE { [] }`
sophiajt

sophiajt commented on Feb 22, 2017

@sophiajt
Contributor

do we try to avoid "labels" that assume you have read the "main" message?

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

nikomatsakis commented on Feb 22, 2017

@nikomatsakis
ContributorAuthor

I am mildly worried that putting the tip 'in line' will also run into trouble because it will tend to wrap.

sophiajt

sophiajt commented on Feb 23, 2017

@sophiajt
Contributor

It could go in attached note...

estebank

estebank commented on Aug 13, 2019

@estebank
Contributor

#63507 (addressing #63506) will cause this case to suggest turning it into a boxed fn trait:

error[E0282]: type annotations needed for the closure
 --> file7.rs:2:17
  |
2 |     let _v = || [];
  |         --      ^^ cannot infer type
  |         |
  |         consider giving `_v` a boxed closure type like `Box<dyn Fn() -> [_; 0]>`

Suggesting annotating the closure itself would probably be better.


Edit: changed to be

error[E0282]: type annotations needed for the closure `fn() -> [_; 0]`
  --> $DIR/suggest-closure-return-type-3.rs:2:17
   |
LL |     let _v = || [];
   |                 ^^ cannot infer type
help: give this closure an explicit return type without `_` placeholders
   |
LL |     let _v = || -> [_; 0] { [] };
   |                 ^^^^^^^^^^^    ^
added a commit that references this issue on Aug 14, 2019

Rollup merge of rust-lang#63507 - estebank:type-inference-error, r=Ce…

d2d49d2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @cengiz-io@nikomatsakis@sophiajt@estebank@Mark-Simulacrum

      Issue actions

        detect when unconstrained type parameters could be resolved by a closure return type · Issue #40014 · rust-lang/rust