Skip to content

feat: properly parse and resolve tuples#3018

Open
JairusSW wants to merge 26 commits intoAssemblyScript:mainfrom
JairusSW:main
Open

feat: properly parse and resolve tuples#3018
JairusSW wants to merge 26 commits intoAssemblyScript:mainfrom
JairusSW:main

Conversation

@JairusSW
Copy link
Copy Markdown
Contributor

@JairusSW JairusSW commented Apr 22, 2026

This PR adds basic support for the Tuples. Roadmap wise, this, along with a few more PRs, will pave the way for the Multi Value Proposal to be properly implemented.

The goal of this PR is not to add full-fledged support for Tuples. Instead, it supports the basic functionality for parsing and resolving them since optimal Tuple implementation relies on Multi Value support.

The order of the PRs to implement are:

  1. Parse and resolve tuples
  2. Hook up Multi Value support to binaryen
  3. Clean up, test, and enable multi-value by default

Speaking of that, all the features here hide behind --enable multi-value

So far, this implements:

  • Parser

    • Parameters: function foo(x: [i32, i32]) {}
    • Return types: function foo(): [i32, i32] {}
    • Return values syntax: return [0, 1]
    • Type aliases: type Foo = [i32, i32]
    • Type names: [x: f64, y: f64]
  • Compiler

    • Tuple usages error with not implemented (for now)
  • I've read the contributing guidelines

  • I've added my name and email to the NOTICE file

@JairusSW JairusSW marked this pull request as ready for review April 23, 2026 03:52
Comment thread src/program.ts Outdated
Comment thread src/parser.ts Outdated
Comment thread src/parser.ts Outdated
Comment thread src/parser.ts
Comment thread tests/parser/tuple.ts
Comment thread tests/parser/tuple.ts
Comment thread tests/parser/tuple.ts
Comment thread src/parser.ts Outdated
Comment thread src/parser.ts Outdated
Co-authored-by: Max Graey <maxgraey@gmail.com>
Comment thread src/parser.ts Outdated
Comment thread src/program.ts Outdated
Co-authored-by: Max Graey <maxgraey@gmail.com>
Comment thread src/parser.ts Outdated
JairusSW and others added 2 commits April 23, 2026 13:02
Co-authored-by: Max Graey <maxgraey@gmail.com>
Copy link
Copy Markdown
Member

@MaxGraey MaxGraey left a comment

Choose a reason for hiding this comment

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

LGTM

But let someone else take a fresh look at this

@JairusSW JairusSW requested a review from dcodeIO April 23, 2026 19:27
Comment thread tests/compiler/tuple-errors.json Outdated
Comment thread tests/parser/tuple.ts.fixture.ts Outdated
Copy link
Copy Markdown
Member

@HerrCai0907 HerrCai0907 left a comment

Choose a reason for hiding this comment

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

LGTM.
But I think tuple does not rely on multi-value. Here is my implement of tuple in AS. maybe you can ref it if you are interesting to implement the whole tuple support. wasm-ecosystem/warpo#269

Comment thread src/parser.ts
type.range.start = startPos;

// '[' ((Identifier ':')? Type (',' (Identifier ':')? Type)*)? ']'
} else if (token == Token.OpenBracket && this.options && this.options!.hasFeature(Feature.MultiValue)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Actually tuple doesn't and is impossible to rely on mutli-value.

declare function f(): [i32,i32];
function b() {
  let a = f();
  let b = a;
  b[0] = 1;
  assert(a[0] == 1);
}

In this cases, multiple value cannot handle this case since it treat the whole tuple as a value instead of a heap object.

Copy link
Copy Markdown
Contributor Author

@JairusSW JairusSW Apr 25, 2026

Choose a reason for hiding this comment

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

@HerrCai0907, I'll add full support for tuples in the future. Tuples will need to exist in two forms:

  1. Tuple becomes multi value when returned from function
  2. When used as a heap type (eg. store<[i32, i32]>(), Array<[i32, i32]>), it becomes a heap type

Then, depending on usage, tuples will be lifted and lowered from multi-value to tuple

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.

4 participants