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

Suggest annotating traits rather than methods when appropriate #89064

Closed
hkmatsumoto opened this issue Sep 18, 2021 · 7 comments · Fixed by #100838
Closed

Suggest annotating traits rather than methods when appropriate #89064

hkmatsumoto opened this issue Sep 18, 2021 · 7 comments · Fixed by #100838
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@hkmatsumoto
Copy link
Member

hkmatsumoto commented Sep 18, 2021

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d2a32fe1f3460b6583534eb53d045165

fn foo() {
    let _ = "foo".into::<Option<_>>();
}

The current output is:

Compiling playground v0.0.1 (/playground)
error[E0107]: this associated function takes 0 generic arguments but 1 generic argument was supplied
   --> src/lib.rs:2:19
    |
2   |     let _ = "foo".into::<Option<_>>();
    |                   ^^^^------------- help: remove these generics
    |                   |
    |                   expected 0 generic arguments
    |
note: associated function defined here, with 0 generic parameters

For more information about this error, try `rustc --explain E0107`.
error: could not compile `playground` due to previous error

Ideally the output should look like:

error[E0107]: this associated function takes 0 generic arguments but 1 generic argument was supplied
   --> src/lib.rs:2:19
    |
2   |     let _ = "foo".into::<Option<_>>();
    |                   ^^^^------------- help: remove these generics
    |                   |
    |                   expected 0 generic arguments
    |
note: associated function defined here, with 0 generic parameters
help: `Into` trait takes 1 generic argument, try annotating that instead: `Into::<Option<_>>::into("foo")

When a user called into() with an annotation, it's helpful to suggest T::from(_) too.

@hkmatsumoto hkmatsumoto added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 18, 2021
@chansuke
Copy link
Contributor

@hkmatsumoto If it's ok, I would like to give a shot. If you plan to work on it, please go ahead.

@hkmatsumoto
Copy link
Member Author

hkmatsumoto commented Sep 18, 2021

It's yours :)

@chansuke
Copy link
Contributor

@rustbot claim

@chansuke
Copy link
Contributor

chansuke commented Oct 9, 2021

Hi, I would like to ask a question...
I'm reading the rustc_typecheck code now and am thinking of extending the error messages by adding fn diagnostic_extended to wrong_number_of_generics_args.rs. My plan is that start adding some fields (like ty::TraitRef) to pub struct WrrongnarofGenericArgs<'a, 'tcx> {} and dbg! the output to get the value of trait information like Into for a new error message.
Is my approach out of line or is there a better approach for that.

@hkmatsumoto
Copy link
Member Author

hkmatsumoto commented Oct 9, 2021

My plan is that start adding some fields (like ty::TraitRef) to pub struct WrrongnarofGenericArgs<'a, 'tcx> {} and dbg! the output to get the value of trait information like Into for a new error message.

You don't have to hold information of TraitRef in WrongNumberOfGenericArgs because we can get the info on-demand by the combination of TyCtxt::trait_of_item and TyCtxt::generics_of.
Say you'd add a method of WrongNumberOfGenericArgs, the code will be something like this:

let trait_: Option<DefId> = self.tcx.trait_of_item(self.def_id);
let generics: &Generics = self.tcx.generics_of(trait_.unwrap());

For getting the name of the trait in question, TyCtxt::item_name or TyCtxt::def_path_str will be handy for you.

@hkmatsumoto hkmatsumoto added the E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. label Oct 9, 2021
@chansuke
Copy link
Contributor

I appreciate your answer.

@vincenzopalazzo
Copy link
Member

@chansuke are you still working on this? :)

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 31, 2022
…-when-appropriate, r=davidtwco

Suggest moving redundant generic args of an assoc fn to its trait

Closes rust-lang#89064
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 31, 2022
…-when-appropriate, r=davidtwco

Suggest moving redundant generic args of an assoc fn to its trait

Closes rust-lang#89064
@bors bors closed this as completed in 2af2cda Sep 1, 2022
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 E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. 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.

3 participants