Skip to content

TIR + MIR should use ItemTree firewall queries - #4092

Merged
2kai2kai2 merged 3 commits into
canaryfrom
kai/hir-salsa
Jul 20, 2026
Merged

TIR + MIR should use ItemTree firewall queries#4092
2kai2kai2 merged 3 commits into
canaryfrom
kai/hir-salsa

Conversation

@2kai2kai2

@2kai2kai2 2kai2kai2 commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Instead of querying the ItemTree directly (which will force consumers to fully recompute whenever anything changes), we should use the firewall queries for granular salsa cache invalidation.

This builds on #4064 and will be followed by further PR(s) to complete this migration (eventually making ItemTree private to consumers)

Summary by CodeRabbit

  • Improvements

    • Improved generic handling across interfaces, associated types, and method dispatch, using more consistent type metadata throughout compilation.
    • Enhanced type inference and throw/exception analysis reliability, with more accurate source spans and diagnostics.
    • Type display output is now byte-identical to the source syntax for representative types (including unions, generics, function types, and associated-type projections).
  • Tests

    • Updated default-parameter metadata checks to rely on real parsed input.
    • Expanded type rendering and diagnostic coverage for interface and parameter default scenarios.

@vercel

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

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 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR migrates compiler2 lowering and resolution from HIR item trees and AST type expressions to PPIR item data and TypeRef arenas. It adds TypeRef display and signature contracts, updates interface and MIR handling, and revises diagnostics, throw inference, LSP checks, and metadata tests.

Changes

Compiler2 PPIR and TypeRef migration

Layer / File(s) Summary
TypeRef rendering and signature contracts
baml_language/crates/baml_compiler2_hir/src/type_ref.rs, baml_language/crates/baml_compiler2_tir/src/signature.rs, baml_language/crates/baml_compiler2_tir/src/lower_type_expr.rs
Adds recursive TypeRef display formatting, TypeRef-backed callable signatures, generic-bound lowering, and TypeRef-based bare-Self checks.
TIR interface and inference resolution
baml_language/crates/baml_compiler2_tir/src/interfaces*, baml_language/crates/baml_compiler2_tir/src/builder*, baml_language/crates/baml_compiler2_tir/src/inference.rs
Moves interface members, associated types, bounds, requires clauses, inference environments, and source spans to PPIR and TypeRef data.
MIR PPIR lowering and dispatch
baml_language/crates/baml_compiler2_mir/src/lower.rs
Updates item references, schemas, interface closures, implementor resolution, default-method dispatch, associated-type substitution, and runtime type-argument handling to use PPIR metadata.
Integration and validation
baml_language/crates/baml_compiler2_tir/src/package_interface.rs, baml_language/crates/baml_compiler2_tir/src/throw_inference.rs, baml_language/crates/baml_lsp2_actions/src/check.rs, baml_language/crates/baml_compiler2_emit/src/lib.rs
Updates package exports, throw inference, LSP checks, and metadata tests to consume PPIR locations, parameters, spans, and TypeRefs.

Estimated code review effort: 5 (Critical) | ~120 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Source as BAML source
  participant PPIR as PPIR item_data
  participant TIR as TIR resolution
  participant MIR as MIR lowering
  Source->>PPIR: parse functions, interfaces, impls, and TypeRefs
  PPIR->>TIR: provide item data and source-map spans
  TIR->>MIR: provide lowered types, bounds, and interface targets
  MIR-->>Source: produce lowered schemas, dispatch, and diagnostics
Loading

Possibly related PRs

Suggested reviewers: antoniosarosi, hellovai

Poem

I hop through types where old trees grew,
With PPIR paths and bounds anew.
Interfaces bloom, defaults align,
TypeRefs render crisp and fine.
The compiler carrot shines tonight! 🐇

🚥 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 matches the main refactor: TIR and MIR now route item-tree access through firewall queries.
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 kai/hir-salsa

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 – promptfiddle2 July 20, 2026 18:48 Inactive
@2kai2kai2
2kai2kai2 enabled auto-merge July 20, 2026 18:53
@vercel
vercel Bot temporarily deployed to Preview – beps July 20, 2026 18:54 Inactive
@github-actions

github-actions Bot commented Jul 20, 2026

Copy link
Copy Markdown

Binary size checks passed

7 passed

Artifact Platform File Gzip Gated on Baseline Delta Status
baml-cli Linux 🔒 25.2 MB 10.7 MB file 25.3 MB -24.6 KB (-0.1%) OK
packed-program Linux 🔒 17.0 MB 7.0 MB file 17.0 MB -32.8 KB (-0.2%) OK
baml-cli macOS 🔒 19.5 MB 9.3 MB file 19.5 MB -16.4 KB (-0.1%) OK
packed-program macOS 🔒 13.2 MB 6.1 MB file 13.2 MB -16.4 KB (-0.1%) OK
baml-cli Windows 🔒 21.1 MB 9.5 MB file 21.1 MB -17.9 KB (-0.1%) OK
packed-program Windows 🔒 14.1 MB 6.2 MB file 14.1 MB -31.7 KB (-0.2%) OK
bridge_wasm WASM 16.1 MB 🔒 4.4 MB gzip 4.4 MB -15.2 KB (-0.3%) 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

@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_mir/src/lower.rs (1)

9145-9196: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Append lambda generics on the early-return paths

enclosing_generic_params() is still used while lowering lambda bodies, with self.func_loc pointing at the enclosing method. Returning early here drops self.lambda_generic_params, so generic lambdas nested in FreeImpl and Interface methods resolve their own type vars against the wrong frame slots.

Patch
             params.extend(function_data(self.db, fl).generic_params.iter().cloned());
+            params.extend(self.lambda_generic_params.iter().cloned());
             return params;
         }

Apply the same append before the Interface return too.

🤖 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_mir/src/lower.rs` around lines 9145 -
9196, Update enclosing_generic_params() so both the FreeImpl and Interface
early-return paths append self.lambda_generic_params after the enclosing and
function generic parameters, matching the normal path and preserving correct
lambda type-variable frame slots.
🧹 Nitpick comments (1)
baml_language/crates/baml_compiler2_hir/src/type_ref.rs (1)

244-247: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add a paren-sensitive projection case to the round-trip test. The unqualified projection path already parenthesizes Union/Function bases the same way as ast::TypeExpr, but the current byte-identical test only covers T.Item. Add a case like (int | string).Item to lock that behavior in.

🤖 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_hir/src/type_ref.rs` around lines 244 -
247, Add a `(int | string).Item` case to the existing byte-identical type
round-trip test covering projection formatting. Ensure it exercises the
unqualified projection path around `write_postfix_base` and verifies that
union/function bases retain the required parentheses.
🤖 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_mir/src/lower.rs`:
- Around line 9145-9196: Update enclosing_generic_params() so both the FreeImpl
and Interface early-return paths append self.lambda_generic_params after the
enclosing and function generic parameters, matching the normal path and
preserving correct lambda type-variable frame slots.

---

Nitpick comments:
In `@baml_language/crates/baml_compiler2_hir/src/type_ref.rs`:
- Around line 244-247: Add a `(int | string).Item` case to the existing
byte-identical type round-trip test covering projection formatting. Ensure it
exercises the unqualified projection path around `write_postfix_base` and
verifies that union/function bases retain the required parentheses.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 0ae75c16-8604-404c-86c1-80830bb8a105

📥 Commits

Reviewing files that changed from the base of the PR and between cfcb5a7 and 55a25f1.

📒 Files selected for processing (18)
  • baml_language/crates/baml_compiler2_emit/src/lib.rs
  • baml_language/crates/baml_compiler2_hir/src/type_ref.rs
  • baml_language/crates/baml_compiler2_mir/src/lower.rs
  • baml_language/crates/baml_compiler2_tir/src/builder.rs
  • baml_language/crates/baml_compiler2_tir/src/builder/associated_projection.rs
  • baml_language/crates/baml_compiler2_tir/src/builder/interface_resolution.rs
  • baml_language/crates/baml_compiler2_tir/src/callable.rs
  • baml_language/crates/baml_compiler2_tir/src/infer_context.rs
  • baml_language/crates/baml_compiler2_tir/src/inference.rs
  • baml_language/crates/baml_compiler2_tir/src/interfaces.rs
  • baml_language/crates/baml_compiler2_tir/src/interfaces/coherence.rs
  • baml_language/crates/baml_compiler2_tir/src/interfaces/impl_rules.rs
  • baml_language/crates/baml_compiler2_tir/src/lower_type_expr.rs
  • baml_language/crates/baml_compiler2_tir/src/package_interface.rs
  • baml_language/crates/baml_compiler2_tir/src/resolve.rs
  • baml_language/crates/baml_compiler2_tir/src/signature.rs
  • baml_language/crates/baml_compiler2_tir/src/throw_inference.rs
  • baml_language/crates/baml_lsp2_actions/src/check.rs

@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 20, 2026 19:01 Inactive
@2kai2kai2
2kai2kai2 added this pull request to the merge queue Jul 20, 2026

@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_mir/src/lower.rs (1)

11074-11089: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Use the interface package items for associated-type defaults. assoc.default lowers target_data.type_refs with target_iface_pkg.namespace_path but still passes class_pkg_items, so bare or relative names in an interface default resolve from the implementing class’s package instead of the interface’s. baml_language/crates/baml_compiler2_mir/src/lower.rs:11074-11089

🤖 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_mir/src/lower.rs` around lines 11074 -
11089, Update the assoc.default lowering in the surrounding interface
implementation flow to pass the interface package’s items instead of
class_pkg_items, while retaining target_data.type_refs and
target_iface_pkg.namespace_path. Ensure bare and relative names in
associated-type defaults resolve within the interface package.
🤖 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_mir/src/lower.rs`:
- Around line 11074-11089: Update the assoc.default lowering in the surrounding
interface implementation flow to pass the interface package’s items instead of
class_pkg_items, while retaining target_data.type_refs and
target_iface_pkg.namespace_path. Ensure bare and relative names in
associated-type defaults resolve within the interface package.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: f5ce8c6d-806d-4363-9e8f-c5cd7be70468

📥 Commits

Reviewing files that changed from the base of the PR and between 55a25f1 and 08c741e.

📒 Files selected for processing (18)
  • baml_language/crates/baml_compiler2_emit/src/lib.rs
  • baml_language/crates/baml_compiler2_hir/src/type_ref.rs
  • baml_language/crates/baml_compiler2_mir/src/lower.rs
  • baml_language/crates/baml_compiler2_tir/src/builder.rs
  • baml_language/crates/baml_compiler2_tir/src/builder/associated_projection.rs
  • baml_language/crates/baml_compiler2_tir/src/builder/interface_resolution.rs
  • baml_language/crates/baml_compiler2_tir/src/callable.rs
  • baml_language/crates/baml_compiler2_tir/src/infer_context.rs
  • baml_language/crates/baml_compiler2_tir/src/inference.rs
  • baml_language/crates/baml_compiler2_tir/src/interfaces.rs
  • baml_language/crates/baml_compiler2_tir/src/interfaces/coherence.rs
  • baml_language/crates/baml_compiler2_tir/src/interfaces/impl_rules.rs
  • baml_language/crates/baml_compiler2_tir/src/lower_type_expr.rs
  • baml_language/crates/baml_compiler2_tir/src/package_interface.rs
  • baml_language/crates/baml_compiler2_tir/src/resolve.rs
  • baml_language/crates/baml_compiler2_tir/src/signature.rs
  • baml_language/crates/baml_compiler2_tir/src/throw_inference.rs
  • baml_language/crates/baml_lsp2_actions/src/check.rs
🚧 Files skipped from review as they are similar to previous changes (16)
  • baml_language/crates/baml_compiler2_tir/src/callable.rs
  • baml_language/crates/baml_compiler2_tir/src/signature.rs
  • baml_language/crates/baml_compiler2_tir/src/resolve.rs
  • baml_language/crates/baml_compiler2_hir/src/type_ref.rs
  • baml_language/crates/baml_compiler2_emit/src/lib.rs
  • baml_language/crates/baml_lsp2_actions/src/check.rs
  • baml_language/crates/baml_compiler2_tir/src/throw_inference.rs
  • baml_language/crates/baml_compiler2_tir/src/lower_type_expr.rs
  • baml_language/crates/baml_compiler2_tir/src/builder/interface_resolution.rs
  • baml_language/crates/baml_compiler2_tir/src/interfaces.rs
  • baml_language/crates/baml_compiler2_tir/src/package_interface.rs
  • baml_language/crates/baml_compiler2_tir/src/builder/associated_projection.rs
  • baml_language/crates/baml_compiler2_tir/src/interfaces/impl_rules.rs
  • baml_language/crates/baml_compiler2_tir/src/infer_context.rs
  • baml_language/crates/baml_compiler2_tir/src/inference.rs
  • baml_language/crates/baml_compiler2_tir/src/builder.rs

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