Skip to content

StructuralPartialEq diagnostic inconsistent with code #150298

Description

@cedtwo

Code

use std::marker::PhantomData;

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct StructuralType<T: PartialEq> {
    ty: PhantomData<T>,
    var: u32,
}

impl<T: PartialEq> StructuralType<T> {
    const A: Self = StructuralType {
        ty: PhantomData,
        var: 0,
    };

    const B: Self = StructuralType {
        ty: PhantomData,
        var: 0,
    };

    fn broken(self) {
        match self {
            Self::A => {}
            Self::B => {}
            _ => {}
        }
    }
}

Current output

error: constant of non-structural type `StructuralType<T>` in a pattern
  --> src/lib.rs:22:13
   |
 4 | pub struct StructuralType<T: PartialEq> {
   | --------------------------------------- `StructuralType<T>` must be annotated with `#[derive(PartialEq)]` to be usable in patterns
...
 9 | impl<T: PartialEq> StructuralType<T> {
   | ------------------------------------
10 |     const A: Self = StructuralType {
   |     ------------- constant defined here
...
22 |             Self::A => {}
   |             ^^^^^^^ constant of non-structural type

error: constant of non-structural type `StructuralType<T>` in a pattern
  --> src/lib.rs:23:13
   |
 4 | pub struct StructuralType<T: PartialEq> {
   | --------------------------------------- `StructuralType<T>` must be annotated with `#[derive(PartialEq)]` to be usable in patterns
...
 9 | impl<T: PartialEq> StructuralType<T> {
   | ------------------------------------
...
15 |     const B: Self = StructuralType {
   |     ------------- constant defined here
...
23 |             Self::B => {}
   |             ^^^^^^^ constant of non-structural type

error: could not compile `test_structural_ty` (lib) due to 2 previous errors

Desired output

Undecided, however the suggestion to annotate #[derive(PartialEq)] is incorrect.

Rust Version

rustc 1.92.0 (ded5c06cf 2025-12-08)
binary: rustc
commit-hash: ded5c06cf21d2b93bffd5d884aa6e96934ee4234
commit-date: 2025-12-08
host: x86_64-unknown-linux-gnu
release: 1.92.0
LLVM version: 21.1.3

Additional Information

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.T-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