-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.Diagnostics: A structured suggestion resulting in incorrect code.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Given the following code:
fn foo<T:>(t: T) {
t.clone();
}
The current output is:
error[E0599]: no method named `clone` found for type parameter `T` in the current scope
--> src/lib.rs:2:7
|
2 | t.clone();
| ^^^^^ 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 `clone`, perhaps you need to restrict type parameter `T` with it:
|
1 | fn foo<T: Clone:>(t: T) {
| ~~~~~~~~
Ideally the output should look like:
error[E0599]: no method named `clone` found for type parameter `T` in the current scope
--> src/lib.rs:2:7
|
2 | t.clone();
| ^^^^^ 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 `clone`, perhaps you need to restrict type parameter `T` with it:
|
1 | fn foo<T: Clone>(t: T) {
| ++++++
Note: The T:
bound is treated incorrectly.
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.Diagnostics: A structured suggestion resulting in incorrect code.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
PoorlyDefinedBehaviour commentedon Apr 10, 2022
@rustbot claim
PoorlyDefinedBehaviour commentedon Apr 11, 2022
You mean that
suggest_traits_to_import
does not handleT:
correctly, right?compiler-errors commentedon Apr 11, 2022
Correct, the suggestion reads
T: Clone:
which is not syntactical. The fix would be to detect this case and correctly suggestT: Clone
PoorlyDefinedBehaviour commentedon Apr 12, 2022
It's my first time contributing and i was taking a look at
suggest_traits_to_import
and didn't find a way to detectT:
.So i ended up adding a flag to
GenericParam
, setting it inparse_ty_param
and checking the flag insuggest_traits_to_import
to handle theT:
case.Does that seem reasonable?
compiler-errors commentedon Apr 12, 2022
Adding a new flag to
hir::GenericParam
seems a bit heavy. I was thinking you could one of the methods onSpan
to detect if there is a colon following the generic param.Also hmm... I think @WaffleLapkin was also looking at trying to solve this... I opened up this issue before he said that he was trying to fix this issue, though I'm not sure if in general or just a specific case in something he was working on (i.e. another suggestion). Perhaps he could comment to see if he's already solved this, in order to deduplicate work?
WaffleLapkin commentedon Apr 12, 2022
So. I was fixing the same problem in
rustc_middle::ty::suggest_constraining_type_params
and I just got a working fix!My solution is to use
SourceMap
in order to find the span of:
. It may be hacky, but I think it's good enough for diagnostics.It seems like my fix doesn't fix this issue, because this suggestion is created using different API (
suggest_traits_to_import
), but it should be easy to extend it to this case too. For examplesuggest_traits_to_import
could usesuggest_constraining_type_params
internally (suggest_constraining_type_params
produces better suggestions anyway :D).I'll try to clean my fix & create a PR today.
T:
bounds #95970Rollup merge of rust-lang#95970 - WaffleLapkin:nicer_trait_suggestion…
PoorlyDefinedBehaviour commentedon Apr 12, 2022
I took a look at your PR, that's way better than my solution.
I will use the method you added to
GenericParam
to fix this issue if you aren't working on it as well.Rollup merge of rust-lang#95991 - PoorlyDefinedBehaviour:fix/issue_95…