Skip to content

feat(stdlib): baml.iter lazy iterator protocol (WIP / status snapshot) - #3701

Closed
hellovai wants to merge 1 commit into
hellovai/iterators2from
hellovai/iterators
Closed

hellovai wants to merge 1 commit into
hellovai/iterators2from
hellovai/iterators

Conversation

@hellovai

@hellovai hellovai commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Draft / status snapshot. Stacked on #3698 (hellovai/iterators2). Base it there; do not merge into canary until #3698 lands. This PR documents where the baml.iter lazy-iterator protocol stands and what compiler bugs still block it.

What this adds

A pure-BAML baml.iter namespace under crates/baml_builtins2/baml_std/baml/ns_iter/:

  • iter.baml — Done sentinel, Iterable<T, E> / Iterator<T, E> requires Iterable<T, E> interfaces, lazy adapter methods (map/filter/filter_map/flat_map/peekable/step_by/chain) and consumer methods (collect/reduce/count/some/every/find).
  • utility.baml — ArrayIterator<T>, Range, Repeat<T> sources.
  • adapters.baml — Map/Filter/FilterMap/FlatMap/Flatten/Peekable/StepBy/Chain adapter classes.

All implements blocks are in-body. The out-of-body implements<T> Iface for Class<T> form does not dispatch correctly for inferred generic instantiations (see Known issues), so everything is written in-body.

Also adds #![allow(clippy::iter_not_returning_iterator)] in bex_vm/src/package_baml — the BAML iter() method generates native glue literally named iter returning Value, which trips that lint on generated code.

Status

Verified against this branch (cargo test -p baml_tests --test baml_src, namespace iter):

✅ Working

  • ArrayIterator.new(...) and Range.new(...) sources
  • consumers: collect, reduce, count
  • chain

These pass because #3698 fixes inferred-generic type-arg threading for in-body implementors, and Chain<T, E> implements Iterator<T, E> (no union error arg).

❌ Blocked — each on a distinct, still-open compiler bug

Feature Blocking bug
.map() / .filter() / .filter_map() / .flat_map() Union error-arg interface coercion. An adapter whose interface instantiation has a union error arg — Iterator<R, E | E2> — fails to subtype-coerce. Even an explicit Map<int,int,never,never>{} is rejected as Iterator<int,never>. This is the main blocker for the closure adapters.
.step_by() Its next() uses a C-style for loop, which is separately buggy in the VM.
[1,2,3].iter() The T[] blanket implements (out-of-body, on a builtin array type) does not attach .iter() — type int[] has no member iter.
out-of-body implements<T> Iface for Class<T> + inferred ctor Still misdispatches (the in-body conversion is the workaround used here).

The blocked cases are kept in ns_iter/iter.baml as commented-out tests with the bug each waits on.

Repro for the headline remaining bug

Minimal, verified repro for the union error-arg coercion issue is the natural failure of it.map(f).collect(); the earlier inferred-static-ctor dispatch bug (now fixed by #3698) has a standalone repro in the task notes.

Not done / out of scope here

  • for (let x in iterable) desugaring (TIR/MIR) — deferred.
  • Native $rust_function next() + ForIter opcodes (perf) — deferred.
  • Python/TypeScript host projection — deferred.
  • Native next() for the closure adapters depends on the union-coercion fix first.

🤖 Generated with Claude Code

Adds the `baml.iter` namespace (Iterator/Iterable, Done sentinel,
ArrayIterator/Range/Repeat sources, lazy adapters, and consumer methods),
implemented in pure BAML. All `implements` blocks are IN-BODY (the
out-of-body `implements<T> Iface for Class<T>` form does not yet dispatch
correctly for inferred generic instantiations).

Stacked on #3698 (thread inferred generic type args to callee frames),
which fixes the core dispatch bug for in-body implementors.

Working today (see crates/baml_tests/baml_src/ns_iter): ArrayIterator/Range
sources, `collect`/`reduce`/`count`, and `chain`.

Known-blocked (documented in the test file), each on a distinct compiler bug:
- `.map()`/`.filter()`/`.filter_map()`/`.flat_map()` — adapter whose interface
  instantiation has a union error arg `Iterator<R, E | E2>` fails to subtype-coerce.
- `.step_by()` — its `next()` uses a C-style `for` loop (separately buggy).
- `[1,2,3].iter()` — the `T[]` blanket `implements` does not attach `.iter()`.

Also adds `#![allow(clippy::iter_not_returning_iterator)]` in bex_vm's
package_baml so the generated native glue for the BAML `iter()` method lints clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@vercel

vercel Bot commented Jun 4, 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 4, 2026 11:39pm
promptfiddle Ready Ready Preview, Comment Jun 4, 2026 11:39pm
promptfiddle2 Ready Ready Preview, Comment Jun 4, 2026 11:39pm

Request Review

@coderabbitai

coderabbitai Bot commented Jun 4, 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: 0519da3d-f7d1-4271-8a97-4887fe744b93

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 hellovai/iterators

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.

@vercel
vercel Bot temporarily deployed to Preview – beps June 4, 2026 23:21 Inactive
@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

⏭️ Performance benchmarks were skipped

Perf benchmarks (CodSpeed) are opt-in on pull requests — they no longer run on every push. They always run automatically after merge to canary/main.

To run them on this PR, do any of the following, then push a commit (or re-run CI):

  • Add RUN_CODSPEED=1 to the PR description, or
  • Include run-perf or /perf in the PR title or any commit message.

@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 June 4, 2026 23:26 Inactive
@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

Binary size checks passed

✅ 7 passed

Artifact Platform File Gzip Gated on Baseline Delta Status
✅ baml-cli Linux 🔒 17.8 MB 7.6 MB file 24.1 MB -6.3 MB (-26.2%) OK
✅ packed-program Linux 🔒 13.1 MB 5.5 MB file 15.1 MB -2.1 MB (-13.7%) OK
✅ baml-cli macOS 🔒 13.6 MB 6.6 MB file 18.2 MB -4.6 MB (-25.4%) OK
✅ packed-program macOS 🔒 10.0 MB 4.8 MB file 11.5 MB -1.5 MB (-13.1%) OK
✅ baml-cli Windows 🔒 14.6 MB 6.7 MB file 19.6 MB -5.0 MB (-25.4%) OK
✅ packed-program Windows 🔒 10.5 MB 4.8 MB file 12.2 MB -1.7 MB (-13.9%) OK
✅ bridge_wasm WASM 11.9 MB 🔒 3.4 MB gzip 3.9 MB -498.1 KB (-12.9%) OK

🔒 = the size this artifact is GATED on (ceiling + delta). Binaries gate on file size (installed binary); WASM gates on gzip (download size). The other size is shown for information only.


Generated by cargo size-gate · workflow run

@vercel
vercel Bot temporarily deployed to Preview – promptfiddle June 4, 2026 23:39 Inactive
@aaronvg

aaronvg commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

implemented

@aaronvg aaronvg closed this Jun 8, 2026

This branch was previously deployed

3 inactive deployments
Preview – promptfiddle — ee16c962 Deployed Jun 4, 2026 by vercel[bot]
Preview – promptfiddle2 — ee16c962 Deployed Jun 4, 2026 by vercel[bot]
Preview – beps — ee16c962 Deployed Jun 4, 2026 by vercel[bot]
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