Skip to content
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

"Try invoking the function" hint #63100

Open
Patryk27 opened this issue Jul 29, 2019 · 10 comments

Comments

@Patryk27
Copy link

commented Jul 29, 2019

Hi,

Let's consider following code:

fn foo() -> usize {
    0
}

fn bar(i: usize) {
    
}

fn main() {
    bar(foo);
}

Right now it fails with the expected usize, found fn item message - I think it would be helpful if compiler bragged about missing parentheses when function's return type matches expected type:

fn main() {
    bar(foo);
//      ^^^ expected usize, found fn item
//          hint: try invoking the function: foo()
}

It may even help a little bit when dealing with async functions - recently I've written:

async fn foo() {
    
}

fn bar(f: impl Future<Output=()>) {
    
}

fn main() {
    bar(foo);
}

... and it took me a few minutes to notice that what I actually wanted was bar(foo()).

@cramertj

This comment has been minimized.

Copy link
Member

commented Jul 29, 2019

@Centril why was this tagged async-await?

@Centril

This comment has been minimized.

Copy link
Member

commented Jul 29, 2019

@Centril why was this tagged async-await?

It may even help a little bit when dealing with async functions - recently I've written:

@cramertj

This comment has been minimized.

Copy link
Member

commented Jul 30, 2019

I don't think that makes this an async/await issue in any way, though. 🤷‍♀

@gilescope

This comment has been minimized.

Copy link
Contributor

commented Jul 31, 2019

It's a great suggestion either way. Going to take a crack at it if I may?
@rustbot claim

@gilescope

This comment has been minimized.

Copy link
Contributor

commented Aug 17, 2019

Move to close this as PR #63337 now merged in nightly fixes this:

   |
2  | fn foo() -> usize {
   | ----------------- fn() -> usize {foo} defined here
...
11 |     bar(foo);
   |         ^^^
   |         |
   |         expected usize, found fn item
   |         help: use parentheses to call this function: `foo()`
   |
   = note: expected type `usize`
              found type `fn() -> usize {foo}`
@gilescope

This comment has been minimized.

Copy link
Contributor

commented Aug 17, 2019

@Centril thoughts?

@Centril

This comment has been minimized.

Copy link
Member

commented Aug 17, 2019

The message for async/await is still bad:


error[E0277]: the trait bound `fn() -> impl std::future::Future {foo}: std::future::Future` is not satisfied
  --> src/main.rs:10:5
   |
10 |     bar(foo);
   |     ^^^ the trait `std::future::Future` is not implemented for `fn() -> impl std::future::Future {foo}`
   |
note: required by `bar`
  --> src/main.rs:7:1
   |
7  | fn bar(f: impl Future<Output = ()>) {}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Leaving this up to @estebank

@Patryk27

This comment has been minimized.

Copy link
Author

commented Aug 17, 2019

IMO the {foo} defined here part looks weird (why curly braces?) - how about:

2  | fn foo() -> usize {
   | ----------------- function `fn() -> usize` (named `foo`) defined here
@estebank

This comment has been minimized.

Copy link
Contributor

commented Aug 19, 2019

@Patryk27 I feel like fn foo() -> usize could also work and be more natural. At the same time, I'd like to unify how we refer to functions and closures (currently [closure@file.rs:LL:CC]), and I feel that the braces style is would be suitable for them: fn(_) -> _ {file.rs:LL::CC}.

@Patryk27

This comment has been minimized.

Copy link
Author

commented Aug 20, 2019

@estebank Thanks, I understand - I'm not certain about curly braces in this position, since it changes their meaning though (from denoting block to denoting location, depending on whether it is part of code block or message) and thus, to me, they are kinda confusing.

IMO the fn(_) -> _ [file.rs:LL:CC] construct could be more legible, since brackets are not usually set together with functions (contrary to curly braces), so they stand out more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.