Skip to content

Suggestion to add the question mark operator creates invalid code for structs that use the shorthand syntax #118145

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

Closed
shepmaster opened this issue Nov 21, 2023 · 2 comments · Fixed by #118413
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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

@shepmaster
Copy link
Member

shepmaster commented Nov 21, 2023

Code

struct Stuff { count: i32 }
struct Error;

fn demo() -> Result<Stuff, Error> {
    let count = Ok(1);
    Ok(Stuff { count })
}

Current output

error[E0308]: mismatched types
 --> src/lib.rs:6:16
  |
6 |     Ok(Stuff { count })
  |                ^^^^^ expected `i32`, found `Result<{integer}, _>`
  |
  = note: expected type `i32`
             found enum `Result<{integer}, _>`
help: use the `?` operator to extract the `Result<{integer}, _>` value, propagating a `Result::Err` value to the caller
  |
6 |     Ok(Stuff { count? })
  |                     +

Desired output

error[E0308]: mismatched types
 --> src/lib.rs:6:16
  |
6 |     Ok(Stuff { count })
  |                ^^^^^ expected `i32`, found `Result<{integer}, _>`
  |
  = note: expected type `i32`
             found enum `Result<{integer}, _>`
help: use the `?` operator to extract the `Result<{integer}, _>` value, propagating a `Result::Err` value to the caller
  |
6 |     Ok(Stuff { count: count? })
  |                     ++++++++

Rationale and extra context

No response

Other cases

No response

Anything else?

This appears to be a new diagnostic, as it appears in 1.75.0-beta.3 (2023-11-20 b66b795) but not in 1.74.0.

@shepmaster shepmaster 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 21, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 21, 2023
@shepmaster
Copy link
Member Author

Introduced in f3d20be; /cc @chenyukang @b-naber

@shepmaster
Copy link
Member Author

The same happens when the ? operator is not applicable and unwrap is suggested instead:

struct Stuff { count: i32 }
struct Error;

fn demo() -> Stuff {
    let count = Ok(1);
    Ok(Stuff { count })
}
help: consider using `Result::expect` to unwrap the `Result<{integer}, _>` value, panicking if the value is a `Result::Err`
  |
6 |     Ok(Stuff { count.expect("REASON") })
  |                     +++++++++++++++++

@clubby789 clubby789 added D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 21, 2023
@chenyukang chenyukang self-assigned this Nov 22, 2023
@bors bors closed this as completed in 8727538 Nov 29, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 29, 2023
Rollup merge of rust-lang#118413 - chenyukang:yukang-fix-118145-unwrap-for-shorthand, r=compiler-errors

Fix the issue of suggesting unwrap/expect for shorthand field

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

4 participants