Skip to content

"Structural match" annotations should be moved from struct/enum items to impl items #63438

Closed

Description

"Structural match" property of a type means that the constants of this type can be used in patterns (#31434) or in const generics in the future.

Right now this property can be given to a type in two ways:

  • #[structural_match] attribute, rarely used, PhantomData is the only example.
  • If the struct/enum has both #[derive(PartialEq)] and #[derive(Eq)] then it's marked as "structural match". Derives cannot modify the items they are applied to, so this tracking is done through a special table in resolver (which is kind of a hack).

To avoid hacks we need to pass the knowledge about "structural match" not through types themselves, but through impls on them (which can be generated by derives easily).

The condition for the type being "structural match" is:

  • PartialEq methods are derived so their behavior is fully known to the compiler.
  • Eq is implemented, but not necessarily derived, it's just a marker without methods.

So, the "structural match" annotation needs to be generated by derive(PartialEq) in the form of either

  • an attribute #[structural_match] impl PartialEq for ... (in this case the const check will be "implements Eq + implements PartialEq + the PartialEq impl has the attribute"), or
  • an impl of some third trait impl StructuralMatch for ... (in this case the const check will be "implements Eq + implements StructuralMatch").

cc #63248 @matthewjasper @varkor @Centril @pnkfelix

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

Metadata

Assignees

Labels

A-const-genericsArea: const generics (parameters and arguments)Area: const generics (parameters and arguments)A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-resolveArea: Name resolutionArea: Name resolutionA-traitsArea: Trait systemArea: Trait systemA-typesystemArea: The type systemArea: The type systemT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team, which will review and decide on the PR/issue.Relevant to the language 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