Sitelet https://github.com/starkware-libs/cairo/pull/10111
Skip to content

feat(semantic): support tuple element access via t.0 syntax - #10111

Merged
TomerStarkware merged 1 commit into
mainfrom
tomer/tuple-index-semantic
Jun 24, 2026
Merged

TomerStarkware merged 1 commit into
mainfrom
tomer/tuple-index-semantic

Conversation

@TomerStarkware

Copy link
Copy Markdown
Collaborator

Adds positional tuple element access by index using dot notation (e.g. t.0, t.1), resolved in the semantic model. Member access is generalized via a new MemberAccessKind enum (Struct{..} | Index{..}) threaded through ExprMemberAccess, ExprVarMemberPath and usage::MemberPath, so a member is identified either by a struct member id or by a numeric tuple index.

The index must be a plain non-negative decimal literal (no base prefix or type suffix); out-of-bounds and non-tuple accesses are reported. Lowering of tuple index access is not yet implemented and currently reports an "Unsupported" error; it will be added in a follow-up.

@TomerStarkware
TomerStarkware requested a review from orizi June 16, 2026 15:45
@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi made 1 comment.
Reviewable status: 0 of 17 files reviewed, 1 unresolved discussion (waiting on TomerStarkware).


crates/cairo-lang-semantic/src/expr/compute.rs line 3776 at r1 (raw file):

/// Computes the semantic model of a tuple-index access expression (e.g. "expr.0").
fn tuple_index_access_expr<'db>(

why can't this be fully part of the member access expr handling?

@TomerStarkware
TomerStarkware force-pushed the tomer/tuple-index-semantic branch from 0aa64af to af4bfbb Compare June 21, 2026 12:59

@TomerStarkware TomerStarkware left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomerStarkware made 1 comment.
Reviewable status: 0 of 18 files reviewed, 1 unresolved discussion (waiting on orizi).


crates/cairo-lang-semantic/src/expr/compute.rs line 3776 at r1 (raw file):

Previously, orizi wrote…

why can't this be fully part of the member access expr handling?

Done.

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi reviewed 14 files and all commit messages, made 1 comment, and resolved 1 discussion.
Reviewable status: 14 of 18 files reviewed, 1 unresolved discussion (waiting on TomerStarkware).


crates/cairo-lang-semantic/src/expr/compute.rs line 4191 at r3 (raw file):

            TypeLongId::Tuple(tys) => tuple_index_members(ctx.db, &tys, *explored_derefs),
            _ => continue,
        };

make the helpers return iterators - and avoid the extra map allocations.

Code quote:

        let (_, long_ty) = finalized_snapshot_peeled_ty(ctx, deref_info.target_ty, stable_ptr)?;
        let new_members = match long_ty {
            TypeLongId::Concrete(ConcreteTypeId::Struct(concrete_struct_id)) => {
                let members = ctx.db.concrete_struct_members(concrete_struct_id)?;
                struct_enriched_members(concrete_struct_id, members, *explored_derefs)
            }
            TypeLongId::Tuple(tys) => tuple_index_members(ctx.db, &tys, *explored_derefs),
            _ => continue,
        };

@TomerStarkware
TomerStarkware force-pushed the tomer/tuple-index-semantic branch from 9d86e35 to b57a97e Compare June 23, 2026 11:41
TomerStarkware added a commit that referenced this pull request Jun 23, 2026
Implements the lowering half of `t.0` tuple-index member access, building
on the semantic support (#10111).

Generalizes the member-access machinery from struct-only to any aggregate
(struct or tuple):
- Replace `concrete_struct_id`-keyed scattering with a `TypeId`-keyed
  `Scattered`, keyed by `MemberAccessKind` instead of `MemberId`.
- Add `aggregate_ty`, `aggregate_members`, and `member_access_components`
  helpers in `refs.rs` that handle both structs and tuples uniformly.
- Handle `MemberAccessKind::Index` in member-access lowering, ref-binding,
  and struct deconstruct/reconstruct.

Adds lowering test_data and corelib tests covering tuple index read,
nested access, assignment, `ref` arguments, and access through snapshots.

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi reviewed 3 files and all commit messages, made 2 comments, and resolved 1 discussion.
Reviewable status: 16 of 18 files reviewed, 2 unresolved discussions (waiting on TomerStarkware).


crates/cairo-lang-semantic/src/expr/compute.rs line 4195 at r4 (raw file):

            // Insert member if there is not already a member with the same name.
            enriched.entry(name).or_insert(value);
        }

rather not use the Either

Suggestion:

        match &long_ty {
            TypeLongId::Concrete(ConcreteTypeId::Struct(concrete_struct_id)) => {
                let members = ctx.db.concrete_struct_members(*concrete_struct_id)?;
                for (name, value) in struct_enriched_members(
                    *concrete_struct_id,
                    members,
                    *explored_derefs,
                )) {
                    enriched.entry(name).or_insert(value);
                }
            }
            TypeLongId::Tuple(tys) => {
                for (name, value) in tuple_index_members(ctx.db, tys, *explored_derefs) {
                    enriched.entry(name).or_insert(value);
                }
            }
            _ => continue,
        };

crates/cairo-lang-semantic/src/resolve/mod.rs line 165 at r4 (raw file):

    /// How the member is accessed: a struct member id or a tuple index.
    pub kind: MemberAccessKind<'db>,
    /// The (unsnapshotted) type of the member.

unsnapshotted?

Code quote:

    /// The (unsnapshotted) type of the member.

@TomerStarkware
TomerStarkware force-pushed the tomer/tuple-index-semantic branch from b57a97e to 18e7f93 Compare June 23, 2026 14:33
TomerStarkware added a commit that referenced this pull request Jun 23, 2026
Implements the lowering half of `t.0` tuple-index member access, building
on the semantic support (#10111).

Generalizes the member-access machinery from struct-only to any aggregate
(struct or tuple):
- Replace `concrete_struct_id`-keyed scattering with a `TypeId`-keyed
  `Scattered`, keyed by `MemberAccessKind` instead of `MemberId`.
- Add `aggregate_ty`, `aggregate_members`, and `member_access_components`
  helpers in `refs.rs` that handle both structs and tuples uniformly.
- Handle `MemberAccessKind::Index` in member-access lowering, ref-binding,
  and struct deconstruct/reconstruct.

Adds lowering test_data and corelib tests covering tuple index read,
nested access, assignment, `ref` arguments, and access through snapshots.
@TomerStarkware
TomerStarkware force-pushed the tomer/tuple-index-semantic branch from 18e7f93 to 68342c1 Compare June 23, 2026 14:41

@TomerStarkware TomerStarkware left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomerStarkware made 3 comments.
Reviewable status: 15 of 18 files reviewed, 2 unresolved discussions (waiting on orizi).


crates/cairo-lang-semantic/src/expr/compute.rs line 4191 at r3 (raw file):

Previously, orizi wrote…

make the helpers return iterators - and avoid the extra map allocations.

Done.


crates/cairo-lang-semantic/src/expr/compute.rs line 4195 at r4 (raw file):

Previously, orizi wrote…

rather not use the Either

Done.


crates/cairo-lang-semantic/src/resolve/mod.rs line 165 at r4 (raw file):

Previously, orizi wrote…

unsnapshotted?

removed

TomerStarkware added a commit that referenced this pull request Jun 23, 2026
Implements the lowering half of `t.0` tuple-index member access, building
on the semantic support (#10111).

Generalizes the member-access machinery from struct-only to any aggregate
(struct or tuple):
- Replace `concrete_struct_id`-keyed scattering with a `TypeId`-keyed
  `Scattered`, keyed by `MemberAccessKind` instead of `MemberId`.
- Add `aggregate_ty`, `aggregate_members`, and `member_access_components`
  helpers in `refs.rs` that handle both structs and tuples uniformly.
- Handle `MemberAccessKind::Index` in member-access lowering, ref-binding,
  and struct deconstruct/reconstruct.

Adds lowering test_data and corelib tests covering tuple index read,
nested access, assignment, `ref` arguments, and access through snapshots.

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

@orizi reviewed 3 files and all commit messages, made 1 comment, and resolved 2 discussions.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

@TomerStarkware
TomerStarkware force-pushed the tomer/tuple-index-semantic branch from 68342c1 to 43cf84b Compare June 24, 2026 08:04

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi reviewed 8 files and all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

Adds positional tuple element access by index using dot notation (e.g. `t.0`,
`t.1`), resolved in the semantic model. Member access is generalized via a new
`MemberAccessKind` enum (`Struct{..}` | `Index{..}`) threaded through
`ExprMemberAccess`, `ExprVarMemberPath` and `usage::MemberPath`, so a member is
identified either by a struct member id or by a numeric tuple index.

The index must be a plain non-negative decimal literal (no base prefix or type
suffix); out-of-bounds and non-tuple accesses are reported. Lowering of tuple
index access is not yet implemented and currently reports an "Unsupported"
error; it will be added in a follow-up.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@TomerStarkware
TomerStarkware force-pushed the tomer/tuple-index-semantic branch from 43cf84b to 9cb2c32 Compare June 24, 2026 10:38

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi reviewed 2 files and all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

@TomerStarkware
TomerStarkware added this pull request to the merge queue Jun 24, 2026
Merged via the queue into main with commit ad769e3 Jun 24, 2026
53 checks passed
@TomerStarkware
TomerStarkware deleted the tomer/tuple-index-semantic branch June 24, 2026 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants