Skip to content

Don't suggest unwrap for Result in const #149316

@Kivooeo

Description

@Kivooeo

Code

const fn f(value: u32) -> Result<u32, ()> {
    Ok(value)
}

const TEST: u32 = f(2);

Current output

error[E0308]: mismatched types
  --> src/lib.rs:10:19
   |
10 | const TEST: u32 = f(2);
   |                   ^^^^ expected `u32`, found `Result<u32, ()>`
   |
   = note: expected type `u32`
              found enum `Result<u32, ()>`
help: consider using `Result::expect` to unwrap the `Result<u32, ()>` value, panicking if the value is a `Result::Err`
   |
10 | const TEST: u32 = f(2).expect("REASON");
   |                       +++++++++++++++++

Desired output

error[E0308]: mismatched types
  --> src/lib.rs:10:19
   |
10 | const TEST: u32 = f(2);
   |                   ^^^^ expected `u32`, found `Result<u32, ()>`
   |
   = note: expected type `u32`
              found enum `Result<u32, ()>`

Rationale and extra context

I don't quiet sure about what is the best desired output, but for start it should be nice to remove this missleading suggestion to unwrap Result's value in const context which is not possible since neither unwrap or expect is const

maybe even give a note instead about that you can't unwrap Result in const so they need to found other workaround

Other cases

Rust Version

was tested in stable and nightly on playground

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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