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

Diagnostics: bad suggestion when I have wrong parentheses around a dyn trait pointer type #114797

Closed
RalfJung opened this issue Aug 14, 2023 · 2 comments · Fixed by #114802
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@RalfJung
Copy link
Member

RalfJung commented Aug 14, 2023

This code

trait Trait {}

fn assert_send(ptr: *mut dyn Trait) -> *mut dyn (Trait + Send) {
    ptr as _
}

produces the following error

error: incorrect braces around trait bounds
 --> src/lib.rs:3:49
  |
3 | fn assert_send(ptr: *mut dyn Trait) -> *mut dyn (Trait + Send) {
  |                                                 ^            ^
  |
help: remove the parentheses
  |
3 - fn assert_send(ptr: *mut dyn Trait) -> *mut dyn (Trait + Send) {
3 + fn assert_send(ptr: *mut dyn Trait) -> *mut dyn  Trait + Send {
  |

error: could not compile `playground` (lib) due to previous error

First of all, "braces" is the wrong word here I think, AFAIK "braces" means {...}, but here we have parentheses.

More importantly though, if I apply the suggestion, the code still does not build! Instead it now shows a different error:

error: ambiguous `+` in a type
 --> src/lib.rs:3:45
  |
3 | fn assert_send(ptr: *mut dyn Trait) -> *mut dyn Trait + Send {
  |                                             ^^^^^^^^^^^^^^^^ help: use parentheses to disambiguate: `(dyn Trait + Send)`

The correct syntax is to write *mut (dyn Trait + Send).

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 14, 2023
@chenyukang chenyukang self-assigned this Aug 14, 2023
@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-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Aug 14, 2023
@estebank
Copy link
Contributor

estebank commented Aug 14, 2023

FWIW, I consider it ok if suggestions give you incomplete suggestions as long as subsequent errors close the gap (as it these errors do). In some cases making the initial error have enough understanding to produce the correct suggestion can cause an explosion of complexity (and sometimes it is flat out impossible unless we made the compiler less stage based than it is now).

@RalfJung
Copy link
Member Author

I felt somewhat mislead by the suggestion. The fix was simply to move the ( from after dyn to before dyn. But the compiler made me entirely remove the parentheses, only to then tell me to add them back in a different way.

But also more generally, I think any time there is a concrete suggestion like in the original issue, that suggestion must compile. Otherwise I'll stop trusting the compiler's suggestions and just ignore them -- it'll be quicker to look for the right fix directly than hunt a chain of compiler errors.

@bors bors closed this as completed in 0768872 Aug 17, 2023
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-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-papercut Diagnostics: An error or lint that needs small tweaks. 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.

4 participants