Skip to content

Weird error messages when using ? operator in enum discriminant value #105242

@jruderman

Description

@jruderman

Code

playground

enum M {
    D = Some(0)?
}

fn main() {}

Current output

error[E0658]: `?` is not allowed in a `const`
 --> kfb.rs:2:9
  |
2 |     D = Some(0)?
  |         ^^^^^^^^
  |
  = note: see issue #74935 <https://github.com/rust-lang/rust/issues/74935> for more information
  = help: add `#![feature(const_try)]` to the crate attributes to enable

error[E0572]: return statement outside of function body
 --> kfb.rs:2:16
  |
2 |     D = Some(0)?
  |                ^

Why these messages are weird

  • The first error refers to const, but I didn't type const. Instead, we're in a const context implied by writing = while defining an enum discriminant.
  • The second error refers to return, but I didn't type return. Instead, the return comes from desugaring ?.

Ideal output

  • Skip the first error
  • Turn the second error into: ? operator outside function body
  • Suggest replacing ? with .unwrap(), perhaps

Notes

  • I noticed this while looking at fuzzer output. It's probably not important.
  • Would it make sense to allow ? here as a shorthand for .unwrap()? Either way we want compilation to stop if the value is None.

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
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions