rust-lang / rust-lang/rust

Using `?` in a constant offers a confusing error message

Open
#156,200 1 comment 0 reactions 1 assignee View on GitHub

@willpuhr is already working on this.

Since May 6, 2026.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
fn can_fail() -> Result<(), ()> { Ok(()) }

fn main() -> Result<(), ()> {
    const foo = can_fail()?;
    Ok(())
}
Current output
error[E0572]: return statement outside of function body
 --> src/main.rs:4:27
  |
4 |     const foo = can_fail()?;
  |                           ^
Desired output
error[E0572]: the `?` operator may not be used to return while defining a constant
 --> src/main.rs:4:27
  |
4 |     const foo = can_fail()?;
  |                           ^
Rationale and extra context

This is confusing for two reasons:

  1. The thing pointed at is not a literal return statement. Presumably there is a return-like-thing in the desugared code.
  2. The thing pointed at is inside a function body. It just happens to also be inside a constant.
Rust Version
rustc 1.95.0 (59807616e 2026-04-14)
binary: rustc
commit-hash: 59807616e1fa2540724bfbac14d7976d7e4a3860
commit-date: 2026-04-14
host: aarch64-apple-darwin
release: 1.95.0
LLVM version: 22.1.2
rustc 1.97.0-nightly (913e4bea8 2026-04-22)
binary: rustc
commit-hash: 913e4bea83424658d76712fee9c52452a3a9ef0e
commit-date: 2026-04-22
host: aarch64-apple-darwin
release: 1.97.0-nightly
LLVM version: 22.1.2
Anything else?

See also #105242

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.