Skip to content

fix(compiler): defer inferred generic argument diagnostics - #4160

Closed
codeshaunted wants to merge 1 commit into
canaryfrom
avery/generic-union-argument-validation
Closed

codeshaunted wants to merge 1 commit into
canaryfrom
avery/generic-union-argument-validation

Conversation

@codeshaunted

@codeshaunted codeshaunted commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • infer direct generic arguments while their type-variable bindings are provisional
  • retain existing checking for concrete parameters, explicit type arguments, nested types, rigid generics, and lambdas
  • validate arguments against the finalized union through the existing final call validation
  • add diagnostic snapshots for return-constrained and context-free calls, with concrete and explicit controls

Tests

  • cargo test -p baml_tests --lib
  • cargo test -p baml_compiler2_tir --lib
  • cargo clippy -p baml_compiler2_tir -p baml_tests --all-targets -- -D warnings
  • cargo fmt --all -- --check

Linear: https://linear.app/boundaryml2/issue/B-932/union-inferred-generic-call-incorrectly-rejects-a-contributing-argument

Summary by CodeRabbit

  • Bug Fixes

    • Improved generic type inference so mismatched argument types are consistently checked across return values, assignments, nested generic calls, and explicitly typed expressions.
    • Prevented certain generic type errors from being skipped or producing incomplete diagnostic results.
  • Tests

    • Added coverage for generic functions with conflicting argument types across multiple usage patterns.
    • Added validation that type errors remain detectable even when intermediate results are ignored or reassigned.

@vercel

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

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.

@vercel
vercel Bot temporarily deployed to Preview – beps July 23, 2026 20:28 Inactive
@coderabbitai

coderabbitai Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The compiler changes generic argument handling by distinguishing directly inferable type variables from substituted types containing variables. A diagnostic test program adds mismatched generic-call scenarios covering return context, ignored results, concrete parameters, and explicit type arguments.

Changes

Generic argument checking

Layer / File(s) Summary
Generic inference control flow
baml_language/crates/baml_compiler2_tir/src/builder.rs
Adds direct_inference_var checks and updates the infer_expr versus check_expr branch for generic arguments.
Diagnostic coverage
baml_language/crates/baml_tests/projects/diagnostic_errors/generic_union_argument_no_cascade/main.baml
Adds generic helpers and mismatched-call cases covering contextual, non-contextual, concrete-parameter, and explicit-type scenarios.

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

Possibly related PRs

Suggested reviewers: aaronvg, 2kai2kai2

Poem

I’m a rabbit checking types in flight,
With generic carrots lined up right.
infer_expr hops, check_expr stays,
Mismatched burrows earn errors’ praise.
Test cases bloom across the hay—
No cascade gets away!

🚥 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: deferring diagnostics for inferred generic arguments in the compiler.
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 docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch avery/generic-union-argument-validation

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.

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

🧹 Nitpick comments (1)
baml_language/crates/baml_compiler2_tir/src/builder.rs (1)

3776-3789: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Extract the shared "rigid vs. inferable typevar" predicate.

direct_inference_var here is logically identical to the rigid/defers_typevar check later in this same function (~lines 4132-4138): applying De Morgan's law to !(rigid_self_var == Some(name) || (self.generic_params.contains(name) && !generic_params.contains(name))) yields exactly rigid_self_var != Some(name) && (!self.generic_params.contains(name) || generic_params.contains(name)) — the same formula. Duplicating this subtle boolean (which encodes "is this typevar the caller's own rigid generic vs. one the callee can still infer") risks the two copies silently drifting apart in a future edit and reintroducing the cascading-diagnostic bug this PR fixes.

♻️ Suggested consolidation
+    fn is_directly_inferable_typevar(
+        &self,
+        name: &Name,
+        callee_generic_params: &[Name],
+        rigid_self_var: Option<&Name>,
+    ) -> bool {
+        rigid_self_var != Some(name)
+            && (!self.generic_params.iter().any(|p| p == name)
+                || callee_generic_params.iter().any(|p| p == name))
+    }

Then use it at both call sites (here, and in the defers_typevar closure) instead of re-deriving the formula.

🤖 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 3776 -
3789, Extract the shared rigid-versus-inferable type-variable predicate from the
current direct_inference_var logic into a local helper or closure within the
function. Replace both the direct_inference_var calculation and the later
defers_typevar check with this shared predicate, preserving the existing inputs
and behavior.

Source: Linters/SAST tools

🤖 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 3776-3789: Extract the shared rigid-versus-inferable type-variable
predicate from the current direct_inference_var logic into a local helper or
closure within the function. Replace both the direct_inference_var calculation
and the later defers_typevar check with this shared predicate, preserving the
existing inputs and behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: f19aeddc-6e2e-4fad-8f7a-2d924818f123

📥 Commits

Reviewing files that changed from the base of the PR and between 4bf53a5 and bb3e65f.

⛔ Files ignored due to path filters (6)
  • baml_language/crates/baml_tests/snapshots/diagnostic_errors/generic_union_argument_no_cascade/baml_tests__diagnostic_errors__generic_union_argument_no_cascade__01_lexer__main.snap is excluded by !**/*.snap
  • baml_language/crates/baml_tests/snapshots/diagnostic_errors/generic_union_argument_no_cascade/baml_tests__diagnostic_errors__generic_union_argument_no_cascade__02_parser__main.snap is excluded by !**/*.snap
  • baml_language/crates/baml_tests/snapshots/diagnostic_errors/generic_union_argument_no_cascade/baml_tests__diagnostic_errors__generic_union_argument_no_cascade__03_ppir.snap is excluded by !**/*.snap
  • baml_language/crates/baml_tests/snapshots/diagnostic_errors/generic_union_argument_no_cascade/baml_tests__diagnostic_errors__generic_union_argument_no_cascade__04_tir.snap is excluded by !**/*.snap
  • baml_language/crates/baml_tests/snapshots/diagnostic_errors/generic_union_argument_no_cascade/baml_tests__diagnostic_errors__generic_union_argument_no_cascade__05_diagnostics.snap is excluded by !**/*.snap
  • baml_language/crates/baml_tests/snapshots/diagnostic_errors/generic_union_argument_no_cascade/baml_tests__diagnostic_errors__generic_union_argument_no_cascade__10_formatter__main.snap is excluded by !**/*.snap
📒 Files selected for processing (2)
  • baml_language/crates/baml_compiler2_tir/src/builder.rs
  • baml_language/crates/baml_tests/projects/diagnostic_errors/generic_union_argument_no_cascade/main.baml

@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 23, 2026 20:35 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 🔒 26.8 MB 11.4 MB file 26.2 MB +623.4 KB (+2.4%) OK
✅ packed-program Linux 🔒 17.4 MB 7.2 MB file 17.0 MB +353.2 KB (+2.1%) OK
✅ baml-cli macOS 🔒 20.8 MB 9.9 MB file 20.3 MB +480.3 KB (+2.4%) OK
✅ packed-program macOS 🔒 13.6 MB 6.3 MB file 13.2 MB +347.3 KB (+2.6%) OK
✅ baml-cli Windows 🔒 22.4 MB 10.2 MB file 21.9 MB +527.4 KB (+2.4%) OK
✅ packed-program Windows 🔒 14.5 MB 6.4 MB file 14.2 MB +300.5 KB (+2.1%) OK
✅ bridge_wasm WASM 16.4 MB 🔒 4.5 MB gzip 4.4 MB +66.2 KB (+1.5%) 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 – promptfiddle July 23, 2026 20:48 Inactive

This branch was previously deployed

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