Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
petrochenkov opened this issue Aug 10, 2019 · 4 comments · Fixed by #65519
Closed
Assignees
Labels
A-const-generics Area: const generics (parameters and arguments) A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-resolve Area: Name resolution A-traits Area: Trait system A-typesystem Area: The type system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@petrochenkov
Copy link
Contributor

"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

@Centril Centril added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. A-const-generics Area: const generics (parameters and arguments) A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-resolve Area: Name resolution labels Aug 10, 2019
@Centril
Copy link
Contributor

Centril commented Aug 10, 2019

cc #60286

@Centril Centril added A-traits Area: Trait system A-typesystem Area: The type system labels Aug 10, 2019
@matthewjasper
Copy link
Contributor

cc #61188

@pnkfelix
Copy link
Member

I'm in favor of the second suggested approach, namely:

an impl of some third trait impl StructuralMatch for ... (in this case the const check will be "implements Eq + implements StructuralMatch").

@pnkfelix
Copy link
Member

pnkfelix commented Oct 7, 2019

(i've been working on this and have a plausible prototype. I hope to have a PR up soon.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-resolve Area: Name resolution A-traits Area: Trait system A-typesystem Area: The type system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants