Skip to content

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

Closed
@RalfJung

Description

@RalfJung

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).

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions