Skip to content

Const patterns can have different behavior from PartialEq, due to incorrect bounds in derive(PartialEq) #147714

@theemathas

Description

@theemathas

I tried this code:

#[allow(dead_code)]
#[derive(PartialEq)]
enum Thing<T> {
    A(T),
    B,
}

struct Incomparable;

impl PartialEq for Thing<Incomparable> {
    fn eq(&self, _: &Self) -> bool {
        panic!()
    }
}

const X: Thing<Incomparable> = Thing::B;

fn main() {
    if let X = X {
        println!("equal");
    }
}

I expected the code to either produce a compile error, or panic at run time. Instead, it compiled and printed "equal". That is, if let X = X has different behavior from if X == X.

This seems to be caused by the derive(PartialEq) expanding to the following code, which seems incorrect, due to the lack of bounds on T:

impl<T> ::core::marker::StructuralPartialEq for Thing<T> { }

The reference seems to be ambiguous on whether this behavior is correct or not, but I think that this behavior is incorrect, since I interpret "its PartialEq instance" to mean "the impl that caused the const's type to implement PartialEq".

Meta

Reproducible on the playground with version 1.92.0-nightly (2025-10-13 4b94758d2ba7d0ef71cc)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-patternsRelating to patterns and pattern matchingC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions