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 "implementsEq
+ implementsPartialEq
+ thePartialEq
impl has the attribute"), or - an impl of some third trait
impl StructuralMatch for ...
(in this case the const check will be "implementsEq
+ implementsStructuralMatch
").
Metadata
Metadata
Assignees
Labels
Area: const generics (parameters and arguments)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: Name/path resolution done by `rustc_resolve` specificallyArea: Trait systemArea: Type systemRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the language team, which will review and decide on the PR/issue.