Unboxing suggestion does not add parentheses where needed · Issue #132924 · rust-lang/rust (original) (raw)

Code

use std::sync::Arc; fn main() { let x = Box::new(Some(1));

let test: Option<i32> = x as Box<Option<i32>>;

}

Current output

error[E0308]: mismatched types --> src/main.rs:5:29 | 5 | let test: Option = x as Box<Option>; | ----------- ^^^^^^^^^^^^^^^^^^^^^ expected Option<i32>, found Box<Option<i32>> | | | expected due to this | = note: expected enum Option<_> found struct Box<Option<_>> help: consider unboxing the value | 5 | let test: Option = *x as Box<Option>; | +

Desired output

error[E0308]: mismatched types --> src/main.rs:5:29 | 5 | let test: Option = x as Box<Option>; | ----------- ^^^^^^^^^^^^^^^^^^^^^ expected Option<i32>, found Box<Option<i32>> | | | expected due to this | = note: expected enum Option<_> found struct Box<Option<_>> help: consider unboxing the value | 5 | let test: Option = *(x as Box<Option>); | ++ +

Rationale and extra context

The current output is incorrect, and leads to the same suggestion over and over again if we keep applying it. The relevant code is

.

See

let needs_parens = match expr.kind {

for an example of adding parentheses.

Other cases

Rust Version

rustc 1.84.0-dev binary: rustc commit-hash: unknown commit-date: unknown host: x86_64-unknown-linux-gnu release: 1.84.0-dev LLVM version: 19.1.3


Tested on commit 42b24963202f31d417a972e56e48a17e916b279b

Anything else?

No response