rust-lang / rust-lang/rust

Suboptimal diagnostics when "&function()" in const is rejected due to interior mutability

Open
#143,345 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I expected to be able to write

const { &Backtrace::disabled() }

but it yields the error

error[E0492]: constants cannot refer to interior mutable data

This is surprising because:

  1. A disabled Backtrace is an empty enum variant without interior mutable data.
  2. The error refers to a library implementation detail which is a bit confusing.
  3. This seems like something I ought to be able to do.

Although there is an easy workaround which is to use a static.

Rust version: 1.88.0

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.

Research direction

Reproduce the reported const { &Backtrace::disabled() } case with Rust 1.88.0 and confirm the E0492 diagnostic. Trace the compiler handling for this rejection, then determine whether the intended resolution is to allow the reference or improve the diagnostic; completion should include regression coverage for the chosen behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.