Skip to content

Type with StructuralPartialEq impl with unsatisfied lifetime bounds can be used as const pattern #149841

@theemathas

Description

@theemathas

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:

// 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

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-patternsRelating to patterns and pattern matchingC-bugCategory: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions