Skip to content

Add indexed Array.set and fixed-size array constructor#3716

Draft
ATX24 wants to merge 1 commit into
canaryfrom
cursor/mutable-array-access-5c28
Draft

Add indexed Array.set and fixed-size array constructor#3716
ATX24 wants to merge 1 commit into
canaryfrom
cursor/mutable-array-access-5c28

Conversation

@ATX24

@ATX24 ATX24 commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

The error

A mutable random-write array update was not expressible. Before this change, the smallest .set repro failed:

function main() -> int {
  let dp = [0, 0, 0, 0, 0, 0];
  dp.set(5, 9);
  dp.at(5) ?? 0
}

Old compiler output:

error: type `int[]` has no member `set`
   ╭─[ main.baml:2:31 ]
   │
 2 │ ╭─▶   let dp = [0, 0, 0, 0, 0, 0];
 3 │ ├─▶   dp.set(5, 9);
   │ │                     
   │ ╰───────────────────── type `int[]` has no member `set`
   │     
   │     Note: Error code: E0007
───╯

The requested fixed-size constructor syntax also failed before the change:

function main() -> int {
  let dp = int[](6, 0);
  dp.set(5, 9);
  dp.at(5) ?? 0
}

Old compiler output:

error: unresolved name: int
   ╭─[ main.baml:2:12 ]
   │
 2 │   let dp = int[](6, 0);
   │            ─┬─  
   │             ╰─── unresolved name: int
   │ 
   │ Note: Error code: E0003
───╯

error: Expected expression, found ']'
   ╭─[ main.baml:2:16 ]
   │
 2 │   let dp = int[](6, 0);
   │                ┬  
   │                ╰── Expected expression, found ']'
   │ 
   │ Note: Error code: E0010
───╯

error: Expected ']', found ';'
   ╭─[ main.baml:2:23 ]
   │
 2 │   let dp = int[](6, 0);
   │                       ┬  
   │                       ╰── Expected ']', found ';'
   │ 
   │ Note: Error code: E0010
───╯

Root cause

  • crates/baml_builtins2/baml_std/baml/containers.baml defined Array<T> methods such as at, push, and splice, but did not define an indexed set method or any fixed-size constructor.
  • crates/baml_compiler_parser/src/parser.rs only treated [] in expression position as array literals or indexing, so int[](6, 0) was parsed as a value path int followed by an invalid empty index expression rather than as an array constructor.
  • crates/baml_compiler2_tir/src/builder.rs::try_container_method_call only had evolving-container special handling for push/append, so unannotated arrays could not establish or validate element type through .set.
  • crates/bex_vm/src/package_baml/array.rs had no native runtime implementation for either operation.

The fix

  • Added Array<T>.set(index: int, value: T) -> null throws baml.errors.InvalidArgument to the stdlib and implemented it in the VM array package with negative/out-of-bounds validation.
  • Added Array<T>.new(size: int, default: T) -> T[] throws baml.errors.InvalidArgument and parser/AST lowering for T[](size, default) as sugar for baml.Array.new<T>(size, default).
  • Extended TIR evolving-array mutation handling so let xs = []; xs.set(i, v) type-checks consistently with push, including index validation as int and value validation against the element type.
  • Added focused engine tests for successful preallocation + indexed write, out-of-bounds set, and negative constructor size.
  • Updated stdlib/compiler snapshots and compiler2 mutability docs.

Verification

Reproduction after the change:

PATH="$HOME/.local/bin:$PATH" RUSTFLAGS="-C link-arg=-lnode" \
  cargo run -q -p baml_cli --manifest-path /workspace/baml_language/Cargo.toml -- \
  check --from /tmp/baml-array-project

Output:

warning: using the internal BAML toolchain binary directly is not recommended. Use `baml` instead.
     Loading /tmp/baml-array-project/baml_src/main.baml
    Checking 1 file(s)
   Compiling 1 file(s)
    Finished checked 1 file(s) in 4s
PATH="$HOME/.local/bin:$PATH" RUSTFLAGS="-C link-arg=-lnode" \
  cargo run -q -p baml_cli --manifest-path /workspace/baml_language/Cargo.toml -- \
  run main --from /tmp/baml-array-project

Output:

warning: using the internal BAML toolchain binary directly is not recommended. Use `baml` instead.
     Loading /tmp/baml-array-project/baml_src/main.baml
    Checking 1 file(s)
   Compiling 1 file(s)
warning: Your code is unformatted — run `baml fmt` to format it. Continuing.
     Running main
9
    Finished main in 4s

Test commands run from baml_language/:

cargo test -p bex_engine --test array_set_constructor
# test result: ok. 3 passed; 0 failed; 0 ignored

PATH="$HOME/.local/bin:$PATH" RUSTFLAGS="-C link-arg=-lnode" cargo test --lib
# passed

PATH="$HOME/.local/bin:$PATH" RUSTFLAGS="-C link-arg=-lnode" cargo test --workspace --exclude sdk_test_typescript_node
# exit_code: 0

CI=true PATH="$HOME/.local/bin:$PATH" ./sdk_tests/crates/typescript_node/setup.sh
PATH="$HOME/.local/bin:$PATH" CARGO_MANIFEST_DIR="/workspace/baml_language/sdk_tests/crates/typescript_node" \
  ./target/debug/deps/sdk_test_typescript_node-0df1538f20582eac
# test result: ok. 15 passed; 0 failed; 0 ignored

Note: sdk_test_typescript_node is run with its documented setup split because sdk_tests/README.md states plain cargo test regenerates the TypeScript fixtures without running their setup script.

Open in Web Open in Cursor 

@vercel

vercel Bot commented Jun 8, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
beps Ready Ready Preview, Comment Jun 8, 2026 7:27pm
promptfiddle Ready Ready Preview, Comment Jun 8, 2026 7:27pm
promptfiddle2 Ready Ready Preview, Comment Jun 8, 2026 7:27pm

Request Review

@coderabbitai

coderabbitai Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: ddc1733d-c4d1-4d8f-a41f-927682c0c60d

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch cursor/mutable-array-access-5c28

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

2 participants