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

Unboxing suggestion does not add parentheses where needed #132924

Closed
uellenberg opened this issue Nov 11, 2024 · 1 comment · Fixed by #132944
Closed

Unboxing suggestion does not add parentheses where needed #132924

uellenberg opened this issue Nov 11, 2024 · 1 comment · Fixed by #132944
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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@uellenberg
Copy link
Contributor

uellenberg commented Nov 11, 2024

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<i32> = x as Box<Option<i32>>;
  |               -----------   ^^^^^^^^^^^^^^^^^^^^^ 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<i32> = *x as Box<Option<i32>>;
  |                             +

Desired output

error[E0308]: mismatched types
 --> src/main.rs:5:29
  |
5 |     let test: Option<i32> = x as Box<Option<i32>>;
  |               -----------   ^^^^^^^^^^^^^^^^^^^^^ 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<i32> = *(x as Box<Option<i32>>);
  |                             ++                     +

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

@uellenberg uellenberg 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. labels Nov 11, 2024
@linyihai
Copy link
Contributor

@rustbot claim

Thanks for your question and code snippet reminder, I will submit a PR later

@fmease fmease changed the title Unboxing suggestion does not add parenthases where needed Unboxing suggestion does not add parentheses where needed Nov 12, 2024
@fmease fmease added D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels Nov 12, 2024
@bors bors closed this as completed in c874121 Nov 18, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 18, 2024
Rollup merge of rust-lang#132944 - linyihai:needing-parenthases-issue-132924, r=chenyukang

add parentheses when unboxing suggestion needed

This PR tried to `add parentheses when unboxing suggestion needed`

Fixes rust-lang#132924
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. 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.

3 participants