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

"no method found for type parameter" missing generic args #132407

Closed
lcnr opened this issue Oct 31, 2024 · 1 comment · Fixed by #132487
Closed

"no method found for type parameter" missing generic args #132407

lcnr opened this issue Oct 31, 2024 · 1 comment · Fixed by #132487
Assignees
Labels
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.

Comments

@lcnr
Copy link
Contributor

lcnr commented Oct 31, 2024

Code

trait Trait<I> {
    fn method(&self) {}
}

fn foo<T>(value: T) {
    value.method()
}

Current output

error[E0599]: no method named `method` found for type parameter `T` in the current scope
 --> src/lib.rs:6:11
  |
5 | fn foo<T>(value: T) {
  |        - method `method` not found for this type parameter
6 |     value.method()
  |           ^^^^^^ method not found in `T`
  |
  = help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `method`, perhaps you need to restrict type parameter `T` with it:
  |
5 | fn foo<T: Trait>(value: T) {
  |         +++++++

Desired output

error[E0599]: no method named `method` found for type parameter `T` in the current scope
 --> src/lib.rs:6:11
  |
5 | fn foo<T>(value: T) {
  |        - method `method` not found for this type parameter
6 |     value.method()
  |           ^^^^^^ method not found in `T`
  |
  = help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `method`, perhaps you need to restrict type parameter `T` with it:
  |
5 | fn foo<T: Trait<_>>(value: T) {
  |         ++++++++++
@lcnr lcnr 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 Oct 31, 2024
@dianne
Copy link
Contributor

dianne commented Nov 1, 2024

@rustbot claim

@bors bors closed this as completed in 9098e03 Nov 12, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue 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
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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
2 participants