-
-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Open
Labels
A-borrow-checkerArea: The borrow checkerArea: The borrow checkerA-patternsRelating to patterns and pattern matchingRelating to patterns and pattern matchingC-bugCategory: This is a bug.Category: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.This issue requires a nightly compiler in some way.
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:
rust/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs
Lines 503 to 512 in 2e667b0
| // This *could* accept a type that isn't actually `PartialEq`, because region bounds get | |
| // ignored. However that should be pretty much impossible since consts that do not depend on | |
| // generics can only mention the `'static` lifetime, and how would one have a type that's | |
| // `PartialEq` for some lifetime but *not* for `'static`? If this ever becomes a problem | |
| // we'll need to leave some sort of trace of this requirement in the MIR so that borrowck | |
| // can ensure that the type really implements `PartialEq`. | |
| // We also do *not* require `const PartialEq`, not even in `const fn`. This violates the model | |
| // that patterns can only do things that the code could also do without patterns, but it is | |
| // needed for backwards compatibility. The actual pattern matching compares primitive values, | |
| // `PartialEq::eq` never gets invoked, so there's no risk of us running non-const code. |
See also #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)
Metadata
Metadata
Assignees
Labels
A-borrow-checkerArea: The borrow checkerArea: The borrow checkerA-patternsRelating to patterns and pattern matchingRelating to patterns and pattern matchingC-bugCategory: This is a bug.Category: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.This issue requires a nightly compiler in some way.