Skip to content

Fix closure resolution in loop headers - #4232

Merged
codeshaunted merged 1 commit into
canaryfrom
avery/b-532
Jul 28, 2026
Merged

Fix closure resolution in loop headers#4232
codeshaunted merged 1 commit into
canaryfrom
avery/b-532

Conversation

@codeshaunted

@codeshaunted codeshaunted commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Issue Reference

B-532

Changes

  • Record arena-qualified local BindingIds during HIR lexical traversal.
  • Consume stable local identities in TIR, MIR, and find-references instead of resolving from source offsets.
  • Preserve the canonical binding identity across or-pattern alternatives.
  • Add for and while loop-header closure regressions.

Testing

  • Unit tests added
  • mise exec -- prek run --all-files
  • Focused loop-header closure regressions
  • Compiler, LSP, and baml_tests library suites: 1,453 passed, 6 ignored

PR Checklist

  • Read and followed the contributing guidelines
  • Code follows the project style guidelines
  • Performed a self-review
  • No documentation changes are required
  • No new warnings

Summary by CodeRabbit

  • Bug Fixes

    • Improved closure, lambda, and parameter-default name/path resolution across nested scopes, including correct handling of shadowing and captured locals.
    • Enhanced assignment and call/lvalue lowering so locals are resolved more reliably for both single- and multi-segment paths.
    • Improved local reference finding for usage queries to better reflect actual capture and nesting behavior.
  • Tests

    • Added lexical scoping regression coverage for closure parameters used in for iterables and while conditions.
    • Added cursor-based usages_at coverage for references inside nested lambda closures.
    • Added prompt runtime coverage for ${ctx.output_format} interpolation companions.

@linear

linear Bot commented Jul 27, 2026

Copy link
Copy Markdown

B-532

@vercel

vercel Bot commented Jul 27, 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 Jul 27, 2026 11:58pm
promptfiddle Ready Ready Preview, Comment Jul 27, 2026 11:58pm
promptfiddle2 Ready Ready Preview, Comment Jul 27, 2026 11:58pm

Request Review

@vercel
vercel Bot temporarily deployed to Preview – beps July 27, 2026 22:44 Inactive
@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 6cdb5cac-9f47-40bf-8991-92c75e248037

📥 Commits

Reviewing files that changed from the base of the PR and between 490db81 and 1d6e212.

⛔ Files ignored due to path filters (2)
  • baml_language/crates/baml_tests/snapshots/baml_src/lexical_scoping.snap is excluded by !**/*.snap
  • baml_language/crates/baml_tests/snapshots/baml_src/prompt_tag_runtime.snap is excluded by !**/*.snap
📒 Files selected for processing (10)
  • baml_language/crates/baml_compiler2_hir/src/builder.rs
  • baml_language/crates/baml_compiler2_hir/src/lib.rs
  • baml_language/crates/baml_compiler2_hir/src/semantic_index.rs
  • baml_language/crates/baml_compiler2_mir/src/lower.rs
  • baml_language/crates/baml_compiler2_tir/src/builder.rs
  • baml_language/crates/baml_lsp2_actions/src/annotations.rs
  • baml_language/crates/baml_lsp2_actions/src/usages.rs
  • baml_language/crates/baml_lsp2_actions/src/usages_at_tests.rs
  • baml_language/crates/baml_tests/baml_src/ns_lexical_scoping/lexical_scoping.baml
  • baml_language/crates/baml_tests/baml_src/ns_prompt_tag_runtime/prompt_tag_runtime.baml
🚧 Files skipped from review as they are similar to previous changes (7)
  • baml_language/crates/baml_tests/baml_src/ns_lexical_scoping/lexical_scoping.baml
  • baml_language/crates/baml_compiler2_hir/src/lib.rs
  • baml_language/crates/baml_lsp2_actions/src/usages.rs
  • baml_language/crates/baml_compiler2_tir/src/builder.rs
  • baml_language/crates/baml_compiler2_hir/src/semantic_index.rs
  • baml_language/crates/baml_compiler2_hir/src/builder.rs
  • baml_language/crates/baml_compiler2_mir/src/lower.rs

📝 Walkthrough

Walkthrough

Expression metadata now uses scope-aware keys and binding-based path resolutions. HIR records body and parameter-default contexts, while TIR, MIR, and LSP usage lookup consume the shared semantic-index APIs. Lambda, lexical-scoping, pattern-binding, and prompt-rendering coverage was added.

Changes

Scoped semantic-index contracts

Layer / File(s) Summary
Semantic index contracts
baml_language/crates/baml_compiler2_hir/src/semantic_index.rs, baml_language/crates/baml_compiler2_hir/src/lib.rs
Adds scoped expression keys, binding-based local resolutions, keyed lookup APIs, and owner-scoped lambda lookup.

Compiler metadata and lowering

Layer / File(s) Summary
HIR metadata collection
baml_language/crates/baml_compiler2_hir/src/builder.rs
Records body and parameter-default expressions under scoped keys and resolves path roots into semantic-index entries.
TIR metadata context
baml_language/crates/baml_compiler2_tir/src/builder.rs
Tracks metadata scopes and queries keyed path resolutions during inference.
MIR metadata and path lowering
baml_language/crates/baml_compiler2_mir/src/lower.rs
Uses shared metadata contracts and binding-aware places for paths, receivers, captures, calls, fallbacks, lvalues, and default roots.
Pattern and LSP resolution
baml_language/crates/baml_compiler2_mir/src/lower.rs, baml_language/crates/baml_lsp2_actions/src/*.rs
Preserves Or-pattern binding roots and resolves usages across nested lambda and parameter-default arenas.

Regression validation

Layer / File(s) Summary
Runtime and usage coverage
baml_language/crates/baml_tests/baml_src/ns_lexical_scoping/lexical_scoping.baml, baml_language/crates/baml_tests/baml_src/ns_prompt_tag_runtime/prompt_tag_runtime.baml, baml_language/crates/baml_lsp2_actions/src/usages_at_tests.rs
Adds closure-parameter, nested-usage, and prompt-companion interpolation tests.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant HIRBuilder
  participant FileSemanticIndex
  participant TIRBuilder
  participant MIRLowering
  participant LSPUsages
  HIRBuilder->>FileSemanticIndex: record scoped expression keys and path resolutions
  TIRBuilder->>FileSemanticIndex: query PathResolution by ExprMetadataKey
  MIRLowering->>FileSemanticIndex: resolve binding identity and expression metadata
  LSPUsages->>FileSemanticIndex: query nested lambda and default-expression paths
  FileSemanticIndex-->>LSPUsages: return matching BindingId
Loading

Possibly related PRs

  • BoundaryML/baml#3425: Touches related semantic-index and binding-identity resolution machinery.
  • BoundaryML/baml#4114: Removes older ExprId-based semantic-index lookup APIs superseded by scoped metadata keys.

Suggested reviewers: hellovai

Poem

A rabbit hops through scopes so neat,
With keys beneath each carrot’s seat.
Paths find bindings, shadows flee,
Lambdas loop most happily.
HIR, TIR, MIR align—
“No more name mix-ups!” says this bunny fine.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately reflects the main user-visible fix around closure resolution in loop headers.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch avery/b-532

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.

@github-actions

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.

Comment thread baml_language/crates/baml_tests/tests/loop_header_closures.rs Outdated
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 27, 2026 22:51 Inactive

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@baml_language/crates/baml_lsp2_actions/src/usages.rs`:
- Around line 212-216: Update the usage collector to derive and pass each
candidate expression’s owning ExprMetadataScope instead of always using
ExprMetadataScope::Body(enclosing_func_scope). Ensure nested closure expressions
resolve through their own body scope so closure parameters and locals are
included, and add a usages_at regression covering nested-closure references.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d78f48ab-c034-4dbd-a3ea-c20186487a41

📥 Commits

Reviewing files that changed from the base of the PR and between f4db7f8 and c27ef90.

📒 Files selected for processing (7)
  • baml_language/crates/baml_compiler2_hir/src/builder.rs
  • baml_language/crates/baml_compiler2_hir/src/lib.rs
  • baml_language/crates/baml_compiler2_hir/src/semantic_index.rs
  • baml_language/crates/baml_compiler2_mir/src/lower.rs
  • baml_language/crates/baml_compiler2_tir/src/builder.rs
  • baml_language/crates/baml_lsp2_actions/src/usages.rs
  • baml_language/crates/baml_tests/tests/loop_header_closures.rs

Comment thread baml_language/crates/baml_lsp2_actions/src/usages.rs Outdated
@github-actions

github-actions Bot commented Jul 27, 2026

Copy link
Copy Markdown

Binary size checks passed

7 passed

Artifact Platform File Gzip Gated on Baseline Delta Status
baml-cli Linux 🔒 27.3 MB 11.6 MB file 27.3 MB +12.6 KB (+0.0%) OK
packed-program Linux 🔒 17.7 MB 7.3 MB file 17.7 MB +16.7 KB (+0.1%) OK
baml-cli macOS 🔒 21.2 MB 10.1 MB file 21.2 MB +16.5 KB (+0.1%) OK
packed-program macOS 🔒 13.8 MB 6.4 MB file 13.8 MB +32 B (+0.0%) OK
baml-cli Windows 🔒 22.8 MB 10.3 MB file 22.8 MB +11.3 KB (+0.0%) OK
packed-program Windows 🔒 14.7 MB 6.5 MB file 14.7 MB +16.2 KB (+0.1%) OK
bridge_wasm WASM 16.6 MB 🔒 4.5 MB gzip 4.5 MB +4.1 KB (+0.1%) 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 – beps July 27, 2026 23:04 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 27, 2026 23:12 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle July 27, 2026 23:25 Inactive
@codeshaunted
codeshaunted enabled auto-merge July 27, 2026 23:43
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 27, 2026 23:45 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle July 27, 2026 23:58 Inactive
@codeshaunted
codeshaunted added this pull request to the merge queue Jul 28, 2026
Merged via the queue into canary with commit 6b23753 Jul 28, 2026
81 checks passed
@codeshaunted
codeshaunted deleted the avery/b-532 branch July 28, 2026 00:13
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