Skip to content

Add T: PartialEq bounds to derived StructuralPartialEq impls.#156807

Draft
kpreid wants to merge 1 commit into
rust-lang:mainfrom
kpreid:speq
Draft

Add T: PartialEq bounds to derived StructuralPartialEq impls.#156807
kpreid wants to merge 1 commit into
rust-lang:mainfrom
kpreid:speq

Conversation

@kpreid
Copy link
Copy Markdown
Contributor

@kpreid kpreid commented May 21, 2026

Fixes #147714.

Currently has a severe regression in diagnostic quality — suggesting derive(PartialEq) on the type that already has that derive — apparently because the diagnostic logic effectively checks whether StructuralPartialEq exists as a proxy for whether the type has the derive. I don’t yet know how to solve that problem.

This is a breaking change to fix a bug, so it will need a crater run if it is to be done at all.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 21, 2026
@kpreid
Copy link
Copy Markdown
Contributor Author

kpreid commented May 21, 2026

This PR is probably less correct than the just-posted #156809, because the documentation of StructuralPartialEq says that it ensures that “interpreting the value of the constant as a pattern is equivalent to calling PartialEq”, and so the trait bounds should require transitive StructuralPartialEq, not just that the applicable PartialEq impl is the derived one. Still, I'll leave this open until things settle down more.

Currently waiting on an answer to my question about a route to to fixing the diagnostics.

@rust-log-analyzer

This comment has been minimized.

@theemathas
Copy link
Copy Markdown
Contributor

Adding T: StructuralPartialEq bounds like in #156809 is incorrect. Consider the following code, which compiles in current stable rust, despite Box not implementing StructuralPartialEq:

const X: Option<Box<()>> = None;

fn main() {
    if let X = X {}
}

@theemathas
Copy link
Copy Markdown
Contributor

Relevant documentation: https://doc.rust-lang.org/nightly/reference/patterns.html#r-patterns.const

The StructuralPartialEq trait does not correspond to the concept of "structural equality" in the documentation. In fact, this concept of "structural equality" cannot be represented as a trait at all, due to the "active variant" clause for enums, which makes structural equality depend on the const's value, not just the type.

Instead, the StructuralPartialEq trait corresponds to some notion of "shallow" structural equality, which is not written down in the reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants