feat(syntax,semantic,lowering): support tuple struct syntax (struct Foo(T1, T2))#9885
Conversation
Draft
5 tasks
Collaborator
Author
|
Warning This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
This stack of pull requests is managed by Graphite. Learn more about stacking. |
5 tasks
41506df to
5862e6d
Compare
5e73005 to
3574eae
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Adds support for tuple struct syntax in Cairo, allowing structs to be defined with positional, unnamed fields using parentheses:
struct Foo(pub u32, u64). This introduces a newStructBodyenum in the AST with two variants —StructBodyBraces(the existing{ field: Type }form) andStructBodyParens(the new(Type, Type)form) — replacing the flatlbrace/members/rbracefields onItemStruct.New AST nodes
StructArgMember,StructArgMemberList,StructBodyBraces, andStructBodyParensare added to the syntax spec and generated code. The parser is extended to detect a leading(after the struct name and parse aStructArgMemberListinstead of aMemberList.On the semantic side,
StructDefinitionDatagains apositional_tysfield (aVec<(TypeId, Visibility)>) populated for tuple structs. Two new Salsa queries —struct_positional_typesandconcrete_struct_positional_types— expose these types with generic substitution applied.type_membersis updated to returnTupleElement-kinded members (keyed by stringified index, e.g."0","1") for tuple structs, enabling member access expressions likep.0.Lowering is updated so that
TupleElementaccess on a concrete struct type fetches positional types viaconcrete_struct_positional_typesrather than assuming the container is always aTypeLongId::Tuple.The
MemberId::struct_idparent pointer traversal is corrected from 2 levels to 3 levels up to account for the newStructBodyBracesintermediate node.All call sites that previously called
struct_ast.members(db)directly are updated to first match onStructBody::Braces, gracefully skipping or returning early for tuple structs.Type of change
Please check one:
Why is this change needed?
Cairo previously only supported braced struct syntax. Tuple structs (positional, unnamed fields) are a common pattern in Rust-like languages and are needed to support use cases where field names are unnecessary or where struct types should behave similarly to tuples with named types.
What was the behavior or documentation before?
Structs could only be declared with named fields inside braces:
struct Foo { x: u32, y: u64 }. TheItemStructAST node directly heldlbrace,members, andrbracechildren.What is the behavior or documentation after?
Structs can additionally be declared with positional fields inside parentheses:
struct Foo(pub u32, u64). Fields are accessed by numeric index (e.g.,foo.0). TheItemStructAST node now holds a singlebodychild of typeStructBody, which is eitherStructBodyBracesorStructBodyParens.Related issue or discussion (if any)
N/A
Additional context
Parser test data snapshots (
test1,test2,attrs) are updated to reflect the newStructBodyBraceswrapper node in the AST. A new unit testtest_tuple_structverifies positional type extraction, visibility, andtype_membersbehavior for tuple structs.