Skip to content

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

Closed
@shepmaster

Description

@shepmaster

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.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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