From f15e458fd0978b7122edd7d93b2ad867b148f352 Mon Sep 17 00:00:00 2001 From: Jarod Castillo <43973221+Smotto@users.noreply.github.com> Date: Thu, 26 Nov 2026 18:33:33 -0800 Subject: [PATCH] psi: select generic and applied conformance members through static binders Calling a conformance member through a static record binder that another type parameter applies (`machine use_map>(x: T) -> bool { E::paint(x) }`), calling a generic member through a static record binder (`E::keep::(x)` or `E::pick::<4>(x)`), and supplying a generic member through a selection row (`Keep::keep = hold;` with `machine hold`) now compile, check, and agree on Psi and native execution. Previously each of the three paths stopped at a different stage: - **Static record binders applied by another parameter.** `Operand::read` in `static_operand_binding/mod.rs`, `static_record_source_receiver` in `evidence.rs`, `selected_record_call_operand` in `callable_signature_shapes.rs`, and `const_argument` in `declare.rs` compared the binder's declared schema (`Render`) directly against the concrete conformance (`Red satisfies Render`) without substituting the call's other static arguments (`T -> u8`). `Operand::require_static_records` now instantiates the declared schema with the full application tuple after all operands are read, and the receiver/publication checks instantiate the schema in the enclosing specialization frame via `static_record_schema_in_frame`. - **Generic members called through a static record binder.** When stage 04 copied a generic machine containing a closed requirement call (`E::keep::` or `E::pick::<4>`), `substitute_arguments` cleared the copied application's bindings without resetting the call target back to the requirement template, so stage 05 rejected the call as having an incomplete bound argument tuple. `substitute_arguments` now resets copied closed requirement targets to their template so stage 04 re-prepares them in the specialized caller's scope; `named_conformance_call_member` allows retained local bindings on closed requirement calls; `record_requirement_signature`, `static_value_arguments`, and `StaticRecordRequirementInstance` retain each closed requirement's own signature symbol so distinct applications (such as `E::pick::<4>` and `E::pick::<5>`) stay separate in checking. - **Selection rows of generic members.** In `read/machine.rs`, `requirement_body` fell through to `self.machine(symbol)` for a reference row's free machine, reading it as `Reading::Closed` when checking a generic witness (`self.rigid = true`), and `witnessed` kept the unapplied requirement template when a specialized free machine had no owning conformance. Both now read the generic template under `Reading::Implementation` and resolve the closed requirement header for specialized reference rows. In stage 02, `initializer_dependencies.rs` also resolves reference rows via `row.realization_machine` instead of demanding an owned inline realization. The completed sub-bullet under **STATIC-MACHINE-MIGRATION** is deleted from `TASKS.md`. Gate: RC-SOURCE-SEMANTICS | select conformance members through type-applied static record binders, call generic members through static record binders, and supply generic members via selection rows Spec: wiki/spec/language/conformances.md#declaration-and-selection, wiki/spec/language/generics.md#generic-machine-requirements Proof: `mbx test -p source-compiler --features test-support --test checking_requirement_interfaces --test checking_indexed_and_member_contracts` (12 + 18 passed), `mbx test -p typed-trees-to-prepared-typed-trees --lib -- specialization` (48 passed), `mbx test -p terminal-check-tables --lib` (13 passed), `mbx test -p lowered-program-to-checked-program --lib -- boundary_calls` (84 passed), `mbx clippy --no-deps` on all 7 touched crates clean, `mbx xtask fmt --check --base origin/main`, `mbx xtask check-boundary`, `mbx xtask check-rules` clean, and `omega run --engine native` / `--engine psi` on Windows x86_64 agree on positive (`native exit 0` / `Psi exit 0`) and trap negative twin (`native exit -1073741795` / `Psi crash: Trap`) | clean. Not run: Linux, macOS. Agent: Antigravity | Model: unknown | Effort: high | Tokens: 20.07M in (19.69M cached), 68.4k out | Steps: 100 | Time: 40m54s | Steers: 0 --- TASKS.md | 1 - .../src/constants/initializer_dependencies.rs | 31 ++++-- .../src/passes/specialization.rs | 36 +++++-- .../src/declare.rs | 26 ++++- .../src/read/calls.rs | 11 ++ .../src/read/machine.rs | 5 + .../src/read/record_parameters.rs | 45 ++++---- .../src/calls/static_contracts.rs | 10 +- .../tests/requirement_interfaces.rs | 79 ++++++++++++++ .../lowered-program/tables/contracts.rs | 2 + .../callables/callable_applications.rs | 10 +- .../callables/callable_signature_shapes.rs | 2 +- .../callables/static_operand_binding/mod.rs | 101 +++++++++++++++++- .../static_operand_binding/requirement.rs | 1 + .../trait_definition/applications/evidence.rs | 38 ++++++- 15 files changed, 342 insertions(+), 56 deletions(-) diff --git a/TASKS.md b/TASKS.md index 1a32b5201ae2..616d76fd59b4 100644 --- a/TASKS.md +++ b/TASKS.md @@ -46,7 +46,6 @@ Find Rust owners in the [compiler map](source-rust/README.md#where-code-belongs) - Qualify `positional_implementation_binder`/`open_const_implementation`/`open_record_const_implementation`/`named_conformance_dispatch`/`generic_calling_policy`/`domain_selected_cleanup` on all hosts. - Move Build facets to `Build::*` and retire `machine Data::operation` and attached helpers. - Keep live loans and their backing valid across suspension. - - Select a conformance member through a static binder that a type parameter applies or that is itself generic. `machine use_map>(x: T) -> bool { E::paint(x) }` called as `use_map::(x)` is refused in preparation ("a static record operand has another declared schema"), and `E::keep::(x)` or `E::pick::<4>(x)` on a member declared `machine keep` or `machine pick` is refused in lowering ("closed requirement call has an incomplete bound argument tuple"). Named directly, `Local::keep::(x)` and `Local::pick::<4>(x)` enter the member instance prepared for their arguments and run. A selection row of a generic member (`Keep::keep = hold;` with `machine hold`) is still refused ("generic machine read without arguments"), as a generic reference row of a boundary trait is. - Migrate remaining attached machines in samples, targets and system fixtures with their original execution oracles. - Recover the authored `Count` and nominal callback contract in [package review](source-rust/subsystems/compiler/tests/fixtures/facts/pass/static_callback_reach_boundary_schema/main.omg). Preserve `Step<2>` and `Step<3>`, the unused effectful selection and the recovered schema. Migrate the free boundary machine under the [supply rules](wiki/spec/language/machines.md#supply). diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/02_syntax-trees-to-symbol-resolved-trees/src/constants/initializer_dependencies.rs b/source-rust/subsystems/compiler/source-to-psi/pipeline/02_syntax-trees-to-symbol-resolved-trees/src/constants/initializer_dependencies.rs index 32341f7e2915..cb30630e8c0e 100644 --- a/source-rust/subsystems/compiler/source-to-psi/pipeline/02_syntax-trees-to-symbol-resolved-trees/src/constants/initializer_dependencies.rs +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/02_syntax-trees-to-symbol-resolved-trees/src/constants/initializer_dependencies.rs @@ -324,15 +324,28 @@ mod types { self.type_parameters(definition.type_parameters)?; self.type_arguments(definition.arguments)?; for row in &definition.rows { - let machine = - crate::resolution::names::owned_conformance_realization(self.program, row)?; - if crate::resolution::names::owned_conformance_namespace(self.program, machine)? - != symbol - { - return Err( - "constant dependency implementation member lost its exact owner".into(), - ); - } + let machine = if row.source.owns_realization() { + let machine = + crate::resolution::names::owned_conformance_realization(self.program, row)?; + if crate::resolution::names::owned_conformance_namespace(self.program, machine)? + != symbol + { + return Err( + "constant dependency implementation member lost its exact owner".into(), + ); + } + machine + } else { + let machine = row.realization_machine; + if !machine.is_valid() + || self.program.machines.unique_by_symbol(machine).is_none() + { + return Err( + "constant dependency reference row lost its selected machine".into(), + ); + } + machine + }; self.machines.push(machine); } Ok(()) diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/04_typed-trees-to-prepared-typed-trees/src/passes/specialization.rs b/source-rust/subsystems/compiler/source-to-psi/pipeline/04_typed-trees-to-prepared-typed-trees/src/passes/specialization.rs index bf77135f52ec..74183242c2da 100644 --- a/source-rust/subsystems/compiler/source-to-psi/pipeline/04_typed-trees-to-prepared-typed-trees/src/passes/specialization.rs +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/04_typed-trees-to-prepared-typed-trees/src/passes/specialization.rs @@ -2577,12 +2577,16 @@ fn substitute_arguments( .map(|(handle, _)| handle) .collect::>(); for handle in calls { - let mut machine_arguments = match program.expression_table.expression(handle) { - ExpressionNode::Call(call) if !call.build_selection.is_valid() => { - Some(call.machine_arguments.clone()) - } - _ => None, - }; + let (mut machine_arguments, closed_template) = + match program.expression_table.expression(handle) { + ExpressionNode::Call(call) if !call.build_selection.is_valid() => ( + Some(call.machine_arguments.clone()), + program + .closed_requirement_application(call.target_symbol) + .map(|closed| closed.template), + ), + _ => (None, None), + }; if let Some(arguments) = machine_arguments.as_mut() { replace(program, arguments); } @@ -2592,6 +2596,9 @@ fn substitute_arguments( if let Some(arguments) = machine_arguments { call.machine_arguments = arguments; } + if let Some(template) = closed_template { + call.target_symbol = template; + } if let Some(target) = selected_callable(call.target_symbol) { call.static_machine_parameter = call.target_symbol; call.target_symbol = target; @@ -2600,10 +2607,16 @@ fn substitute_arguments( } } for &handle in &body.statements { - let mut machine_arguments = match program.statement_table.statement(handle) { - StatementNode::Call(call) => Some(call.machine_arguments.clone()), - _ => None, - }; + let (mut machine_arguments, closed_template) = + match program.statement_table.statement(handle) { + StatementNode::Call(call) => ( + Some(call.machine_arguments.clone()), + program + .closed_requirement_application(call.target_symbol) + .map(|closed| closed.template), + ), + _ => (None, None), + }; if let Some(arguments) = machine_arguments.as_mut() { replace(program, arguments); } @@ -2613,6 +2626,9 @@ fn substitute_arguments( if let Some(arguments) = machine_arguments { call.machine_arguments = arguments; } + if let Some(template) = closed_template { + call.target_symbol = template; + } if let Some(target) = selected_callable(call.target_symbol) { call.static_machine_parameter = call.target_symbol; call.target_symbol = target; diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/declare.rs b/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/declare.rs index 4f5cbe04bd3c..90434aaf20ea 100644 --- a/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/declare.rs +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/declare.rs @@ -1159,7 +1159,8 @@ fn application_bindings_commitment( // Boolean is stated the same way, by `true` or `false`: its // parameter's type is `bool`, which no integer literal lands in. StaticMachineBindingValue::Const(value_reference) => { - let argument = const_argument(typed, binding.parameter, *value_reference)?; + let argument = + const_argument(typed, binding.parameter, *value_reference, bindings)?; commitment.const_argument(&argument)?; if let Some(prepared) = prepared.as_mut() { prepared.parameter_kinds.push(1); @@ -1375,6 +1376,7 @@ fn const_argument( typed: &TypedTrees, binder: SymbolHandle, reference: crate::source_state::types::TypeReferenceHandle, + bindings: &[crate::source_state::static_applications::StaticMachineBinding], ) -> Result { use crate::source_state::data::TypeParameterKind; use application_identity::const_value::CanonicalConstValue; @@ -1387,9 +1389,25 @@ fn const_argument( else { return Err("an application's value binding belongs to another binder kind"); }; - if let Some(required) = - typed.static_record_schema(type_reference, typed.symbols.get(binder).parent) - { + let owner = typed.symbols.get(binder).parent; + if let Some(required) = typed.static_record_schema(type_reference, owner) { + let type_bindings = bindings + .iter() + .filter_map(|binding| match binding.value { + crate::source_state::static_applications::StaticMachineBindingValue::Type( + reference, + ) + | crate::source_state::static_applications::StaticMachineBindingValue::Const( + reference, + ) => reference + .is_valid() + .then_some((binding.parameter, reference)), + _ => None, + }) + .collect::>(); + let required = required + .under_invocation(typed, owner, owner, &type_bindings, &[]) + .ok_or("an application's record argument lost its instantiated schema")?; let operand = typed.static_operand_from_type_reference(reference)?; if operand.application.is_some() || operand.evidence_projection.is_some() diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/calls.rs b/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/calls.rs index 059466f4199b..7de52d4a65aa 100644 --- a/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/calls.rs +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/calls.rs @@ -2139,6 +2139,17 @@ impl<'t> Program<'t> { if !authored_arguments.is_empty() && authored_arguments != arguments { return unserved("requirement call whose static arguments differ from its application"); } + if binders.is_empty() + && self + .typed + .closed_requirement_application(declaration) + .is_some() + { + self.typed + .require_closed_requirement_call_application(declaration, application_handle) + .map_err(|reason| refuse_at(id, Unserved::Call(reason)))?; + return Ok(Vec::new()); + } if arguments.len() != binders.len() || bindings.len() != binders.len() { return unserved("requirement call with static arguments"); } diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/machine.rs b/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/machine.rs index 7e2eb9f2a4b4..aabed8ec9ddf 100644 --- a/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/machine.rs +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/machine.rs @@ -748,6 +748,10 @@ impl<'t> Program<'t> { self.implementation_body = Some(id); } Ok(id) + } else if self.rigid && matches!(reading, Reading::Implementation) { + let id = self.read_machine(id, symbol, reading)?; + self.implementation_body = Some(id); + Ok(id) } else { self.machine(symbol) } @@ -1135,6 +1139,7 @@ impl<'t> Program<'t> { .is_some_and(|owner| { !owner.type_parameters.is_empty() || !owner.lifetime_parameters.is_empty() }) + || (!self.rigid && !declared.type_parameters.is_empty()) { // A body a reference row selects (`Render::paint = red;`) owns // no map, so the requirement it refines is the one the selecting diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/record_parameters.rs b/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/record_parameters.rs index f6924cfffc36..dca6994991ba 100644 --- a/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/record_parameters.rs +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/05_prepared-typed-trees-to-lowered-program/src/read/record_parameters.rs @@ -163,6 +163,7 @@ impl Program<'_> { let signature = binding.signatures.get(position).ok_or_else(|| { crate::read::unavailable(id, "const record requirement lost its prepared signature") })?; + let mut resolved = signature; if signature.symbol != target { // A receiver can name the original requirement or its prepared // application. The receiver's declared schema must select this @@ -171,28 +172,31 @@ impl Program<'_> { .typed .static_record_bound_requirement(binding.account.owner, receiver, target) .map_err(|reason| crate::read::unavailable(id, reason))?; - let same_member = exact.is_some_and(|required| { - required.signature.symbol == signature.symbol - || self - .typed - .closed_requirement_application(target) - .is_some_and(|closed| { - closed.template == origin - && closed.local_arguments.is_empty() - && self.typed.requirement_application_matches_scope( - target, - &required.application, - ) - }) - }); - if !same_member { + if let Some(required) = exact.as_ref() + && required.signature.symbol == signature.symbol + { + // Same requirement signature. + } else if let Some(required) = exact.as_ref() + && self + .typed + .closed_requirement_application(target) + .is_some_and(|closed| { + closed.template == origin + && self.typed.requirement_application_matches_scope( + target, + &required.application, + ) + }) + { + resolved = required.signature; + } else { return Err(crate::read::unavailable( id, "const record call has an unrepresented local requirement application", )); } } - Ok(Some((position, signature.clone()))) + Ok(Some((position, resolved.clone()))) } pub(super) fn record_requirement( @@ -206,10 +210,10 @@ impl Program<'_> { .record_parameter(receiver) .and_then(|binding| binding.account.members.get(position)) .and_then(|member| { - member - .instances - .iter() - .find(|instance| instance.arguments == arguments) + member.instances.iter().find(|instance| { + self.symbols.get(instance.machine) == Some(&signature.symbol) + && instance.arguments == arguments + }) }) { return Ok(instance.machine); @@ -249,6 +253,7 @@ impl Program<'_> { })?; member.instances.push(StaticRecordRequirementInstance { machine: id, + signature: signature.symbol, arguments: arguments.to_vec(), }); Ok(id) diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/src/calls/static_contracts.rs b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/src/calls/static_contracts.rs index 7b04c3eebb32..74d427efe8e9 100644 --- a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/src/calls/static_contracts.rs +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/src/calls/static_contracts.rs @@ -626,12 +626,14 @@ pub(crate) fn admit(elaboration: &Elaboration) -> Checked<()> { )); } members.push(member.requirement); - let mut arguments = Vec::new(); + let mut seen = Vec::new(); for instance in &member.instances { + let key = (instance.signature, &instance.arguments); if instances.contains(&instance.machine) - || arguments.contains(&instance.arguments) + || seen.contains(&key) + || !instance.signature.is_valid() || !elaboration.requirements.contains(&instance.machine) - || elaboration.machines.get(instance.machine) != Some(&member.signature) + || elaboration.machines.get(instance.machine) != Some(&instance.signature) || elaboration.value_arguments.get(instance.machine) != Some(&instance.arguments) || elaboration @@ -645,7 +647,7 @@ pub(crate) fn admit(elaboration: &Elaboration) -> Checked<()> { )); } instances.push(instance.machine); - arguments.push(instance.arguments.clone()); + seen.push(key); } } } diff --git a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/requirement_interfaces.rs b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/requirement_interfaces.rs index 4d5b580fd692..d3ab018213d0 100644 --- a/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/requirement_interfaces.rs +++ b/source-rust/subsystems/compiler/source-to-psi/pipeline/06_lowered-program-to-checked-program/tests/requirement_interfaces.rs @@ -316,3 +316,82 @@ fn a_named_call_of_a_generic_member_enters_the_instance_for_its_arguments() { "{generic:?}" ); } + +#[test] +fn a_static_binder_applied_by_a_type_parameter_selects_its_member() { + let positive = "trait Render { machine paint(x: T) -> bool; } +machine red(x: u8) -> bool { true } +Red satisfies Render { Render::paint = red; } +machine use_map>(x: T) -> bool { E::paint(x) } +machine call(x: u8) -> bool { use_map::(x) }"; + accepted(positive); + + let wrong_schema = "trait Render { machine paint(x: T) -> bool; } +machine red(x: u8) -> bool { true } +Red satisfies Render { Render::paint = red; } +machine use_map>(x: T) -> bool { E::paint(x) } +machine call(x: u16) -> bool { use_map::(x) }"; + let refusal = Program::try_of(&[("product/main.omg", wrong_schema)]) + .err() + .expect("Red satisfies Render, not Render"); + assert!( + format!("{refusal:?}").contains("a static record operand has another declared schema"), + "{refusal:?}" + ); +} + +#[test] +fn a_generic_member_called_through_a_static_binder_enters_its_instance() { + let source = |requires: &str| { + format!( + "trait Keep {{ + machine keep(x: T, n: u8) -> T; + machine pick(x: u8) -> u8; +}} +Local satisfies Keep {{ + machine keep(x: T, n: u8) -> T {requires} {{ x }} + machine pick(x: u8) -> u8 {{ x +% N }} +}} +machine use_keep(x: u8) -> u8 {{ E::pick::<5>(E::pick::<4>(E::keep::(x, 0u8))) }} +machine call(x: u8) -> u8 {{ use_keep::(x) }}" + ) + }; + accepted(&source("")); + + let twin = verdict(&source("requires n != 0")).rejected(); + assert!( + twin.unproved + .iter() + .any(|kind| kind.starts_with("CallRequirement")), + "{twin:?}" + ); +} + +#[test] +fn a_selection_row_of_a_generic_member_checks_and_enters_its_instance() { + let source = |requires: &str| { + format!( + "trait Keep {{ + machine keep(x: T, n: u8) -> T; + machine pick(x: u8) -> u8; +}} +machine hold(x: T, n: u8) -> T {requires} {{ x }} +machine step(x: u8) -> u8 {{ x +% N }} +Local satisfies Keep {{ + Keep::keep = hold; + Keep::pick = step; +}} +machine use_keep(x: u8) -> u8 {{ E::pick::<5>(E::pick::<4>(E::keep::(x, 0u8))) }} +machine call(x: u8) -> u8 {{ use_keep::(x) }}" + ) + }; + accepted(&source("")); + + let twin = verdict(&source("requires n != 0")).rejected(); + assert!( + twin.unproved + .iter() + .any(|kind| kind.starts_with("CallRequirement")), + "{twin:?}" + ); +} diff --git a/source-rust/subsystems/compiler/source-to-psi/representations/lowered-program/tables/contracts.rs b/source-rust/subsystems/compiler/source-to-psi/representations/lowered-program/tables/contracts.rs index 850089b64e7a..602d575bf180 100644 --- a/source-rust/subsystems/compiler/source-to-psi/representations/lowered-program/tables/contracts.rs +++ b/source-rust/subsystems/compiler/source-to-psi/representations/lowered-program/tables/contracts.rs @@ -396,6 +396,7 @@ impl StaticRecordBinding { #[derive(Debug, Clone, PartialEq, Eq)] pub struct StaticRecordRequirementInstance { pub machine: MachineId, + pub signature: SymbolHandle, pub arguments: Vec, } @@ -475,6 +476,7 @@ mod static_record_instance_tests { application: "Ops".into(), instances: vec![StaticRecordRequirementInstance { machine: machine(instance), + signature: symbol(3), arguments: Vec::new(), }], }], diff --git a/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/callables/callable_applications.rs b/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/callables/callable_applications.rs index b84f4e123e4c..a54497db3b56 100644 --- a/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/callables/callable_applications.rs +++ b/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/callables/callable_applications.rs @@ -2263,15 +2263,17 @@ pub(crate) mod namespaces { _ => return Err("named application changed its complete map constructor"), }; let source = self.static_machine_application(call.authored_machine_application); - let template = self - .closed_requirement_application(call.target_symbol) + let closed = self.closed_requirement_application(call.target_symbol); + let template = closed + .as_ref() .map_or(call.target_symbol, |closed| closed.template); // A bodyless trait requirement is no machine declaration, so - // source retention leaves its local binding declaration absent. + // source retention leaves its local binding declaration absent + // unless stage 04 closed a generic requirement application. // Its registered operation and supplying owner remain exact. if source.registration_operation != template || (source.binding_declaration.is_valid() && source.binding_declaration != template) - || !source.bindings.is_empty() + || (closed.is_none() && !source.bindings.is_empty()) || !source.lifetime_bindings.is_empty() || !source.lexical_owner.is_valid() || !self diff --git a/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/callables/callable_signature_shapes.rs b/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/callables/callable_signature_shapes.rs index 334e02dd812f..c97095f17341 100644 --- a/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/callables/callable_signature_shapes.rs +++ b/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/callables/callable_signature_shapes.rs @@ -664,7 +664,7 @@ impl crate::source_state::TypedTrees { return Err("selected record was not supplied as its declared Const value".into()); }; let schema = self - .static_record_parameter_schema(frame.declaration, parameter)? + .static_record_schema_in_frame(&frame, parameter)? .ok_or("selected record receiver has no exact declared schema")?; let actual = self.static_operand_from_type_reference(reference)?; if identity(&actual, frame.argument_owner)? != identity(selected, selected_supplier)? { diff --git a/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/callables/static_operand_binding/mod.rs b/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/callables/static_operand_binding/mod.rs index 073d7bf46246..e74946aff514 100644 --- a/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/callables/static_operand_binding/mod.rs +++ b/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/callables/static_operand_binding/mod.rs @@ -325,6 +325,84 @@ impl Operand { &bindings, )?; } + Self::require_static_records(program, parameters, operands, supplying_owner) + } + + /// Judge every static record operand against its declared schema after all + /// operands of the application are read, so a schema that mentions another + /// parameter (`use_map>`) is instantiated with the + /// call's arguments before comparing against the supplied conformance. + pub fn require_static_records( + program: &TypedTrees, + parameters: &[crate::source_state::data::TypeParameter], + operands: &[Operand], + supplying_owner: SymbolHandle, + ) -> Served<()> { + let bindings = operands + .iter() + .filter_map(|operand| match &operand.value { + StaticMachineBindingValue::Type(reference) + | StaticMachineBindingValue::Const(reference) => reference + .is_valid() + .then_some((operand.parameter, *reference)), + _ => None, + }) + .collect::>(); + for operand in operands { + let Some(binder) = parameters + .iter() + .find(|binder| binder.symbol == operand.parameter) + else { + return Err(Stop::Defect("an operand lost its declared parameter")); + }; + let TypeParameterKind::Const { type_reference } = binder.kind else { + continue; + }; + let owner = program.symbols.get(operand.parameter).parent; + let Some(required) = program.static_record_schema(type_reference, owner) else { + continue; + }; + let required = required + .under_invocation(program, owner, supplying_owner, &bindings, &[]) + .ok_or(unserved( + "a static record binder needs its call's static arguments", + ))?; + let symbol = operand.argument.symbol; + if program.symbols.get(symbol).kind == SymbolKind::TypeParameter { + let declaration = program + .data_type_parameter_by_symbol(symbol) + .ok_or(unserved("a forwarded static record has no declared binder"))?; + let TypeParameterKind::Const { type_reference } = declaration.kind else { + return Err(unserved("a forwarded static record is not a const value")); + }; + let actual = program + .static_record_schema(type_reference, program.symbols.get(symbol).parent) + .ok_or(unserved( + "a forwarded static record lost its declared schema", + ))?; + if !required.same_application(program, &actual) { + return Err(Stop::Rejected( + "a forwarded static record has another declared schema".into(), + )); + } + } else { + let selected = program.conformance_by_symbol(symbol).ok_or(unserved( + "a static record operand has no named record declaration", + ))?; + let actual = + crate::source_state::trait_definition::AppliedTraitScope::from_conformance( + program, selected, + ) + .ok_or(unserved( + "a static record operand lost its complete schema application", + ))?; + if !required.same_application(program, &actual) { + return Err(Stop::Rejected( + "a static record operand has another declared schema".into(), + )); + } + } + } Ok(()) } @@ -573,6 +651,15 @@ fn named_static_record( .ok_or(unserved( "a static record parameter lost its exact declared schema", ))?; + // When the declared schema mentions another parameter of the same + // declaration (such as `use_map>`), check the trait + // family here and defer exact application matching to + // `Operand::require_static_records` once the full tuple is read. + let closed_schema = crate::source_state::domain::index_argument_is_closed_with_substitutions( + program, + declared_type, + &[], + ); let symbol = argument.symbol; if program.symbols.get(symbol).kind == SymbolKind::TypeParameter { let declaration = program @@ -588,7 +675,12 @@ fn named_static_record( .ok_or(unserved( "a forwarded static record lost its declared schema", ))?; - if !required.same_application(program, &actual) { + let matches = if closed_schema { + required.same_application(program, &actual) + } else { + required.declaration.symbol == actual.declaration.symbol + }; + if !matches { return Err(Stop::Rejected( "a forwarded static record has another declared schema".into(), )); @@ -611,7 +703,12 @@ fn named_static_record( .ok_or(unserved( "a static record operand lost its complete schema application", ))?; - if !required.same_application(program, &actual) { + let matches = if closed_schema { + required.same_application(program, &actual) + } else { + required.declaration.symbol == actual.declaration.symbol + }; + if !matches { return Err(Stop::Rejected( "a static record operand has another declared schema".into(), )); diff --git a/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/callables/static_operand_binding/requirement.rs b/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/callables/static_operand_binding/requirement.rs index 9f11eafb0386..742e8f4eb558 100644 --- a/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/callables/static_operand_binding/requirement.rs +++ b/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/callables/static_operand_binding/requirement.rs @@ -106,6 +106,7 @@ pub fn prepare_requirement_in_scope( supplying_owner, )?); } + Operand::require_static_records(program, ¶meters, &operands, supplying_owner)?; bind(program, application, &operands)?; let table = &mut program.expression_table; table diff --git a/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/definitions/trait_definition/applications/evidence.rs b/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/definitions/trait_definition/applications/evidence.rs index 7c4550512368..4a47ca7b8263 100644 --- a/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/definitions/trait_definition/applications/evidence.rs +++ b/source-rust/subsystems/compiler/source-to-psi/representations/typed-trees/definitions/trait_definition/applications/evidence.rs @@ -66,6 +66,42 @@ impl TypedTrees { Ok(Some(schema)) } + /// Instantiate a const-record binder's declared schema at an enclosing + /// specialization frame's retained static bindings. + pub(crate) fn static_record_schema_in_frame( + &self, + frame: &crate::callables::machine_instances::EnclosingInvocationFrame<'_>, + binder: SymbolHandle, + ) -> Result>, String> { + let Some(root) = self.static_record_parameter_schema(frame.declaration, binder)? else { + return Ok(None); + }; + let frame_bindings = frame + .bindings + .iter() + .filter_map(|binding| match binding.value { + crate::source_state::static_applications::StaticMachineBindingValue::Type( + reference, + ) + | crate::source_state::static_applications::StaticMachineBindingValue::Const( + reference, + ) => reference + .is_valid() + .then_some((binding.parameter, reference)), + _ => None, + }) + .collect::>(); + root.under_invocation( + self, + frame.declaration, + frame.argument_owner, + &frame_bindings, + &[], + ) + .map(Some) + .ok_or_else(|| "static record receiver lost its instantiated schema".into()) + } + pub fn static_record_bound_requirement<'program>( &'program self, owner: SymbolHandle, @@ -168,7 +204,7 @@ impl TypedTrees { )? .ok_or("static record receiver has no exact declared requirement")?; let root = self - .static_record_parameter_schema(frame.declaration, binder)? + .static_record_schema_in_frame(&frame, binder)? .ok_or("static record receiver has no exact declared schema")?; if !self.type_reference_table.contains_type_reference(reference) || !self