Skip to content

fix(compiler): validate arithmetic through operator interfaces - #4222

Merged
codeshaunted merged 1 commit into
canaryfrom
avery/b-987
Jul 27, 2026
Merged

codeshaunted merged 1 commit into
canaryfrom
avery/b-987

Conversation

@codeshaunted

@codeshaunted codeshaunted commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Issue Reference

B-987

Changes

  • Make baml.ops implementations the sole source of TIR arithmetic validity and Output types.
  • Add the missing Add for string implementation and native driver.
  • Centralize builtin companion-class primitive normalization.
  • Cover the scalar matrix, structural operands, compound assignment, generic dispatch, and runtime behavior.

Testing

  • prek run --last-commit
  • cargo test -p baml_type
  • cargo test -p baml_tests --lib compiler2_tir::
  • cargo test -p baml_tests --test interfaces
  • cargo test -p baml_compiler2_mir
  • BAML operator suite: 52 passed, 0 failed
  • baml describe and baml run expression checks

Screenshots

Not applicable.

PR Checklist

  • Code follows the repository style guidelines.
  • Self-review completed.
  • Tests cover the behavior change.
  • No new warnings.

Summary by CodeRabbit

  • New Features

    • Enabled string concatenation with the + operator (e.g., "hello" + " world"), including correct runtime behavior for string addition.
  • Bug Fixes

    • Improved arithmetic operator validation and type handling across primitives and builtin companion types.
    • Strengthened rejection of invalid compound assignments such as numeric += with string operands, with more consistent diagnostics.
  • Tests

    • Added coverage for string + dispatch/drivers, arithmetic dispatch matrices, and unsupported structural arithmetic.
    • Updated expected diagnostics for invalid compound assignment cases.

@linear

linear Bot commented Jul 27, 2026

Copy link
Copy Markdown

B-987

@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 8:55pm
promptfiddle Ready Ready Preview, Comment Jul 27, 2026 8:55pm
promptfiddle2 Ready Ready Preview, Comment Jul 27, 2026 8:55pm

Request Review

@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: d34c4bd1-0cb6-4405-8541-2d0a0ae5cbe5

📥 Commits

Reviewing files that changed from the base of the PR and between eff05e7 and b10b114.

⛔ Files ignored due to path filters (6)
  • baml_language/crates/baml_tests/snapshots/baml_src/operators.snap is excluded by !**/*.snap
  • baml_language/crates/baml_tests/snapshots/compiles/__baml_std__/baml_tests__compiles____baml_std____03_ppir.snap is excluded by !**/*.snap
  • baml_language/crates/baml_tests/snapshots/compiles/__baml_std__/baml_tests__compiles____baml_std____04_5_mir.snap is excluded by !**/*.snap
  • baml_language/crates/baml_tests/snapshots/compiles/__baml_std__/baml_tests__compiles____baml_std____06_codegen.snap is excluded by !**/*.snap
  • baml_language/crates/baml_tests/tests/bytecode_format/snapshots/bytecode_format__bytecode_display_expanded.snap is excluded by !**/*.snap
  • baml_language/crates/baml_tests/tests/bytecode_format/snapshots/bytecode_format__bytecode_display_expanded_unoptimized.snap is excluded by !**/*.snap
📒 Files selected for processing (10)
  • baml_language/crates/baml_builtins2/baml_std/baml/ns_ops/math.baml
  • baml_language/crates/baml_compiler2_mir/src/lower.rs
  • baml_language/crates/baml_compiler2_tir/src/builder.rs
  • baml_language/crates/baml_lsp2_actions_tests/test_files/syntax/invalid_assignment/compound_ops.baml
  • baml_language/crates/baml_tests/baml_src/ns_operators/operators.baml
  • baml_language/crates/baml_tests/src/compiler2_tir/phase3a.rs
  • baml_language/crates/baml_tests/tests/interfaces.rs
  • baml_language/crates/baml_type/src/lib.rs
  • baml_language/crates/baml_type/src/names.rs
  • baml_language/crates/bex_vm/src/package_baml/ops_math.rs
🚧 Files skipped from review as they are similar to previous changes (10)
  • baml_language/crates/baml_tests/baml_src/ns_operators/operators.baml
  • baml_language/crates/baml_lsp2_actions_tests/test_files/syntax/invalid_assignment/compound_ops.baml
  • baml_language/crates/baml_type/src/names.rs
  • baml_language/crates/baml_builtins2/baml_std/baml/ns_ops/math.baml
  • baml_language/crates/baml_type/src/lib.rs
  • baml_language/crates/bex_vm/src/package_baml/ops_math.rs
  • baml_language/crates/baml_tests/src/compiler2_tir/phase3a.rs
  • baml_language/crates/baml_compiler2_mir/src/lower.rs
  • baml_language/crates/baml_tests/tests/interfaces.rs
  • baml_language/crates/baml_compiler2_tir/src/builder.rs

📝 Walkthrough

Walkthrough

Arithmetic inference now resolves through operator interfaces and shared primitive metadata before folding. String addition is declared in the standard library, implemented in the VM, and covered by driver and compiler tests.

Changes

Arithmetic interfaces

Layer / File(s) Summary
Primitive metadata and type construction
baml_language/crates/baml_type/src/lib.rs, baml_language/crates/baml_type/src/names.rs
Adds primitive-to-type construction and builtin companion-class recognition, with tests for primitive preservation and aliases.
Interface-driven arithmetic resolution
baml_language/crates/baml_compiler2_tir/src/builder.rs, baml_language/crates/baml_compiler2_mir/src/lower.rs
Routes arithmetic validity through baml.ops, widens builtin wrapper classes to primitive types, and performs constant folding after interface resolution.
String addition implementation and wiring
baml_language/crates/baml_builtins2/.../math.baml, baml_language/crates/bex_vm/src/package_baml/ops_math.rs
Adds Add<string> for string and wires VM string concatenation through BexStr.
Arithmetic compatibility validation
baml_language/crates/baml_tests/..., baml_language/crates/baml_lsp2_actions_tests/...
Adds string driver parity coverage, invalid compound-assignment diagnostics, scalar compatibility matrices, and structural operand rejection tests.

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

Sequence Diagram(s)

sequenceDiagram
  participant Source
  participant TIRBuilder
  participant OpsInterface
  participant PackageBamlImpl
  participant BexStr
  Source->>TIRBuilder: parse arithmetic expression
  TIRBuilder->>OpsInterface: resolve arithmetic operator
  OpsInterface-->>TIRBuilder: validate operand and result types
  TIRBuilder->>PackageBamlImpl: execute string addition
  PackageBamlImpl->>BexStr: concatenate operands
  BexStr-->>PackageBamlImpl: return concatenated value
Loading

Possibly related PRs

  • BoundaryML/baml#3299: Both modify arithmetic type inference in builder.rs around primitive and union operand handling.
  • BoundaryML/baml#4042: Extends the same operator-interface machinery with string addition support.

Poem

I’m a bunny with strings in my paws,
I joined two words without pauses.
Through ops they now flow,
And the VM makes them grow—
“hello” plus “ world” earns applause! 🐇

🚥 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 accurately summarizes the main change: arithmetic validation now goes through operator interfaces.
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-987

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.

@vercel
vercel Bot temporarily deployed to Preview – beps July 27, 2026 19:58 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 27, 2026 20:06 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_tests/src/compiler2_tir/phase3a.rs`:
- Around line 1018-1037: Run `cargo test --lib` for the affected crates
`baml_type`, `baml_compiler2_mir`, `baml_compiler2_tir`, and `baml_tests`, and
resolve any failures before merging the regression test
`compound_assign_float_plus_string_is_rejected_in_tir`.
🪄 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: ebed1f88-242b-4dd3-a77e-4a10a46442f3

📥 Commits

Reviewing files that changed from the base of the PR and between 5fc67e6 and c4c50fe.

📒 Files selected for processing (9)
  • baml_language/crates/baml_builtins2/baml_std/baml/ns_ops/math.baml
  • baml_language/crates/baml_compiler2_mir/src/lower.rs
  • baml_language/crates/baml_compiler2_tir/src/builder.rs
  • baml_language/crates/baml_tests/baml_src/ns_operators/operators.baml
  • baml_language/crates/baml_tests/src/compiler2_tir/phase3a.rs
  • baml_language/crates/baml_tests/tests/interfaces.rs
  • baml_language/crates/baml_type/src/lib.rs
  • baml_language/crates/baml_type/src/names.rs
  • baml_language/crates/bex_vm/src/package_baml/ops_math.rs

@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 +5.0 KB (+0.0%) OK
✅ packed-program Linux 🔒 17.7 MB 7.3 MB file 17.7 MB +8.6 KB (+0.0%) OK
✅ baml-cli macOS 🔒 21.2 MB 10.1 MB file 21.2 MB +32 B (+0.0%) OK
✅ packed-program macOS 🔒 13.8 MB 6.4 MB file 13.8 MB +16 B (+0.0%) OK
✅ baml-cli Windows 🔒 22.8 MB 10.3 MB file 22.8 MB +3.1 KB (+0.0%) OK
✅ packed-program Windows 🔒 14.7 MB 6.5 MB file 14.7 MB +7.0 KB (+0.0%) OK
✅ bridge_wasm WASM 16.5 MB 🔒 4.5 MB gzip 4.5 MB +1.9 KB (+0.0%) 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 – promptfiddle2 July 27, 2026 20:21 Inactive
@vercel
vercel Bot temporarily deployed to Preview – beps July 27, 2026 20:34 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 27, 2026 20:41 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle July 27, 2026 20:55 Inactive
@codeshaunted
codeshaunted added this pull request to the merge queue Jul 27, 2026
Merged via the queue into canary with commit 0a00523 Jul 27, 2026
81 checks passed
@codeshaunted
codeshaunted deleted the avery/b-987 branch July 27, 2026 21:41
This was referenced Aug 12, 2026

This branch was previously deployed

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