Skip to content

Spurious error[E0492]: constants cannot refer to interior mutable data when an undeclared type is found #134796

Closed
@kpreid

Description

@kpreid

Code

struct MyStruct {
    foo: Option<UndefinedType>,
}
impl MyStruct {
    pub const EMPTY_REF: &'static Self = &Self::EMPTY;
    pub const EMPTY: Self = Self {
        foo: None,
    };
}

Current output

error[E0412]: cannot find type `UndefinedType` in this scope
 --> src/lib.rs:2:17
  |
2 |     foo: Option<UndefinedType>,
  |                 ^^^^^^^^^^^^^ not found in this scope
  |
help: you might be missing a type parameter
  |
1 | struct MyStruct<UndefinedType> {
  |                +++++++++++++++

error[E0492]: constants cannot refer to interior mutable data
 --> src/lib.rs:5:42
  |
5 |     pub const EMPTY_REF: &'static Self = &Self::EMPTY;
  |                                          ^^^^^^^^^^^^ this borrow of an interior mutable value may end up in the final value

Desired output

The E0492 interior mutability error is spurious and should not be emitted.

Rust Version

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: aarch64-apple-darwin
release: 1.83.0
LLVM version: 19.1.1

Metadata

Metadata

Labels

A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)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