a-mir-formality icon indicating copy to clipboard operation
a-mir-formality copied to clipboard

dedicated struct/union/enum forms in rust decl layer

Open nikomatsakis opened this issue 3 years ago • 1 comments

Currently we have one declaration for all ADTs in the decl grammar:

https://github.com/nikomatsakis/a-mir-formality/blob/81ba52b8695db7f87f50b0916940b2bb41fe6d59/src/decl/grammar.rkt#L27-L31

resulting instances like this

    (; struct Foo { }
     AdtDecl_Foo (term (Foo (struct
                              () ; no generic parameters
                              () ; no where clauses
                              ((Foo ())) ; the 1 variant (named `Foo`)
                              ))))

it might be cool to introduce StructDecl, EnumDecl, and friends. We could still factor out the common code by introducing some helper functions to convert those into an "adt decl". Then we could write:

    (; struct Foo { }
     StructDecl_Foo (term (Foo (struct
                              () ; no generic parameters
                              () ; no where clauses
                              () ; list of fields
                              ))))

it's a minor thing, but not having to introduce a "dummy variant" would feel a lot more Rust-like to me.

nikomatsakis avatar May 31 '22 17:05 nikomatsakis

My proposed grammar would be that AdtDecl remains unchanged, but we have

-  (CrateItemDecl ::= FeatureDecl AdtDecl TraitDecl TraitImplDecl ConstDecl FnDecl)
+  (CrateItemDecl ::= FeatureDecl StructDecl EnumDecl UnionDecl TraitDecl TraitImplDecl ConstDecl FnDecl)

and

(StructDecl ::= (AdtId (struct KindedVarIds WhereClauses FieldDecls)))
(UnionDecl ::= (AdtId (union KindedVarIds WhereClauses FieldDecls)))
(EnumDecl ::= AdtDecl)

and then some helper functions struct-decl->adt-decl and so forth.

nikomatsakis avatar May 31 '22 18:05 nikomatsakis