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

E0277: suggest dereferencing function arguments in more cases #133292

Merged
merged 1 commit into from
Jan 1, 2025

Conversation

dianne
Copy link
Contributor

@dianne dianne commented Nov 21, 2024

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 #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 #90997

@rustbot
Copy link
Collaborator

rustbot commented Nov 21, 2024

r? @fee1-dead

rustbot has assigned @fee1-dead.
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 21, 2024
// reference, `ty` is `Copy`, or we're moving out of a (potentially nested) `Box`.
let can_deref = is_under_ref.is_some()
|| self.type_is_copy_modulo_regions(obligation.param_env, ty)
|| ty.is_numeric() // for inference vars (presumably but not provably `Copy`)
Copy link
Contributor Author

@dianne dianne Nov 22, 2024

Choose a reason for hiding this comment

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

The ty.is_numeric() case is admittedly a hack for tests/ui/traits/suggest-dereferences/suggest-dereferencing-receiver-argument.rs, since it wants a deref suggestion for an &{integer}. I think a slightly more principled (still hacky) way to do this could be to walk through base_ty and add Copy bounds to the ParamEnv for any numeric inference vars. It should maybe also downgrade the suggestion to MaybeIncorrect if any type inference vars are present. {integer}: Sized does appear to be provable already, at least.

@fee1-dead
Copy link
Member

r? compiler

@rustbot rustbot assigned pnkfelix and unassigned fee1-dead Nov 23, 2024
@pnkfelix
Copy link
Member

r? @estebank

@rustbot rustbot assigned estebank and unassigned pnkfelix Dec 12, 2024
@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Dec 31, 2024

📌 Commit 403c8c2 has been approved by estebank

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 Dec 31, 2024
Zalathar added a commit to Zalathar/rust that referenced this pull request 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 added a commit to rust-lang-ci/rust that referenced this pull request Jan 1, 2025
Rollup of 7 pull requests

Successful merges:

 - rust-lang#131439 (Remove allowing static_mut_refs lint)
 - rust-lang#133292 (E0277: suggest dereferencing function arguments in more cases)
 - rust-lang#134080 (Avoid use of LFS64 symbols on Emscripten)
 - rust-lang#134877 (add suggestion for wrongly ordered format parameters)
 - rust-lang#134926 (Update books)
 - rust-lang#134945 (Some small nits to the borrowck suggestions for mutating a map through index)
 - rust-lang#134979 (Provide structured suggestion for `impl Default` of type where all fields have defaults)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 1, 2025
Rollup of 6 pull requests

Successful merges:

 - rust-lang#131439 (Remove allowing static_mut_refs lint)
 - rust-lang#133292 (E0277: suggest dereferencing function arguments in more cases)
 - rust-lang#134877 (add suggestion for wrongly ordered format parameters)
 - rust-lang#134945 (Some small nits to the borrowck suggestions for mutating a map through index)
 - rust-lang#134950 (bootstrap: Overhaul and simplify the `tool_check_step!` macro)
 - rust-lang#134979 (Provide structured suggestion for `impl Default` of type where all fields have defaults)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 0204259 into rust-lang:master Jan 1, 2025
6 checks passed
@rustbot rustbot added this to the 1.85.0 milestone Jan 1, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request 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
@dianne dianne deleted the e0277-suggest-deref branch January 1, 2025 21:27
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.

Trait errors should suggest * if appropriate for the boudn to succeed
6 participants