fix(compiler): make narrowing safe across spawn - #4128
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe compiler now lowers atomic typed-pattern tests into ChangesAtomic narrow-bind pipeline
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant PatternLowering
participant MIR
participant BytecodeEmitter
participant BexVM
PatternLowering->>MIR: emit NarrowBind with type template and branches
MIR->>BytecodeEmitter: lower NarrowBind terminator
BytecodeEmitter->>BexVM: execute NarrowBind opcode
BexVM->>BexVM: match type constant and conditionally bind destination
BexVM-->>PatternLowering: push match result and continue branch
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
⏭️ Performance benchmarks were skippedPerf benchmarks (CodSpeed) are opt-in on pull requests — they no longer run on every push. They always run automatically after merge to To run them on this PR, do any of the following, then push a commit (or re-run CI):
|
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Nitpick comments (1)
baml_language/crates/baml_tests/tests/narrow_bind.rs (1)
4-29: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winCover the spawned-mutation binding case.
This only tests a direct match. Add a regression where a captured source is mutated by
spawnand the typed binding is consumed after synchronization; otherwise a reload-after-test regression can still pass.Also verify the Rust changes with
cargo test --lib.🤖 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_tests/tests/narrow_bind.rs` around lines 4 - 29, Extend typed-pattern coverage in typed_pattern_tests_and_binds_the_same_value with a regression where a captured source is mutated inside spawn, synchronization completes, and the typed binding is then consumed afterward. Assert the expected result and narrow_bind behavior so reload-after-test regressions are detected, then verify the Rust changes with cargo test --lib.Source: Coding guidelines
🤖 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_compiler2_mir/src/lower.rs`:
- Around line 12224-12228: Update the stale comment in the pattern-lowering
check around pattern_narrow_type to state that narrow-bind patterns are
deliberately excluded from switch/jump-table lowering because Switch cannot
preserve bind-on-match semantics. Remove the outdated wording about recovering
the narrow annotation and treating it as a TypeTag arm; leave the return false
behavior unchanged.
In `@baml_language/crates/baml_tests/src/compiler2_emit/mod.rs`:
- Around line 62-86: Update the test around the NarrowBind instruction to record
its index alongside the destination slot, then restrict the destination-use
search to instructions after that index, specifically the succeeding then-arm
instructions. Preserve the existing slot matching for LoadVar, StoreVarLoadVar,
and LoadVar2 so the assertion verifies the bound local is used only after
NarrowBind rather than matching an earlier scrutinee use.
In `@baml_language/crates/bex_vm/src/debug.rs`:
- Around line 168-174: Update the Instruction::NarrowBind formatting branch to
resolve ty through operand_meta before constructing the output, matching the
textual renderer’s metadata lookup. Render the resolved type metadata instead of
the raw constant index while preserving the existing destination-name handling.
---
Nitpick comments:
In `@baml_language/crates/baml_tests/tests/narrow_bind.rs`:
- Around line 4-29: Extend typed-pattern coverage in
typed_pattern_tests_and_binds_the_same_value with a regression where a captured
source is mutated inside spawn, synchronization completes, and the typed binding
is then consumed afterward. Assert the expected result and narrow_bind behavior
so reload-after-test regressions are detected, then verify the Rust changes with
cargo test --lib.
🪄 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
Run ID: 1c87df28-cdaa-4e7b-90c6-76cbdad61716
⛔ Files ignored due to path filters (12)
baml_language/crates/baml_tests/snapshots/compiles/__baml_std__/baml_tests__compiles____baml_std____04_5_mir.snapis excluded by!**/*.snapbaml_language/crates/baml_tests/snapshots/compiles/__baml_std__/baml_tests__compiles____baml_std____06_codegen.snapis excluded by!**/*.snapbaml_language/crates/baml_tests/snapshots/compiles/__testing_std__/baml_tests__compiles____testing_std____04_5_mir.snapis excluded by!**/*.snapbaml_language/crates/baml_tests/snapshots/compiles/__testing_std__/baml_tests__compiles____testing_std____06_codegen.snapis excluded by!**/*.snapbaml_language/crates/baml_tests/snapshots/compiles/generic_match_typevar_arm/baml_tests__compiles__generic_match_typevar_arm__04_5_mir.snapis excluded by!**/*.snapbaml_language/crates/baml_tests/snapshots/compiles/generic_match_typevar_arm/baml_tests__compiles__generic_match_typevar_arm__06_codegen.snapis excluded by!**/*.snapbaml_language/crates/baml_tests/snapshots/compiles/json_alias_basic/baml_tests__compiles__json_alias_basic__04_5_mir.snapis excluded by!**/*.snapbaml_language/crates/baml_tests/snapshots/compiles/json_alias_basic/baml_tests__compiles__json_alias_basic__06_codegen.snapis excluded by!**/*.snapbaml_language/crates/baml_tests/snapshots/compiles/json_parse_stringify_intrinsics/baml_tests__compiles__json_parse_stringify_intrinsics__04_5_mir.snapis excluded by!**/*.snapbaml_language/crates/baml_tests/snapshots/compiles/json_parse_stringify_intrinsics/baml_tests__compiles__json_parse_stringify_intrinsics__06_codegen.snapis excluded by!**/*.snapbaml_language/crates/baml_tests/snapshots/compiles/patterns_new/baml_tests__compiles__patterns_new__04_5_mir.snapis excluded by!**/*.snapbaml_language/crates/baml_tests/snapshots/compiles/patterns_new/baml_tests__compiles__patterns_new__06_codegen.snapis excluded by!**/*.snap
📒 Files selected for processing (18)
baml_language/crates/baml_compiler2_emit/src/analysis.rsbaml_language/crates/baml_compiler2_emit/src/emit.rsbaml_language/crates/baml_compiler2_emit/src/stack_carry.rsbaml_language/crates/baml_compiler2_mir/src/builder.rsbaml_language/crates/baml_compiler2_mir/src/ir.rsbaml_language/crates/baml_compiler2_mir/src/lower.rsbaml_language/crates/baml_compiler2_mir/src/optimize.rsbaml_language/crates/baml_compiler2_mir/src/pretty.rsbaml_language/crates/baml_compiler2_tir/src/builder.rsbaml_language/crates/baml_compiler2_tir/src/narrowing.rsbaml_language/crates/baml_tests/src/compiler2_emit/mod.rsbaml_language/crates/baml_tests/src/compiler2_mir/mod.rsbaml_language/crates/baml_tests/src/compiler2_tir/phase7.rsbaml_language/crates/baml_tests/tests/narrow_bind.rsbaml_language/crates/bex_vm/src/debug.rsbaml_language/crates/bex_vm/src/vm.rsbaml_language/crates/bex_vm_types/src/bytecode.rsbaml_language/crates/bex_vm_types/src/relink.rs
Binary size checks passed✅ 7 passed
Generated by |
…cal-narrowing # Conflicts: # baml_language/crates/baml_tests/snapshots/compiles/__baml_std__/baml_tests__compiles____baml_std____04_5_mir.snap # baml_language/crates/baml_tests/snapshots/compiles/__testing_std__/baml_tests__compiles____testing_std____04_5_mir.snap # baml_language/crates/baml_tests/snapshots/compiles/patterns_new/baml_tests__compiles__patterns_new__04_5_mir.snap # baml_language/crates/baml_tests/tests/bytecode_format/snapshots/bytecode_format__bytecode_display_expanded.snap # baml_language/crates/baml_tests/tests/bytecode_format/snapshots/bytecode_format__bytecode_display_expanded_unoptimized.snap
There was a problem hiding this comment.
🧹 Nitpick comments (1)
baml_language/crates/baml_compiler2_tir/src/builder.rs (1)
1356-1398: 📐 Maintainability & Code Quality | 🔵 TrivialConsider documenting the new capture-narrowing helpers.
local_is_uncaptured,narrow_uncaptured_local, anduncaptured_condition_narrowingsimplement the core invariant this PR introduces (narrowing only applies to uncaptured locals), but unlike nearly every other method in this file they carry no doc comment explaining the captured-bindings semantics or the fail-closed behavior (missing source map / unresolved binding → treated as captured). A short doc note would help future maintainers understand why narrowing silently no-ops in these cases.🤖 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 1356 - 1398, Add concise doc comments to local_is_uncaptured, narrow_uncaptured_local, and uncaptured_condition_narrowings describing that narrowing applies only to uncaptured locals, and that missing source maps or unresolved bindings fail closed by treating the local as captured. Keep the existing behavior unchanged.
🤖 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.
Nitpick comments:
In `@baml_language/crates/baml_compiler2_tir/src/builder.rs`:
- Around line 1356-1398: Add concise doc comments to local_is_uncaptured,
narrow_uncaptured_local, and uncaptured_condition_narrowings describing that
narrowing applies only to uncaptured locals, and that missing source maps or
unresolved bindings fail closed by treating the local as captured. Keep the
existing behavior unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: fb7ffca8-a8a1-47ba-9921-560fc8f8f94d
📒 Files selected for processing (5)
baml_language/crates/baml_compiler2_emit/src/emit.rsbaml_language/crates/baml_compiler2_mir/src/optimize.rsbaml_language/crates/baml_compiler2_tir/src/builder.rsbaml_language/crates/bex_vm/src/vm.rsbaml_language/crates/bex_vm_types/src/bytecode.rs
🚧 Files skipped from review as they are similar to previous changes (4)
- baml_language/crates/baml_compiler2_emit/src/emit.rs
- baml_language/crates/bex_vm/src/vm.rs
- baml_language/crates/baml_compiler2_mir/src/optimize.rs
- baml_language/crates/bex_vm_types/src/bytecode.rs
|
@coderabbitai review |
✅ Action performedReview finished.
|
Issue Reference
B-335
Changes
narrow_bindinstruction across MIR, bytecode, and the VM.Testing
cargo test -p baml_tests --lib --quietcargo test -p baml_tests --test narrow_bindcargo test -p bex_vmcargo test -p bex_vm_typescargo check -p baml_compiler2_tir -p baml_compiler2_mir -p baml_compiler2_emit -p bex_vm_types -p bex_vm -p baml_testscargo fmt --all -- --checkSummary by CodeRabbit