From 1d6f6b5b8ada25b8620e449a4d515ef271c1e719 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 4 Dec 2026 05:32:12 +0000 Subject: [PATCH] psi: select a boundary trait's operator slot from its token An operator declared on a boundary trait could only be called by name. Given `boundary trait Comparison { machine == equal(left: i32 in Compared, right: i32 in Compared) -> bool reaches none; }`, the call `Comparison::equal(a, b)` worked, but `a == b` was refused with "builtin operator `==` requires unqualified operands". A boundary operation that needs provider selection is spelled this way: the spec says it is "a boundary-trait requirement (using a one-member trait when it needs independent provider selection)", and "the token identifies the required operator slot". So without the token, no program could use a provider-selected operator through its syntax. Operator selection now considers the token-bearing requirements of visible, non-generic boundary traits when no direct declaration and no static binder matches. The operator selects the requirement itself, the same target a named call selects. Provider planning then binds the realization from the Build's `select_provider` row, as it does for the named call. If two such requirements match one operand tuple, the operator is refused as ambiguous. Direct declarations and static binders are selected exactly as before. Two probes move to this spelling: - `boundary_crash_route` now compiles. It used a top-level `boundary machine ==` with a `satisfies` machine, a form the parser now retires. It now declares a one-member boundary trait, a `Checked` implementation and `select_provider` rows. Its operands take `as +Compared` / `as -Compared` casts. `may_crash` now publishes the `crashes? Trap` route it propagates. - `operator_requires_effects` gets the same rewrite, plus a local for the effect-bearing operand, which needs an expected result domain. It now passes checking. It then stops in the native build with `canonical operation evidence: cannot attach obligation evidence: UnownedSeal`. The named call `Integer::equal(prepared, held)` stops the same way, so that failure is not from this change. ## Why Found while getting the M to R sample shard green. Gate: RC-SOURCE-SEMANTICS | a boundary trait's operator token selects its provider-supplied requirement Spec: wiki/spec/language/expressions.md#executable-supply Proof: new `a_boundary_trait_token_selects_its_requirement` (stage 03: `left == right` targets `equal`; a second matching boundary slot is refused as `OperatorAmbiguous`). `mbx test -p symbol-resolved-trees-to-typed-trees` passes (239). `mbx clippy -p typed-trees -p symbol-resolved-trees-to-typed-trees --all-targets -- -D warnings` is clean. `mbx xtask sample-gate --exact --filter samples/cli/probes/boundary_crash_route` compiles (a probe without an oracle); dropping `crashes? Trap` from `may_crash` is refused for the uncovered route. `operator_requires_effects` stops at the native evidence error above | that error is separate from this change Agent: Claude Code | Model: claude-opus-5-5 | Effort: low | Tokens: unavailable in (unavailable cached), unavailable out | Steps: unavailable | Time: unavailable | Steers: 0 --- .../cli/probes/boundary_crash_route/README.md | 19 +++---- .../cli/probes/boundary_crash_route/build.omg | 5 ++ .../cli/probes/boundary_crash_route/main.omg | 46 ++++++++------- .../operator_requires_effects/README.md | 20 +++---- .../operator_requires_effects/build.omg | 18 +++--- .../probes/operator_requires_effects/main.omg | 49 ++++++++-------- .../src/expressions/operator_calls.rs | 21 +++++++ .../tests/qualified_operator_home_tests.rs | 41 ++++++++++++++ .../operator_declarations/operator.rs | 6 +- .../operator/trait_operators.rs | 56 +++++++++++++++++++ 10 files changed, 206 insertions(+), 75 deletions(-) diff --git a/samples/cli/probes/boundary_crash_route/README.md b/samples/cli/probes/boundary_crash_route/README.md index 2fe00e3a48c7..59602ee3a25e 100644 --- a/samples/cli/probes/boundary_crash_route/README.md +++ b/samples/cli/probes/boundary_crash_route/README.md @@ -1,18 +1,17 @@ # Boundary Crash Route -Depend-free compile-scope probe: boundary machines and conditional crash -routes — the first sample declaring a `boundary machine` and satisfying -it. +Depend-free compile-scope probe: a boundary operator with a conditional +crash route. -- `boundary machine == Comparison::equal` overloads `==` for - `i32 in Compared`, the probe's own domain home (bare i32 operands would - join the core's primitive family), through the Comparison carrier and declares a conditional crash route: - `crashes Trap !(right >= 0)` — any `==` whose right operand can't be +- The boundary trait `Comparison` declares `==` for `i32 in Compared`, + the probe's own domain home (bare i32 operands would join the core's + primitive family), with a conditional crash route: + `crashes? Trap !(right >= 0)` — any `==` whose right operand can't be proven non-negative propagates the Trap route. -- `ComparisonProvider::equal` satisfies the boundary machine — the - provider is an ordinary machine bound by `satisfies`. +- `Checked satisfies Comparison` supplies the operator, and `build.omg` + selects it as the provider on every target. - `safe` discharges the route with `requires value >= 0`; `may_crash` - propagates it and `wrapper` covers it by declaration. + publishes it with `crashes? Trap` and `wrapper` covers it by declaration. - The entry calls both on the dormant path (right = 0 ≥ 0). Compile-checked only (no system dependency, no ProcessExit provider): diff --git a/samples/cli/probes/boundary_crash_route/build.omg b/samples/cli/probes/boundary_crash_route/build.omg index cd457ab518fe..0aad76ecc24d 100644 --- a/samples/cli/probes/boundary_crash_route/build.omg +++ b/samples/cli/probes/boundary_crash_route/build.omg @@ -5,6 +5,11 @@ use main.omg as app; machine build(builder: @mutable BuildPlan) { builder.product = BuildProduct::Application { name: "boundary_crash_route" }; + Build::select_provider::<|app|::Comparison, |app|::Checked>(builder, windows_x86_64); + Build::select_provider::<|app|::Comparison, |app|::Checked>(builder, linux_x86_64); + Build::select_provider::<|app|::Comparison, |app|::Checked>(builder, linux_arm64); + Build::select_provider::<|app|::Comparison, |app|::Checked>(builder, macos_arm64); + Build::select_provider::<|app|::Comparison, |app|::Checked>(builder, macos_x86_64); Build::bind(builder, windows_x86_64::ProgramEntry, |app|::boundary_crash_route); Build::bind(builder, linux_x86_64::ProgramEntry, |app|::boundary_crash_route); Build::bind(builder, linux_arm64::ProgramEntry, |app|::boundary_crash_route); diff --git a/samples/cli/probes/boundary_crash_route/main.omg b/samples/cli/probes/boundary_crash_route/main.omg index 11ec6fcfbed1..d2de28d7b078 100644 --- a/samples/cli/probes/boundary_crash_route/main.omg +++ b/samples/cli/probes/boundary_crash_route/main.omg @@ -1,40 +1,44 @@ -// boundary_crash_route: boundary machines and conditional crash routes — -// the first sample declaring a `boundary machine` and satisfying it. +// boundary_crash_route: a boundary operator and its conditional crash +// route. // -// * `boundary machine == Comparison::equal` overloads `==` for +// * the boundary trait `Comparison` overloads `==` for // `i32 in Compared`, the probe's own domain home (bare i32 operands -// would join the core's primitive family), through the Comparison carrier and declares a conditional crash -// route: `crashes Trap !(right >= 0)` — any `==` whose right operand +// would join the core's primitive family), and declares a conditional +// crash route: `crashes? Trap !(right >= 0)` — any `==` whose right operand // can't be proven non-negative propagates the Trap route. -// * `equal` satisfies the boundary machine — the -// provider IS an ordinary machine bound by `satisfies`. +// * `Checked satisfies Comparison` supplies the operator; the +// build selects it as the provider on every target. // * `safe` discharges the route with its own `requires value >= 0` // (the call's context covers the boundary's `right >= 0` fact); -// `may_crash` propagates it and `wrapper` covers by declaration. +// `may_crash` publishes it with `crashes? Trap` and `wrapper` covers +// it by declaration. // * The entry calls both on the dormant path (right = 0 ≥ 0). // // Compile-checked only: no standard-library dependency, no ProcessExit // provider. public domain i32::Compared; -boundary machine == equal_requirement(left: i32 in Compared, right: i32 in Compared) -> bool -crashes? Trap !(right >= 0); +public boundary trait Comparison { + machine == equal(left: i32 in Compared, right: i32 in Compared) -> bool + reaches none crashes? Trap !((right as -Compared) >= 0); +} + -machine equal(left: i32 in Compared, right: i32 in Compared) -> bool - satisfies equal_requirement -{ - transition left < right { - true -> false, - _ -> !(right < left) +Checked satisfies Comparison { + machine equal(left: i32 in Compared, right: i32 in Compared) -> bool { + transition (left as -Compared) < (right as -Compared) { + true -> false, + _ -> !((right as -Compared) < (left as -Compared)) + } } } public machine safe(value: i32 in Compared) -> bool -requires value >= 0 { - 1 == value +requires (value as -Compared) >= 0 { + (1 as +Compared) == value } -machine may_crash(left: i32 in Compared, right: i32 in Compared) -> bool { +machine may_crash(left: i32 in Compared, right: i32 in Compared) -> bool crashes? Trap { left == right } @@ -44,8 +48,8 @@ crashes? Trap { } machine boundary_crash_route() crashes? Trap { - let discharged: bool = safe(1); - let covered: bool = wrapper(2, 2); + let discharged: bool = safe(1 as +Compared); + let covered: bool = wrapper(2 as +Compared, 2 as +Compared); transition discharged && covered { true -> done(70), _ -> done(71) diff --git a/samples/cli/probes/operator_requires_effects/README.md b/samples/cli/probes/operator_requires_effects/README.md index 7ea575afc87e..910f95c70249 100644 --- a/samples/cli/probes/operator_requires_effects/README.md +++ b/samples/cli/probes/operator_requires_effects/README.md @@ -4,13 +4,13 @@ Runnable probe; no standard-library dependency. `omega run main.omg` must exit 0 natively after `omega packages update --project .` reviews the local package inputs. A boundary operator's operand `requires` is discharged through an -effect-bearing call: `boundary machine == Integer::equal ... requires -right >= 0`, `prepare(@mutable held) ensures value >= 0` — -`prepare(@mutable held) == held` proves the right operand after the effect lands. -The operands are `i32 in Counted`, the probe's own domain home: bare i32 -operands would join the core's primitive `==` family. -`build.omg` selects the checked satisfier `CheckedEquality::equal` for the -`Integer::equal` slot, so the comparison runs its body. The source's direct -boundary declaration fixes `==` to that slot; the Build row supplies only its -provider and cannot activate or change the token binding. - +effect-bearing call: the boundary trait `Integer` declares +`machine == equal ... requires right >= 0`, and +`prepare(@mutable held) ensures value >= 0` — `prepared == held` after +`let prepared = prepare(@mutable held)` proves the right operand after the +effect lands. The operands are `i32 in Counted`, the probe's own domain home: +bare i32 operands would join the core's primitive `==` family. +`build.omg` selects the named implementation `CheckedEquality` as the +`Integer` provider, so the comparison runs its body. The trait's token fixes +`==` to that slot; the Build row supplies only its provider and cannot +activate or change the token binding. diff --git a/samples/cli/probes/operator_requires_effects/build.omg b/samples/cli/probes/operator_requires_effects/build.omg index 6b01f73b4d84..35943f69fa34 100644 --- a/samples/cli/probes/operator_requires_effects/build.omg +++ b/samples/cli/probes/operator_requires_effects/build.omg @@ -1,15 +1,15 @@ use main.omg as app; machine build(builder: @mutable BuildPlan) { builder.product = BuildProduct::Application { name: "operator_requires_effects" }; - Build::select_provider::<|app|::Integer::equal, |app|::CheckedEquality>(builder, linux_arm64); - Build::select_provider::<|app|::Integer::equal, |app|::CheckedEquality>(builder, linux_x86_64); - Build::select_provider::<|app|::Integer::equal, |app|::CheckedEquality>(builder, macos_arm64); - Build::select_provider::<|app|::Integer::equal, |app|::CheckedEquality>(builder, macos_x86_64); - Build::select_provider::<|app|::Integer::equal, |app|::CheckedEquality>(builder, windows_x86_64); - Build::select_provider::<|app|::Integer::equal, |app|::CheckedEquality>(builder, uefi_x86_64); - Build::select_provider::<|app|::Integer::equal, |app|::CheckedEquality>(builder, cross_platform_cli); - Build::select_provider::<|app|::Integer::equal, |app|::CheckedEquality>(builder, local_unchecked); - Build::select_provider::<|app|::Integer::equal, |app|::CheckedEquality>(builder, alpha_bootstrap); + Build::select_provider::<|app|::Integer, |app|::CheckedEquality>(builder, linux_arm64); + Build::select_provider::<|app|::Integer, |app|::CheckedEquality>(builder, linux_x86_64); + Build::select_provider::<|app|::Integer, |app|::CheckedEquality>(builder, macos_arm64); + Build::select_provider::<|app|::Integer, |app|::CheckedEquality>(builder, macos_x86_64); + Build::select_provider::<|app|::Integer, |app|::CheckedEquality>(builder, windows_x86_64); + Build::select_provider::<|app|::Integer, |app|::CheckedEquality>(builder, uefi_x86_64); + Build::select_provider::<|app|::Integer, |app|::CheckedEquality>(builder, cross_platform_cli); + Build::select_provider::<|app|::Integer, |app|::CheckedEquality>(builder, local_unchecked); + Build::select_provider::<|app|::Integer, |app|::CheckedEquality>(builder, alpha_bootstrap); Build::bind(builder, windows_x86_64::ProgramEntry, |app|::entry_start); Build::bind(builder, linux_x86_64::ProgramEntry, |app|::entry_start); Build::bind(builder, linux_arm64::ProgramEntry, |app|::entry_start); diff --git a/samples/cli/probes/operator_requires_effects/main.omg b/samples/cli/probes/operator_requires_effects/main.omg index f3a3748e9977..9b4249d04b58 100644 --- a/samples/cli/probes/operator_requires_effects/main.omg +++ b/samples/cli/probes/operator_requires_effects/main.omg @@ -1,13 +1,14 @@ // A boundary operator with an operand `requires` clause: -// `boundary machine == Integer::equal(left, right)` +// `boundary trait Integer { machine == equal(left, right) }` // declares `right >= 0`, and the caller's effect-bearing // operand discharges it — `prepare(@mutable value)` carries -// `ensures value >= 0`, so `prepare(@mutable value) == value` -// proves the right operand after the effect lands. +// `ensures value >= 0`, so `prepared == value` after +// `let prepared = prepare(@mutable value)` proves the right +// operand after the effect lands. // -// * Boundary operator decl + `requires right >= 0`. The token -// resolves the public slot; `equal` is the -// selected checked satisfier, so `==` on these operands runs its +// * Boundary trait `Integer` declares `==` with `requires right >= 0`. +// The token resolves the trait's slot; `CheckedEquality` is the +// selected checked provider, so `==` on these operands runs its // body rather than primitive equality. // * The operands are `i32 in Counted`, the probe's own domain home: // bare i32 operands would join the core's primitive `==` family. @@ -22,42 +23,46 @@ // Expected exit: 0. use {omega}/language/core/numeric_policy/numeric_policy.omg; -domain i32::Counted requires self >= -1000000; self <= 1000000; +public domain i32::Counted requires self >= -1000000; self <= 1000000; -boundary machine == Integer::equal(left: i32 in Counted, right: i32 in Counted) -> bool -requires right >= 0; - -data CheckedEquality {} +public boundary trait Integer { + machine == equal(left: i32 in Counted, right: i32 in Counted) -> bool + requires (right as -Counted) >= 0 + reaches none; +} -machine equal(left: i32 in Counted, right: i32 in Counted) -> bool -satisfies Integer::equal -requires right >= 0 -{ - left <= right && left >= right +CheckedEquality satisfies Integer { + machine equal(left: i32 in Counted, right: i32 in Counted) -> bool + requires (right as -Counted) >= 0 + reaches none + { + (left as -Counted) <= (right as -Counted) && (left as -Counted) >= (right as -Counted) + } } machine prepare(value: @mutable i32 in Counted) -> i32 in Counted -ensures $value >= 0 { - $value = 1; - 1 +ensures ($value as -Counted) >= 0 { + $value = 1 as +Counted; + 1 as +Counted } machine compare(value: i32 in Counted) -> bool { let mutable held: i32 in Counted = value; - prepare(@mutable held) == held + let prepared: i32 in Counted = prepare(@mutable held); + prepared == held } data Main { hits: i32 in |numeric_policy|::Wrapping; } machine main(main: @mutable Main) crashes? Trap { main.hits = (0i32) as + |numeric_policy|::Wrapping; - let flag: bool = compare(5); + let flag: bool = compare(5 as +Counted); transition flag { true -> pass(main), _ -> fail(main) } machine pass(main: @mutable Main) { main.hits = (1i32) as + |numeric_policy|::Wrapping; } - machine fail(main: @mutable Main) { + machine fail(main: @mutable Main) crashes Trap { main.hits = (-1i32) as + |numeric_policy|::Wrapping; crash Trap; } diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/03_symbol-resolved-trees-to-typed-trees/src/expressions/operator_calls.rs b/source-rust/subsystems/compiler/source-to-psi/pipeline/03_symbol-resolved-trees-to-typed-trees/src/expressions/operator_calls.rs index 45ab2dc36ebe..2aa1f8486546 100644 --- a/source-rust/subsystems/compiler/source-to-psi/pipeline/03_symbol-resolved-trees-to-typed-trees/src/expressions/operator_calls.rs +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/03_symbol-resolved-trees-to-typed-trees/src/expressions/operator_calls.rs @@ -257,6 +257,27 @@ fn selection_for_operands_in_scope( .with_source_span(span)); } } + if candidates.is_empty() && !form.indexed { + let requirements = crate::source_state::operator::boundary_trait_operator_requirements( + program, spelling, operands, span, + ); + return match requirements.as_slice() { + [] => Ok(None), + [requirement] => Ok(Some(SelectedOperator { + target: requirement.symbol, + result_type: Some(requirement.return_type), + label: requirement.name.clone(), + receiver: None, + arguments: Vec::new(), + collection_borrow: None, + })), + _ => Err(Diagnostic::error( + "operator matches several boundary-trait requirements; call one by name", + ) + .with_refusal_kind(TypingRefusalKind::OperatorAmbiguous) + .with_source_span(span)), + }; + } let selected = match candidates.as_slice() { [] => return Ok(None), [selected] => selected, diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/03_symbol-resolved-trees-to-typed-trees/src/tests/qualified_operator_home_tests.rs b/source-rust/subsystems/compiler/source-to-psi/pipeline/03_symbol-resolved-trees-to-typed-trees/src/tests/qualified_operator_home_tests.rs index 02cec2edd6bb..7fb14fb140d8 100644 --- a/source-rust/subsystems/compiler/source-to-psi/pipeline/03_symbol-resolved-trees-to-typed-trees/src/tests/qualified_operator_home_tests.rs +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/03_symbol-resolved-trees-to-typed-trees/src/tests/qualified_operator_home_tests.rs @@ -1838,3 +1838,44 @@ fn operator_independent_domain_composition_accepts_multiple_owners_and_requires_ "combined qualification must select the exact combined operator" ); } + +// A boundary trait's token-bearing requirement is the operator slot its +// Build-selected provider supplies: `a == b` selects that requirement as +// `Comparison::equal(a, b)` does, and two such slots for one operand tuple +// leave the token ambiguous. +#[test] +fn a_boundary_trait_token_selects_its_requirement() { + use crate::source_state::expression::ExpressionNode; + + let source = r#" +domain i32::Compared; +boundary trait Comparison { + machine == equal(left: i32 in Compared, right: i32 in Compared) -> bool reaches none; +} +machine same(left: i32 in Compared, right: i32 in Compared) -> bool { left == right } +"#; + let program = typed(&[("main.omg", source)]); + let targets = program + .expression_table + .iter_backing_expressions() + .filter_map(|(_, node)| match node { + ExpressionNode::Call(call) => Some(call.target.as_str().to_owned()), + _ => None, + }) + .collect::>(); + assert_eq!(targets, ["equal"]); + + let twice = source.replace( + "machine same", + "boundary trait Other {\n machine == other(left: i32 in Compared, right: i32 in Compared) -> bool reaches none;\n}\nmachine same", + ); + let error = checked_diagnostic(&[("main.omg", &twice)]) + .expect_err("two boundary slots cannot supply one token"); + assert_eq!( + error.refusal_kind, + Some(diagnostics::RefusalKind::Typing( + diagnostics::TypingRefusalKind::OperatorAmbiguous + )), + "{error:?}" + ); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/definitions/operator_declarations/operator.rs b/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/definitions/operator_declarations/operator.rs index d101f1540f49..1eb7ab524c97 100644 --- a/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/definitions/operator_declarations/operator.rs +++ b/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/definitions/operator_declarations/operator.rs @@ -34,9 +34,9 @@ pub use spellings::{ }; pub use trait_operators::{ BoundTraitOperatorMeaning, SelectedTraitOperatorMeaning, bound_trait_operator_meanings, - selected_trait_operator_meaning_exists, selected_trait_operator_meanings, - trait_operator_matches_application, trait_operator_operand_bindings, - trait_scope_operator_meaning_exists, + boundary_trait_operator_requirements, selected_trait_operator_meaning_exists, + selected_trait_operator_meanings, trait_operator_matches_application, + trait_operator_operand_bindings, trait_scope_operator_meaning_exists, }; use application_identity::const_value::CanonicalConstIdentity; diff --git a/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/definitions/operator_declarations/operator/trait_operators.rs b/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/definitions/operator_declarations/operator/trait_operators.rs index e0f9ed46eca3..bc2c99c01785 100644 --- a/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/definitions/operator_declarations/operator/trait_operators.rs +++ b/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/definitions/operator_declarations/operator/trait_operators.rs @@ -399,3 +399,59 @@ pub(crate) fn abstract_requirement_may_have_operator_bound( } false } + +/// A token-bearing requirement of a non-generic boundary trait names the +/// operator slot its Build-selected provider supplies (expressions.md, +/// "Executable supply"). Like a named call `Trait::member(...)`, the operator +/// selects the requirement itself; provider planning binds the realization. +pub fn boundary_trait_operator_requirements<'program>( + program: &'program TypedTrees, + spelling: OperatorSpelling, + operand_types: &[Option], + use_site: source_text::SourceSpan, +) -> Vec<&'program crate::source_state::signature::StateSignature> { + use crate::source_state::trait_definition::AppliedTraitScope; + program + .traits() + .iter() + .filter(|declaration| { + declaration.is_boundary + && program.trait_type_parameters(declaration).is_empty() + && boundary_trait_visible(program, declaration, use_site) + }) + .flat_map(|declaration| { + program + .trait_machine_signatures(declaration) + .iter() + .filter(move |requirement| { + requirement.spelling == Some(spelling) + && program + .state_signature_type_parameters(requirement) + .is_empty() + && trait_operator_operand_bindings( + program, + &AppliedTraitScope::abstract_scope(declaration), + requirement, + operand_types, + ) + .is_some() + }) + }) + .collect() +} + +fn boundary_trait_visible( + program: &TypedTrees, + declaration: &crate::source_state::trait_definition::TraitDefinition, + use_site: source_text::SourceSpan, +) -> bool { + let provenance = program + .symbols + .symbol_provenance_source_span(declaration.symbol); + (declaration.is_public + || provenance + .is_none_or(|provenance| program.symbols.same_source_package(use_site, provenance))) + && program + .symbols + .source_reference_can_see_symbol(use_site, declaration.symbol) +}