Type with StructuralPartialEq impl with unsatisfied lifetime bounds can be used as const pattern
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I tried this code:
#![feature(structural_match)]
struct Thing;
trait IsStatic {}
impl<'a: 'static> IsStatic for &'a () {}
impl std::marker::StructuralPartialEq for Thing where for<'a> &'a (): IsStatic {}
impl PartialEq for Thing
where
for<'a> &'a (): IsStatic,
{
fn eq(&self, _: &Thing) -> bool {
panic!()
}
}
const A: Thing = Thing;
fn main() {
let A = Thing;
}
In this code, the for<'a> &'a (): IsStatic bound is unsatisfied. Therefore, the Thing type implements neither StructuralPartialEq nor PartialEq. Therefore, this code should not compile. However, the code compiles without errors, which seems wrong.
There's this comment in the compiler which seems relevant: https://github.com/rust-lang/rust/blob/2e667b0c6491678642a83e3aff86626397360af5/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs#L503-L512
See also https://github.com/rust-lang/rust/issues/147714, which involves an incorrect StructuralPartialEq impl from derive(PartialEq).
Meta
Reproducible on the playground with version 1.94.0-nightly (2025-12-09 c61a3a44d1a5bee35914)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the supplied example with the stated nightly compiler, then inspect compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs around the referenced comment. Trace how StructuralPartialEq and the unsatisfied higher-ranked lifetime bound are handled for const patterns. Done means the example is rejected consistently with the missing trait implementations, with regression coverage for this case.
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
- Clearly specified
- Newbie friendliness
- 35/100