Skip to content

Conversation

dianne
Copy link
Contributor

@dianne dianne commented Nov 2, 2024

In the diagnostics for the error no method named `method` found for type parameter `T` in the current scope [E0599], the compiler will suggest adding bounds on T for traits that define a method named method. However, these suggestions didn't include any generic arguments, so applying them would result in a missing generics for trait or missing lifetime specifier error. This PR adds placeholder arguments to the suggestion in such cases. Specifically, I tried to base the placeholders off of what's done in suggestions for when generics are missing or too few are provided:

  • The placeholder for a parameter without a default is the name of the parameter.
  • Placeholders are not provided for parameters with defaults.

Placeholder arguments are enclosed in /* and */, and the applicability of the suggestion is downgraded to Applicability::HasPlaceholders if any placeholders are provided.

Fixes #132407

@rustbot
Copy link
Collaborator

rustbot commented Nov 2, 2024

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 2, 2024
Copy link
Member

@fmease fmease left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you surround the placeholder arguments with /* and */ to highlight the fact that they are placeholders? E.g., Trait2</* '_, A, B */>. Otherwise users may take them literally and wonder whether the suggestion is wrong if it leads to more errors. It's not a hypothetical btw, people have reported such suggestions as bugs.

This would also be consistent with the diagnostic suggestion for E0220 (associated type not found). See 02a2f02 were I impl'ed that.

let candidate_strs: Vec<String> = candidates
.iter()
.map(|cand| {
use ty::GenericParamDefKind::{Const, Lifetime, Type};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to simplify this by using identity_for_item over generics_of. Compare:

let trait_args = &ty::GenericArgs::identity_for_item(tcx, best_trait)[1..];
let mut trait_ref = trait_name.clone();
let applicability = if let [arg, args @ ..] = trait_args {
use std::fmt::Write;
write!(trait_ref, "</* {arg}").unwrap();
args.iter().try_for_each(|arg| write!(trait_ref, ", {arg}")).unwrap();
trait_ref += " */>";
Applicability::HasPlaceholders
} else {
Applicability::MaybeIncorrect
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, disregard, that wouldn't consider generic parameter defaults.

@dianne dianne force-pushed the include-trait-args-in-suggestion branch from 2afb931 to aa1dfdd Compare November 2, 2024 06:35
@dianne
Copy link
Contributor Author

dianne commented Nov 2, 2024

The placeholders should now be surrounded by /* and */. I'll open a PR to do that for E0107 too, if that's alright. That's what I was using as a reference, and it looks easy to fix.

@dianne dianne requested a review from fmease November 2, 2024 06:36
@fmease fmease assigned fmease and unassigned nnethercote Nov 3, 2024
Copy link
Member

@fmease fmease left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Sorry for the slight delay. I was just dumbfounded that we don't have an API for that already somewhere. I guess one could use GenericArgs::identity_for_item plus Generics::own_args_no_defaults but er, that would perform much worse in this case.

I have small stylistic suggestions but nothing major. Lastly, could you squash your commits into one?

@fmease fmease added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 11, 2024
@dianne dianne force-pushed the include-trait-args-in-suggestion branch from 8a3c058 to 02add7d Compare November 11, 2024 20:36
@dianne
Copy link
Contributor Author

dianne commented Nov 11, 2024

Done. Thanks!
@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 11, 2024
@fmease
Copy link
Member

fmease commented Nov 11, 2024

@bors r+ rollup (diagnostics)

@bors
Copy link
Collaborator

bors commented Nov 11, 2024

📌 Commit 02add7d has been approved by fmease

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 11, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 12, 2024
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#132487 (Provide placeholder generics for traits in "no method found for type parameter" suggestions)
 - rust-lang#132627 (cleanup: Remove outdated comment of `thir_body`)
 - rust-lang#132653 (Don't use `maybe_unwrap_block` when checking for macro calls in a block expr)
 - rust-lang#132793 (Update mdbook to 0.4.42)
 - rust-lang#132847 (elem_offset / subslice_range: use addr() instead of 'as usize')
 - rust-lang#132869 (split up the first paragraph of doc comments for better summaries)
 - rust-lang#132929 (Check for null in the `alloc_zeroed` example)
 - rust-lang#132933 (Make sure that we suggest turbofishing the right type arg for never suggestion)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 9098e03 into rust-lang:master Nov 12, 2024
6 checks passed
@rustbot rustbot added this to the 1.84.0 milestone Nov 12, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Nov 12, 2024
Rollup merge of rust-lang#132487 - dianne:include-trait-args-in-suggestion, r=fmease

Provide placeholder generics for traits in "no method found for type parameter" suggestions

In the diagnostics for the error ``no method named `method` found for type parameter `T` in the current scope [E0599]``, the compiler will suggest adding bounds on `T` for traits that define a method named `method`. However, these suggestions didn't include any generic arguments, so applying them would result in a `missing generics for trait` or `missing lifetime specifier` error. This PR adds placeholder arguments to the suggestion in such cases. Specifically, I tried to base the placeholders off of what's done in suggestions for when generics are missing or too few are provided:
- The placeholder for a parameter without a default is the name of the parameter.
- Placeholders are not provided for parameters with defaults.

Placeholder arguments are enclosed in `/*` and `*/`, and the applicability of the suggestion is downgraded to `Applicability::HasPlaceholders` if any placeholders are provided.

Fixes rust-lang#132407
@dianne dianne deleted the include-trait-args-in-suggestion branch November 13, 2024 05:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"no method found for type parameter" missing generic args
5 participants