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

Trait errors should suggest * if appropriate for the boudn to succeed #90997

Closed
estebank opened this issue Nov 18, 2021 · 0 comments · Fixed by #133292
Closed

Trait errors should suggest * if appropriate for the boudn to succeed #90997

estebank opened this issue Nov 18, 2021 · 0 comments · Fixed by #133292
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

Trait errors should suggest dereferencing just like type errors do in the second error below:

error[E0277]: the trait bound `&rustc_span::def_id::DefId: rustc_middle::ty::query::sealed::IntoQueryParam<rustc_span::def_id::DefId>` is not satisfied
   --> compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs:151:39
    |
151 |             if self.tcx.trait_of_item(item) == Some(trait_ref.def_id) {
    |                         ------------- ^^^^ the trait `rustc_middle::ty::query::sealed::IntoQueryParam<rustc_span::def_id::DefId>` is not implemented for `&rustc_span::def_id::DefId`
    |                         |
    |                         required by a bound introduced by this call

error[E0308]: mismatched types
   --> compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs:152:49
    |
152 |                 let method = self.tcx.item_name(item);
    |                                                 ^^^^ expected struct `rustc_span::def_id::DefId`, found `&rustc_span::def_id::DefId`
    |
help: consider dereferencing the borrow
    |
152 |                 let method = self.tcx.item_name(*item);
    |                                                 +
@estebank estebank 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. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Nov 18, 2021
Zalathar added a commit to Zalathar/rust that referenced this issue Jan 1, 2025
…bank

E0277: suggest dereferencing function arguments in more cases

This unifies and generalizes some of the logic in `TypeErrCtxt::suggest_dereferences` so that it will suggest dereferencing arguments to function/method calls in order to satisfy trait bounds in more cases.

Previously it would only fire on reference types, and it had two separate cases (one specifically to get through custom `Deref` impls when passing by-reference, and one specifically to catch rust-lang#87437). I've based the new checks loosely on what's done for `E0308` in `FnCtxt::suggest_deref_or_ref`: it will suggest dereferences to satisfy trait bounds whenever the referent is `Copy`, is boxed (& so can be moved out of the boxes), or is being passed by reference.

This doesn't make the suggestion fire in contexts other than function arguments or binary operators (which are in a separate case that this doesn't touch), and doesn't make it suggest a combination of `&`-removal and dereferences. Those would require a bit more restructuring, so I figured just doing this would be a decent first step.

Closes rust-lang#90997
@bors bors closed this as completed in 0204259 Jan 1, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 1, 2025
Rollup merge of rust-lang#133292 - dianne:e0277-suggest-deref, r=estebank

E0277: suggest dereferencing function arguments in more cases

This unifies and generalizes some of the logic in `TypeErrCtxt::suggest_dereferences` so that it will suggest dereferencing arguments to function/method calls in order to satisfy trait bounds in more cases.

Previously it would only fire on reference types, and it had two separate cases (one specifically to get through custom `Deref` impls when passing by-reference, and one specifically to catch rust-lang#87437). I've based the new checks loosely on what's done for `E0308` in `FnCtxt::suggest_deref_or_ref`: it will suggest dereferences to satisfy trait bounds whenever the referent is `Copy`, is boxed (& so can be moved out of the boxes), or is being passed by reference.

This doesn't make the suggestion fire in contexts other than function arguments or binary operators (which are in a separate case that this doesn't touch), and doesn't make it suggest a combination of `&`-removal and dereferences. Those would require a bit more restructuring, so I figured just doing this would be a decent first step.

Closes rust-lang#90997
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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. 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.

1 participant