Skip to content

Missing "parentheses are required to parse this as an expression" hint with multiple unsafe blocks #102171

Closed
@jruderman

Description

@jruderman

Given the following code:

unsafe fn id(x: u32) -> u32 {
    x
}

fn main() {
    let y = unsafe {
        unsafe { id(3) } + unsafe { id(4) }
    };
    println!("{}", y);
}

The current output is:

error: expected expression, found `+`
 --> src/main.rs:7:26
  |
7 |         unsafe { id(3) } + unsafe { id(4) }
  |                          ^ expected expression

error[E0308]: mismatched types
 --> src/main.rs:7:18
  |
7 |         unsafe { id(3) } + unsafe { id(4) }
  |                  ^^^^^- help: consider using a semicolon here: `;`
  |                  |
  |                  expected `()`, found `u32`

For more information about this error, try `rustc --explain E0308`.

Ideally the output should look like:

error: expected expression, found `+`
 --> src/main.rs:7:26
  |
7 |         unsafe { id(3) } + unsafe { id(4) }
  |                          ^ expected expression
  |
help: parentheses are required to parse this as an expression
  |
7 |         (unsafe { id(3) }) + unsafe { id(4) }
  |         +                +

Related to #67347

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.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