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

Better user experience when attempting to call associated functions with dot notation #22692

Closed
frewsxcv opened this issue Feb 22, 2015 · 6 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. WG-diagnostics Working group: Diagnostics

Comments

@frewsxcv
Copy link
Member

New users might attempt to write something like this, (e.g. if they're coming from Java):

fn main() {
    let _ = String.new();
}

This will not compile since they're not using ::. This is what the compiler outputs when compiling:

~ $ rustc program.rs
program.rs:2:13: 2:19 error: unresolved name `String`
program.rs:2     let _ = String.new();
                         ^~~~~~

I find this error message pretty misleading. It'd be great if there was a lint that would check the available static methods of a struct/enum and if there's one that matches, suggest it instead (in the case above, suggest using String::new()).

@birkenfeld
Copy link
Contributor

Current error:

<anon>:2:13: 2:19 error: `String` is the name of a struct or struct variant, but this expression uses it like a function name [E0423]
<anon>:2     let _ = String.new();
                     ^~~~~~
<anon>:2:13: 2:19 help: see the detailed explanation for E0423
<anon>:2:13: 2:19 help: did you mean to write: `String { /* fields */ }`?

Not ideal either...

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 22, 2017
@estebank
Copy link
Contributor

Current output:

error[E0423]: expected value, found struct `String`
 --> src/main.rs:2:13
  |
2 |     let _ = String.new();
  |             ^^^^^^ did you mean `String { /* fields */ }`?

I believe the following would be appropriate output:

error[E0423]: expected value, found struct `String`
 --> src/main.rs:2:13
  |
2 |     let _ = String.new();
  |             ^^^^^^---- help: did you mean to call static method `new`?: `String::new`

@sourcefrog
Copy link
Contributor

I'd like to work on this. I think I can see where to change it. If someone wants to mentor let me know, otherwise I might ask on IRC or Discourse if I get stuck.

@estebank
Copy link
Contributor

estebank commented Jan 2, 2018

@sourcefrog feel free to do so and to reach out to anyone in the team! As soon as you have some code written, make an early PR so that we can take a look at it and provide feedback on the code itself.

Also, you should probably be looking at librustc_resolve/lib.rs:smart_resolve_path_fragment, which is where the suggestion is being provided. The only problem I see is that because this happens before typecheck, I don't think you can verify wether new is a method of String yet, but you can still provide it as a suggestion as a best effort guess.


For the code String.new(), smart_resolve_path_fragment's argument source will be Expr(Some(expr(10: String.new()))). In the suggestion path you would need to check if source is an Expr and that it is a method call. If it is, instead of suggesting $Type { /* fields */ } suggest $Type::$method_name on the Expr's span.

@estebank estebank added E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. and removed E-needs-mentor labels Jan 2, 2018
@dsciarra
Copy link
Contributor

dsciarra commented Sep 12, 2018

@estebank

  1. what is $Type::? a substring of String.new(), or is $Type a type?
  2. how to check that the argument source is a method call? is $method_name then the other part of the string String.new()?

@estebank
Copy link
Contributor

estebank commented Sep 12, 2018

$Type is just a way to refer to String (or any type that appears in the code). $method_name would be new in String.new().

You need to check that source is PathSource::Expr(Expr { kind: ExprKind::Call { ty, fun, args}, .. }), where ty is the self type, fun is an ExprRef containing an Expr. I'm not sure about the exact Expr tree for the method call without looking at a running example, but this should give you an idea of the direction to take (and I would just add some debug statements or start up the debugger to see it).

@frewsxcv frewsxcv changed the title Better user experience when attempting to call static methods with dot notation Better user experience when attempting to call associated functions with dot notation Sep 12, 2018
kennytm added a commit to kennytm/rust that referenced this issue Oct 1, 2018
Better user experience when attempting to call associated functions with dot notation

Closes rust-lang#22692
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 C-enhancement Category: An issue proposing an enhancement or a PR with one. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

No branches or pull requests

7 participants