baml icon indicating copy to clipboard operation
baml copied to clipboard

[bug] BEP-057: conflicting required-interface pins make qualified projections declaration-order dependent

Open Haoxincode opened this issue 4 months ago • 2 comments

Product

BAML (compiler2 / BAML 1.0 interfaces)

Describe the bug

On current canary commit 663ad178593106d8b10b0324ba47c077b956329b, compiler2 accepts an interface that requires the same parent interface twice with conflicting associated-type bindings.

A qualified projection then selects the first realization. Swapping only the two requires entries changes the compiled function signature from int -> int to string -> string. Source declaration order therefore changes type semantics.

Reproduction Steps

Compile this BAML 1.0 program:

interface Parent {
  type Item
}

interface Child requires Parent<Item = int>, Parent<Item = string> {}

function identity<T extends Child>(value: (T as Parent).Item)
    -> (T as Parent).Item {
  return value
}

It is accepted without diagnostics, and the compiled parameter/return metadata is:

(["int"], "int")

Now reverse only the two requirements:

interface Child requires Parent<Item = string>, Parent<Item = int> {}

The same function's metadata becomes:

(["string"], "string")

A focused compiler regression test reproduces the difference:

cargo test -p baml_tests --test interfaces_associated_types \
  conflicting_duplicate_required_interface_pins_are_order_independent \
  -- --exact --nocapture

Observed assertion output:

left:  (["int"], "int")
right: (["string"], "string")

Expected behavior

The contract is contradictory: one (implementor, Parent, Item) cannot be both int and string. It should be rejected consistently. At minimum, declaration order must not select the projected type.

This follows BEP-057's functional-dependency rule that each (type, interface, associated-type) triple resolves to one concrete type.

Likely cause

realize_qualifier_through_roots walks the requires closure and returns the first matching QTN whose written qualifier is compatible. A bare Parent qualifier is compatible with both Parent<Item = int> and Parent<Item = string>, so traversal order becomes semantic.

Interface requirement validation also does not reject the conflicting duplicate requirements before projection lowering.

A likely fix is to reject conflicting realizations while validating the requirement contract, plus defensively detect multiple non-equivalent realizations in qualified lookup instead of returning the first match.

Independent audit evidence

This case was generated first by a Quint model and then translated mechanically into the BAML regression test above; it was not derived from a pre-existing failing BAML test.

The model's oracle reports the conflicting realizations as ambiguous, while forward and reverse implementation abstractions resolve to int and string. Exhaustive TLC exploration completed 53,640 distinct states for the core safety properties; a separate calibration check produced the expected order-independence violation.

BAML Version

canary at 663ad178593106d8b10b0324ba47c077b956329b

Language/Framework

Other — compiler-level BAML/Rust regression test

LLM Provider / Model

Not applicable; this is a compile-time type-resolution issue.

Operating System

Linux

Haoxincode avatar Jul 12 '26 10:07 Haoxincode

I found three additional requirement-closure shapes that exercise the same contract-validation gap. The most important one is a transitive diamond: the conflicting Parent.Item realizations do not have to appear as duplicate direct requirements.

interface Parent { type Item }
interface Left requires Parent<Item = int> {}
interface Right requires Parent<Item = string> {}
interface Child requires Left, Right {}

function identity<T extends Child>(value: (T as Parent).Item)
    -> (T as Parent).Item {
  return value
}

This compiles without diagnostics as int -> int. Changing only the last requirement to:

interface Child requires Right, Left {}

changes the compiled signature to string -> string.

Two non-order-dependent variants are also silently accepted:

  1. A direct and transitive conflict:
interface Via requires Parent<Item = string> {}
interface Child requires Parent<Item = int>, Via {}
  1. An explicit pin conflicting with the parent's default:
interface Parent { type Item = string }
interface Child requires Parent<Item = int>, Parent {}

These suggest requirement validation needs to compare fully realized interfaces across the entire requires closure, including completed defaults, rather than only duplicate direct entries. Qualified lookup should still defensively reject multiple non-equivalent realizations instead of returning the first one.

I have prepared focused failing tests for each shape:

  • diamond_required_interface_pin_conflicts_are_order_independent
  • direct_and_transitive_required_interface_pin_conflict_is_rejected
  • required_interface_pin_conflicting_with_default_is_rejected

All results above were reproduced on the same current canary commit, 663ad178593106d8b10b0324ba47c077b956329b.

Haoxincode avatar Jul 12 '26 11:07 Haoxincode

I'd like to pick this up. @Haoxincode , I don't see a PR from you; if you still have those failing tests, I'm happy to build on them and credit you. Otherwise I'll start fresh.

I reproduced this on current canary (a5db82f06) with baml check: both the direct duplicate and the diamond (Child requires Left, Right vs Right, Left) still resolve (T as Parent). Item by declaration order, and neither reports a conflict.

My plan:

  1. Reject contradictory realizations when the interface is declared, next to the existing requires-cycle check (E0118): walk the requires closure and report a new diagnostic when the same parent is realized with non-equivalent associated types. Equivalent duplicates stay accepted.
  2. Backstop in lookup: realize_qualifier_through_roots currently returns the first proven match, in both the local and mounted-interface loops. I'd collect every match and return the existing Determination::Ambiguous when they disagree, so the existing ambiguity diagnostic fires instead of declaration order deciding.

Two questions, @2kai2kai2 , since you've worked most on interfaces.rs:

  • Should rejecting at declaration be the behavior, or only the ambiguity error at lookup?
  • For Parent<Item = int>, Parent where Parent declares type Item = string: does a bare Parent in requires mean "Item takes the default" (so it's a conflict) or "Item is unconstrained" (so it's fine)?

Nedas-Jaronis avatar Nov 05 '26 15:11 Nedas-Jaronis