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

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

Open
jruderman opened this issue Dec 4, 2022 · 0 comments
Open
Labels
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.

Comments

@jruderman
Copy link
Contributor

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.
@jruderman jruderman 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 Dec 4, 2022
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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant