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

When calling a function that is actually a method, mention it #102518

Open
estebank opened this issue Sep 30, 2022 · 7 comments · Fixed by #102694
Open

When calling a function that is actually a method, mention it #102518

estebank opened this issue Sep 30, 2022 · 7 comments · Fixed by #102694
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Sep 30, 2022

Given code like cos(3.3); where cos is a method of f64, we currently look for free functions elsewhere in the dependency tree:

error[E0425]: cannot find function `cos` in this scope
 --> src/main.rs:2:13
  |
2 |     let _ = cos(3.3);
  |             ^^^ not found in this scope
  |
help: consider importing this function
  |
1 | use libm::cos;
  |

But we should also check for existence of methods under that name for its sole argument.

error[E0425]: cannot find function `cos` in this scope
 --> src/main.rs:2:13
  |
2 |     let _ = cos(3.3);
  |             ^^^ not found in this scope
  |
help: you might have meant to call method `f64::cos`
  |
2 |     let _ = 3.3.cos();
  |             ~~~~~~~~~

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Sep 30, 2022
@chenyukang
Copy link
Member

This error is reported in the phase of name resolving,
for func(obj), seems we need to know the type of obj and then check whether it has func,
This seems need to be in type checking phase?

@compiler-errors
Copy link
Member

Made some progress:

error[E0425]: cannot find function `len` in this scope
 --> /home/gh-compiler-errors/test2.rs:2:13
  |
2 |     let x = len([1i32]);
  |             ^^^ not found in this scope
  |
help: use the `.` operator to call method `len` on `&[i32]`
  |
2 -     let x = len([1i32]);
2 +     let x = [1i32].len();
  |

@rustbot claim

@chenyukang
Copy link
Member

Made some progress:

error[E0425]: cannot find function `len` in this scope
 --> /home/gh-compiler-errors/test2.rs:2:13
  |
2 |     let x = len([1i32]);
  |             ^^^ not found in this scope
  |
help: use the `.` operator to call method `len` on `&[i32]`
  |
2 -     let x = len([1i32]);
2 +     let x = [1i32].len();
  |

@rustbot claim

The code in the issue seems still does not report new suggestion:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=1b4aad8d1252936ed279ddb2f71839fc

@compiler-errors
Copy link
Member

This is because in the cos example, we don't suggest turning it into .cos if there was a "import this function" suggestion made -- in this case, there was already the suggestion to import libm::cos.

@compiler-errors
Copy link
Member

Not sure if that's actually the behavior we want -- but if we did make that suggestion even if there was a previous import suggestion, I think it would be kinda noisy. Just my opinion though.

Though, it's kinda hard to give the import suggestion a lower priority than the fn->method suggestion, due to the fact that the former happens during name resolution, and the latter happens during type check...

@estebank
Copy link
Contributor Author

estebank commented Oct 9, 2022

@compiler-errors in that case, lets keep the ticket open for this case in particular. Your PR already improves things quite a bit, we can take some time to think about how to best go about this other case.

@Dylan-DPC
Copy link
Member

Current output:

error[E0425]: cannot find function `cos` in this scope
 --> src/main.rs:3:13
  |
3 |     let _ = cos(3.3 as f32);
  |             ^^^ not found in this scope
  |
help: consider importing this function
  |
2 + use libm::cos;
  |

error[E0425]: cannot find function `len` in this scope
 --> src/main.rs:5:13
  |
5 |     let x = len([1i32]);
  |             ^^^ not found in this scope
  |
help: use the `.` operator to call the method `len` on `&[i32]`
  |
5 -     let x = len([1i32]);
5 +     let x = [1i32].len();
  |

For more information about this error, try `rustc --explain E0425`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants