Skip to content

Suggestion to "surround the struct literal in parentheses" sometimes places the opening parenthesis after the module name #112278

Closed
@bernardosulzbach

Description

@bernardosulzbach

Code

mod module {
    #[derive(Eq, PartialEq)]
    pub struct Type {
        pub x: u8,
        pub y: u8,
    }

    pub const C: u8 = 32u8;
}

fn test(x: module::Type) -> i32 {
    if x == module::Type { x: module::C, y: 1 } {
        1
    } else {
        0
    }
}

Current output

error: invalid struct literal
  --> <source>:12:21
   |
12 |     if x == module::Type { x: module::C, y: 1 } {
   |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: you might need to surround the struct literal in parentheses
   |
12 |     if x == module::(Type { x: module::C, y: 1 }) {
   |                     +                           +

Desired output

error: invalid struct literal
  --> <source>:12:21
   |
12 |     if x == module::Type { x: module::C, y: 1 } {
   |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
help: you might need to surround the struct literal in parentheses
   |
12 |     if x == (module::Type { x: module::C, y: 1 }) {
   |             +                                   +

Rationale and extra context

No response

Other cases

No response

Anything else?

This defect is reproducible with (at least) 1.68, 1.69, beta, and nightly.

Furthermore, I think it would be good to reword the message to suggest surrounding with parentheses.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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