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) +}