Skip to content

fix(compiler): allow reassigning narrowed inferred locals - #4212

Closed
antoniosarosi wants to merge 2 commits into
canaryfrom
antoniosarosi/b-618
Closed

antoniosarosi wants to merge 2 commits into
canaryfrom
antoniosarosi/b-618

Conversation

@antoniosarosi

@antoniosarosi antoniosarosi commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Preserve the declaration-site inferred type as the stable assignment bound for unannotated locals.
  • Keep flow narrowing read-only and update the flow type after assignment.
  • Add regressions for valid union reassignment, narrowed and re-widened reads, and invalid assignments.

Root cause

Unannotated locals only retained their flow-sensitive current type. An is check therefore replaced the only type available to assignment checking, so the narrowed branch rejected values allowed by the original inferred declaration type.

Validation

  • cargo fmt --all
  • cargo clippy -p baml_compiler2_tir -p baml_tests --lib -- -D warnings
  • cargo test -p baml_tests compiler2_tir::phase7 -- --nocapture
  • cargo test -p baml_tests compiler2_tir::inference -- --nocapture
  • cargo test -p baml_tests compiler2_tir::phase6 -- --nocapture
  • standalone baml-cli repro

Fixes B-618

Summary by CodeRabbit

  • Bug Fixes

    • Improved type checking for reassignment after type narrowing by preserving an inferred declaration-site type and using it for compatibility validation.
    • Kept narrowed reads from overwriting the stable inferred declaration type, while still allowing narrowing to be reevaluated after assignment.
    • Added more precise type mismatch diagnostics when reassigned values fall outside the inferred union type.
  • Tests

    • Added new Phase 7 tests for reassignment behavior with inferred union types after is narrowing.
    • Updated loop syntax test expected diagnostics to include the new mismatch span.

@linear

linear Bot commented Jul 26, 2026

Copy link
Copy Markdown

B-618

@vercel

vercel Bot commented Jul 26, 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 26, 2026 2:24pm
promptfiddle Ready Ready Preview, Comment Jul 26, 2026 2:24pm
promptfiddle2 Ready Ready Preview, Comment Jul 26, 2026 2:24pm

Request Review

@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.

@coderabbitai

coderabbitai Bot commented Jul 26, 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: 3f54a7a1-6582-4167-b301-481b04462473

📥 Commits

Reviewing files that changed from the base of the PR and between 277fc12 and 6061d19.

📒 Files selected for processing (1)
  • baml_language/crates/baml_lsp2_actions_tests/test_files/syntax/loops/for.baml

📝 Walkthrough

Walkthrough

LocalBinding now preserves inferred declaration-site types separately from flow-sensitive types. Reassignment checks use the stable type, invalidate prior narrowing after assignment, and add Phase 7 coverage for compatible and incompatible union reassignments.

Changes

Inferred reassignment contracts

Layer / File(s) Summary
Local binding contract and initialization
baml_language/crates/baml_compiler2_tir/src/builder.rs, baml_language/crates/baml_compiler2_tir/src/narrowing.rs
Adds inferred_declared_ty, populates it for unannotated initializations, and initializes it across other binding creation paths.
Stable reassignment validation
baml_language/crates/baml_compiler2_tir/src/builder.rs, baml_language/crates/baml_tests/src/compiler2_tir/phase7.rs, baml_language/crates/baml_lsp2_actions_tests/test_files/syntax/loops/for.baml
Validates assignments against the stable inferred type, resets current_ty after simple-path assignments, and updates narrowing and diagnostic coverage.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Narrowing
  participant LocalBinding
  participant Reassignment
  Narrowing->>LocalBinding: store narrowed current_ty
  Reassignment->>LocalBinding: read inferred_declared_ty
  Reassignment->>Reassignment: check assignment compatibility
  Reassignment->>LocalBinding: replace current_ty with assigned type
Loading

Possibly related PRs

  • BoundaryML/baml#3779: Updates reassignment and type-compatibility logic for unannotated locals in the same TIR builder.

Suggested reviewers: codeshaunted

Poem

I’m a rabbit guarding types in a burrow so bright,
Stable unions keep reassignment just right.
Narrow, then widen, let the flow gently bend,
Reject stray booleans before errors begin.
Hop through the tests—green carrots in sight!

🚥 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 clearly matches the main compiler change: allowing reassignment of narrowed inferred locals.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch antoniosarosi/b-618

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.

@vercel
vercel Bot temporarily deployed to Preview – beps July 26, 2026 13:53 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 26, 2026 14:00 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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
baml_language/crates/baml_compiler2_tir/src/builder.rs (1)

7622-7724: 🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift

Use current_ty or update inferred_declared_ty for evolved containers.

For unannotated locals, assign_local(...) only updates current_ty, while get_inferred_declared_type(...) returns the declaration-time type. So a.push(1) makes a: EvolvingList(int), but a later a = ["hi"] still checks against the still EvolvingList(Never) stable bound and is accepted. Make the mutable-update sites also update inferred_declared_ty, or keep this branch checking current_ty for this compatibility decision.

🤖 Prompt for 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.

In `@baml_language/crates/baml_compiler2_tir/src/builder.rs` around lines 7622 -
7724, Update the unannotated-local assignment branch around inferred_declared_ty
and reassignment_is_compatible so evolved container types from prior
assign_local updates are used for compatibility checks. Either make mutable
container updates refresh inferred_declared_ty or use the local’s current_ty for
this decision, ensuring a.push(1) followed by a = ["hi"] is rejected while
preserving existing narrowing behavior.
🤖 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.

Outside diff comments:
In `@baml_language/crates/baml_compiler2_tir/src/builder.rs`:
- Around line 7622-7724: Update the unannotated-local assignment branch around
inferred_declared_ty and reassignment_is_compatible so evolved container types
from prior assign_local updates are used for compatibility checks. Either make
mutable container updates refresh inferred_declared_ty or use the local’s
current_ty for this decision, ensuring a.push(1) followed by a = ["hi"] is
rejected while preserving existing narrowing behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 33ab7a44-5f87-4f71-b5e7-60671beee9b9

📥 Commits

Reviewing files that changed from the base of the PR and between 788d5c0 and 277fc12.

📒 Files selected for processing (3)
  • baml_language/crates/baml_compiler2_tir/src/builder.rs
  • baml_language/crates/baml_compiler2_tir/src/narrowing.rs
  • baml_language/crates/baml_tests/src/compiler2_tir/phase7.rs

@vercel
vercel Bot temporarily deployed to Preview – beps July 26, 2026 14:03 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 26, 2026 14:11 Inactive
@github-actions

Copy link
Copy Markdown

Binary size checks passed

✅ 7 passed

Artifact Platform File Gzip Gated on Baseline Delta Status
✅ baml-cli Linux 🔒 27.2 MB 11.6 MB file 27.1 MB +152.3 KB (+0.6%) OK
✅ packed-program Linux 🔒 17.6 MB 7.3 MB file 17.6 MB +7.0 KB (+0.0%) OK
✅ baml-cli macOS 🔒 21.2 MB 10.1 MB file 21.0 MB +149.1 KB (+0.7%) OK
✅ packed-program macOS 🔒 13.8 MB 6.4 MB file 13.8 MB +33.1 KB (+0.2%) OK
✅ baml-cli Windows 🔒 22.8 MB 10.3 MB file 22.7 MB +127.5 KB (+0.6%) OK
✅ packed-program Windows 🔒 14.7 MB 6.5 MB file 14.7 MB +6.0 KB (+0.0%) OK
✅ bridge_wasm WASM 16.5 MB 🔒 4.5 MB gzip 4.5 MB +19.4 KB (+0.4%) 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

@antoniosarosi

Copy link
Copy Markdown
Contributor Author

Closing — this was an orchestration experiment run; not landing.

@antoniosarosi
antoniosarosi deleted the antoniosarosi/b-618 branch July 26, 2026 14:15
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle July 26, 2026 14:24 Inactive

This branch was previously deployed

3 inactive deployments
Preview – promptfiddle — 6061d190 Deployed Jul 26, 2026 by vercel[bot]
Preview – promptfiddle2 — 6061d190 Deployed Jul 26, 2026 by vercel[bot]
Preview – beps — 6061d190 Deployed Jul 26, 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.

1 participant