From 19c2e3d7d967e9609bb8104a1d8e7a5873df7a1f Mon Sep 17 00:00:00 2001 From: Brian Searls Date: Fri, 24 Apr 2026 03:01:22 -0400 Subject: [PATCH 1/2] WIP: T-PB-A-B --- scripts/slow-test-exemptions.txt | 1 - src/v3/compiler/build.rs | 1 - src/v3/compiler/src/bin/regen_lower.rs | 89 - src/v3/compiler/src/lower_generated.rs | 6599 ----------------- src/v3/compiler/tests/integration.rs | 2 - .../tests/integration/sg0_census_test.rs | 10 +- .../integration/sg3_lower_authority_test.rs | 44 - .../sg6_hand_authored_census_test.rs | 8 +- 8 files changed, 6 insertions(+), 6748 deletions(-) delete mode 100644 src/v3/compiler/src/bin/regen_lower.rs delete mode 100644 src/v3/compiler/src/lower_generated.rs delete mode 100644 src/v3/compiler/tests/integration/sg3_lower_authority_test.rs diff --git a/scripts/slow-test-exemptions.txt b/scripts/slow-test-exemptions.txt index 30f9ae97318..cac5592b8c3 100644 --- a/scripts/slow-test-exemptions.txt +++ b/scripts/slow-test-exemptions.txt @@ -77,7 +77,6 @@ sg1_tokenize_authority_test::tokenize_generated_module_matches_checked_in_snapsh sg2_parse_authority_test::parse_generated_module_matches_checked_in_snapshot # SG-2 parser-staging snapshot: in-process authority compile + rustfmt stdout (same paydown class as SG-1 tokenize snapshot gate). sg2_parse_authority_test::parse_surface_dag_compiles_cleanly_for_regen_parse # SG-2 parser-staging: `compile_parse_surface_std_authority_dag` + cold bootstrap work can exceed 2s wall under default libtest parallelism; ROADMAP SG-2 / TESTING.md paydown (same lane as `parse_generated_module_matches_checked_in_snapshot`). sg2c1_parse_tables_authority_test::parse_tables_generated_module_matches_checked_in_snapshot # SG-2c-1 grammar-tables prototype snapshot: in-process authority compile (parse_tables.dag + tokenize.dag) + rustfmt stdout; same paydown class as SG-1 tokenize / SG-2 parse snapshot gates. -sg3_lower_authority_test::lower_generated_module_matches_regen_snapshot # SG-3f-prep: `cargo run regen_lower` cold-links on CI; snapshot gate mirrors SG-1 tokenize pattern; canonical lowering remains `lower.rs`. sg6_hand_authored_census_test::sg6_regen_lens_cli_smoke_regenerates_named_entry_without_drift # SG-6 regen CLI smoke intentionally shells out to regen path; CLI smoke cost is tracked in SG-6, not TM-0. thesis_validation_test::kf_1_structural_list_operation_ordering_holds # KF-1 thesis validation remains compile-heavy on CI runners; TESTING.md test-shape paydown owns future decomposition, not TM-0. diff --git a/src/v3/compiler/build.rs b/src/v3/compiler/build.rs index 92cf2322dc6..626dbcfc47b 100644 --- a/src/v3/compiler/build.rs +++ b/src/v3/compiler/build.rs @@ -303,7 +303,6 @@ fn main() { "src/v3/compiler/src/lens_provenance_generated.rs", "src/v3/compiler/src/lens_structural_resolution_generated.rs", "src/v3/compiler/src/lens_unused_parameters_generated.rs", - "src/v3/compiler/src/lower_generated.rs", "src/v3/compiler/src/lower_helpers_generated.rs", "src/v3/compiler/src/operators_generated.rs", "src/v3/compiler/src/parse_generated.rs", diff --git a/src/v3/compiler/src/bin/regen_lower.rs b/src/v3/compiler/src/bin/regen_lower.rs deleted file mode 100644 index 2227bf327d1..00000000000 --- a/src/v3/compiler/src/bin/regen_lower.rs +++ /dev/null @@ -1,89 +0,0 @@ -//! Regenerate `lower_generated.rs` from the canonical hand-maintained -//! `src/v3/compiler/src/lower.rs` (the `lower` module). -//! -//! **SG-3f-prep (Option B):** `lower.rs` remains on the SG-0 census as the real -//! lowering implementation. This binary is a pass-through + header + rustfmt — -//! it does not dissolve the algorithm into `.dag`. The emitted -//! `lower_generated.rs` is **not** imported by `lib.rs`; it exists so the -//! regen + snapshot test harness stay wired for a future `lower.dag` cutover -//! after substrate reflection (`Surface*` in `substrate.dag`, SELF_HOSTING.md §4). -//! Do not treat `lower_generated.rs` as the live authority until that migration lands. -//! -//! **CLI:** `--out PATH` (or `-o PATH`) writes the formatted snapshot to `PATH` instead -//! of `src/lower_generated.rs`. Integration tests use this so the workspace tree is not -//! mutated (TESTING.md hermetic discipline); humans omit the flag. - -use std::io::Write; -use std::path::{Path, PathBuf}; -use std::process::{Command, Stdio}; - -use v3_compiler::generated_files::GENERATED_FILES; - -const GENERATED_FILE: &str = "src/v3/compiler/src/lower_generated.rs"; -/// Canonical lowering source (hand-maintained; counted by SG-0). -const LOWER_RS: &str = "src/lower.rs"; - -const HEADER: &str = "// AUTO-GENERATED from `src/v3/compiler/src/lower.rs` via\n\ - // `regen_lower`. Regenerate instead of hand-editing.\n\ - //\n\ - // SG-3f-prep: not wired into `lib.rs` — canonical implementation is\n\ - // `lower.rs` pending `lower.dag` + reflected `Surface*` (SELF_HOSTING.md §4).\n\ - //\n\ - // Body text (including scaffold / tracked-debt comments) is copied verbatim\n\ - // from `lower.rs`; edit `lower.rs` for any change to that prose — not here.\n\ - \n"; - -fn out_path_from_args(manifest_dir: &Path) -> PathBuf { - let mut args = std::env::args().skip(1); - while let Some(a) = args.next() { - if a == "--out" || a == "-o" { - let path = args.next().unwrap_or_else(|| { - panic!("`regen_lower {a}` requires a path argument"); - }); - return PathBuf::from(path); - } - } - manifest_dir.join("src").join("lower_generated.rs") -} - -fn main() { - let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - let out_path = out_path_from_args(&manifest_dir); - let canonical_out = manifest_dir.join("src").join("lower_generated.rs"); - if out_path == canonical_out { - assert!( - GENERATED_FILES.contains(&GENERATED_FILE), - "`regen_lower` writes `{GENERATED_FILE}` but that path is not \ - registered in `REGEN_OUTPUTS` in `src/v3/compiler/build.rs`." - ); - } - - let lower_path = manifest_dir.join(LOWER_RS); - let body = std::fs::read_to_string(&lower_path).unwrap_or_else(|e| { - panic!( - "read canonical lowering source `{}`: {e}", - lower_path.display() - ) - }); - let combined = format!("{HEADER}{body}"); - - let mut child = Command::new("rustfmt") - .arg("--emit") - .arg("stdout") - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn() - .expect("spawn rustfmt"); - child - .stdin - .as_mut() - .unwrap() - .write_all(combined.as_bytes()) - .unwrap(); - let output = child.wait_with_output().expect("rustfmt"); - assert!(output.status.success(), "rustfmt failed"); - let formatted = String::from_utf8(output.stdout).expect("utf8"); - - std::fs::write(&out_path, &formatted).expect("write lower_generated.rs"); - println!("wrote {}", out_path.display()); -} diff --git a/src/v3/compiler/src/lower_generated.rs b/src/v3/compiler/src/lower_generated.rs deleted file mode 100644 index 8a1a3715779..00000000000 --- a/src/v3/compiler/src/lower_generated.rs +++ /dev/null @@ -1,6599 +0,0 @@ -// AUTO-GENERATED from `src/v3/compiler/src/lower.rs` via -// `regen_lower`. Regenerate instead of hand-editing. -// -// SG-3f-prep: not wired into `lib.rs` — canonical implementation is -// `lower.rs` pending `lower.dag` + reflected `Surface*` (SELF_HOSTING.md §4). -// -// Body text (including scaffold / tracked-debt comments) is copied verbatim -// from `lower.rs`; edit `lower.rs` for any change to that prose — not here. - -// Surface → Dag lowering. -// -// Two-pass. Pass 1 walks all top-level items and allocates placeholder -// Declarations for each named type/fn, populating a symbol table -// (name → DeclarationId). Pass 2 fills in each declaration's connective -// and lowers function/let bodies to L1 behaviors, using the symbol table -// to resolve identifier references. See M1_DESIGN.md §8.1. -// -// Computation-side lowering follows M0 semantics unchanged: -// -// SurfaceLiteral::{Int,Bool,String} → Value(LiteralBits::*) -// Var (local) → scope lookup -// Var (unresolved) → placeholder port + ResolveError -// Call → Transform { target: TransformTarget::Callable(DeclarationId), inputs } -// Operator → Transform { target: TransformTarget::Operator(OperatorKind), inputs } -// If → Branch with 2 Paths -// Fn item → Bind with non-empty params + optional Loop wrapper -// Let item → Bind with empty params -// -// `TransformTarget::Callable` points at a DeclarationId for user -// function calls and resolved named declarations. `TransformTarget::Operator` -// carries a structural `OperatorKind` directly — no anonymous stub -// declaration, no name-based inhabitance walk at infer time. - -use std::collections::{HashMap, HashSet}; - -use crate::dag::{ - ArrowBody, AtomPayload, Behavior, BindNode, BranchNode, BranchPattern, CardinalityBound, - Cluster, Dag, Declaration, DeclarationId, Field, IntraClusterCall, LiteralBits, LoopBound, - LoopNode, MemberDescent, NodeId, NonEmptyList, NonSingletonList, Path, PayloadBinding, PortId, - TemplateArgument, TransformNode, TransformTarget, TypeConnective, ValueNode, -}; -use crate::diagnostics::{ - declaration_display_name, witness_correction_for_decl, Diagnostic, SourceSpan, -}; -use crate::infer::{concretize_decl_with_subst, SubstStack}; -use crate::lower_helpers::{expr_span, item_span, pattern_binding_names}; -use crate::operators::{ArithmeticOp, LogicalOp, OperatorKind}; -use crate::parse::{ - SurfaceExpr, SurfaceField, SurfaceItem, SurfaceLiteral, SurfaceModule, SurfaceParam, - SurfacePattern, SurfacePatternField, SurfaceType, SurfaceVariant, VariantPayload, -}; -use crate::types::TypeShape; - -type CallableScope = HashMap; - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -struct RecursiveEdge { - caller: DeclarationId, - callee: DeclarationId, -} - -#[derive(Debug, Clone)] -struct MutualClusterShape { - members: Vec, - member_names: Vec, - per_member_positions: HashMap, - recursive_edges: HashSet, -} - -impl MutualClusterShape { - fn has_recursive_edge(&self, caller: DeclarationId, callee: DeclarationId) -> bool { - self.recursive_edges - .contains(&RecursiveEdge { caller, callee }) - } -} - -#[derive(Debug, Clone)] -struct InvalidMutualCluster { - members: Vec, -} - -#[derive(Debug, Clone, Default)] -struct MutualRecursionPlans { - clusters: Vec, - by_member: HashMap, - invalid_by_member: HashMap, -} - -impl MutualRecursionPlans { - fn cluster_for_member(&self, member: DeclarationId) -> Option<&MutualClusterShape> { - self.by_member - .get(&member) - .and_then(|idx| self.clusters.get(*idx)) - } -} - -#[derive(Debug, Clone)] -struct PendingMutualLoop { - bind_id: Option, - source: PortId, - body_port: PortId, - loop_output: PortId, - body_root: Option, - span: SourceSpan, -} - -#[derive(Debug, Default)] -struct MutualClusterBuildState { - pending_loops: HashMap, - intra_cluster_transform_ids: Vec, -} - -#[derive(Debug, Default)] -struct MutualRecursionState { - clusters: Vec, -} - -impl MutualRecursionState { - fn new(plans: &MutualRecursionPlans) -> Self { - Self { - clusters: (0..plans.clusters.len()) - .map(|_| MutualClusterBuildState::default()) - .collect(), - } - } -} - -fn declaration_name_preference_rank(file: &str) -> usize { - if file.starts_with("src/v3/") { - 2 - } else if file.starts_with("dsl/") { - 0 - } else { - 1 - } -} - -#[derive(Default)] -struct ScopeState { - values: HashMap, - callables: CallableScope, -} - -struct LambdaLoweringContext<'a> { - dag: &'a mut Dag, - scope: &'a HashMap, - callable_scope: &'a CallableScope, - symbols: &'a HashMap, -} - -pub fn lower(module: &SurfaceModule) -> Dag { - let mut dag = Dag::new(); - let user_start = dag.declarations().len(); - lower_into(&mut dag, module); - finalize_strict_user_lower_range(&mut dag, user_start); - dag -} - -/// Strict identifier resolution + unparsed-scaffold rejection for declarations -/// allocated at or after `strict_from` (the user module range after bootstrap). -pub(crate) fn finalize_strict_user_lower_range(dag: &mut Dag, strict_from: usize) { - // User-module sweep: every Identifier stub allocated during - // `lower_into` (id >= strict_from) must resolve, or it becomes a - // fail-closed ResolveError. Stubs in the bootstrap range are - // resolved opportunistically but tolerated — the canonical std/ - // files (`dsl/std/algebra.dag`, etc.) have dangling references to - // types that live in std/ modules outside the M1(2.6) load set - // (e.g., `Tuple`), and those aren't user errors. - resolve_pending_identifiers_strict(dag, strict_from); - // User-mode scaffold rejection: `FnExternalBody` / - // `ArrowBody::Unparsed` and `Data` / `ValueBody::Unparsed` - // are load-bearing scaffolds for the std/bootstrap files - // whose bodies the M1(2.8) parser cannot yet lower - // (match / record literals / lambdas / etc.). - // User-range declarations that rely on the scaffold are - // fail-closed: ordinary user code has no business shipping - // an opaque body the compiler cannot validate. Without this - // sweep, `fn foo(x: Int) -> Int { junk }` would compile - // cleanly and callers would get Resolved types from an - // unvalidated body. - reject_user_unparsed_scaffolds(dag, strict_from); -} - -/// Lower a surface module into an existing Dag as a single-shot call. -/// Used by user-code compilation (`lower()` above) where there's only -/// one module in the pass. Bootstrap calls -/// `collect_symbols_phase` + `lower_bodies_phase` separately so it can -/// run phase 1 over ALL std/ files before phase 2 on any of them, -/// which is required for cross-file forward references (e.g., -/// `bit.dag`'s `Word64 { bytes: List }` references `List` from -/// `types.dag`, which loads later). -pub(crate) fn lower_into(dag: &mut Dag, module: &SurfaceModule) { - let (symbols, is_first) = collect_symbols_phase(dag, &module.items); - lower_bodies_phase(dag, module, &symbols, &is_first); -} - -/// Phase 1 of two-phase lowering. Allocates placeholder declarations -/// and TypeParam children for every top-level named item in `items`, -/// updates the declaration table in place, and returns the symbols -/// map plus the duplicate-detection vector. Safe to call multiple -/// times for different modules — later calls seed their symbols from -/// earlier calls' declarations, so cross-module forward references -/// resolve via `resolve_pending_identifiers` at the end of phase 2. -pub(crate) fn collect_symbols_phase( - dag: &mut Dag, - items: &[SurfaceItem], -) -> (HashMap, Vec) { - collect_symbols(dag, items) -} - -/// Phase 2 of two-phase lowering. Given the already-allocated -/// placeholder declarations + type_params from phase 1, fills in each -/// item's connective and emits any behavior sub-DAGs. Intentionally -/// does NOT run `resolve_pending_identifiers` — bootstrap batches the -/// sweep across all files, and the user-code `lower()` entry point -/// runs the strict sweep itself. -pub(crate) fn lower_bodies_phase( - dag: &mut Dag, - module: &SurfaceModule, - symbols: &HashMap, - is_first: &[bool], -) { - seed_function_signatures_phase(dag, &module.items, symbols, is_first); - let mut scope = ScopeState::default(); - let empty_mutual_recursion = MutualRecursionPlans::default(); - let mut empty_mutual_state = MutualRecursionState::default(); - // DB-10 (3a.2) ordering: dependency-ordered pre-passes so - // references to top-level declarations resolve independent of - // source order. - // - // Pass 1: types (TypeRecord / TypeSum / TypeAlias / TypeAtom) - // — pure type connectives; don't reference values. - // Pass 2: data (SurfaceItem::Data) — depend on types for - // inhabitance checking; populate `value_body` so later - // fn-body `SurfaceExpr::Var` lookups find it. - // Pass 3: everything else in source order (fns, let, modules). - // Types and data are skipped here because they were - // lowered in the pre-passes. - // - // Without pass 2, a fn body referencing a later-declared `data` - // item (e.g. `fn f() -> Int = answer` followed by - // `data answer: Int = 42`) would resolve `answer` to a - // declaration whose `value_body` is still `None` and fall - // through to the unresolved diagnostic — order-dependent name - // resolution. - for (idx, item) in module.items.iter().enumerate() { - if !is_first[idx] { - continue; - } - if matches!( - item, - SurfaceItem::TypeRecord { .. } - | SurfaceItem::TypeSum { .. } - | SurfaceItem::TypeAlias { .. } - | SurfaceItem::TypeAtom { .. } - ) { - scope = lower_item( - item, - dag, - scope, - symbols, - &empty_mutual_recursion, - &mut empty_mutual_state, - ); - } - } - for (idx, item) in module.items.iter().enumerate() { - if !is_first[idx] { - continue; - } - if let SurfaceItem::Data { - name, - ty, - body, - body_span, - .. - } = item - { - lower_data_item(name, ty, body.as_ref(), body_span, dag, symbols); - } - } - // DB-11 (3a.3) phase-ordered refinement lowering. Runs AFTER the - // data pre-pass so predicates referencing top-level `data` - // constants resolve against lowered declarations, not placeholders. - // Sole caller of `lower_parameter_refinement` for parameter - // `where` clauses (single construction authority). - lower_parameter_refinements_phase(dag, module, symbols, is_first); - let mutual_recursion = compute_mutually_recursive(&module.items, dag, symbols, is_first); - let mut mutual_state = MutualRecursionState::new(&mutual_recursion); - for (idx, item) in module.items.iter().enumerate() { - if !is_first[idx] { - // Duplicate declaration — skipped at lower time so the - // first-of-name's filled connective is not overwritten. - // `collect_symbols` already emitted a fail-closed - // diagnostic for the duplicate. - continue; - } - // Types + Data lowered in the pre-passes above; skip here - // so their lowering doesn't run twice. - if matches!( - item, - SurfaceItem::Data { .. } - | SurfaceItem::TypeRecord { .. } - | SurfaceItem::TypeSum { .. } - | SurfaceItem::TypeAlias { .. } - | SurfaceItem::TypeAtom { .. } - ) { - continue; - } - scope = lower_item( - item, - dag, - scope, - symbols, - &mutual_recursion, - &mut mutual_state, - ); - } - finalize_mutual_clusters(dag, &mutual_recursion, &mutual_state); -} - -fn finalize_mutual_clusters( - dag: &mut Dag, - mutual_recursion: &MutualRecursionPlans, - mutual_state: &MutualRecursionState, -) { - for (cluster_index, cluster_plan) in mutual_recursion.clusters.iter().enumerate() { - let Some(build_state) = mutual_state.clusters.get(cluster_index) else { - continue; - }; - if build_state.pending_loops.len() != cluster_plan.members.len() { - fail_mutual_cluster_build( - dag, - build_state, - cluster_plan, - "not every cluster member produced a pending loop witness", - ); - continue; - } - - let members: Vec = cluster_plan - .members - .iter() - .filter_map(|member_decl| { - let pending_loop = build_state.pending_loops.get(member_decl)?; - let bind_id = pending_loop.bind_id?; - let slot = *cluster_plan.per_member_positions.get(member_decl)?; - dag.param_of(bind_id, slot) - .map(|param| MemberDescent { param }) - }) - .collect(); - let intra_cluster_calls: Vec = build_state - .intra_cluster_transform_ids - .iter() - .copied() - .filter_map(|transform_id| { - dag.as_transform_ref(transform_id) - .map(|transform| IntraClusterCall { transform }) - }) - .collect(); - - let Some(member_list) = NonSingletonList::from_vec(members) else { - fail_mutual_cluster_build( - dag, - build_state, - cluster_plan, - "member descent witnesses could not be materialized", - ); - continue; - }; - let Some(call_list) = NonEmptyList::from_vec(intra_cluster_calls) else { - fail_mutual_cluster_build( - dag, - build_state, - cluster_plan, - "lowered bodies did not produce any intra-cluster calls", - ); - continue; - }; - - let cluster_id = dag.push_cluster(Cluster { - members: member_list, - intra_cluster_calls: call_list, - }); - for member_decl in &cluster_plan.members { - let Some(pending_loop) = build_state.pending_loops.get(member_decl) else { - continue; - }; - let loop_id = dag.alloc_node_id(); - dag.set_port_producer(pending_loop.loop_output, Some(loop_id)); - dag.push_node(Behavior::Loop(LoopNode { - id: loop_id, - source: pending_loop.source, - init: pending_loop.source, - body: pending_loop.body_root.unwrap_or(loop_id), - bound: LoopBound::Descent { - cluster: cluster_id, - }, - output: pending_loop.loop_output, - span: pending_loop.span.clone(), - })); - if let Some(bind_id) = pending_loop.bind_id { - dag.patch_bind_value(bind_id, pending_loop.loop_output); - } - } - } -} - -fn fail_mutual_cluster_build( - dag: &mut Dag, - build_state: &MutualClusterBuildState, - cluster_plan: &MutualClusterShape, - detail: &str, -) { - let members = cluster_plan.member_names.join(", "); - let reason = format!("cannot finalize mutual recursion cluster [{members}]: {detail}"); - for pending_loop in build_state.pending_loops.values() { - dag.mark_unresolved( - pending_loop.body_port, - Diagnostic::ResolveError { - name: reason.clone(), - span: pending_loop.span.clone(), - fixes: Vec::new(), - }, - ); - dag.mark_unresolved( - pending_loop.loop_output, - Diagnostic::ResolveError { - name: reason.clone(), - span: pending_loop.span.clone(), - fixes: Vec::new(), - }, - ); - } -} - -fn seed_function_signatures_phase( - dag: &mut Dag, - items: &[SurfaceItem], - symbols: &HashMap, - is_first: &[bool], -) { - for (idx, item) in items.iter().enumerate() { - if !is_first[idx] { - continue; - } - match item { - SurfaceItem::Fn { - name, - params, - return_type, - .. - } => { - seed_function_signature(name, params, return_type, ArrowBody::Pending, dag, symbols) - } - SurfaceItem::FnExternalBody { - name, - params, - return_type, - body_span, - .. - } => seed_function_signature( - name, - params, - return_type, - ArrowBody::Unparsed(body_span.clone()), - dag, - symbols, - ), - _ => {} - } - } -} - -/// DB-11 (3a.3): lower a parameter's `where` refinement into a -/// predicate `Declaration` and a refined type `Declaration`. -/// -/// - `base_decl_id`: the parameter's declared type declaration -/// (e.g. `Int` for `d: Int where d != 0`). -/// - `predicate`: the parsed surface expression after `where`. Lowered -/// in an isolated scope containing only the parameter (bound to a -/// fresh port typed as `base_decl_id`). -/// - Returns a refined type Declaration whose connective is -/// `Atom(ResolvedIdentifier(base_decl_id))` (structurally -/// equivalent to the base type) with `refinement: Some(pred_decl)` -/// pointing at the predicate declaration. -/// -/// The predicate is itself a Declaration with an `Arrow` connective -/// taking the base type and returning `Bool`, with body -/// `UserDefined(bind_id)` pointing at a Bind that owns the predicate -/// sub-DAG. The Bind's `params[0]` is the predicate parameter port — -/// structural equality on predicates treats this slot as "the refined -/// parameter" and pairs it across both sides. -fn lower_parameter_refinement( - base_decl_id: DeclarationId, - predicate: &SurfaceExpr, - param_name: &str, - symbols: &HashMap, - dag: &mut Dag, - param_span: SourceSpan, -) -> DeclarationId { - let pred_span = expr_span(predicate); - let pred_param_port = dag.alloc_port(None); - match declaration_to_port_shape(base_decl_id, dag, ¶m_span) { - Ok(shape) => dag.set_port_type(pred_param_port, shape), - Err(diag) => dag.mark_unresolved(pred_param_port, diag), - } - - let mut pred_scope: HashMap = HashMap::new(); - pred_scope.insert(param_name.to_string(), pred_param_port); - let pred_callable_scope: CallableScope = CallableScope::new(); - let bool_decl_id = symbols - .get("Bool") - .copied() - .unwrap_or_else(|| alloc_identifier_stub(dag, "Bool", &pred_span)); - let pred_value_port = lower_expr( - predicate, - dag, - &pred_scope, - &pred_callable_scope, - symbols, - Some(bool_decl_id), - ); - - let bind_id = dag.alloc_node_id(); - dag.push_node(Behavior::Bind(BindNode { - id: bind_id, - name: format!(""), - value: pred_value_port, - params: vec![pred_param_port], - span: pred_span.clone(), - lane2_workflow: None, - })); - - let pred_decl_id = dag.alloc_declaration_id(); - dag.push_declaration(Declaration { - id: pred_decl_id, - name: None, - connective: TypeConnective::Arrow { - inputs: vec![base_decl_id], - output: bool_decl_id, - body: ArrowBody::UserDefined(bind_id), - }, - type_params: Vec::new(), - meta_tag: None, - specialization_parent: None, - inhabits: None, - value_body: None, - refinement: None, - span: pred_span, - }); - - let refined_id = dag.alloc_declaration_id(); - dag.push_declaration(Declaration { - id: refined_id, - name: None, - connective: TypeConnective::Atom(AtomPayload::ResolvedByStructure(base_decl_id)), - type_params: Vec::new(), - meta_tag: None, - specialization_parent: None, - inhabits: None, - value_body: None, - refinement: Some(pred_decl_id), - span: param_span, - }); - refined_id -} - -fn seed_function_signature( - name: &str, - params: &[SurfaceParam], - return_type: &SurfaceType, - body: ArrowBody, - dag: &mut Dag, - symbols: &HashMap, -) { - let fn_decl_id = symbols[name]; - let local = local_scope_from_parent(dag, fn_decl_id); - // DB-11 (3a.3) phase-ordering fix: seed the Arrow with BASE - // declaration ids only. Parameter `where` clauses are lowered by - // `lower_parameter_refinements_phase`, which runs AFTER the data - // pre-pass populates top-level `data` declarations' connectives - // and `value_body`s. Lowering a predicate here would evaluate any - // references to top-level data constants against placeholder - // declarations — the references would mark `Unresolved` even - // though the constants are valid. - let param_decl_inputs: Vec = params - .iter() - .map(|p| type_to_declaration_id(&p.ty, symbols, &local, dag)) - .collect(); - let return_decl_id = type_to_declaration_id(return_type, symbols, &local, dag); - dag.declaration_mut(fn_decl_id).connective = TypeConnective::Arrow { - inputs: param_decl_inputs, - output: return_decl_id, - body, - }; -} - -/// DB-11 (3a.3): lower parameter `where` clauses for every Fn / -/// FnExternalBody item and update the fn's Arrow inputs with the -/// refined declaration ids. Runs between the data pre-pass and the -/// main fn-body pass so references inside predicates to top-level -/// `data` constants see fully-lowered declarations (not placeholders) -/// and resolve cleanly. -/// -/// Sole caller of `lower_parameter_refinement` for parameter `where` -/// clauses — preserves single construction authority even though -/// the work is split off from seeding. The alternative (lowering -/// refinements at seed time) ordered-broke predicates that referenced -/// top-level data; the alternative (lowering at fn-body time) split -/// the authority across `Fn` (body path) and `FnExternalBody` (which -/// has no body to hook into). One dedicated phase handles both. -fn lower_parameter_refinements_phase( - dag: &mut Dag, - module: &SurfaceModule, - symbols: &HashMap, - is_first: &[bool], -) { - for (idx, item) in module.items.iter().enumerate() { - if !is_first[idx] { - continue; - } - let (name, params) = match item { - SurfaceItem::Fn { name, params, .. } => (name, params), - SurfaceItem::FnExternalBody { name, params, .. } => (name, params), - _ => continue, - }; - let fn_decl_id = symbols[name]; - // Read the seeded Arrow's inputs and output (set by - // `seed_function_signature` to the base declarations). - let (existing_inputs, output, body) = match &dag.declaration(fn_decl_id).connective { - TypeConnective::Arrow { - inputs, - output, - body, - } => (inputs.clone(), *output, body.clone()), - _ => continue, - }; - let mut refined_inputs = Vec::with_capacity(params.len()); - for (param, &base_decl) in params.iter().zip(existing_inputs.iter()) { - let input_decl = match ¶m.refinement { - Some(predicate) => match refinement_predicate_out_of_fragment(predicate) { - Some((shape_label, span)) => { - // DB-11 (3a.3) fail-closed boundary. The - // discharge walker (`predicate_discharges` + - // `refinement_ports_equal`) and the - // composite-narrowing clone - // (`clone_predicate_body`) both model only - // `Value` and `Transform` predicate bodies. - // Admitting a `where` predicate that lowers - // through `Branch` / `Loop` / `Bind` would - // produce a substrate state the consumers - // silently disagree about — discharge would - // reject matching shapes as "not equal," and - // narrowing would discard the composite. - // Reject at the lowering boundary so the - // user gets an honest "unsupported shape" - // diagnostic instead of a misleading - // downstream mismatch. - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "`where` predicate shape not supported in \ - DB-11 3a.3: `{shape_label}` lowers through \ - a Branch/Loop/Bind node, but the discharge \ - walker and composite-narrowing path only \ - support Value and Transform expression \ - bodies. Use a direct comparison or a call \ - to a Bool-returning helper instead." - ), - fixes: Vec::new(), - span, - }, - ); - base_decl - } - None => lower_parameter_refinement( - base_decl, - predicate, - ¶m.name, - symbols, - dag, - param.ty.span().clone(), - ), - }, - None => base_decl, - }; - refined_inputs.push(input_decl); - } - dag.declaration_mut(fn_decl_id).connective = TypeConnective::Arrow { - inputs: refined_inputs, - output, - body, - }; - } -} - -/// DB-11 (3a.3) fragment gate. Walks a `where`-predicate -/// `SurfaceExpr` and returns `Some((shape_label, span))` for the -/// first sub-expression whose lowering would produce a `Branch`, -/// `Loop`, or `Bind` behavior — i.e., an out-of-fragment shape that -/// the discharge walker and composite-narrowing clone path cannot -/// compare or reproduce. -/// -/// Supported shapes return `None`: -/// - `Literal`, `Var`, `Path` — leaf Value / field-project -/// Transforms. -/// - `Call` / `Operator` — Transform nodes; arguments are recursed -/// into. -/// -/// Out-of-fragment shapes: -/// - `If` / `Match` — lower to `Branch`. -/// - `Lambda` — lowers to `Bind`. -/// - `Record` / `List` — lower to fail-closed diagnostics at user -/// scope today; still rejected here so the user sees the refinement -/// boundary clearly rather than the generic lowering failure. -fn refinement_predicate_out_of_fragment(expr: &SurfaceExpr) -> Option<(&'static str, SourceSpan)> { - match expr { - SurfaceExpr::Literal { .. } | SurfaceExpr::Var { .. } | SurfaceExpr::Path { .. } => None, - SurfaceExpr::Call { args, .. } | SurfaceExpr::Operator { args, .. } => { - args.iter().find_map(refinement_predicate_out_of_fragment) - } - SurfaceExpr::Lambda { span, .. } => Some(("lambda", span.clone())), - SurfaceExpr::If { span, .. } => Some(("if", span.clone())), - SurfaceExpr::Match { span, .. } => Some(("match", span.clone())), - SurfaceExpr::VariantRecord { span, .. } => { - Some(("named constructor literal", span.clone())) - } - SurfaceExpr::Record { span, .. } => Some(("record literal", span.clone())), - SurfaceExpr::List { span, .. } => Some(("list literal", span.clone())), - } -} - -/// DB-11 (3a.3) arm-local refinement narrowing. Given an `if` / match -/// guard expression `cond` and the outer scope, if `cond` is a -/// recognizable predicate on a scope-bound name (e.g. `d != 0` where -/// `d` is in scope), returns a new scope where that name is rebound to -/// a freshly-allocated port whose type carries a refinement matching -/// the predicate. The predicate is lowered identically to a parameter -/// refinement, so call-site structural discharge in -/// `infer::decide_transform` treats the two predicates as equal. -/// -/// Returns `None` when the cond doesn't match a narrowing shape, when -/// no scope-bound Var appears in the cond's operand list, or when the -/// outer port's type cannot be recovered. The caller keeps using the -/// unmodified outer scope in that case. -/// -/// Scope of recognition (3a.3 M-sized subset): two-argument -/// `SurfaceExpr::Operator` / `SurfaceExpr::Call` forms where exactly -/// one operand is a `Var` bound in the outer scope. More complex -/// predicate shapes (conjunctions, nested calls on the narrowed -/// parameter, negation-of-predicate on else-arm) fall through with -/// `None` — users either add an explicit guard or live without -/// narrowing. Expanding this recognition is 3a.3-follow-up work. -fn narrow_scope_for_predicate( - cond: &SurfaceExpr, - scope: &HashMap, - symbols: &HashMap, - dag: &mut Dag, -) -> Option> { - let narrow_name = narrowable_var_name(cond, scope)?; - let &outer_port = scope.get(&narrow_name)?; - let outer_shape = match dag.port(outer_port).state() { - crate::dag::PortState::Resolved(ty) => *ty, - _ => return None, - }; - // DB-11 (3a.3) composite-canonical narrowing. Walk past any - // existing refinement to the TRUE BASE; the new refined - // declaration aliases that base directly, not the outer refined - // declaration. No alias chain survives. If the outer port already - // carried a refinement, its predicate body is cloned into the new - // composite Bind with the refined-parameter slot re-pointed at the - // composite's fresh param, then `&&`-joined with the freshly - // lowered cond — the result is one composite predicate Declaration, - // structurally identical to what a user-written - // `where outer_pred && new_cond` would produce. - let (true_base_decl, outer_pred_decl) = walk_to_refinement_base(dag, outer_shape.declaration); - let refined_decl = build_narrowed_refinement( - dag, - symbols, - true_base_decl, - outer_pred_decl, - cond, - &narrow_name, - )?; - let narrow_port = dag.alloc_port(None); - match declaration_to_port_shape(refined_decl, dag, &expr_span(cond)) { - Ok(shape) => dag.set_port_type(narrow_port, shape), - Err(_) => return None, - } - let mut narrowed = scope.clone(); - narrowed.insert(narrow_name, narrow_port); - Some(narrowed) -} - -/// Walk the `Atom(ResolvedIdentifier(...))` chain from a parameter's -/// declaration, skipping past any refinement-carrier declarations to -/// reach the true base declaration. Returns `(base_decl, deepest_refinement)` -/// where `deepest_refinement` is the top-level refinement that was on -/// the input declaration (if any) — the outer predicate that the -/// caller wants to compose with a new narrowing predicate. -/// -/// For a freshly seeded `Int where d != 0` parameter, returns -/// `(Int_decl, Some(d_ne_0_pred_decl))`. For an unrefined `Int` -/// parameter, returns `(Int_decl, None)`. The loop is depth-bounded -/// so pathological cyclic aliases can't hang lowering. -fn walk_to_refinement_base( - dag: &Dag, - decl_id: DeclarationId, -) -> (DeclarationId, Option) { - const DEPTH_LIMIT: usize = 32; - let top_refinement = dag.declaration(decl_id).refinement; - let mut current = decl_id; - for _ in 0..DEPTH_LIMIT { - let decl = dag.declaration(current); - if decl.refinement.is_none() { - return (current, top_refinement); - } - match &decl.connective { - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => { - current = *next; - } - _ => return (current, top_refinement), - } - } - (current, top_refinement) -} - -/// Build a refined parameter declaration for a narrowed scope. The -/// returned declaration aliases `true_base_decl` directly (no alias -/// chain) and carries a single composite predicate: -/// -/// - If `outer_pred_decl` is `None`, the predicate is the freshly -/// lowered `new_cond` — identical shape to what a bare -/// `where new_cond` parameter clause would produce. -/// - If `outer_pred_decl` is `Some`, the predicate body is -/// `outer_body && new_body`, where `outer_body` is cloned from -/// the outer predicate's `Bind` with its param slot re-pointed at -/// the fresh composite param. Structurally identical to -/// `where outer_pred_source && new_cond_source` if the user had -/// written it that way. -fn build_narrowed_refinement( - dag: &mut Dag, - symbols: &HashMap, - true_base_decl: DeclarationId, - outer_pred_decl: Option, - new_cond: &SurfaceExpr, - narrow_name: &str, -) -> Option { - let Some(outer_pred_decl) = outer_pred_decl else { - // No existing refinement — the fresh predicate IS the - // refinement. Same shape as a seeded `where` clause. - return Some(lower_parameter_refinement( - true_base_decl, - new_cond, - narrow_name, - symbols, - dag, - expr_span(new_cond), - )); - }; - - let pred_span = expr_span(new_cond); - // Allocate a fresh composite parameter port typed as the true - // base — the composite predicate's sole parameter slot. - let composite_param_port = dag.alloc_port(None); - match declaration_to_port_shape(true_base_decl, dag, &pred_span) { - Ok(shape) => dag.set_port_type(composite_param_port, shape), - Err(diag) => dag.mark_unresolved(composite_param_port, diag), - } - - // Clone the outer predicate's body into the composite's scope, - // substituting outer's Bind.params[0] → composite_param_port. - // Narrowing operates within a single function's scope; no - // TypeParam substitution applies, so pass an empty SubstStack - // (DB-16: the Transform-target walk becomes a structural no-op). - let (outer_param_port, outer_value_port) = outer_predicate_slots(dag, outer_pred_decl)?; - let outer_cloned_value = clone_predicate_body( - dag, - outer_value_port, - outer_param_port, - composite_param_port, - &SubstStack::new(), - 0, - )?; - - // Lower the new cond in a scope where narrow_name resolves to - // composite_param_port. - let mut pred_scope: HashMap = HashMap::new(); - pred_scope.insert(narrow_name.to_string(), composite_param_port); - let pred_callable_scope: CallableScope = CallableScope::new(); - let bool_decl_id = symbols - .get("Bool") - .copied() - .unwrap_or_else(|| alloc_identifier_stub(dag, "Bool", &pred_span)); - let new_value_port = lower_expr( - new_cond, - dag, - &pred_scope, - &pred_callable_scope, - symbols, - Some(bool_decl_id), - ); - - // `outer_cloned && new` — single Transform(Logical(And)). The - // output port inherits Bool shape via `resolve_operator_arrow` - // during inference. - let and_node_id = dag.alloc_node_id(); - let and_output = dag.alloc_port(Some(and_node_id)); - dag.push_node(Behavior::Transform(TransformNode { - id: and_node_id, - target: TransformTarget::Operator(OperatorKind::Logical(LogicalOp::And)), - inputs: vec![outer_cloned_value, new_value_port], - output: and_output, - span: pred_span.clone(), - })); - - // Wrap in the composite Bind; single params slot, composite body. - let bind_id = dag.alloc_node_id(); - dag.push_node(Behavior::Bind(BindNode { - id: bind_id, - name: format!(""), - value: and_output, - params: vec![composite_param_port], - span: pred_span.clone(), - lane2_workflow: None, - })); - - // Predicate declaration with Arrow body, same shape as - // `lower_parameter_refinement` produces — so discharge logic - // does not need a special case. - let pred_decl_id = dag.alloc_declaration_id(); - dag.push_declaration(Declaration { - id: pred_decl_id, - name: None, - connective: TypeConnective::Arrow { - inputs: vec![true_base_decl], - output: bool_decl_id, - body: ArrowBody::UserDefined(bind_id), - }, - type_params: Vec::new(), - meta_tag: None, - specialization_parent: None, - inhabits: None, - value_body: None, - refinement: None, - span: pred_span.clone(), - }); - - // Refined type declaration aliasing the TRUE BASE (not the - // outer refined decl) — no alias chain. - let refined_id = dag.alloc_declaration_id(); - dag.push_declaration(Declaration { - id: refined_id, - name: None, - connective: TypeConnective::Atom(AtomPayload::ResolvedByStructure(true_base_decl)), - type_params: Vec::new(), - meta_tag: None, - specialization_parent: None, - inhabits: None, - value_body: None, - refinement: Some(pred_decl_id), - span: pred_span, - }); - Some(refined_id) -} - -/// Read a predicate declaration's Bind slots: the sole parameter -/// port (`Bind.params[0]`) and the body value port (`Bind.value`). -/// Returns `None` if the predicate declaration isn't the expected -/// `Arrow { body: UserDefined(bind) }` shape with a single param -/// slot — in which case composite narrowing falls back to treating -/// the outer refinement as absent (safe degradation). -pub(crate) fn outer_predicate_slots( - dag: &Dag, - pred_decl: DeclarationId, -) -> Option<(PortId, PortId)> { - let TypeConnective::Arrow { - body: ArrowBody::UserDefined(bind_id), - .. - } = &dag.declaration(pred_decl).connective - else { - return None; - }; - let Behavior::Bind(bind) = dag.node(*bind_id) else { - return None; - }; - let param = *bind.params.first()?; - Some((param, bind.value)) -} - -/// Clone a predicate body sub-DAG, rewriting every reference to -/// `substitute_from` into `substitute_to`, and routing each -/// `Transform` target's declaration-level references through -/// `concretize_decl_with_subst` so generic-helper calls and -/// generic-record projections land on concretized declarations. -/// Fresh node ids + port ids are allocated for each cloned node so -/// the result is structurally independent from the source — two -/// composite Binds in the same DAG do not share intermediate ports. -/// -/// Walks only `Value` and `Transform` nodes, matching the predicate -/// fragment that `predicates_structurally_equal` already accepts. -/// `Branch` / `Loop` / `Bind` in a predicate body are out of scope -/// for DB-11's admitted fragment — clone returns `None` and callers -/// treat that as "cannot produce a well-formed composite." -/// -/// DB-16 (3a.3 closure): the `subst` parameter carries the active -/// substitution stack so Transform targets with `Callable(id)` or -/// `FieldProject.field_child` referencing template TypeParams get -/// re-rooted to their concrete instantiations. DB-11 callers -/// (`build_narrowed_refinement`) pass an empty `SubstStack`, which -/// makes the Transform-target walk a structural no-op (no -/// substitution lookup ever fires). -pub(crate) fn clone_predicate_body( - dag: &mut Dag, - source_port: PortId, - substitute_from: PortId, - substitute_to: PortId, - subst: &SubstStack, - depth: usize, -) -> Option { - const DEPTH_LIMIT: usize = 64; - if depth >= DEPTH_LIMIT { - return None; - } - if source_port == substitute_from { - return Some(substitute_to); - } - let Some(node_id) = dag.port(source_port).produced_by else { - // No producer — external port (e.g. another function's - // param). Reuse directly; no rewrite needed. - return Some(source_port); - }; - let cloned = match dag.node(node_id).clone() { - Behavior::Value(v) => { - let new_node_id = dag.alloc_node_id(); - let new_output = dag.alloc_port(Some(new_node_id)); - dag.push_node(Behavior::Value(ValueNode { - id: new_node_id, - data: v.data, - output: new_output, - span: v.span, - lane2_workflow: v.lane2_workflow.clone(), - })); - new_output - } - Behavior::Transform(t) => { - let mut new_inputs = Vec::with_capacity(t.inputs.len()); - for input_port in &t.inputs { - new_inputs.push(clone_predicate_body( - dag, - *input_port, - substitute_from, - substitute_to, - subst, - depth + 1, - )?); - } - let new_target = substitute_transform_target(dag, t.target, subst); - let new_node_id = dag.alloc_node_id(); - let new_output = dag.alloc_port(Some(new_node_id)); - dag.push_node(Behavior::Transform(TransformNode { - id: new_node_id, - target: new_target, - inputs: new_inputs, - output: new_output, - span: t.span, - })); - new_output - } - Behavior::Branch(_) | Behavior::Loop(_) | Behavior::Bind(_) => { - // Out-of-fragment predicate shapes — tracked as DB-11 - // 3a.3 ROADMAP debt (admitted surface > supported - // fragment). Fail-closed at narrowing time rather than - // producing a malformed composite. - return None; - } - }; - Some(cloned) -} - -/// DB-16 (3a.3 closure): re-root a `TransformTarget`'s declaration- -/// level references through the active substitution stack so cloned -/// predicate bodies that reference generic helpers or generic-record -/// projections land on concretized declarations. -/// -/// - `Operator(_)`: no substitution. Operator kinds are intrinsic; -/// `resolve_operator_arrow` handles TypeParam operands through its -/// own substitution pipeline at inference time. -/// - `FieldProject { field_child, .. }`: route `field_child` through -/// `concretize_decl_with_subst`. When the parent record is generic -/// (e.g., `Box` scoped under `fn f(x: Box where ...)`), -/// substitution re-roots `field_child` to the concrete -/// instantiation (e.g., `Box`). -/// - `Callable(id)`: route `id` through `concretize_decl_with_subst`. -/// When `id` is an `Instantiation` whose arguments reference -/// substitution-bound TypeParams, the result is a fresh -/// Instantiation with concretized arguments, deduped via -/// `find_equivalent_anonymous_instantiation`. -/// -/// With an empty `SubstStack`, every call to -/// `concretize_decl_with_subst` short-circuits to the input id (no -/// TypeParam lookup ever hits) — DB-11 callers see no behavior change. -fn substitute_transform_target( - dag: &mut Dag, - target: TransformTarget, - subst: &SubstStack, -) -> TransformTarget { - match target { - TransformTarget::Operator(_) => target, - TransformTarget::FieldProject { - field_label, - field_child, - } => TransformTarget::FieldProject { - field_label, - field_child: field_child.map(|id| concretize_decl_with_subst(dag, id, subst, 0)), - }, - TransformTarget::Callable(id) => { - TransformTarget::Callable(concretize_decl_with_subst(dag, id, subst, 0)) - } - } -} - -/// Return the name of the single scope-bound `Var` referenced in a -/// predicate-shaped cond expression, or `None` if the cond doesn't -/// match the narrowing-eligible shape. -/// -/// A cond is narrowing-eligible iff: -/// - it is a two-argument `Operator` or `Call` expression, AND -/// - it references **exactly one** scope-bound name (the candidate -/// refined parameter). Top-level symbols and literals are fine. -/// -/// The single-scope-bound-var requirement is load-bearing: a predicate -/// that references two scope-locals (e.g. `x != y`) would need a -/// multi-arity refinement, which DB-11's structural-equality proof -/// theory does not model. Inside a match-arm body where a pattern -/// binding (e.g. `Cons(payload)`) is in scope, predicates like -/// `behavior_id(payload.head) == node_id` have two scope-bound vars -/// (`payload` and `node_id`) and skip narrowing — otherwise the -/// predicate-lowering side effects would attempt to resolve the -/// non-candidate vars in an isolated scope and fail, polluting the -/// main DAG with unresolvable sub-expressions. -fn narrowable_var_name(cond: &SurfaceExpr, scope: &HashMap) -> Option { - match cond { - SurfaceExpr::Operator { args, .. } if args.len() == 2 => {} - SurfaceExpr::Call { args, .. } if args.len() == 2 => {} - _ => return None, - } - let mut seen: std::collections::HashSet = std::collections::HashSet::new(); - collect_scope_bound_free_vars(cond, scope, &mut seen); - if seen.len() == 1 { - seen.into_iter().next() - } else { - None - } -} - -/// Walk a surface expression and collect the names of all free -/// variables that resolve to entries in `scope`. Lambda parameter -/// shadowing and match-arm binding shadowing are respected. Top-level -/// declarations (not in `scope`) are filtered out by construction -/// since `scope` holds only local-variable bindings, not symbols. -fn collect_scope_bound_free_vars( - expr: &SurfaceExpr, - scope: &HashMap, - out: &mut std::collections::HashSet, -) { - match expr { - SurfaceExpr::Var { name, .. } => { - if scope.contains_key(name) { - out.insert(name.clone()); - } - } - SurfaceExpr::Literal { .. } => {} - SurfaceExpr::Path { segments, .. } => { - // The head of a dotted path is a local-variable reference - // when it resolves to scope (field access). A top-level - // typed path (e.g. `OrderedRing.add`) wouldn't be in - // `scope` so it's filtered out. - if let Some(head) = segments.first() { - if scope.contains_key(head) { - out.insert(head.clone()); - } - } - } - SurfaceExpr::Call { args, .. } | SurfaceExpr::Operator { args, .. } => { - for arg in args { - collect_scope_bound_free_vars(arg, scope, out); - } - } - SurfaceExpr::VariantRecord { fields, .. } => { - for field in fields { - collect_scope_bound_free_vars(&field.value, scope, out); - } - } - SurfaceExpr::Lambda { params, body, .. } => { - let mut shadowed = scope.clone(); - for p in params { - shadowed.remove(p); - } - // Use a placeholder port so `contains_key` would return - // true only for non-shadowed names; we don't use it. - collect_scope_bound_free_vars(body, &shadowed, out); - } - SurfaceExpr::If { - cond, - then_branch, - else_branch, - .. - } => { - collect_scope_bound_free_vars(cond, scope, out); - collect_scope_bound_free_vars(then_branch, scope, out); - collect_scope_bound_free_vars(else_branch, scope, out); - } - SurfaceExpr::Match { - scrutinee, arms, .. - } => { - collect_scope_bound_free_vars(scrutinee, scope, out); - for arm in arms { - let mut arm_scope = scope.clone(); - for binding in pattern_binding_names(&arm.pattern) { - arm_scope.remove(&binding); - } - collect_scope_bound_free_vars(&arm.body, &arm_scope, out); - } - } - SurfaceExpr::Record { fields, .. } => { - for field in fields { - collect_scope_bound_free_vars(&field.value, scope, out); - } - } - SurfaceExpr::List { elements, .. } => { - for el in elements { - collect_scope_bound_free_vars(el, scope, out); - } - } - } -} - -/// Pass 1: allocate a placeholder Declaration for every named top-level -/// item and record `name → DeclarationId` in the symbol table. Let / -/// Module / Import items produce no declaration. -/// -/// Returns `(symbols, is_first)`. `symbols` maps each name to the -/// declaration currently selected by the temporary duplicate-authority -/// preference scaffold shared with `Dag::declaration_by_name`. -/// `is_first[idx]` is false only for items that lose selection to an -/// already-present declaration of the same name. A later higher-rank -/// `src/v3/` declaration can replace a seeded lower-rank duplicate and -/// remain `true`. `lower_into` skips only the losing duplicates so the -/// selected declaration's filled connective is not overwritten later. -/// -/// Duplicate declarations emit a fail-closed `ResolveError` via a -/// phantom port, so the compile surfaces through -/// `Err(CompileError::Semantic)`. The duplicate's own declaration slot -/// is still allocated (for structural consistency in the declaration -/// table), but it stays as a placeholder and is unreachable by name. -fn collect_symbols( - dag: &mut Dag, - items: &[SurfaceItem], -) -> (HashMap, Vec) { - // Seed from already-present declarations with the same temporary - // duplicate-authority preference scaffold as - // `Dag::declaration_by_name`: `src/v3/` declarations outrank - // legacy `dsl/` duplicates. Equal-rank ties remain deterministic - // by insertion order but are not the intended steady-state rule. - // - // **🟡 Scaffold — v3 migration preference mirror.** This mirrors the - // rank policy defined at `Dag::declaration_name_preference_rank`. Kept - // as a scaffold (not deleted) because the duplicates it resolves carry - // v3-only substrate content that cannot yet be hosted under `dsl/std/`. - // Dissolution trigger: when duplicate authorities converge to a single - // canonical home, delete this seeding branch together with the rank - // function in `dag.rs` and remove the preference rule entirely. At that - // point name lookup must resolve against the single surviving authority - // or fail closed on multiple matches. Convergence checklist - // (ROADMAP.md "Post-merge debt"): `module std.effects`, - // `module std.verification`, and the embedded `http_path` mirror inside - // `src/v3/std/effects.dag`. - let mut symbols: HashMap = HashMap::new(); - for d in dag.declarations() { - if let Some(name) = &d.name { - match symbols.get(name).copied() { - None => { - symbols.insert(name.clone(), d.id); - } - Some(existing_id) => { - let existing = dag.declaration(existing_id); - let new_rank = declaration_name_preference_rank(&d.span.file); - let existing_rank = declaration_name_preference_rank(&existing.span.file); - if new_rank > existing_rank { - symbols.insert(name.clone(), d.id); - } - } - } - } - } - - let mut is_first = vec![true; items.len()]; - for (idx, item) in items.iter().enumerate() { - // Extract the surface-level name and type parameter list. - // Items that don't allocate declarations: - // - Let bindings (they produce a BindNode at lower time, not - // a Declaration). - // - Module / Import items (parsed facts preserved but have no - // lowered declaration at M1(2.7)). - // Every other surface form allocates one top-level declaration - // with the declared name, including `FnExternalBody` and `Data` - // scaffold forms — their facts flow forward. - let (name, surface_type_params): (String, &[String]) = match item { - SurfaceItem::Let { .. } => continue, - SurfaceItem::Module { .. } => continue, - SurfaceItem::Import { .. } => continue, - SurfaceItem::Fn { - name, type_params, .. - } => (name.clone(), type_params.as_slice()), - SurfaceItem::FnExternalBody { - name, type_params, .. - } => (name.clone(), type_params.as_slice()), - SurfaceItem::Data { name, .. } => (name.clone(), &[]), - SurfaceItem::TypeAtom { - name, type_params, .. - } - | SurfaceItem::TypeRecord { - name, type_params, .. - } - | SurfaceItem::TypeSum { - name, type_params, .. - } - | SurfaceItem::TypeAlias { - name, type_params, .. - } => (name.clone(), type_params.as_slice()), - }; - let span = item_span(item); - let id = dag.alloc_declaration_id(); - dag.push_declaration(Declaration { - id, - name: Some(name.clone()), - connective: placeholder_connective(&name), - type_params: Vec::new(), - meta_tag: None, - specialization_parent: None, - inhabits: None, - value_body: None, - refinement: None, - span: span.clone(), - }); - - // Allocate TypeParam declarations for this item's generic - // parameters and link them via `Declaration.type_params`. - // Doing this in collect_symbols (pass 1) instead of - // lower_type_* (pass 2) means that by the time ANY other - // module's body references `List`, List's - // `type_params` slot is already populated — so - // `build_template_arguments` can always look up the real - // template parameter id at construction time. No half-valid - // state, no post-sweep fixup pass. - if !surface_type_params.is_empty() { - let mut param_ids = Vec::with_capacity(surface_type_params.len()); - for param in surface_type_params { - let param_id = dag.alloc_declaration_id(); - dag.push_declaration(Declaration { - id: param_id, - name: None, - connective: TypeConnective::Atom(AtomPayload::TypeParam(param.clone())), - type_params: Vec::new(), - meta_tag: None, - specialization_parent: None, - inhabits: None, - - value_body: None, - refinement: None, - span: span.clone(), - }); - param_ids.push(param_id); - } - dag.declaration_mut(id).type_params = param_ids; - } - - if let Some(&existing_id) = symbols.get(&name) { - let existing = dag.declaration(existing_id); - let new_rank = declaration_name_preference_rank(&span.file); - let existing_rank = declaration_name_preference_rank(&existing.span.file); - if new_rank > existing_rank { - symbols.insert(name, id); - } else { - is_first[idx] = false; - let existing_span = existing.span.clone(); - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "duplicate declaration `{name}` (first declared in `{}` at bytes {}..{})", - existing_span.file, - existing_span.byte_start, - existing_span.byte_end, - ), - fixes: Vec::new(), - span, - }, - ); - } - } else { - symbols.insert(name, id); - } - } - (symbols, is_first) -} - -fn placeholder_connective(name: &str) -> TypeConnective { - TypeConnective::Atom(AtomPayload::UnresolvedIdentifier(name.to_string())) -} - -fn lower_item( - item: &SurfaceItem, - dag: &mut Dag, - scope: ScopeState, - symbols: &HashMap, - mutual_recursion: &MutualRecursionPlans, - mutual_state: &mut MutualRecursionState, -) -> ScopeState { - let mut scope = scope; - match item { - SurfaceItem::Let { - name, - type_ann, - expr, - } => { - let mut lambda_callable: Option = None; - let value_port = if let SurfaceExpr::Lambda { params, body, span } = expr { - let Some(ty) = type_ann else { - let port = dag.alloc_port(None); - dag.mark_unresolved( - port, - Diagnostic::ResolveError { - name: "lambda expressions currently require an expected function type (for example a `let` annotation or a function-typed parameter position)".to_string(), - span: span.clone(), - fixes: Vec::new(), - }, - ); - scope.values.insert(name.clone(), port); - return scope; - }; - let decl_id = type_to_declaration_id(ty, symbols, &HashMap::new(), dag); - let mut lambda_ctx = LambdaLoweringContext { - dag, - scope: &scope.values, - callable_scope: &scope.callables, - symbols, - }; - match lower_lambda_expr(params, body, span, decl_id, &mut lambda_ctx) { - Ok(lambda_decl_id) => { - lambda_callable = Some(lambda_decl_id); - let port = lambda_ctx.dag.alloc_port(None); - match declaration_to_port_shape(decl_id, lambda_ctx.dag, ty.span()) { - Ok(expected) => lambda_ctx.dag.set_port_type(port, expected), - Err(diag) => lambda_ctx.dag.mark_unresolved(port, diag), - } - port - } - Err(diag) => { - let port = lambda_ctx.dag.alloc_port(None); - lambda_ctx.dag.mark_unresolved(port, diag); - port - } - } - } else { - let expected_decl = type_ann - .as_ref() - .map(|ty| type_to_declaration_id(ty, symbols, &HashMap::new(), dag)); - lower_expr( - expr, - dag, - &scope.values, - &scope.callables, - symbols, - expected_decl, - ) - }; - if let Some(ty) = type_ann { - // Compute the annotated type's declaration id ONCE. - // Port TypeShape wraps the same id — no second - // allocation, no lower↔infer identity split. - let local: HashMap = HashMap::new(); - let decl_id = type_to_declaration_id(ty, symbols, &local, dag); - match declaration_to_port_shape(decl_id, dag, ty.span()) { - Ok(declared) => dag.set_port_type(value_port, declared), - Err(diag) => dag.mark_unresolved(value_port, diag), - } - } - let bind_id = dag.alloc_node_id(); - let bind_span = match type_ann { - Some(ty) => ty.span().clone(), - None => expr_span(expr), - }; - dag.push_node(Behavior::Bind(BindNode { - id: bind_id, - name: name.clone(), - value: value_port, - params: Vec::new(), - span: bind_span, - lane2_workflow: None, - })); - scope.values.insert(name.clone(), value_port); - if let Some(lambda_decl_id) = lambda_callable { - scope.callables.insert(name.clone(), lambda_decl_id); - } - scope - } - SurfaceItem::Fn { - name, - params, - return_type, - body, - span: _, - .. - } => { - scope.values = lower_fn_item_expr_body( - name, - params, - return_type, - body, - dag, - scope.values, - symbols, - mutual_recursion, - mutual_state, - ); - scope - } - SurfaceItem::FnExternalBody { .. } => { - // DB-11 (3a.3): signature was fully built by - // `seed_function_signatures_phase` with - // `ArrowBody::Unparsed(body_span)`. Nothing remains to do - // at body-lowering time — the declaration is already in - // its final form. - scope - } - SurfaceItem::Data { - name, - ty, - body, - body_span, - span: _, - } => { - lower_data_item(name, ty, body.as_ref(), body_span, dag, symbols); - scope - } - SurfaceItem::Module { .. } | SurfaceItem::Import { .. } => { - // No-op at M1(2.7). The parsed facts are preserved in the - // SurfaceItem for M2 module scoping to consume; lowering - // doesn't currently need them because name resolution - // spans the flat declaration table. - scope - } - SurfaceItem::TypeAtom { name, .. } => { - let decl_id = symbols[name]; - // Empty product — equivalent to a unit type. - dag.declaration_mut(decl_id).connective = TypeConnective::Conj { - children: Vec::new(), - }; - scope - } - SurfaceItem::TypeRecord { - name, - type_params, - fields, - .. - } => { - lower_type_record(dag, symbols, name, type_params, fields); - scope - } - // E-6: `SurfaceItem::TypeSum.inhabits` is not parse-only — `lower_type_sum` - // writes `Declaration.inhabits`, then `infer::resolve_operator_arrow` and - // emitters' `walk_to_algebra_conj` follow it (Lane 1e-2b / BooleanAlgebra). - SurfaceItem::TypeSum { - name, - type_params, - variants, - inhabits, - .. - } => { - lower_type_sum(dag, symbols, name, type_params, variants, inhabits.as_ref()); - scope - } - SurfaceItem::TypeAlias { - name, - type_params, - target, - .. - } => { - lower_type_alias(dag, symbols, name, type_params, target); - scope - } - } -} - -/// Read a parent declaration's pre-populated `type_params` slot and -/// build a `name → DeclarationId` local scope map for field-type and -/// variant-payload lookups. The actual TypeParam declarations were -/// allocated up front in `collect_symbols` so every declaration -/// reaches `lower_type_*` with its `type_params` slot already filled. -/// This is the shape that lets `build_template_arguments` look up real -/// parameter ids at construction time — no half-valid state, no -/// post-sweep fixup pass. -fn local_scope_from_parent(dag: &Dag, parent_id: DeclarationId) -> HashMap { - let parent = dag.declaration(parent_id); - let mut scope: HashMap = parent - .type_params - .iter() - .filter_map(|pid| { - if let TypeConnective::Atom(AtomPayload::TypeParam(name)) = - &dag.declaration(*pid).connective - { - Some((name.clone(), *pid)) - } else { - None - } - }) - .collect(); - // Recursive declarations must be able to refer to themselves - // while their connective is still being lowered. Without this, - // `tail: List` inside `type List = ...` allocates an - // UnresolvedIdentifier stub that later resolves back to the - // declaration and trips the "resolves to itself" fail-closed - // guard intended for bad aliases, not real recursive sums. - if let Some(name) = &parent.name { - scope.insert(name.clone(), parent_id); - } - scope -} - -fn lower_type_record( - dag: &mut Dag, - symbols: &HashMap, - name: &str, - _type_params: &[String], - fields: &[SurfaceField], -) { - let decl_id = symbols[name]; - let local = local_scope_from_parent(dag, decl_id); - let mut children: Vec = Vec::with_capacity(fields.len()); - for field in fields { - let ty_id = type_to_declaration_id(&field.ty, symbols, &local, dag); - children.push(Field { - label: field.name.clone(), - ty: ty_id, - }); - } - dag.declaration_mut(decl_id).connective = TypeConnective::Conj { children }; -} - -fn lower_type_sum( - dag: &mut Dag, - symbols: &HashMap, - name: &str, - _type_params: &[String], - variants: &[SurfaceVariant], - inhabits: Option<&SurfaceType>, -) { - let decl_id = symbols[name]; - let local = local_scope_from_parent(dag, decl_id); - let mut variant_fields: Vec = Vec::with_capacity(variants.len()); - for variant in variants { - // Build payload children FIRST, then allocate the variant declaration. - // Allocating the variant id before its payload children would wedge - // the dense-sequential invariant on `Dag.declarations` because the - // child declarations push into slots between the variant's reserved - // id and its eventual push. - let connective = match &variant.payload { - // Unit variants surface as `Positional(vec![])` — the - // empty-positional case handles them with zero Field - // children, which is structurally indistinguishable - // from a Unit variant and avoids a dedicated enum arm. - VariantPayload::Positional(payload_types) => { - let children: Vec = payload_types - .iter() - .enumerate() - .map(|(idx, ty)| Field { - label: format!("_{idx}"), - ty: type_to_declaration_id(ty, symbols, &local, dag), - }) - .collect(); - TypeConnective::Conj { children } - } - VariantPayload::Record(fields) => { - let children: Vec = fields - .iter() - .map(|f| Field { - label: f.name.clone(), - ty: type_to_declaration_id(&f.ty, symbols, &local, dag), - }) - .collect(); - TypeConnective::Conj { children } - } - }; - // Sum variant declarations are anonymous. The variant name - // lives structurally in the parent `Disj.variants` Field label, - // not in the child declaration's name slot. Keeps variant names - // (`True`, `False`, `Less`, `Equal`, `Greater`) out of - // `Dag::declaration_by_name`'s flat scan. - let variant_id = dag.alloc_declaration_id(); - dag.push_declaration(Declaration { - id: variant_id, - name: None, - connective, - type_params: Vec::new(), - meta_tag: None, - specialization_parent: None, - inhabits: None, - - value_body: None, - refinement: None, - span: variant.span.clone(), - }); - variant_fields.push(Field { - label: variant.name.clone(), - ty: variant_id, - }); - } - dag.declaration_mut(decl_id).connective = TypeConnective::Disj { - variants: variant_fields, - }; - if let Some(inh_ty) = inhabits { - let inh_id = type_to_declaration_id(inh_ty, symbols, &local, dag); - dag.declaration_mut(decl_id).inhabits = Some(inh_id); - } -} - -fn lower_type_alias( - dag: &mut Dag, - symbols: &HashMap, - name: &str, - _type_params: &[String], - target: &SurfaceType, -) { - let decl_id = symbols[name]; - let local = local_scope_from_parent(dag, decl_id); - let connective = type_to_connective(target, symbols, &local, dag); - dag.declaration_mut(decl_id).connective = connective; -} - -/// Lower a `SurfaceType` to a fresh DeclarationId. Used for field types, -/// Arrow parameters, and template arguments. Allocates anonymous (unnamed) -/// declarations for composite shapes; looks up named references against -/// `local` (type params in scope) first, then `symbols` (top-level names). -/// Unknown names get an Identifier Atom stub with `resolved: None` — pass -/// 2 and inference are responsible for filling it in or reporting. -fn type_to_declaration_id( - ty: &SurfaceType, - symbols: &HashMap, - local: &HashMap, - dag: &mut Dag, -) -> DeclarationId { - match ty { - SurfaceType::Named { name, .. } => { - if let Some(id) = local.get(name) { - return *id; - } - if let Some(id) = symbols.get(name) { - return *id; - } - alloc_identifier_stub(dag, name, ty.span()) - } - SurfaceType::Parameterized { name, args, span } => { - let template_id = local - .get(name) - .or_else(|| symbols.get(name)) - .copied() - .unwrap_or_else(|| alloc_identifier_stub(dag, name, span)); - let arguments = - build_template_arguments(dag, symbols, local, template_id, name, args, span); - let id = dag.alloc_declaration_id(); - dag.push_declaration(Declaration { - id, - name: None, - connective: TypeConnective::Instantiation { - template: template_id, - arguments, - }, - type_params: Vec::new(), - meta_tag: None, - specialization_parent: None, - inhabits: None, - - value_body: None, - refinement: None, - span: span.clone(), - }); - id - } - SurfaceType::Optional { inner, span } => { - let element = type_to_declaration_id(inner, symbols, local, dag); - let id = dag.alloc_declaration_id(); - dag.push_declaration(Declaration { - id, - name: None, - connective: TypeConnective::Cardinality { - element, - bound: CardinalityBound::AtMostOne, - }, - type_params: Vec::new(), - meta_tag: None, - specialization_parent: None, - inhabits: None, - - value_body: None, - refinement: None, - span: span.clone(), - }); - id - } - SurfaceType::Arrow { - inputs, - output, - span, - } => { - let input_ids: Vec = inputs - .iter() - .map(|i| type_to_declaration_id(i, symbols, local, dag)) - .collect(); - let output_id = type_to_declaration_id(output, symbols, local, dag); - let id = dag.alloc_declaration_id(); - dag.push_declaration(Declaration { - id, - name: None, - connective: TypeConnective::Arrow { - inputs: input_ids, - output: output_id, - // Anonymous nested Arrow inside a larger type expression - // (e.g. `cb: fn(Int) -> Int` as a parameter type, or - // `handler: fn(Int) -> Int` as an algebra/record field). - // Function-as-data: no executable body, by construction. - // `NoBody` makes that fact structural so any future named - // refactor of these declarations does not silently - // become an `Arrow(Pending)` lens target. - body: ArrowBody::NoBody, - }, - type_params: Vec::new(), - meta_tag: None, - specialization_parent: None, - inhabits: None, - - value_body: None, - refinement: None, - span: span.clone(), - }); - id - } - } -} - -/// Lower a `SurfaceType` directly to a connective (not a new declaration). -/// Used for `TypeAlias` targets where we want the alias declaration itself -/// to carry the aliased shape, not a one-level-indirect wrapper. -fn type_to_connective( - ty: &SurfaceType, - symbols: &HashMap, - local: &HashMap, - dag: &mut Dag, -) -> TypeConnective { - match ty { - SurfaceType::Named { name, .. } => { - let template = local - .get(name) - .or_else(|| symbols.get(name)) - .copied() - .unwrap_or_else(|| alloc_identifier_stub(dag, name, ty.span())); - TypeConnective::Instantiation { - template, - arguments: Vec::new(), - } - } - SurfaceType::Parameterized { name, args, span } => { - let template = local - .get(name) - .or_else(|| symbols.get(name)) - .copied() - .unwrap_or_else(|| alloc_identifier_stub(dag, name, span)); - let arguments = - build_template_arguments(dag, symbols, local, template, name, args, span); - TypeConnective::Instantiation { - template, - arguments, - } - } - SurfaceType::Optional { inner, .. } => TypeConnective::Cardinality { - element: type_to_declaration_id(inner, symbols, local, dag), - bound: CardinalityBound::AtMostOne, - }, - SurfaceType::Arrow { inputs, output, .. } => TypeConnective::Arrow { - inputs: inputs - .iter() - .map(|i| type_to_declaration_id(i, symbols, local, dag)) - .collect(), - output: type_to_declaration_id(output, symbols, local, dag), - // `type_to_connective` is invoked from two callers: - // `lower_type_alias` (`type Callback = fn(Int) -> Int`) and - // `lower_data_item` (`data x: fn(Int) -> Int = ...`). In - // both contexts an Arrow shape here is a TYPE-LEVEL function - // type with no executable body, so `NoBody` is correct in - // both. `Pending` would false-positive on - // `lens_structural_resolution` for the named-declaration - // outputs of either caller. - body: ArrowBody::NoBody, - }, - } -} - -/// Build the `TemplateArgument` list for an `Instantiation`. -/// -/// Two-phase bootstrap means every real declaration's `type_params` -/// slot is populated by the time lowering runs, so real templates -/// always produce well-formed `TemplateArgument`s whose `parameter` -/// edge points at a genuine TypeParam atom — honoring the field's -/// contract in `dag::TemplateArgument`. -/// -/// **Stub templates** — anonymous `UnresolvedIdentifier` stubs -/// created when a name can't be resolved at lower time — produce an -/// **empty argument list**. The stub itself already has a diagnostic -/// waiting for it (bootstrap mode tolerates bootstrap-range dangling -/// refs like `Tuple` in `algebra.dag`; user-code mode fails closed -/// via the strict sweep), so there is no additional consumer that -/// needs a per-argument `TemplateArgument` when the template is -/// unresolved. The old "self-reference tolerated scaffold" path is -/// deleted per QW4 — `TemplateArgument.parameter` is no longer -/// representable as a non-TypeParam reference at construction -/// time. -fn build_template_arguments( - dag: &mut Dag, - symbols: &HashMap, - local: &HashMap, - template: DeclarationId, - template_name: &str, - args: &[SurfaceType], - span: &SourceSpan, -) -> Vec { - let template_decl = dag.declaration(template); - let template_is_stub = template_decl.name.is_none() - && matches!( - &template_decl.connective, - TypeConnective::Atom(AtomPayload::UnresolvedIdentifier(_)) - ); - if template_is_stub { - // Consume the argument surface types for their side effects - // (nested stub allocation, diagnostic attachment), but do not - // construct any TemplateArgument values — the template's - // pending error is the authoritative failure for this - // instantiation, and a TemplateArgument whose parameter - // wasn't a TypeParam would violate the field contract. - for arg in args { - let _ = type_to_declaration_id(arg, symbols, local, dag); - } - return Vec::new(); - } - let template_param_count = dag.declaration(template).type_params.len(); - if template_param_count != args.len() { - report_declaration_error( - dag, - Diagnostic::ArityMismatch { - function: format!("type `{template_name}`"), - expected: template_param_count, - actual: args.len(), - span: span.clone(), - fixes: Vec::new(), - }, - ); - // Arity mismatch is an authoritative failure. Consume the - // argument surface types for their side effects and return - // nothing — building partial TemplateArguments would require - // inventing parameter references that don't exist, which - // would violate the field contract. - for arg in args { - let _ = type_to_declaration_id(arg, symbols, local, dag); - } - return Vec::new(); - } - args.iter() - .enumerate() - .map(|(idx, arg)| { - let value = type_to_declaration_id(arg, symbols, local, dag); - let parameter = template_param_id(dag, template, idx).expect( - "template_param_count equality was checked immediately above — \ - param lookup at idx < count must succeed", - ); - TemplateArgument { parameter, value } - }) - .collect() -} - -/// Allocate a stub Identifier-atom declaration. Callers reach this path when -/// name resolution fails against the local type-parameter scope and the -/// current top-level symbol table snapshot. The stub deliberately carries -/// `resolved: None` and stays in the declaration graph so that a later -/// `resolve_pending_identifiers` sweep can either fill it in (forward -/// references across bootstrap fixtures) or emit a fail-closed diagnostic. -fn alloc_identifier_stub(dag: &mut Dag, name: &str, span: &SourceSpan) -> DeclarationId { - let id = dag.alloc_declaration_id(); - dag.push_declaration(Declaration { - id, - name: None, - connective: TypeConnective::Atom(AtomPayload::UnresolvedIdentifier(name.to_string())), - type_params: Vec::new(), - meta_tag: None, - specialization_parent: None, - inhabits: None, - - value_body: None, - refinement: None, - span: span.clone(), - }); - id -} - -/// Emit a declaration-level diagnostic via `Dag::attach_diagnostic`. -/// Thin wrapper kept for naming symmetry with the per-item fail-closed -/// call sites in lowering; bootstrap and tests use -/// `Dag::attach_diagnostic` directly. -fn report_declaration_error(dag: &mut Dag, diag: Diagnostic) { - dag.attach_diagnostic(diag); -} - -/// Final-pass resolution over anonymous Identifier-atom declarations in -/// **bootstrap** mode. Resolves every stub whose name is in the -/// declaration table; **tolerates** stubs whose name is dangling. -/// Tolerance is bootstrap-specific: the canonical `dsl/std/*.dag` -/// files carry forward references to types that live in std/ modules -/// outside the M1(2.6) load set (e.g., `Tuple` referenced from -/// `algebra.dag` with no defining `.dag` file among the seven loaded), -/// and those are not bootstrap errors. User-facing code uses the -/// strict variant below. -pub(crate) fn resolve_pending_identifiers(dag: &mut Dag) { - run_identifier_sweep(dag, /*strict_from=*/ usize::MAX); -} - -/// Final-pass resolution in **strict** mode: every Identifier stub at -/// a declaration id `>= strict_from` that cannot resolve emits a -/// fail-closed `ResolveError`. Used by `lower` for user modules, where -/// `strict_from` is the declaration count before user lowering began. -/// Stubs in the bootstrap range (id < strict_from) are still resolved -/// opportunistically but tolerated. -pub(crate) fn resolve_pending_identifiers_strict(dag: &mut Dag, strict_from: usize) { - run_identifier_sweep(dag, strict_from); -} - -/// User-mode scaffold rejection. `FnExternalBody` lowers to -/// `ArrowBody::Unparsed(SourceSpan)`; `Data` lowers to a declaration -/// with `value_body = Some(ValueBody::Unparsed(SourceSpan))`. Both -/// are load-bearing scaffolds for std/bootstrap files whose bodies -/// the M1(2.8) parser cannot yet lower. For ordinary user code they -/// are a fail-closed concern: an opaque body that the compiler has -/// not validated shipping through `compile_to_dag` violates the -/// fail-closed static-grounding invariant. -/// -/// This sweep walks declarations at id `>= strict_from` (i.e. the -/// user-lowered range) and attaches a fail-closed diagnostic for -/// each `Unparsed` scaffold found. Bootstrap-range declarations -/// (id < strict_from) are tolerated — those scaffolds exist by -/// design until the parser grows to cover the remaining std/ -/// grammar (match / record literals / lambdas / pipes / data body -/// literals — see `DOWNSTREAM_REQUIREMENTS.md` class-5 gap list). -/// -/// Diagnostics are attached via `report_declaration_error` -/// (`Dag::attach_diagnostic`), which routes through the phantom-port -/// channel so they surface via `compile_to_dag`'s standard -/// `CompileError::Semantic` path. -fn reject_user_unparsed_scaffolds(dag: &mut Dag, strict_from: usize) { - // Snapshot the offending (id, span) pairs first so we can emit - // diagnostics without holding a long immutable borrow of the - // declaration slice. - let mut unparsed_fn_scaffolds: Vec<(DeclarationId, String, SourceSpan)> = Vec::new(); - let mut unparsed_data_scaffolds: Vec<(DeclarationId, String, SourceSpan)> = Vec::new(); - for decl in dag.declarations() { - if (decl.id.raw() as usize) < strict_from { - continue; - } - let name = decl - .name - .clone() - .unwrap_or_else(|| format!("declaration#{}", decl.id.raw())); - if let TypeConnective::Arrow { - body: ArrowBody::Unparsed(span), - .. - } = &decl.connective - { - unparsed_fn_scaffolds.push((decl.id, name.clone(), span.clone())); - } - if let Some(crate::dag::ValueBody::Unparsed(span)) = &decl.value_body { - unparsed_data_scaffolds.push((decl.id, name, span.clone())); - } - } - for (_, name, span) in unparsed_fn_scaffolds { - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "function `{name}` has an opaque block body — M1(2.8) user code cannot yet use match / record literals / lambdas inside block-bodied fn definitions (see DOWNSTREAM_REQUIREMENTS.md class-5 gaps)" - ), - fixes: Vec::new(), - span, - }, - ); - } - for (_, name, span) in unparsed_data_scaffolds { - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "data `{name}` has an opaque body — M1(2.8) user code cannot yet use record / list / map literals inside data bodies (see DOWNSTREAM_REQUIREMENTS.md class-5 gap #3)" - ), - fixes: Vec::new(), - span, - }, - ); - } -} - -fn run_identifier_sweep(dag: &mut Dag, strict_from: usize) { - let snapshot: Vec<(DeclarationId, String, SourceSpan)> = dag - .declarations() - .iter() - .filter_map(|d| match &d.connective { - TypeConnective::Atom(AtomPayload::UnresolvedIdentifier(name)) => { - Some((d.id, name.clone(), d.span.clone())) - } - _ => None, - }) - .collect(); - - for (decl_id, name, span) in snapshot { - // Operator identifiers (`+`, `-`, ...) no longer flow through - // this sweep — they are committed to - // `TransformTarget::Operator(OperatorKind)` at parse time and - // never allocate a stub declaration. Any `UnresolvedIdentifier` - // atom that reaches this point is a real name-reference stub - // waiting for lower-time or sweep-time resolution. - if let Some(target) = dag.declaration_by_name(&name).map(|d| d.id) { - if target == decl_id { - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!("type `{name}` resolves to itself"), - fixes: Vec::new(), - span, - }, - ); - continue; - } - // Structural phase transition: rewrite the connective from - // `UnresolvedIdentifier(name)` to `ResolvedByName(target)`. - // The phase is now visible in the variant, not hidden in - // an Option field. - dag.declaration_mut(decl_id).connective = - TypeConnective::Atom(AtomPayload::ResolvedByName(target)); - } else if (decl_id.raw() as usize) >= strict_from { - // Strict mode for user-lowering stubs: any - // `UnresolvedIdentifier` that survives the sweep is a - // fail-closed ResolveError. After this loop every - // user-range Identifier atom is either a - // ResolvedIdentifier or has attached a diagnostic. - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!("unresolved type identifier `{name}`"), - fixes: Vec::new(), - span, - }, - ); - } - // else: bootstrap-range dangling ref, tolerated. The canonical - // std/ files may forward-reference types that live in std/ - // modules outside the M1(2.6) load set; those are not errors. - } -} - -/// Return the `idx`-th type parameter declaration of a template. Reads the -/// canonical `Declaration.type_params` slot rather than filtering -/// `Conj.children` — type params are first-class on `Declaration` per -/// M1(2.5)'s refactor. Returns None when the template has fewer params than -/// `idx` requires; callers must fail-closed (not substitute a fallback). -fn template_param_id(dag: &Dag, template: DeclarationId, idx: usize) -> Option { - dag.declaration(template).type_params.get(idx).copied() -} - -// DB-11 (3a.3) single-construction-authority cleanup: -// `lower_fn_item_unparsed` used to run a full parameter-decl walk -// and an Arrow overwrite here — duplicating the work that -// `seed_function_signatures_phase` already did for every -// `SurfaceItem::FnExternalBody`. When a parameter carried a `where` -// refinement, the duplication pushed a second predicate Bind and a -// second refined Declaration into the DAG and then overwrote the -// Arrow's `inputs` to point at the duplicates, leaving the seeded -// predicate Bind and refined Declaration orphaned. Seeding already -// produces the final Arrow shape (`ArrowBody::Unparsed(body_span)`), -// so the function was redundant and is now deleted; the caller in -// the `SurfaceItem::FnExternalBody` arm has been simplified to a -// no-op since the signature flows directly from seeding. - -/// Lower a `SurfaceItem::Data` — a typed constant whose body is not -/// yet parseable as a `SurfaceExpr`. The declaration carries two -/// facts: -/// -/// - `connective` = the resolved type annotation (via -/// `type_to_connective`). The declaration's type is accessible -/// through the same path as type aliases. -/// - `value_body = Some(ValueBody::Unparsed(body_span))`. This -/// makes the declaration structurally distinguishable from a -/// type alias (which has `value_body = None`) so consumers can -/// tell "data value with this type" from "type alias of this -/// type" by reading the substrate directly. -/// -/// **QW2 + structural scaffold honesty** (M1(2.7) review round 9): -/// before this fix, lowering dropped the body entirely and set -/// the connective to the type. The declaration was structurally -/// identical to a type alias — the substrate admitted a state -/// where "data vs type" had no distinction. Now the fact survives -/// as an Unparsed scaffold with an explicit dissolution trigger -/// (M2+ record/map/list literal parser extension), and the -/// declaration carries both the declared type and the body span -/// for future parser passes to consume. -fn lower_data_item( - name: &str, - ty: &SurfaceType, - body: Option<&SurfaceExpr>, - body_span: &SourceSpan, - dag: &mut Dag, - symbols: &HashMap, -) { - let decl_id = symbols[name]; - let local: HashMap = HashMap::new(); - // Compute the declaration id of the type annotation (e.g. - // Realization's DeclarationId for `data rust_int: Realization`) - // — the walk starts here. - let ty_decl_id = type_to_declaration_id(ty, symbols, &local, dag); - let connective = type_to_connective(ty, symbols, &local, dag); - // meta_tag edge from the data item to its type annotation. This - // is what makes a data declaration structurally distinct from a - // type alias (R9/R10 + PR-B): the type is readable from the - // same field shape as Realization instances in rust.dag, and - // downstream consumers (emit_rust) can filter by meta_tag to - // find all declarations of a given meta-type. - dag.declaration_mut(decl_id).meta_tag = Some(ty_decl_id); - dag.declaration_mut(decl_id).connective = connective; - // Attempt structural inhabitance checking if the body parsed - // as a record literal. Falls back to Unparsed on any failure - // (walk doesn't terminate at a Conj, missing field, extra - // field, value isn't a literal, type mismatch). Fail-closed - // paths attach a diagnostic via `report_declaration_error` so - // user-facing code sees the error. - // DB-10 (3a.2): `data x: T = v` bodies lower by shape: - // - Record literal → ValueBody::Structural (existing path). - // - Scalar literal → ValueBody::Scalar (new path, DB-10). - // - Anything else → ValueBody::Unparsed fallback. - let value_body = match body { - Some(SurfaceExpr::Record { fields, .. }) => { - lower_record_to_structural(name, fields, ty_decl_id, body_span, symbols, dag) - } - Some(lit_expr @ SurfaceExpr::Literal { .. }) => { - lower_scalar_literal_for_type(lit_expr, ty_decl_id, dag) - .map(crate::dag::ValueBody::Scalar) - .or_else(|| { - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "data `{name}`'s scalar body does not match declared type", - ), - span: body_span.clone(), - fixes: Vec::new(), - }, - ); - None - }) - } - _ => None, - }; - let final_body = - value_body.unwrap_or_else(|| crate::dag::ValueBody::Unparsed(body_span.clone())); - dag.declaration_mut(decl_id).value_body = Some(final_body); -} - -/// Walk a declaration through `Instantiation` / `ResolvedIdentifier` -/// edges until it reaches a declaration whose connective is -/// `Conj`. Returns the `DeclarationId` of that Conj declaration — or -/// `None` if no Conj is reachable within the walk depth limit, or -/// the chain terminates at some other connective. -/// -/// Used by `lower_data_item` for data body inhabitance checking. -/// Mirrors the `walk_to_disj_decl` helper in `infer.rs`: the two -/// walks look for complementary terminal shapes (Conj for data -/// item inhabitance vs Disj for Branch scrutinees), but follow the -/// same alias/instantiation chains otherwise. -fn walk_to_conj_decl(dag: &Dag, start: DeclarationId) -> Option { - let mut current = start; - for _ in 0..32 { - let decl = dag.declaration(current); - match &decl.connective { - TypeConnective::Conj { .. } => return Some(current), - TypeConnective::Instantiation { template, .. } => { - current = *template; - } - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => { - current = *next; - } - _ => return None, - } - } - None -} - -fn walk_to_disj_decl(dag: &Dag, start: DeclarationId) -> Option { - let mut current = start; - for _ in 0..32 { - let decl = dag.declaration(current); - match &decl.connective { - TypeConnective::Disj { .. } => return Some(current), - TypeConnective::Instantiation { template, .. } => { - current = *template; - } - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => { - current = *next; - } - _ => return None, - } - } - None -} - -#[derive(Clone, Default)] -struct LowerSubstStack { - frames: Vec>, -} - -impl LowerSubstStack { - fn push(&mut self, frame: Vec) { - self.frames.push(frame); - } - - fn lookup(&self, parameter: DeclarationId) -> Option { - self.frames - .iter() - .rev() - .find_map(|frame| frame.iter().find(|arg| arg.parameter == parameter)) - .map(|arg| arg.value) - } -} - -fn find_equivalent_decl_instantiation_lower( - dag: &Dag, - template: DeclarationId, - arguments: &[TemplateArgument], -) -> Option { - dag.declarations().iter().find_map(|decl| { - let TypeConnective::Instantiation { - template: existing_template, - arguments: existing_arguments, - } = &decl.connective - else { - return None; - }; - (template == *existing_template - && existing_arguments.len() == arguments.len() - && existing_arguments - .iter() - .zip(arguments.iter()) - .all(|(lhs, rhs)| lhs.parameter == rhs.parameter && lhs.value == rhs.value)) - .then_some(decl.id) - }) -} - -fn resolve_decl_with_subst_lower( - dag: &Dag, - current: DeclarationId, - subst: &LowerSubstStack, - depth: usize, -) -> Option { - if depth >= 32 { - return None; - } - match &dag.declaration(current).connective { - TypeConnective::Atom(AtomPayload::TypeParam(_)) => subst - .lookup(current) - .and_then(|bound| resolve_decl_with_subst_lower(dag, bound, subst, depth + 1)) - .or(Some(current)), - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => { - resolve_decl_with_subst_lower(dag, *next, subst, depth + 1) - } - TypeConnective::Instantiation { - template, - arguments, - } => { - let specialized_arguments: Vec = arguments - .iter() - .map(|arg| { - Some(TemplateArgument { - parameter: arg.parameter, - value: resolve_decl_with_subst_lower(dag, arg.value, subst, depth + 1)?, - }) - }) - .collect::>()?; - if specialized_arguments - .iter() - .zip(arguments.iter()) - .all(|(lhs, rhs)| lhs.parameter == rhs.parameter && lhs.value == rhs.value) - { - return Some(current); - } - find_equivalent_decl_instantiation_lower(dag, *template, &specialized_arguments) - .or(Some(current)) - } - TypeConnective::Cardinality { .. } - | TypeConnective::Arrow { .. } - | TypeConnective::Conj { .. } - | TypeConnective::Disj { .. } - | TypeConnective::Atom(AtomPayload::UnresolvedIdentifier(_)) - | TypeConnective::Atom(AtomPayload::Literal(_)) => Some(current), - } -} - -fn walk_to_conj_decl_with_subst_lower( - dag: &Dag, - start: DeclarationId, - subst: &mut LowerSubstStack, -) -> Option { - let mut current = start; - for _ in 0..32 { - match &dag.declaration(current).connective { - TypeConnective::Conj { .. } => return Some(current), - TypeConnective::Instantiation { - template, - arguments, - } => { - subst.push(arguments.clone()); - current = *template; - } - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => current = *next, - TypeConnective::Atom(AtomPayload::TypeParam(_)) => current = subst.lookup(current)?, - _ => return None, - } - } - None -} - -fn walk_to_type_shape_lower( - dag: &Dag, - current: DeclarationId, - subst: &LowerSubstStack, - depth: usize, -) -> Option { - if depth >= 32 { - return None; - } - let decl = dag.declaration(current); - if decl.name.is_some() { - return Some(TypeShape::new(current)); - } - match &decl.connective { - TypeConnective::Atom(AtomPayload::TypeParam(_)) => { - if let Some(bound) = subst.lookup(current) { - walk_to_type_shape_lower(dag, bound, subst, depth + 1) - } else { - Some(TypeShape::new(current)) - } - } - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => { - walk_to_type_shape_lower(dag, *next, subst, depth + 1) - } - TypeConnective::Instantiation { .. } => { - resolve_decl_with_subst_lower(dag, current, subst, depth + 1) - .map(TypeShape::new) - .or_else(|| Some(TypeShape::new(current))) - } - TypeConnective::Atom(AtomPayload::UnresolvedIdentifier(_)) - | TypeConnective::Atom(AtomPayload::Literal(_)) - | TypeConnective::Conj { .. } - | TypeConnective::Disj { .. } - | TypeConnective::Arrow { .. } - | TypeConnective::Cardinality { .. } => None, - } -} - -fn resolve_static_field_project( - dag: &Dag, - input_port: PortId, - field_label: &str, -) -> Option<(DeclarationId, TypeShape)> { - let input_ty = match dag.port(input_port).state() { - crate::dag::PortState::Resolved(ty) => *ty, - crate::dag::PortState::Uninferred | crate::dag::PortState::Unresolved => return None, - }; - let mut subst = LowerSubstStack::default(); - let conj_id = walk_to_conj_decl_with_subst_lower(dag, input_ty.declaration, &mut subst)?; - let children = match &dag.declaration(conj_id).connective { - TypeConnective::Conj { children } => children, - _ => return None, - }; - let field_decl = children - .iter() - .find(|field| field.label == field_label) - .map(|field| field.ty)?; - let output_ty = walk_to_type_shape_lower(dag, field_decl, &subst, 0)?; - Some((field_decl, output_ty)) -} - -/// Attempt to lower a parsed record literal body into a -/// `ValueBody::Structural { fields }` by matching each record field -/// against the declared type's Conj children. Returns `None` (and -/// attaches one or more diagnostics) if any check fails; the caller -/// falls back to `ValueBody::Unparsed`. -/// -/// Validation rules: -/// 1. The type annotation's declaration must walk to a `Conj` -/// declaration via `walk_to_conj_decl`. Failure → diagnostic -/// "data item's type has no record shape" → return None. -/// 2. The record literal's field labels must exactly match the -/// Conj's children — no extras, no missing. -/// 3. Each field's value must be a `SurfaceExpr::Literal` -/// (scalar literal). Nested records, variable references, and -/// computed expressions are class-5 gap #3 follow-ups. -/// 4. Each literal's type must match the corresponding Conj -/// field's declared type (validated via the primitive cache). -fn lower_record_to_structural( - data_name: &str, - record_fields: &[crate::parse::SurfaceRecordField], - ty_decl_id: DeclarationId, - body_span: &SourceSpan, - symbols: &HashMap, - dag: &mut Dag, -) -> Option { - let Some(conj_id) = walk_to_conj_decl(dag, ty_decl_id) else { - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "data `{data_name}`'s type annotation does not resolve to a record type (Conj); cannot apply inhabitance checking to the body" - ), - span: body_span.clone(), - fixes: Vec::new(), - }, - ); - return None; - }; - // Snapshot the Conj's children to avoid borrowing conflicts - // while we walk the record literal. - let type_fields: Vec<(String, DeclarationId)> = match &dag.declaration(conj_id).connective { - TypeConnective::Conj { children } => { - children.iter().map(|f| (f.label.clone(), f.ty)).collect() - } - _ => unreachable!("walk_to_conj_decl returned a non-Conj declaration"), - }; - // Check no extra fields in the data body. - for record_field in record_fields { - if !type_fields - .iter() - .any(|(label, _)| *label == record_field.name) - { - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "data `{data_name}` has field `{}` but the type has no such field", - record_field.name - ), - span: record_field.span.clone(), - fixes: Vec::new(), - }, - ); - return None; - } - } - // Check no missing fields. - for (type_label, _) in &type_fields { - if !record_fields.iter().any(|f| f.name == *type_label) { - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!("data `{data_name}` is missing required field `{type_label}`"), - span: body_span.clone(), - fixes: Vec::new(), - }, - ); - return None; - } - } - // Determine which realization category (if any) this data - // item belongs to. Used below to narrow the acceptable shape - // of `target` / `op` field values per category. The category - // is identified by name lookup (Phase 2 doesn't have the - // realization meta cache populated yet — that runs at - // bootstrap end). Same lookup as for primitives above. - let category = realization_category_for_meta(dag, ty_decl_id); - - let mut structural_fields: Vec<(String, crate::dag::FieldValue)> = - Vec::with_capacity(type_fields.len()); - for (type_label, type_field_id) in &type_fields { - let record_field = record_fields - .iter() - .find(|f| f.name == *type_label) - .expect("checked above"); - let field_value = lower_structural_field_value( - data_name, - type_label, - &record_field.value, - *type_field_id, - symbols, - dag, - category, - &record_field.span, - )?; - structural_fields.push((type_label.clone(), field_value)); - } - Some(crate::dag::ValueBody::Structural { - fields: structural_fields, - }) -} - -#[allow(clippy::too_many_arguments)] -fn lower_structural_field_value( - data_name: &str, - field_label: &str, - expr: &SurfaceExpr, - expected_type: DeclarationId, - symbols: &HashMap, - dag: &mut Dag, - category: Option, - span: &SourceSpan, -) -> Option { - if let Some(marker_id) = dag.declaration_by_name("DeclarationRef").map(|d| d.id) { - if walks_to(dag, expected_type, marker_id) { - let decl_id = resolve_field_value_as_declaration_ref(expr, symbols, dag)?; - if let Some(cat) = category { - if let Err(reason) = - validate_realization_field_target(dag, cat, field_label, decl_id) - { - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "data `{data_name}` field `{field_label}` does not satisfy the {cat:?} realization constraint: {reason}" - ), - span: span.clone(), - fixes: Vec::new(), - }, - ); - return None; - } - } - return Some(crate::dag::FieldValue::Reference(decl_id)); - } - } - - if let Some(decl_id) = resolve_field_value_as_declaration_ref(expr, symbols, dag) { - let referenced = dag.declaration(decl_id); - if referenced - .meta_tag - .is_some_and(|actual| declaration_ref_types_equivalent(dag, actual, expected_type, 0)) - { - return Some(crate::dag::FieldValue::Reference(decl_id)); - } - } - - if let Some(literal_bits) = lower_scalar_literal_for_type(expr, expected_type, dag) { - return Some(crate::dag::FieldValue::Literal(literal_bits)); - } - - if let Some(element_type) = list_element_type(dag, expected_type) { - let SurfaceExpr::List { elements, .. } = expr else { - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "data `{data_name}` field `{field_label}` must be a list literal matching the declared List<_> type" - ), - span: span.clone(), - fixes: Vec::new(), - }, - ); - return None; - }; - let mut lowered = Vec::with_capacity(elements.len()); - for element in elements { - lowered.push(lower_structural_field_value( - data_name, - field_label, - element, - element_type, - symbols, - dag, - None, - &expr_span(element), - )?); - } - return Some(crate::dag::FieldValue::List(lowered)); - } - - if let Some(conj_id) = walk_to_conj_decl(dag, expected_type) { - let SurfaceExpr::Record { fields, .. } = expr else { - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "data `{data_name}` field `{field_label}` must be a record literal matching the declared record type" - ), - span: span.clone(), - fixes: Vec::new(), - }, - ); - return None; - }; - let expected_fields: Vec<(String, DeclarationId)> = - match &dag.declaration(conj_id).connective { - TypeConnective::Conj { children } => children - .iter() - .map(|child| (child.label.clone(), child.ty)) - .collect(), - _ => unreachable!("walk_to_conj_decl returned non-Conj"), - }; - for field in fields { - if !expected_fields - .iter() - .any(|(label, _)| *label == field.name) - { - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "data `{data_name}` field `{field_label}` has nested field `{}` but the declared type has no such field", - field.name - ), - span: field.span.clone(), - fixes: Vec::new(), - }, - ); - return None; - } - } - for (label, _) in &expected_fields { - if !fields.iter().any(|field| field.name == *label) { - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "data `{data_name}` field `{field_label}` is missing nested field `{label}`" - ), - span: span.clone(), - fixes: Vec::new(), - }, - ); - return None; - } - } - let mut lowered = Vec::with_capacity(expected_fields.len()); - for (label, ty) in expected_fields { - let nested = fields - .iter() - .find(|field| field.name == label) - .expect("checked above"); - lowered.push(( - label.clone(), - lower_structural_field_value( - data_name, - &label, - &nested.value, - ty, - symbols, - dag, - None, - &nested.span, - )?, - )); - } - return Some(crate::dag::FieldValue::Record(lowered)); - } - - if let Some(disj_id) = walk_to_disj_decl(dag, expected_type) { - let variants: Vec<(String, DeclarationId)> = match &dag.declaration(disj_id).connective { - TypeConnective::Disj { variants } => variants - .iter() - .map(|field| (field.label.clone(), field.ty)) - .collect(), - _ => unreachable!("walk_to_disj_decl returned non-Disj"), - }; - let (variant_name, variant_decl_id, positional_args, named_fields, variant_span) = - match expr { - SurfaceExpr::Call { target, args, span } => { - let Some((_, variant_decl_id)) = - variants.iter().find(|(label, _)| label == target) - else { - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "data `{data_name}` field `{field_label}` uses constructor `{target}` which is not a variant of the declared sum type" - ), - span: span.clone(), - fixes: Vec::new(), - }, - ); - return None; - }; - ( - target.as_str(), - *variant_decl_id, - Some(args.as_slice()), - None, - span, - ) - } - SurfaceExpr::VariantRecord { - target, - fields, - span, - } => { - let Some((_, variant_decl_id)) = - variants.iter().find(|(label, _)| label == target) - else { - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "data `{data_name}` field `{field_label}` uses constructor `{target}` which is not a variant of the declared sum type" - ), - span: span.clone(), - fixes: Vec::new(), - }, - ); - return None; - }; - ( - target.as_str(), - *variant_decl_id, - None, - Some(fields.as_slice()), - span, - ) - } - SurfaceExpr::Var { name, span } => { - let Some((_, variant_decl_id)) = - variants.iter().find(|(label, _)| label == name) - else { - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "data `{data_name}` field `{field_label}` uses constructor `{name}` which is not a variant of the declared sum type" - ), - span: span.clone(), - fixes: Vec::new(), - }, - ); - return None; - }; - (name.as_str(), *variant_decl_id, Some(&[][..]), None, span) - } - _ => { - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "data `{data_name}` field `{field_label}` must be a constructor expression matching the declared sum type" - ), - span: span.clone(), - fixes: Vec::new(), - }, - ); - return None; - } - }; - let payload_fields = match variant_payload_fields_for_lowering(dag, variant_decl_id) { - Some(fields) => fields, - other => { - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "data `{data_name}` field `{field_label}` constructor `{variant_name}` does not lower to a record payload shape: got {other:?}" - ), - span: variant_span.clone(), - fixes: Vec::new(), - }, - ); - return None; - } - }; - let mut payload = Vec::with_capacity(payload_fields.len()); - if let Some(args) = positional_args { - if payload_fields.len() != args.len() { - report_declaration_error( - dag, - Diagnostic::ArityMismatch { - function: variant_name.to_string(), - expected: payload_fields.len(), - actual: args.len(), - span: variant_span.clone(), - fixes: Vec::new(), - }, - ); - return None; - } - for (arg, (_, payload_field_ty)) in args.iter().zip(payload_fields.iter()) { - payload.push(lower_structural_field_value( - data_name, - field_label, - arg, - *payload_field_ty, - symbols, - dag, - None, - &expr_span(arg), - )?); - } - } else if let Some(fields) = named_fields { - for field in fields { - if !payload_fields.iter().any(|(label, _)| label == &field.name) { - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "data `{data_name}` field `{field_label}` constructor `{variant_name}` has payload field `{}` but the variant has no such field", - field.name - ), - span: field.span.clone(), - fixes: Vec::new(), - }, - ); - return None; - } - } - for (payload_field_label, payload_field_ty) in &payload_fields { - let Some(field) = fields - .iter() - .find(|field| field.name == *payload_field_label) - else { - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "data `{data_name}` field `{field_label}` constructor `{variant_name}` is missing payload field `{payload_field_label}`" - ), - span: variant_span.clone(), - fixes: Vec::new(), - }, - ); - return None; - }; - payload.push(lower_structural_field_value( - data_name, - field_label, - &field.value, - *payload_field_ty, - symbols, - dag, - None, - &field.span, - )?); - } - } - return Some(crate::dag::FieldValue::Variant { - constructor: variant_decl_id, - payload, - }); - } - - report_declaration_error( - dag, - Diagnostic::ResolveError { - name: format!( - "data `{data_name}` field `{field_label}` does not match the declared structural type" - ), - span: span.clone(), - fixes: Vec::new(), - }, - ); - None -} - -fn lower_scalar_literal_for_type( - expr: &SurfaceExpr, - expected_type: DeclarationId, - dag: &Dag, -) -> Option { - let SurfaceExpr::Literal { value, .. } = expr else { - return None; - }; - let literal_bits = match value { - SurfaceLiteral::Int(v) => LiteralBits::Int(*v), - SurfaceLiteral::Bool(v) => LiteralBits::Bool(*v), - SurfaceLiteral::String(v) => LiteralBits::String(v.clone()), - }; - let int_decl_id = dag.declaration_by_name("Int").map(|d| d.id); - let bool_decl_id = dag.declaration_by_name("Bool").map(|d| d.id); - let string_decl_id = dag.declaration_by_name("String").map(|d| d.id); - let type_ok = match &literal_bits { - LiteralBits::Int(_) => int_decl_id - .map(|id| walks_to(dag, expected_type, id)) - .unwrap_or(false), - LiteralBits::Bool(_) => bool_decl_id - .map(|id| walks_to(dag, expected_type, id)) - .unwrap_or(false), - LiteralBits::String(_) => string_decl_id - .map(|id| walks_to(dag, expected_type, id)) - .unwrap_or(false), - }; - type_ok.then_some(literal_bits) -} - -fn list_element_type(dag: &Dag, expected_type: DeclarationId) -> Option { - let list_id = dag.declaration_by_name("List")?.id; - match &dag.declaration(expected_type).connective { - TypeConnective::Instantiation { - template, - arguments, - } if *template == list_id && arguments.len() == 1 => Some(arguments[0].value), - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => list_element_type(dag, *next), - _ => None, - } -} - -/// Resolve a record-literal field value as a typed declaration -/// reference. Accepts: -/// -/// - `SurfaceExpr::Var { name }` — top-level identifier looked up -/// in the symbols map. -/// - `SurfaceExpr::Path { segments }` — dotted-path. The first -/// segment is a top-level identifier; subsequent segments walk -/// the parent's `Conj` children by field label or `Disj` -/// variants by variant label, returning the child/variant's -/// `ty` (the field's type declaration / the variant's payload -/// declaration). -/// -/// Walking Disj variants is the path that lets pattern-role -/// realization specs reference a specific sum variant (e.g. -/// `List.Empty`) without the emitter needing a label-matching -/// heuristic — the authority for "which variant plays the empty -/// role" lives in the spec data item, not in emitter code. -/// -/// Returns `None` if the expression is not a recognized reference -/// shape or any segment fails to resolve. -fn resolve_field_value_as_declaration_ref( - expr: &SurfaceExpr, - symbols: &HashMap, - dag: &Dag, -) -> Option { - let segments: Vec<&str> = match expr { - SurfaceExpr::Var { name, .. } => vec![name.as_str()], - SurfaceExpr::Path { segments, .. } => segments.iter().map(|s| s.as_str()).collect(), - _ => return None, - }; - let (first, rest) = segments.split_first()?; - let mut current = symbols.get(*first).copied()?; - for segment in rest { - if let Some(conj_id) = walk_to_conj_decl(dag, current) { - let TypeConnective::Conj { children } = &dag.declaration(conj_id).connective else { - return None; - }; - let next = children.iter().find(|f| f.label == *segment)?; - current = next.ty; - continue; - } - if let Some(disj_id) = walk_to_disj_decl(dag, current) { - let TypeConnective::Disj { variants } = &dag.declaration(disj_id).connective else { - return None; - }; - let next = variants.iter().find(|v| v.label == *segment)?; - current = next.ty; - continue; - } - return None; - } - Some(current) -} - -fn declaration_ref_types_equivalent( - dag: &Dag, - lhs: DeclarationId, - rhs: DeclarationId, - depth: usize, -) -> bool { - if lhs == rhs { - return true; - } - if depth >= 32 { - return false; - } - let lhs_decl = dag.declaration(lhs); - let rhs_decl = dag.declaration(rhs); - match (&lhs_decl.connective, &rhs_decl.connective) { - ( - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)), - _, - ) => declaration_ref_types_equivalent(dag, *next, rhs, depth + 1), - ( - _, - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)), - ) => declaration_ref_types_equivalent(dag, lhs, *next, depth + 1), - ( - TypeConnective::Instantiation { - template, - arguments, - }, - _, - ) if arguments.is_empty() => { - declaration_ref_types_equivalent(dag, *template, rhs, depth + 1) - } - ( - _, - TypeConnective::Instantiation { - template, - arguments, - }, - ) if arguments.is_empty() => { - declaration_ref_types_equivalent(dag, lhs, *template, depth + 1) - } - ( - TypeConnective::Instantiation { - template: lhs_template, - arguments: lhs_arguments, - }, - TypeConnective::Instantiation { - template: rhs_template, - arguments: rhs_arguments, - }, - ) => { - declaration_ref_types_equivalent(dag, *lhs_template, *rhs_template, depth + 1) - && lhs_arguments.len() == rhs_arguments.len() - && lhs_arguments - .iter() - .zip(rhs_arguments.iter()) - .all(|(lhs_arg, rhs_arg)| { - lhs_arg.parameter == rhs_arg.parameter - && declaration_ref_types_equivalent( - dag, - lhs_arg.value, - rhs_arg.value, - depth + 1, - ) - }) - } - _ => false, - } -} - -/// Realization category tag for the lower-time narrowing check. -/// Mirrors `emit_rust::RealizationCategory` but lives in lower's -/// own namespace because the lowerer can't depend on emit_rust. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum RealizationCategoryTag { - Type, - Operator, - Behavior, - Callable, - TypeInstantiation, - Pattern, -} - -/// Identify which realization category (if any) a data item's -/// meta-type belongs to. Returns `None` for non-realization data -/// items (which skip the narrowing check entirely). -/// -/// Looks up the meta-type by name. The realization meta cache on -/// `Dag` isn't populated until `populate_primitive_cache` runs at -/// bootstrap end, but `collect_symbols_phase` has registered every -/// top-level declaration by the time `lower_record_to_structural` -/// runs in Phase 2, so `declaration_by_name` works here. Same -/// pattern as the primitive lookups above. -fn realization_category_for_meta( - dag: &Dag, - meta_decl_id: DeclarationId, -) -> Option { - let type_meta = dag.declaration_by_name("TypeRealization").map(|d| d.id); - let op_meta = dag.declaration_by_name("OperatorRealization").map(|d| d.id); - let behavior_meta = dag.declaration_by_name("BehaviorRealization").map(|d| d.id); - let callable_meta = dag.declaration_by_name("CallableRealization").map(|d| d.id); - let type_instantiation_meta = dag - .declaration_by_name("TypeInstantiationRealization") - .map(|d| d.id); - let pattern_meta = dag.declaration_by_name("PatternRealization").map(|d| d.id); - if Some(meta_decl_id) == type_meta { - Some(RealizationCategoryTag::Type) - } else if Some(meta_decl_id) == op_meta { - Some(RealizationCategoryTag::Operator) - } else if Some(meta_decl_id) == behavior_meta { - Some(RealizationCategoryTag::Behavior) - } else if Some(meta_decl_id) == callable_meta { - Some(RealizationCategoryTag::Callable) - } else if Some(meta_decl_id) == type_instantiation_meta { - Some(RealizationCategoryTag::TypeInstantiation) - } else if Some(meta_decl_id) == pattern_meta { - Some(RealizationCategoryTag::Pattern) - } else { - None - } -} - -/// Validate that the resolved target of a realization-category -/// field satisfies the per-(category, field_label) structural -/// constraint. Returns `Ok(())` on success or `Err(reason)` with -/// a human-readable explanation of the violation. -/// -/// **The constraints encode the "DeclarationRef sentinel is too -/// permissive" narrowing.** PR #445's R1 review on the unwind -/// flagged that `BehaviorRealization { target: Int }` would -/// type-check at the substrate level because the field type is -/// `DeclarationRef` (the universal sentinel). The fully structural -/// fix (typed marker hierarchy via `inhabits` syntax or `where` -/// clauses) requires parser/lower extensions that are out of -/// scope for the PR-B-unwind round; this function is the -/// fail-closed lower-time alternative. Bad wirings now surface -/// as fail-closed diagnostics at lower time, not as silent skips -/// or downstream "missing realization" errors at emit time. -/// -/// Constraints, by category × field label: -/// -/// - `TypeRealization.target` — must be a primitive type -/// declaration (Int / Bool / String). Anything else is a -/// spec error. -/// - `OperatorRealization.target` — must be either a primitive -/// type or an atomic identity handle (the operand type for a -/// realized operator, e.g. Int for `1 + 2` or PortId for -/// reflected handle equality). -/// - `OperatorRealization.op` — must walk to an Arrow -/// declaration that is a child of an algebra Conj (e.g. -/// OrderedRing.add). The constraint is "the resolved -/// declaration's connective is Arrow," which is the structural -/// shape every algebra field has. -/// - `BehaviorRealization.target` — must be one of the v3_l1 -/// substrate behavior markers (Bind / Branch / Loop / -/// Transform / Value / Main). -/// - `CallableRealization.target` — must walk to an Arrow -/// declaration (a callable target). -/// - `TypeInstantiationRealization.target` — must be a named -/// generic declaration with at least one type parameter. -/// - `PatternRealization.target` — must walk to a Disj -/// declaration (a matchable sum carrier). -fn validate_realization_field_target( - dag: &Dag, - category: RealizationCategoryTag, - field_label: &str, - target: DeclarationId, -) -> Result<(), String> { - let int_id = dag.declaration_by_name("Int").map(|d| d.id); - let bool_id = dag.declaration_by_name("Bool").map(|d| d.id); - let string_id = dag.declaration_by_name("String").map(|d| d.id); - let node_id = dag.declaration_by_name("NodeId").map(|d| d.id); - let port_id = dag.declaration_by_name("PortId").map(|d| d.id); - let declaration_id = dag.declaration_by_name("DeclarationId").map(|d| d.id); - - let is_primitive = - |id: DeclarationId| Some(id) == int_id || Some(id) == bool_id || Some(id) == string_id; - let is_atomic_handle = |id: DeclarationId| { - Some(id) == node_id || Some(id) == port_id || Some(id) == declaration_id - }; - let is_behavior_marker = |id: DeclarationId| { - let markers = [ - dag.declaration_by_name("Value"), - dag.declaration_by_name("Transform"), - dag.declaration_by_name("Branch"), - dag.declaration_by_name("Loop"), - dag.declaration_by_name("Bind"), - dag.declaration_by_name("Main"), - ]; - markers.iter().any(|m| m.map(|d| d.id) == Some(id)) - }; - let is_algebra_field = - |id: DeclarationId| matches!(dag.declaration(id).connective, TypeConnective::Arrow { .. }); - let walks_to_arrow = |start: DeclarationId| { - let mut current = start; - for _ in 0..32 { - match &dag.declaration(current).connective { - TypeConnective::Arrow { .. } => return true, - TypeConnective::Instantiation { template, .. } => current = *template, - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => current = *next, - _ => return false, - } - } - false - }; - let walks_to_disj = |start: DeclarationId| { - let mut current = start; - for _ in 0..32 { - match &dag.declaration(current).connective { - TypeConnective::Disj { .. } => return true, - TypeConnective::Instantiation { template, .. } => current = *template, - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => current = *next, - _ => return false, - } - } - false - }; - - match (category, field_label) { - (RealizationCategoryTag::Type, "target") => { - if is_behavior_marker(target) { - Err(format!( - "TypeRealization.target must reference a realizable structural type, not a behavior marker; got declaration {target:?}" - )) - } else { - Ok(()) - } - } - (RealizationCategoryTag::Operator, "target") => { - if is_primitive(target) || is_atomic_handle(target) { - Ok(()) - } else { - Err(format!( - "OperatorRealization.target must reference a primitive operand type or atomic handle (Int/Bool/String/NodeId/PortId/DeclarationId); got declaration {target:?}" - )) - } - } - (RealizationCategoryTag::Operator, "op") => { - if is_algebra_field(target) { - Ok(()) - } else { - Err(format!( - "OperatorRealization.op must reference an algebra field declaration whose connective is Arrow (e.g. OrderedRing.add); got declaration {target:?}" - )) - } - } - (RealizationCategoryTag::Behavior, "target") => { - if is_behavior_marker(target) { - Ok(()) - } else { - Err(format!( - "BehaviorRealization.target must reference one of the v3_l1 behavior markers (Bind/Branch/Loop/Transform/Value/Main); got declaration {target:?}" - )) - } - } - (RealizationCategoryTag::Callable, "target") => { - if walks_to_arrow(target) { - Ok(()) - } else { - Err(format!( - "CallableRealization.target must reference a callable declaration whose connective walks to Arrow; got declaration {target:?}" - )) - } - } - (RealizationCategoryTag::TypeInstantiation, "target") => { - if !dag.declaration(target).type_params.is_empty() { - Ok(()) - } else { - Err(format!( - "TypeInstantiationRealization.target must reference a generic declaration with at least one type parameter; got declaration {target:?}" - )) - } - } - (RealizationCategoryTag::Pattern, "target") => { - if walks_to_disj(target) { - Ok(()) - } else { - Err(format!( - "PatternRealization.target must reference a matchable sum declaration whose connective walks to Disj; got declaration {target:?}" - )) - } - } - // Fields without a category-specific narrowing constraint - // (e.g. carrier and cost are scalar literals, not - // Declaration references). The category check is a - // partial constraint applied only to the fields where it - // makes sense; other fields skip it. - _ => Ok(()), - } -} - -/// Walk a declaration chain looking for a specific target -/// declaration id. Returns true if `current` eventually resolves -/// to `target` via Instantiation/ResolvedIdentifier edges or if -/// `current == target` directly. Used by -/// `lower_record_to_structural` to check "is this field's type -/// annotation ultimately Int/Bool/String?" -fn walks_to(dag: &Dag, start: DeclarationId, target: DeclarationId) -> bool { - let mut current = start; - for _ in 0..32 { - if current == target { - return true; - } - let decl = dag.declaration(current); - match &decl.connective { - TypeConnective::Instantiation { template, .. } => { - current = *template; - } - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => { - current = *next; - } - _ => return false, - } - } - false -} - -#[allow(clippy::too_many_arguments)] -fn lower_fn_item_expr_body( - name: &str, - params: &[SurfaceParam], - return_type: &SurfaceType, - body: &SurfaceExpr, - dag: &mut Dag, - outer_scope: HashMap, - symbols: &HashMap, - mutual_recursion: &MutualRecursionPlans, - mutual_state: &mut MutualRecursionState, -) -> HashMap { - let fn_decl_id = symbols[name]; - - // 1. Read the seeded Arrow — single-construction-authority for - // parameter declarations (DB-11 3a.3). `seed_function_signatures_phase` - // already ran `type_to_declaration_id` for each parameter and, - // when a `where` refinement is present, built the predicate - // Declaration + refined Declaration via `lower_parameter_refinement`. - // Re-lowering them here would push a parallel Bind + refined - // Declaration into the DAG for every `where` clause and then - // overwrite the fn's Arrow inputs to point at the duplicates - // — the seeded ones would stay in the DAG, orphaned. Read - // the seeded inputs directly instead. - let (param_decl_inputs, return_decl_id) = match &dag.declaration(fn_decl_id).connective { - TypeConnective::Arrow { inputs, output, .. } => (inputs.clone(), *output), - _ => { - // Defensive fallback — should not occur because - // `seed_function_signatures_phase` runs unconditionally - // on every `Fn` item before bodies are lowered. If the - // connective is not an Arrow here, the seed phase was - // skipped or the declaration was clobbered; re-derive - // to keep the body-lowering pass going and surface the - // real issue upstream. - let local_fallback = local_scope_from_parent(dag, fn_decl_id); - let inputs: Vec = params - .iter() - .map(|p| { - let base = type_to_declaration_id(&p.ty, symbols, &local_fallback, dag); - match &p.refinement { - Some(predicate) => lower_parameter_refinement( - base, - predicate, - &p.name, - symbols, - dag, - p.ty.span().clone(), - ), - None => base, - } - }) - .collect(); - let output = type_to_declaration_id(return_type, symbols, &local_fallback, dag); - (inputs, output) - } - }; - let mut param_ports: Vec = Vec::with_capacity(params.len()); - let mut param_types: Vec = Vec::with_capacity(params.len()); - let mut body_scope: HashMap = outer_scope.clone(); - let mut callable_scope: CallableScope = CallableScope::new(); - for (param, &input_decl) in params.iter().zip(param_decl_inputs.iter()) { - let port = dag.alloc_port(None); - let ty = match declaration_to_port_shape(input_decl, dag, param.ty.span()) { - Ok(ty) => { - dag.set_port_type(port, ty); - ty - } - Err(diag) => { - dag.mark_unresolved(port, diag); - sentinel_type_shape(dag) - } - }; - body_scope.insert(param.name.clone(), port); - if declaration_is_callable(dag, input_decl, 0) { - callable_scope.insert(param.name.clone(), input_decl); - } - param_ports.push(port); - param_types.push(ty); - } - - // 2. Compute return `TypeShape` from the seeded Arrow's output - // declaration (read above). Same single-construction-authority - // invariant as the parameter loop. - let return_ty = match declaration_to_port_shape(return_decl_id, dag, return_type.span()) { - Ok(ty) => ty, - Err(diag) => { - let err_port = dag.alloc_port(None); - dag.mark_unresolved(err_port, diag); - sentinel_type_shape(dag) - } - }; - - // 3. Mutual recursion check — same as M0. Reject with an - // Unresolved Bind value port AND set the Arrow body to - // `UserDefined(bind_id)` pointing at that Bind. `decide_transform`'s - // `UserDefined` arm reads the Bind's value port state: if it's - // Unresolved, every caller fails with "function has an invalid - // body," cascading the rejection through the normal - // upstream-failure mechanism. - // - // **R13 fix.** Earlier revisions set `body = ArrowBody::Pending` - // here, which `decide_transform` accepts as a realization-lag - // scaffold (signature type-checks, body walking skipped). That - // path doesn't read the Bind, so callers got Resolved types - // from an invalid fn — a FAIL-CLOSED violation. Using - // `UserDefined(bind_id)` routes callers through the - // Unresolved-body guard that poisons them correctly. The - // non-mutually-recursive rejection paths below (zero-param - // recursion, non-descent-provable recursion) already use this - // shape via the common bottom-of-function code. - if let Some(invalid_cluster) = mutual_recursion.invalid_by_member.get(&fn_decl_id) { - let err_port = dag.alloc_port(None); - let body_span = expr_span(body); - dag.mark_unresolved( - err_port, - Diagnostic::ResolveError { - name: format!( - "cannot prove mutually-recursive cluster {{{}}} terminates", - invalid_cluster.members.join(", ") - ), - span: body_span.clone(), - fixes: Vec::new(), - }, - ); - let bind_id = dag.alloc_node_id(); - dag.push_node(Behavior::Bind(BindNode { - id: bind_id, - name: name.to_string(), - value: err_port, - params: param_ports, - span: body_span, - lane2_workflow: None, - })); - dag.declaration_mut(fn_decl_id).connective = TypeConnective::Arrow { - inputs: param_decl_inputs, - output: return_decl_id, - body: ArrowBody::UserDefined(bind_id), - }; - let mut outer_scope = outer_scope; - outer_scope.insert(name.to_string(), err_port); - return outer_scope; - } - - // 4. Lower the body. - let body_start_index = dag.nodes().len(); - let body_return_port = lower_expr( - body, - dag, - &body_scope, - &callable_scope, - symbols, - Some(return_decl_id), - ); - let body_root = dag.port(body_return_port).produced_by; - let body_span = expr_span(body); - let body_end_index = dag.nodes().len(); - - let mutual_cluster = mutual_recursion.cluster_for_member(fn_decl_id); - - // 5. Handle recursion: bounded Loop wrapping (descent-provable) or - // fail-closed rejection (unprovable or zero-arg). Same M0 logic; the - // descent check operates on the unresolved operator-identifier shape - // (SurfaceExpr::Call with target "-"), per §8.9 Option A. - let mut pending_loop: Option = None; - let (bind_value_port, value_port) = if let Some(cluster) = mutual_cluster { - let slot = cluster.per_member_positions[&fn_decl_id]; - let loop_output = dag.alloc_port(None); - dag.set_port_type(loop_output, return_ty); - pending_loop = Some(PendingMutualLoop { - bind_id: None, - source: param_ports[slot], - body_port: body_return_port, - loop_output, - body_root, - span: body_span.clone(), - }); - (body_return_port, loop_output) - } else if is_recursive(body, name) { - if param_ports.is_empty() { - let err_port = dag.alloc_port(None); - dag.mark_unresolved( - err_port, - Diagnostic::ResolveError { - name: format!( - "function `{name}` is recursive but has no parameters; cannot terminate" - ), - span: body_span.clone(), - fixes: witness_correction_for_decl( - dag, - return_decl_id, - body_span.clone(), - format!( - "replace the recursive body of `{name}` with a `{}` base case", - declaration_display_name(dag, return_decl_id) - ), - ) - .into_iter() - .collect(), - }, - ); - (err_port, err_port) - } else if !descent_provable( - body, - dag, - param_decl_inputs[0], - name, - ¶ms[0].name, - &HashMap::new(), - ) { - let err_port = dag.alloc_port(None); - dag.mark_unresolved( - err_port, - Diagnostic::ResolveError { - name: format!( - "cannot prove recursion in `{name}` terminates; expected each recursive call's first argument to be `{param} - ` or a structurally smaller recursive child", - param = ¶ms[0].name, - ), - span: body_span.clone(), - fixes: witness_correction_for_decl( - dag, - return_decl_id, - body_span.clone(), - format!( - "replace the non-terminating body of `{name}` with a `{}` base case", - declaration_display_name(dag, return_decl_id) - ), - ) - .into_iter() - .collect(), - }, - ); - (err_port, err_port) - } else { - let loop_id = dag.alloc_node_id(); - let loop_output = dag.alloc_port(Some(loop_id)); - dag.set_port_type(loop_output, return_ty); - let loop_body_node = body_root.unwrap_or(loop_id); - dag.push_node(Behavior::Loop(LoopNode { - id: loop_id, - source: param_ports[0], - init: param_ports[0], - body: loop_body_node, - bound: LoopBound::Cardinality { - count: param_ports[0], - }, - output: loop_output, - span: body_span.clone(), - })); - (loop_output, loop_output) - } - } else { - (body_return_port, body_return_port) - }; - - dag.set_port_type(bind_value_port, return_ty); - if value_port != bind_value_port { - dag.set_port_type(value_port, return_ty); - } - let bind_id = dag.alloc_node_id(); - dag.push_node(Behavior::Bind(BindNode { - id: bind_id, - name: name.to_string(), - value: bind_value_port, - params: param_ports, - span: body_span, - lane2_workflow: None, - })); - - if let Some(cluster_index) = mutual_recursion.by_member.get(&fn_decl_id).copied() { - let cluster_state = &mut mutual_state.clusters[cluster_index]; - if let Some(mut pending) = pending_loop { - pending.bind_id = Some(bind_id); - cluster_state.pending_loops.insert(fn_decl_id, pending); - } - let cluster_plan = &mutual_recursion.clusters[cluster_index]; - cluster_state.intra_cluster_transform_ids.extend( - dag.nodes()[body_start_index..body_end_index] - .iter() - .filter_map(Behavior::as_transform) - .filter_map(|transform| match transform.target { - TransformTarget::Callable(target_decl) => cluster_plan - .has_recursive_edge(fn_decl_id, callable_target_template(target_decl, dag)) - .then_some(transform.id), - _ => None, - }), - ); - } - - // 6. Fill in the function's Declaration with the Arrow connective. - dag.declaration_mut(fn_decl_id).connective = TypeConnective::Arrow { - inputs: param_decl_inputs, - output: return_decl_id, - body: ArrowBody::UserDefined(bind_id), - }; - - let mut outer_scope = outer_scope; - outer_scope.insert(name.to_string(), value_port); - outer_scope -} - -/// Convert a surface type annotation into a port-level `TypeShape`. -/// User code at M1(2.6) only accepts primitive type annotations -/// Wrap a pre-computed `DeclarationId` as a port `TypeShape`, with a -/// fail-closed check for fresh unresolved stubs. -/// -/// **Dissolution receipt — QW5 SINGLE AUTHORITY + R9 double-lower fix.** -/// Before M1(2.7) port typing hardcoded `"Int"|"Bool"|"String"` as a -/// whitelist parallel to `type_to_declaration_id`'s structural walk. -/// The whitelist dissolved into `type_to_declaration_id` (Class 1 -/// resolution). Before R9, the port path *re-called* -/// `type_to_declaration_id` in a separate helper, so compound types -/// like `List` allocated two anonymous declarations — one for -/// the Arrow input, one for the port TypeShape — with different -/// `DeclarationId`s. R9 collapses that: callers run -/// `type_to_declaration_id` **once** and pass the resulting id to -/// this helper, so the Arrow's declaration slot and the port's -/// `TypeShape` share identity. -/// -/// The fail-closed guard detects a fresh anonymous -/// `UnresolvedIdentifier` stub (name wasn't in the symbol table) -/// and returns `Err` so the caller marks the port `Unresolved`. The -/// resolve sweep ALSO fires against the stub at end of lowering, -/// but this path anchors the failure to the annotation span. -fn declaration_to_port_shape( - decl_id: DeclarationId, - dag: &Dag, - annotation_span: &SourceSpan, -) -> Result { - let decl = dag.declaration(decl_id); - if decl.name.is_none() { - if let TypeConnective::Atom(AtomPayload::UnresolvedIdentifier(name)) = &decl.connective { - return Err(Diagnostic::ResolveError { - name: format!("unknown type `{name}`"), - span: annotation_span.clone(), - fixes: Vec::new(), - }); - } - } - Ok(TypeShape::new(decl_id)) -} - -/// Sentinel TypeShape returned when a type annotation failed to -/// resolve. The port it's assigned to has already been `mark_unresolved`ed -/// with the underlying diagnostic, so the sentinel value itself is -/// never observed by inference — it exists only to satisfy Rust's -/// "must initialize" requirement. Uses the cached `Int` primitive -/// shape, falling back to `DeclarationId(0)` if even the cache is -/// empty (unreachable post-bootstrap). -fn sentinel_type_shape(dag: &Dag) -> TypeShape { - dag.int_shape() - .unwrap_or_else(|| TypeShape::new(dag.declarations()[0].id)) -} - -fn declaration_callable_inputs( - dag: &Dag, - current: DeclarationId, - depth: usize, -) -> Option> { - if depth >= 32 { - return None; - } - match &dag.declaration(current).connective { - TypeConnective::Arrow { inputs, .. } => Some(inputs.clone()), - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => { - declaration_callable_inputs(dag, *next, depth + 1) - } - TypeConnective::Instantiation { template, .. } => { - declaration_callable_inputs(dag, *template, depth + 1) - } - TypeConnective::Atom(AtomPayload::UnresolvedIdentifier(_)) - | TypeConnective::Atom(AtomPayload::TypeParam(_)) - | TypeConnective::Atom(AtomPayload::Literal(_)) - | TypeConnective::Conj { .. } - | TypeConnective::Disj { .. } - | TypeConnective::Cardinality { .. } => None, - } -} - -fn declaration_is_callable(dag: &Dag, current: DeclarationId, depth: usize) -> bool { - declaration_callable_inputs(dag, current, depth).is_some() -} - -fn callable_target_template(target: DeclarationId, dag: &Dag) -> DeclarationId { - match &dag.declaration(target).connective { - TypeConnective::Instantiation { template, .. } => *template, - _ => target, - } -} - -fn declaration_callable_output( - dag: &Dag, - current: DeclarationId, - depth: usize, -) -> Option { - if depth >= 32 { - return None; - } - match &dag.declaration(current).connective { - TypeConnective::Arrow { output, .. } => Some(*output), - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => { - declaration_callable_output(dag, *next, depth + 1) - } - TypeConnective::Instantiation { template, .. } => { - declaration_callable_output(dag, *template, depth + 1) - } - TypeConnective::Atom(AtomPayload::UnresolvedIdentifier(_)) - | TypeConnective::Atom(AtomPayload::TypeParam(_)) - | TypeConnective::Atom(AtomPayload::Literal(_)) - | TypeConnective::Conj { .. } - | TypeConnective::Disj { .. } - | TypeConnective::Cardinality { .. } => None, - } -} - -fn declaration_callable_signature( - dag: &Dag, - current: DeclarationId, - depth: usize, -) -> Option<(Vec, DeclarationId)> { - Some(( - declaration_callable_inputs(dag, current, depth)?, - declaration_callable_output(dag, current, depth)?, - )) -} - -fn lambda_synthetic_name(span: &SourceSpan) -> String { - format!("__anon_lambda_{}_{}", span.byte_start, span.byte_end) -} - -fn collect_lambda_free_names( - expr: &SurfaceExpr, - bound: &HashSet, - free: &mut HashSet, -) { - match expr { - SurfaceExpr::Literal { .. } => {} - SurfaceExpr::Var { name, .. } => { - if !bound.contains(name) { - free.insert(name.clone()); - } - } - SurfaceExpr::Path { segments, .. } => { - if let Some(head) = segments.first() { - if !bound.contains(head) { - free.insert(head.clone()); - } - } - } - SurfaceExpr::Call { target, args, .. } => { - if !bound.contains(target) { - free.insert(target.clone()); - } - for arg in args { - collect_lambda_free_names(arg, bound, free); - } - } - SurfaceExpr::VariantRecord { fields, .. } => { - for field in fields { - collect_lambda_free_names(&field.value, bound, free); - } - } - SurfaceExpr::Operator { args, .. } => { - for arg in args { - collect_lambda_free_names(arg, bound, free); - } - } - SurfaceExpr::Lambda { params, body, .. } => { - let mut inner_bound = bound.clone(); - inner_bound.extend(params.iter().cloned()); - collect_lambda_free_names(body, &inner_bound, free); - } - SurfaceExpr::If { - cond, - then_branch, - else_branch, - .. - } => { - collect_lambda_free_names(cond, bound, free); - collect_lambda_free_names(then_branch, bound, free); - collect_lambda_free_names(else_branch, bound, free); - } - SurfaceExpr::Match { - scrutinee, arms, .. - } => { - collect_lambda_free_names(scrutinee, bound, free); - for arm in arms { - let mut arm_bound = bound.clone(); - for binding in pattern_binding_names(&arm.pattern) { - arm_bound.insert(binding.to_string()); - } - collect_lambda_free_names(&arm.body, &arm_bound, free); - } - } - SurfaceExpr::Record { fields, .. } => { - for field in fields { - collect_lambda_free_names(&field.value, bound, free); - } - } - SurfaceExpr::List { elements, .. } => { - for element in elements { - collect_lambda_free_names(element, bound, free); - } - } - } -} - -fn lower_lambda_expr( - params: &[String], - body: &SurfaceExpr, - span: &SourceSpan, - expected_decl: DeclarationId, - ctx: &mut LambdaLoweringContext<'_>, -) -> Result { - let Some((expected_inputs, expected_output)) = - declaration_callable_signature(ctx.dag, expected_decl, 0) - else { - return Err(Diagnostic::ResolveError { - name: "lambda expression requires an expected function type".to_string(), - span: span.clone(), - fixes: Vec::new(), - }); - }; - if expected_inputs.len() != params.len() { - return Err(Diagnostic::ResolveError { - name: format!( - "lambda parameter count mismatch: expected {} parameter(s) from the surrounding function type, found {}", - expected_inputs.len(), - params.len(), - ), - span: span.clone(), - fixes: Vec::new(), - }); - } - - let mut free = HashSet::new(); - let bound: HashSet = params.iter().cloned().collect(); - collect_lambda_free_names(body, &bound, &mut free); - let mut free_names: Vec = free.into_iter().collect(); - free_names.sort(); - - let mut capture_ports: Vec = Vec::new(); - let mut body_scope: HashMap = HashMap::new(); - let mut body_callable_scope: CallableScope = CallableScope::new(); - for name in free_names { - let mut found_binding = false; - if let Some(&port) = ctx.scope.get(&name) { - capture_ports.push(port); - body_scope.insert(name.clone(), port); - found_binding = true; - } - if let Some(&decl_id) = ctx.callable_scope.get(&name) { - body_callable_scope.insert(name.clone(), decl_id); - found_binding = true; - } - if !found_binding && ctx.symbols.contains_key(&name) { - found_binding = true; - } - if !found_binding { - return Err(Diagnostic::ResolveError { - name, - span: span.clone(), - fixes: Vec::new(), - }); - } - } - - let mut param_ports: Vec = Vec::with_capacity(params.len()); - for (name, input_decl) in params.iter().zip(expected_inputs.iter()) { - let port = ctx.dag.alloc_port(None); - let ty = declaration_to_port_shape(*input_decl, ctx.dag, span)?; - ctx.dag.set_port_type(port, ty); - body_scope.insert(name.clone(), port); - if declaration_is_callable(ctx.dag, *input_decl, 0) { - body_callable_scope.insert(name.clone(), *input_decl); - } - param_ports.push(port); - } - - let body_return_port = lower_expr( - body, - ctx.dag, - &body_scope, - &body_callable_scope, - ctx.symbols, - Some(expected_output), - ); - let bind_id = ctx.dag.alloc_node_id(); - let mut bind_params = capture_ports; - bind_params.extend(param_ports); - ctx.dag.push_node(Behavior::Bind(BindNode { - id: bind_id, - name: lambda_synthetic_name(span), - value: body_return_port, - params: bind_params, - span: span.clone(), - lane2_workflow: None, - })); - - let lambda_decl_id = ctx.dag.alloc_declaration_id(); - ctx.dag.push_declaration(Declaration { - id: lambda_decl_id, - name: None, - connective: TypeConnective::Arrow { - inputs: expected_inputs, - output: expected_output, - body: ArrowBody::UserDefined(bind_id), - }, - type_params: Vec::new(), - meta_tag: None, - specialization_parent: None, - inhabits: None, - value_body: None, - refinement: None, - span: span.clone(), - }); - Ok(lambda_decl_id) -} - -fn resolve_callable_reference( - expr: &SurfaceExpr, - dag: &mut Dag, - callable_scope: &CallableScope, - symbols: &HashMap, -) -> DeclarationId { - match expr { - SurfaceExpr::Var { name, span } => callable_scope - .get(name) - .copied() - .or_else(|| symbols.get(name).copied()) - .unwrap_or_else(|| alloc_identifier_stub(dag, name, span)), - SurfaceExpr::Path { segments, span, .. } => { - alloc_identifier_stub(dag, &segments.join("."), span) - } - other => alloc_identifier_stub(dag, "__callable_argument__", &expr_span(other)), - } -} - -fn push_template_argument_binding( - arguments: &mut Vec, - parameter: DeclarationId, - value: DeclarationId, -) -> bool { - for existing in arguments.iter() { - if existing.parameter == parameter { - return existing.value == value; - } - } - arguments.push(TemplateArgument { parameter, value }); - true -} - -fn template_argument_value( - arguments: &[TemplateArgument], - parameter: DeclarationId, -) -> Option { - arguments - .iter() - .find(|arg| arg.parameter == parameter) - .map(|arg| arg.value) -} - -fn resolve_template_argument_value( - arguments: &[TemplateArgument], - current: DeclarationId, - depth: usize, -) -> DeclarationId { - if depth >= 32 { - return current; - } - let Some(next) = template_argument_value(arguments, current) else { - return current; - }; - if next == current { - return current; - } - resolve_template_argument_value(arguments, next, depth + 1) -} - -fn retained_template_arguments_for_target( - dag: &Dag, - base_target_decl: DeclarationId, - arguments: &[TemplateArgument], -) -> Vec { - let template = match &dag.declaration(base_target_decl).connective { - TypeConnective::Instantiation { template, .. } => *template, - _ => base_target_decl, - }; - let mut allowed: HashSet = dag - .declaration(template) - .type_params - .iter() - .copied() - .collect(); - if let Some(inputs) = direct_invocation_input_decls(dag, template, 0) { - for input in inputs { - if declaration_is_callable(dag, input, 0) { - allowed.insert(input); - } - } - } - - let mut retained: Vec = Vec::new(); - for argument in arguments { - if !allowed.contains(&argument.parameter) { - continue; - } - let resolved_value = resolve_template_argument_value(arguments, argument.value, 0); - if let Some(existing) = retained - .iter_mut() - .find(|existing| existing.parameter == argument.parameter) - { - existing.value = resolved_value; - continue; - } - retained.push(TemplateArgument { - parameter: argument.parameter, - value: resolved_value, - }); - } - retained -} - -fn callable_binding_conflict_diagnostic( - target: &str, - arg_index: usize, - span: &SourceSpan, -) -> Diagnostic { - Diagnostic::ResolveError { - name: format!( - "callable argument {} to `{target}` does not match the expected function type or conflicts with earlier template bindings", - arg_index + 1 - ), - span: span.clone(), - fixes: Vec::new(), - } -} - -fn bind_expected_type_to_actual( - dag: &Dag, - expected_id: DeclarationId, - actual_id: DeclarationId, - arguments: &mut Vec, - depth: usize, -) -> bool { - if depth >= 32 { - return false; - } - let expected_decl = dag.declaration(expected_id); - match &expected_decl.connective { - TypeConnective::Atom(AtomPayload::TypeParam(_)) => { - if let Some(bound) = template_argument_value(arguments, expected_id) { - if bound != expected_id { - return bind_expected_type_to_actual( - dag, - bound, - actual_id, - arguments, - depth + 1, - ); - } - } - push_template_argument_binding(arguments, expected_id, actual_id) - } - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => { - bind_expected_type_to_actual(dag, *next, actual_id, arguments, depth + 1) - } - TypeConnective::Instantiation { - template, - arguments: expected_arguments, - } => { - let actual_decl = dag.declaration(actual_id); - match &actual_decl.connective { - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => { - bind_expected_type_to_actual(dag, expected_id, *next, arguments, depth + 1) - } - TypeConnective::Instantiation { - template: actual_template, - arguments: actual_arguments, - } => { - if *actual_template == expected_id - && expected_arguments.len() == actual_arguments.len() - { - return expected_arguments.iter().zip(actual_arguments.iter()).all( - |(expected, actual)| { - bind_expected_type_to_actual( - dag, - expected.value, - actual.value, - arguments, - depth + 1, - ) - }, - ); - } - if template != actual_template - || expected_arguments.len() != actual_arguments.len() - { - return false; - } - expected_arguments.iter().zip(actual_arguments.iter()).all( - |(expected, actual)| { - bind_expected_type_to_actual( - dag, - expected.value, - actual.value, - arguments, - depth + 1, - ) - }, - ) - } - _ => false, - } - } - TypeConnective::Arrow { inputs, output, .. } => { - let actual_decl = dag.declaration(actual_id); - match &actual_decl.connective { - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => { - bind_expected_type_to_actual(dag, expected_id, *next, arguments, depth + 1) - } - TypeConnective::Arrow { - inputs: actual_inputs, - output: actual_output, - .. - } => { - if inputs.len() != actual_inputs.len() { - return false; - } - inputs - .iter() - .zip(actual_inputs.iter()) - .all(|(expected, actual)| { - bind_expected_type_to_actual( - dag, - *expected, - *actual, - arguments, - depth + 1, - ) - }) - && bind_expected_type_to_actual( - dag, - *output, - *actual_output, - arguments, - depth + 1, - ) - } - _ => false, - } - } - TypeConnective::Cardinality { element, bound } => { - let actual_decl = dag.declaration(actual_id); - match &actual_decl.connective { - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => { - bind_expected_type_to_actual(dag, expected_id, *next, arguments, depth + 1) - } - TypeConnective::Cardinality { - element: actual_element, - bound: actual_bound, - } if bound == actual_bound => bind_expected_type_to_actual( - dag, - *element, - *actual_element, - arguments, - depth + 1, - ), - _ => false, - } - } - _ => { - let actual_decl = dag.declaration(actual_id); - match &actual_decl.connective { - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => { - bind_expected_type_to_actual(dag, expected_id, *next, arguments, depth + 1) - } - _ => expected_id == actual_id, - } - } - } -} - -fn specialize_decl_for_lowering( - dag: &mut Dag, - current: DeclarationId, - arguments: &[TemplateArgument], - depth: usize, -) -> DeclarationId { - if arguments.is_empty() { - return current; - } - if depth >= 32 { - return current; - } - let decl = dag.declaration(current).clone(); - // Stage 3b correction generation must see refinement-bearing - // declarations as refinement-bearing expectations, not their base - // carrier. Lowering does not have a substituted-refined - // materialization path here, so preserve the declared edge and let - // downstream consumers fail closed instead of specializing through - // to a base witness like `Int`. - if decl.refinement.is_some() { - return current; - } - match decl.connective { - TypeConnective::Atom(AtomPayload::TypeParam(_)) => arguments - .iter() - .find(|arg| arg.parameter == current) - .map(|arg| arg.value) - .unwrap_or(current), - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => { - specialize_decl_for_lowering(dag, next, arguments, depth + 1) - } - TypeConnective::Instantiation { - template, - arguments: inner_arguments, - } => { - let specialized_arguments: Vec = inner_arguments - .iter() - .map(|arg| TemplateArgument { - parameter: arg.parameter, - value: specialize_decl_for_lowering(dag, arg.value, arguments, depth + 1), - }) - .collect(); - if specialized_arguments.len() == inner_arguments.len() - && specialized_arguments - .iter() - .zip(inner_arguments.iter()) - .all(|(lhs, rhs)| lhs.parameter == rhs.parameter && lhs.value == rhs.value) - { - return current; - } - if let Some(existing) = - find_equivalent_decl_instantiation_lower(dag, template, &specialized_arguments) - { - return existing; - } - let id = dag.alloc_declaration_id(); - dag.push_declaration(Declaration { - id, - name: None, - connective: TypeConnective::Instantiation { - template, - arguments: specialized_arguments, - }, - type_params: Vec::new(), - meta_tag: None, - specialization_parent: None, - inhabits: None, - value_body: None, - refinement: None, - span: decl.span, - }); - id - } - TypeConnective::Arrow { - inputs, - output, - body, - } => { - let specialized_inputs: Vec = inputs - .iter() - .map(|input| specialize_decl_for_lowering(dag, *input, arguments, depth + 1)) - .collect(); - let specialized_output = - specialize_decl_for_lowering(dag, output, arguments, depth + 1); - if specialized_inputs == inputs && specialized_output == output { - return current; - } - let id = dag.alloc_declaration_id(); - dag.push_declaration(Declaration { - id, - name: None, - connective: TypeConnective::Arrow { - inputs: specialized_inputs, - output: specialized_output, - body, - }, - type_params: Vec::new(), - meta_tag: None, - specialization_parent: None, - inhabits: None, - value_body: None, - refinement: None, - span: decl.span, - }); - id - } - TypeConnective::Conj { children } => { - let specialized_children: Vec = children - .iter() - .map(|child| Field { - label: child.label.clone(), - ty: specialize_decl_for_lowering(dag, child.ty, arguments, depth + 1), - }) - .collect(); - if specialized_children.len() == children.len() - && specialized_children - .iter() - .zip(children.iter()) - .all(|(lhs, rhs)| lhs.label == rhs.label && lhs.ty == rhs.ty) - { - return current; - } - let id = dag.alloc_declaration_id(); - dag.push_declaration(Declaration { - id, - // Specialized products stay `name: None`: reusing the template name - // would admit two `DeclarationId`s with the same Rust type label if - // both were ever emitted outside filtered surfaces. - // No `specialization_parent` here: unlike specialized anonymous `Disj`, - // emit has no consumer that must recover a template `Conj` label today; - // set the edge when a real downstream needs it (E-6 / same-PR consumer). - name: None, - connective: TypeConnective::Conj { - children: specialized_children, - }, - type_params: Vec::new(), - meta_tag: None, - specialization_parent: None, - inhabits: None, - value_body: None, - refinement: None, - span: decl.span, - }); - id - } - TypeConnective::Disj { variants } => { - let specialized_variants: Vec = variants - .iter() - .map(|variant| Field { - label: variant.label.clone(), - ty: specialize_decl_for_lowering(dag, variant.ty, arguments, depth + 1), - }) - .collect(); - if specialized_variants.len() == variants.len() - && specialized_variants - .iter() - .zip(variants.iter()) - .all(|(lhs, rhs)| lhs.label == rhs.label && lhs.ty == rhs.ty) - { - return current; - } - let id = dag.alloc_declaration_id(); - dag.push_declaration(Declaration { - id, - // P2: `Declaration::name` is the only surface-visible authority for - // `Dag::declaration_by_name` — never clone the template name onto a - // fresh `Disj` id (that admits a second winner in name-based lookup). - // Emit qualifies Rust match patterns via `Declaration::specialization_parent` - // (`rust_target::named_disj_enum_name_for_rust_match_emit`). - name: None, - connective: TypeConnective::Disj { - variants: specialized_variants, - }, - type_params: Vec::new(), - meta_tag: None, - specialization_parent: Some(current), - inhabits: None, - value_body: None, - refinement: None, - span: decl.span, - }); - id - } - TypeConnective::Cardinality { .. } - | TypeConnective::Atom(AtomPayload::UnresolvedIdentifier(_)) - | TypeConnective::Atom(AtomPayload::Literal(_)) => current, - } -} - -fn unresolved_port(dag: &mut Dag, diagnostic: Diagnostic) -> PortId { - let port = dag.alloc_port(None); - dag.mark_unresolved(port, diagnostic); - port -} - -fn lower_field_path_expr( - segments: &[String], - segment_spans: &[SourceSpan], - span: &SourceSpan, - dag: &mut Dag, - scope: &HashMap, - symbols: &HashMap, -) -> PortId { - let Some((head, rest)) = segments.split_first() else { - return unresolved_port( - dag, - Diagnostic::ResolveError { - name: "empty dotted path expression".to_string(), - span: span.clone(), - fixes: Vec::new(), - }, - ); - }; - // Path A: head is in local variable scope (fn params, let bindings, - // match-arm pattern bindings). Existing type-driven walk. - if let Some(&port) = scope.get(head) { - let mut current_port = port; - for (index, field_label) in rest.iter().enumerate() { - let node_id = dag.alloc_node_id(); - let output = dag.alloc_port(Some(node_id)); - let static_resolution = resolve_static_field_project(dag, current_port, field_label); - let field_span = segment_spans - .get(index + 1) - .cloned() - .unwrap_or_else(|| span.clone()); - dag.push_node(Behavior::Transform(TransformNode { - id: node_id, - target: TransformTarget::FieldProject { - field_label: field_label.clone(), - field_child: static_resolution.map(|(field_child, _)| field_child), - }, - inputs: vec![current_port], - output, - span: field_span, - })); - if let Some((_, ty)) = static_resolution { - dag.set_port_type(output, ty); - } - current_port = output; - } - return current_port; - } - // Path B: DB-10 (3a.2) — head resolves to a `data` declaration - // with a structural value_body. Walk the compile-time value - // literal rather than performing a runtime FieldProject walk. - // `cfg.host` where cfg: Config = { host: 1, port: 8080 } becomes - // a Value(Int(1)) node directly. - if let Some(&decl_id) = symbols.get(head) { - let value_body_opt = dag.declaration(decl_id).value_body.clone(); - if let Some(value_body) = value_body_opt { - if let Some(port) = resolve_data_path(dag, &value_body, rest, span) { - return port; - } - } - } - unresolved_port( - dag, - Diagnostic::ResolveError { - name: format!( - "dotted path `{}` is not a local field access; expression-position dotted paths currently require a local-variable head or a `data` declaration with a compile-time value", - segments.join("."), - ), - span: span.clone(), - fixes: Vec::new(), - }, - ) -} - -/// DB-10 (3a.2): walk a sequence of field segments through a data -/// declaration's `ValueBody`, emitting a `Value` node at the terminal -/// scalar literal. Returns `None` if the path cannot be fully -/// resolved — caller falls back to an unresolved diagnostic. -/// -/// Only scalar literals terminate the walk; nested `Record` payloads -/// admit further descent but `List`, `Map`, and `Variant` terminal -/// reads are out of scope for 3a.2 and fall through to `None`. -fn resolve_data_path( - dag: &mut Dag, - value_body: &crate::dag::ValueBody, - segments: &[String], - span: &SourceSpan, -) -> Option { - match value_body { - crate::dag::ValueBody::Unparsed(_) => None, - crate::dag::ValueBody::Scalar(bits) => { - if !segments.is_empty() { - return None; - } - Some(emit_literal_as_value_port(dag, bits.clone(), span)) - } - crate::dag::ValueBody::Structural { fields } => { - resolve_structural_field_path(dag, fields, segments, span) - } - } -} - -fn resolve_structural_field_path( - dag: &mut Dag, - fields: &[(String, crate::dag::FieldValue)], - segments: &[String], - span: &SourceSpan, -) -> Option { - let (head, rest) = segments.split_first()?; - let field_value = fields - .iter() - .find(|(label, _)| label == head) - .map(|(_, v)| v.clone())?; - if rest.is_empty() { - return emit_field_value_as_port(dag, &field_value, span); - } - match &field_value { - crate::dag::FieldValue::Record(inner) => { - resolve_structural_field_path(dag, inner, rest, span) - } - _ => None, - } -} - -fn emit_field_value_as_port( - dag: &mut Dag, - fv: &crate::dag::FieldValue, - span: &SourceSpan, -) -> Option { - match fv { - crate::dag::FieldValue::Literal(bits) => { - Some(emit_literal_as_value_port(dag, bits.clone(), span)) - } - // Reference / Record / List / Variant terminal reads require - // more emission machinery than DB-10 covers (acceptance is - // scalar-at-leaf). Leave unresolved; call site falls back to - // an unresolved diagnostic. - _ => None, - } -} - -/// DB-10 (3a.2): emit a scalar literal from its `LiteralBits` -/// directly as a `Behavior::Value` node. Shared by -/// `ValueBody::Scalar` inlining and `FieldValue::Literal` terminal -/// reads. -fn emit_literal_as_value_port(dag: &mut Dag, bits: LiteralBits, span: &SourceSpan) -> PortId { - let node_id = dag.alloc_node_id(); - let output = dag.alloc_port(Some(node_id)); - dag.push_node(Behavior::Value(ValueNode { - id: node_id, - data: bits, - output, - span: span.clone(), - lane2_workflow: None, - })); - output -} - -fn lower_payload_binding(dag: &mut Dag, binding_name: &str) -> PayloadBinding { - let payload_port = dag.alloc_port(None); - PayloadBinding { - binding_name: binding_name.to_string(), - payload_port, - } -} - -fn lower_field_projection_from_port( - dag: &mut Dag, - input_port: PortId, - field_label: &str, - span: &SourceSpan, -) -> PortId { - let node_id = dag.alloc_node_id(); - let output = dag.alloc_port(Some(node_id)); - let static_resolution = resolve_static_field_project(dag, input_port, field_label); - dag.push_node(Behavior::Transform(TransformNode { - id: node_id, - target: TransformTarget::FieldProject { - field_label: field_label.to_string(), - field_child: static_resolution.map(|(field_child, _)| field_child), - }, - inputs: vec![input_port], - output, - span: span.clone(), - })); - if let Some((_, ty)) = static_resolution { - dag.set_port_type(output, ty); - } - output -} - -fn variant_payload_fields_for_lowering( - dag: &Dag, - variant_decl: DeclarationId, -) -> Option> { - let mut subst = LowerSubstStack::default(); - let conj_id = walk_to_conj_decl_with_subst_lower(dag, variant_decl, &mut subst)?; - let TypeConnective::Conj { children } = &dag.declaration(conj_id).connective else { - return None; - }; - Some( - children - .iter() - .map(|child| { - let specialized = - resolve_decl_with_subst_lower(dag, child.ty, &subst, 0).unwrap_or(child.ty); - (child.label.clone(), specialized) - }) - .collect(), - ) -} - -fn synthetic_pattern_payload_binding_name( - fields: &[SurfacePatternField], - span: &SourceSpan, -) -> String { - if fields.len() == 1 { - return fields[0].binding.clone(); - } - format!("__payload_{}_{}", span.byte_start, span.byte_end) -} - -struct LoweredMatchArm { - output: PortId, - body_node: Option, - pattern_name: String, - pattern_span: SourceSpan, - binding: Option, -} - -fn duplicate_payload_pattern_binding_diagnostic( - variant_name: &str, - binding_name: &str, - span: &SourceSpan, -) -> Diagnostic { - Diagnostic::ResolveError { - name: format!( - "named payload pattern `{variant_name}` binds `{binding_name}` more than once" - ), - span: span.clone(), - fixes: Vec::new(), - } -} - -struct VariantRecordExprRef<'a> { - target: &'a str, - fields: &'a [crate::parse::SurfaceRecordField], - span: &'a SourceSpan, -} - -fn lower_expr( - expr: &SurfaceExpr, - dag: &mut Dag, - scope: &HashMap, - callable_scope: &CallableScope, - symbols: &HashMap, - expected_decl: Option, -) -> PortId { - match expr { - SurfaceExpr::Literal { value, span } => { - let node_id = dag.alloc_node_id(); - let output = dag.alloc_port(Some(node_id)); - let data = match value { - SurfaceLiteral::Int(v) => LiteralBits::Int(*v), - SurfaceLiteral::Bool(v) => LiteralBits::Bool(*v), - SurfaceLiteral::String(v) => LiteralBits::String(v.clone()), - }; - dag.push_node(Behavior::Value(ValueNode { - id: node_id, - data, - output, - span: span.clone(), - lane2_workflow: None, - })); - output - } - SurfaceExpr::Var { name, span } => match scope.get(name) { - Some(port) => *port, - None => { - if let Some(target) = resolve_expected_variant_constructor(dag, expected_decl, name) - { - return lower_constructor_invocation(dag, target, Vec::new(), span.clone()); - } - // DB-10 (3a.2): if `name` resolves to a `data` decl - // with a scalar value_body, inline the value at this - // use site as a Value(LiteralBits) node. Emission then - // renders the target-native literal without extra - // wiring. Record-valued data references are currently - // unsupported — those sites fall through to the - // existing unresolved diagnostic (acceptance test - // `test_3a2_data_referenced_in_fn_body_compiles` - // uses scalar only). - if let Some(decl_id) = symbols.get(name) { - if let Some(crate::dag::ValueBody::Scalar(bits)) = - &dag.declaration(*decl_id).value_body - { - let bits = bits.clone(); - return emit_literal_as_value_port(dag, bits, span); - } - } - let port = dag.alloc_port(None); - let fixes = expected_decl - .and_then(|decl| { - witness_correction_for_decl( - dag, - decl, - span.clone(), - format!( - "replace unresolved name `{name}` with a `{}` value", - declaration_display_name(dag, decl) - ), - ) - }) - .into_iter() - .collect(); - dag.mark_unresolved( - port, - Diagnostic::ResolveError { - name: name.clone(), - span: span.clone(), - fixes, - }, - ); - port - } - }, - SurfaceExpr::Call { target, args, span } => { - let Some(base_target_decl) = - resolve_expected_variant_constructor(dag, expected_decl, target) - .or_else(|| callable_scope.get(target).copied()) - .or_else(|| symbols.get(target).copied()) - else { - for arg in args { - let _ = lower_expr(arg, dag, scope, callable_scope, symbols, None); - } - let port = dag.alloc_port(None); - let fixes = expected_decl - .and_then(|decl| { - witness_correction_for_decl( - dag, - decl, - span.clone(), - format!( - "replace unresolved call `{target}` with a `{}` value", - declaration_display_name(dag, decl) - ), - ) - }) - .into_iter() - .collect(); - dag.mark_unresolved( - port, - Diagnostic::ResolveError { - name: target.clone(), - span: span.clone(), - fixes, - }, - ); - return port; - }; - let target_inputs = direct_invocation_input_decls(dag, base_target_decl, 0); - let mut input_ports: Vec = Vec::new(); - let mut template_arguments: Vec = Vec::new(); - if let (Some(expected_result), Some(target_output)) = ( - expected_decl, - declaration_callable_output(dag, base_target_decl, 0), - ) { - let _ = bind_expected_type_to_actual( - dag, - target_output, - expected_result, - &mut template_arguments, - 0, - ); - } - for (idx, arg) in args.iter().enumerate() { - let raw_expected_input = target_inputs - .as_ref() - .and_then(|inputs| inputs.get(idx)) - .copied(); - let specialized_expected_input = raw_expected_input.map(|expected_decl| { - specialize_decl_for_lowering(dag, expected_decl, &template_arguments, 0) - }); - if let Some(expected_decl) = raw_expected_input { - if declaration_is_callable(dag, expected_decl, 0) { - let specialized_expected = - specialized_expected_input.unwrap_or(expected_decl); - let actual_callable = match arg { - SurfaceExpr::Lambda { params, body, span } => { - let mut lambda_ctx = LambdaLoweringContext { - dag, - scope, - callable_scope, - symbols, - }; - match lower_lambda_expr( - params, - body, - span, - specialized_expected, - &mut lambda_ctx, - ) { - Ok(lambda_decl_id) => lambda_decl_id, - Err(diag) => { - report_declaration_error(lambda_ctx.dag, diag); - alloc_identifier_stub(lambda_ctx.dag, "__lambda__", span) - } - } - } - _ => resolve_callable_reference(arg, dag, callable_scope, symbols), - }; - let matches_expected = push_template_argument_binding( - &mut template_arguments, - expected_decl, - actual_callable, - ) && bind_expected_type_to_actual( - dag, - specialized_expected, - actual_callable, - &mut template_arguments, - 0, - ); - if !matches_expected { - let port = dag.alloc_port(None); - dag.mark_unresolved( - port, - callable_binding_conflict_diagnostic(target, idx, &expr_span(arg)), - ); - return port; - } - continue; - } - } - let lowered = lower_expr( - arg, - dag, - scope, - callable_scope, - symbols, - specialized_expected_input, - ); - if let Some(expected_decl) = raw_expected_input { - if let crate::dag::PortState::Resolved(actual_ty) = dag.port(lowered).state() { - let _ = bind_expected_type_to_actual( - dag, - expected_decl, - actual_ty.declaration, - &mut template_arguments, - 0, - ); - } - } - input_ports.push(lowered); - } - let retained_arguments = - retained_template_arguments_for_target(dag, base_target_decl, &template_arguments); - let target_decl = if retained_arguments.is_empty() { - base_target_decl - } else { - let instantiation_id = dag.alloc_declaration_id(); - dag.push_declaration(Declaration { - id: instantiation_id, - name: None, - connective: TypeConnective::Instantiation { - template: base_target_decl, - arguments: retained_arguments, - }, - type_params: Vec::new(), - meta_tag: None, - specialization_parent: None, - inhabits: None, - value_body: None, - refinement: None, - span: span.clone(), - }); - instantiation_id - }; - let node_id = dag.alloc_node_id(); - let output = dag.alloc_port(Some(node_id)); - dag.push_node(Behavior::Transform(TransformNode { - id: node_id, - target: TransformTarget::Callable(target_decl), - inputs: input_ports, - output, - span: span.clone(), - })); - output - } - SurfaceExpr::VariantRecord { - target, - fields, - span, - } => lower_variant_record_expr( - VariantRecordExprRef { - target, - fields, - span, - }, - dag, - scope, - callable_scope, - symbols, - expected_decl, - ), - SurfaceExpr::Operator { op, args, span } => { - let input_ports: Vec = args - .iter() - .map(|a| lower_expr(a, dag, scope, callable_scope, symbols, None)) - .collect(); - let node_id = dag.alloc_node_id(); - let output = dag.alloc_port(Some(node_id)); - dag.push_node(Behavior::Transform(TransformNode { - id: node_id, - target: TransformTarget::Operator(*op), - inputs: input_ports, - output, - span: span.clone(), - })); - output - } - SurfaceExpr::Lambda { span, .. } => { - let port = dag.alloc_port(None); - dag.mark_unresolved( - port, - Diagnostic::ResolveError { - name: "lambda expression requires an expected function type at this position" - .to_string(), - span: span.clone(), - fixes: Vec::new(), - }, - ); - port - } - SurfaceExpr::If { - cond, - then_branch, - else_branch, - span, - } => { - let cond_port = lower_expr(cond, dag, scope, callable_scope, symbols, None); - // DB-11 (3a.3) arm-local refinement narrowing. If `cond` is - // a predicate applied to a scope-bound Var (e.g. `d != 0` - // where `d` is a parameter), allocate a narrowed port in - // the then-arm whose type carries a refinement matching - // the predicate. Call-site refinement discharge in - // `decide_transform` then sees the refinement on the - // narrowed port and accepts calls that would otherwise - // fail. No narrowing on the else-arm — by convention the - // else-arm's refinement would be the negation, and DB-11 - // does not model negated predicates (structural equality - // only). - let then_scope_owned = narrow_scope_for_predicate(cond, scope, symbols, dag); - let then_scope_ref = then_scope_owned.as_ref().unwrap_or(scope); - let then_port = lower_expr( - then_branch, - dag, - then_scope_ref, - callable_scope, - symbols, - expected_decl, - ); - let else_port = lower_expr( - else_branch, - dag, - scope, - callable_scope, - symbols, - expected_decl, - ); - let branch_id = dag.alloc_node_id(); - let branch_output = dag.alloc_port(Some(branch_id)); - let then_body = producer_of(dag, then_port).unwrap_or(branch_id); - let else_body = producer_of(dag, else_port).unwrap_or(branch_id); - // if-then-else is a match on Bool with two arms. Emit - // explicit pattern labels so the discriminator lives - // structurally on Path, not by positional convention. - // Infer resolves each "True"/"False" against Bool's - // Disj children. - dag.push_node(Behavior::Branch(BranchNode { - id: branch_id, - input: cond_port, - paths: vec![ - Path { - body: then_body, - output: then_port, - pattern: BranchPattern::UnresolvedVariant { - name: "True".to_string(), - span: expr_span(then_branch), - }, - binding: None, - }, - Path { - body: else_body, - output: else_port, - pattern: BranchPattern::UnresolvedVariant { - name: "False".to_string(), - span: expr_span(else_branch), - }, - binding: None, - }, - ], - output: branch_output, - span: span.clone(), - })); - branch_output - } - SurfaceExpr::Match { - scrutinee, - arms, - span, - } => { - // Lower scrutinee and all arm bodies FIRST. `alloc_node_id` - // comes after so the Branch's id matches `nodes.len()` - // at push time (arm lowering pushes its own nodes). Same - // ordering pattern as the `If` arm above. - let scrutinee_port = lower_expr(scrutinee, dag, scope, callable_scope, symbols, None); - let mut lowered_arms: Vec = Vec::with_capacity(arms.len()); - for arm in arms { - let mut arm_scope = scope.clone(); - let (pattern_name, pattern_span, binding, arm_error) = match &arm.pattern { - SurfacePattern::BareVariant { name, span } => { - (name.clone(), span.clone(), None, None) - } - SurfacePattern::VariantWith { - name, - binding, - span, - } => { - let payload_binding = lower_payload_binding(dag, binding); - arm_scope.insert( - payload_binding.binding_name.clone(), - payload_binding.payload_port, - ); - (name.clone(), span.clone(), Some(payload_binding), None) - } - SurfacePattern::VariantFields { name, fields, span } => { - let payload_binding_name = - synthetic_pattern_payload_binding_name(fields, span); - let payload_binding = lower_payload_binding(dag, &payload_binding_name); - // Track names INTRODUCED by THIS pattern only, not the - // full outer scope. `arm_scope` was cloned from the - // enclosing scope at line 5057, so any outer binding - // (`let x = ...; match y { Foo { x } => ... }`) would - // otherwise fire a spurious duplicate-binding - // diagnostic. Pattern-local duplicates - // (`Foo { x: a, y: a }`) are still rejected. Match-arm - // shadowing of outer names then works the same way - // `VariantWith` already does (overwrite on insert). - let mut introduced: std::collections::HashSet = - std::collections::HashSet::new(); - let mut duplicate_binding = None; - for field in fields { - if !introduced.insert(field.binding.clone()) { - duplicate_binding = - Some(duplicate_payload_pattern_binding_diagnostic( - name, - &field.binding, - &field.span, - )); - break; - } - let projection = lower_field_projection_from_port( - dag, - payload_binding.payload_port, - &field.name, - &field.span, - ); - arm_scope.insert(field.binding.clone(), projection); - } - ( - name.clone(), - span.clone(), - Some(payload_binding), - duplicate_binding, - ) - } - }; - let arm_output_port = match arm_error { - Some(diag) => unresolved_port(dag, diag), - None => lower_expr( - &arm.body, - dag, - &arm_scope, - callable_scope, - symbols, - expected_decl, - ), - }; - let body_node = producer_of(dag, arm_output_port); - lowered_arms.push(LoweredMatchArm { - output: arm_output_port, - body_node, - pattern_name, - pattern_span, - binding, - }); - } - let branch_id = dag.alloc_node_id(); - let branch_output = dag.alloc_port(Some(branch_id)); - let paths: Vec = lowered_arms - .into_iter() - .map(|arm| Path { - body: arm.body_node.unwrap_or(branch_id), - output: arm.output, - pattern: BranchPattern::UnresolvedVariant { - name: arm.pattern_name, - span: arm.pattern_span, - }, - binding: arm.binding, - }) - .collect(); - dag.push_node(Behavior::Branch(BranchNode { - id: branch_id, - input: scrutinee_port, - paths, - output: branch_output, - span: span.clone(), - })); - branch_output - } - SurfaceExpr::Record { fields, span } => lower_record_literal_expr( - fields, - span, - dag, - scope, - callable_scope, - symbols, - expected_decl, - ), - SurfaceExpr::List { elements, span } => lower_list_literal_expr( - elements, - span, - dag, - scope, - callable_scope, - symbols, - expected_decl, - ), - SurfaceExpr::Path { - segments, - segment_spans, - span, - } => lower_field_path_expr(segments, segment_spans, span, dag, scope, symbols), - } -} - -fn lower_constructor_invocation( - dag: &mut Dag, - target: DeclarationId, - inputs: Vec, - span: SourceSpan, -) -> PortId { - let node_id = dag.alloc_node_id(); - let output = dag.alloc_port(Some(node_id)); - dag.push_node(Behavior::Transform(TransformNode { - id: node_id, - target: TransformTarget::Callable(target), - inputs, - output, - span, - })); - output -} - -fn direct_invocation_input_decls( - dag: &Dag, - current: DeclarationId, - depth: usize, -) -> Option> { - if depth >= 32 { - return None; - } - match &dag.declaration(current).connective { - TypeConnective::Arrow { inputs, .. } => Some(inputs.clone()), - TypeConnective::Conj { children } => Some(children.iter().map(|child| child.ty).collect()), - TypeConnective::Instantiation { template, .. } => { - direct_invocation_input_decls(dag, *template, depth + 1) - } - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => { - direct_invocation_input_decls(dag, *next, depth + 1) - } - TypeConnective::Atom(AtomPayload::UnresolvedIdentifier(_)) - | TypeConnective::Atom(AtomPayload::TypeParam(_)) - | TypeConnective::Atom(AtomPayload::Literal(_)) - | TypeConnective::Disj { .. } - | TypeConnective::Cardinality { .. } => None, - } -} - -fn lower_record_literal_expr( - fields: &[crate::parse::SurfaceRecordField], - span: &SourceSpan, - dag: &mut Dag, - scope: &HashMap, - callable_scope: &CallableScope, - symbols: &HashMap, - expected_decl: Option, -) -> PortId { - let Some(target_decl) = expected_decl else { - return unresolved_port( - dag, - Diagnostic::ResolveError { - name: "record literals require an expected record type at this position" - .to_string(), - span: span.clone(), - fixes: Vec::new(), - }, - ); - }; - let Some(conj_id) = walk_to_conj_decl(dag, target_decl) else { - return unresolved_port( - dag, - Diagnostic::ResolveError { - name: "record literal does not have an expected record type at this position" - .to_string(), - span: span.clone(), - fixes: Vec::new(), - }, - ); - }; - let expected_fields: Vec<(String, DeclarationId)> = match &dag.declaration(conj_id).connective { - TypeConnective::Conj { children } => children - .iter() - .map(|child| (child.label.clone(), child.ty)) - .collect(), - _ => unreachable!("walk_to_conj_decl returned non-Conj"), - }; - for field in fields { - if !expected_fields - .iter() - .any(|(label, _)| *label == field.name) - { - return unresolved_port( - dag, - Diagnostic::ResolveError { - name: format!( - "record literal has field `{}` but the expected type has no such field", - field.name - ), - span: field.span.clone(), - fixes: Vec::new(), - }, - ); - } - } - let mut inputs = Vec::with_capacity(expected_fields.len()); - for (label, field_ty) in expected_fields { - let Some(field) = fields.iter().find(|field| field.name == label) else { - return unresolved_port( - dag, - Diagnostic::ResolveError { - name: format!( - "record literal is missing required field `{label}` for the expected type" - ), - span: span.clone(), - fixes: Vec::new(), - }, - ); - }; - inputs.push(lower_expr( - &field.value, - dag, - scope, - callable_scope, - symbols, - Some(field_ty), - )); - } - lower_constructor_invocation(dag, target_decl, inputs, span.clone()) -} - -fn lower_variant_record_expr( - expr: VariantRecordExprRef<'_>, - dag: &mut Dag, - scope: &HashMap, - callable_scope: &CallableScope, - symbols: &HashMap, - expected_decl: Option, -) -> PortId { - let Some(variant_decl) = resolve_expected_variant_constructor(dag, expected_decl, expr.target) - else { - return unresolved_port( - dag, - Diagnostic::ResolveError { - name: format!( - "named constructor `{}` requires an expected sum type whose variants include `{}`", - expr.target, expr.target - ), - span: expr.span.clone(), - fixes: Vec::new(), - }, - ); - }; - let Some(expected_fields) = variant_payload_fields_for_lowering(dag, variant_decl) else { - return unresolved_port( - dag, - Diagnostic::ResolveError { - name: format!( - "named constructor `{}` does not lower to a record payload shape", - expr.target - ), - span: expr.span.clone(), - fixes: Vec::new(), - }, - ); - }; - for field in expr.fields { - if !expected_fields - .iter() - .any(|(label, _)| label == &field.name) - { - return unresolved_port( - dag, - Diagnostic::ResolveError { - name: format!( - "named constructor `{}` has field `{}` but the payload has no such field", - expr.target, field.name - ), - span: field.span.clone(), - fixes: Vec::new(), - }, - ); - } - } - let mut inputs = Vec::with_capacity(expected_fields.len()); - for (label, field_ty) in expected_fields { - let Some(field) = expr.fields.iter().find(|field| field.name == label) else { - return unresolved_port( - dag, - Diagnostic::ResolveError { - name: format!( - "named constructor `{}` is missing required payload field `{label}`", - expr.target - ), - span: expr.span.clone(), - fixes: Vec::new(), - }, - ); - }; - inputs.push(lower_expr( - &field.value, - dag, - scope, - callable_scope, - symbols, - Some(field_ty), - )); - } - lower_constructor_invocation(dag, variant_decl, inputs, expr.span.clone()) -} - -fn lower_list_literal_expr( - elements: &[SurfaceExpr], - span: &SourceSpan, - dag: &mut Dag, - scope: &HashMap, - callable_scope: &CallableScope, - symbols: &HashMap, - expected_decl: Option, -) -> PortId { - let empty_decl = match symbols.get("empty").copied() { - Some(id) => id, - None => { - return unresolved_port( - dag, - Diagnostic::ResolveError { - name: "std `empty` constructor is unavailable while lowering a list literal" - .to_string(), - span: span.clone(), - fixes: Vec::new(), - }, - ); - } - }; - let singleton_decl = match symbols.get("singleton").copied() { - Some(id) => id, - None => { - return unresolved_port( - dag, - Diagnostic::ResolveError { - name: - "std `singleton` constructor is unavailable while lowering a list literal" - .to_string(), - span: span.clone(), - fixes: Vec::new(), - }, - ); - } - }; - let cons_decl = match symbols.get("cons").copied() { - Some(id) => id, - None => { - return unresolved_port( - dag, - Diagnostic::ResolveError { - name: "std `cons` constructor is unavailable while lowering a list literal" - .to_string(), - span: span.clone(), - fixes: Vec::new(), - }, - ); - } - }; - - if elements.is_empty() { - return lower_constructor_invocation(dag, empty_decl, Vec::new(), span.clone()); - } - - let element_expected = expected_decl.and_then(|decl| list_element_type(dag, decl)); - let mut element_ports: Vec = elements - .iter() - .map(|element| { - lower_expr( - element, - dag, - scope, - callable_scope, - symbols, - element_expected, - ) - }) - .collect(); - let last = element_ports - .pop() - .expect("non-empty list literal has a last element"); - let mut current = lower_constructor_invocation( - dag, - singleton_decl, - vec![last], - expr_span(elements.last().expect("non-empty")), - ); - while let Some(head) = element_ports.pop() { - current = lower_constructor_invocation(dag, cons_decl, vec![head, current], span.clone()); - } - current -} - -fn resolve_expected_variant_constructor( - dag: &mut Dag, - expected_decl: Option, - variant_name: &str, -) -> Option { - let expected_decl = expected_decl?; - let expected_span = dag.declaration(expected_decl).span.clone(); - let disj_id = walk_to_disj_decl(dag, expected_decl)?; - let variant_decl = match &dag.declaration(disj_id).connective { - TypeConnective::Disj { variants } => variants - .iter() - .find(|variant| variant.label == variant_name) - .map(|variant| variant.ty)?, - _ => unreachable!("walk_to_disj_decl returned non-Disj"), - }; - let instantiation_arguments = match &dag.declaration(expected_decl).connective { - TypeConnective::Instantiation { arguments, .. } if !arguments.is_empty() => { - Some(arguments.clone()) - } - _ => None, - }; - match instantiation_arguments { - Some(arguments) => { - let instantiation_id = dag.alloc_declaration_id(); - dag.push_declaration(Declaration { - id: instantiation_id, - name: None, - connective: TypeConnective::Instantiation { - template: variant_decl, - arguments, - }, - type_params: Vec::new(), - meta_tag: None, - specialization_parent: None, - inhabits: None, - value_body: None, - refinement: None, - span: expected_span, - }); - Some(instantiation_id) - } - _ => Some(variant_decl), - } -} - -fn producer_of(dag: &Dag, port: PortId) -> Option { - dag.port(port).produced_by -} - -fn is_recursive(expr: &SurfaceExpr, self_name: &str) -> bool { - match expr { - SurfaceExpr::Literal { .. } - | SurfaceExpr::Var { .. } - | SurfaceExpr::Path { .. } - | SurfaceExpr::List { .. } => false, - SurfaceExpr::Call { target, args, .. } => { - if target == self_name { - return true; - } - args.iter().any(|a| is_recursive(a, self_name)) - } - SurfaceExpr::VariantRecord { fields, .. } => fields - .iter() - .any(|field| is_recursive(&field.value, self_name)), - SurfaceExpr::Operator { args, .. } => args.iter().any(|a| is_recursive(a, self_name)), - SurfaceExpr::If { - cond, - then_branch, - else_branch, - .. - } => { - is_recursive(cond, self_name) - || is_recursive(then_branch, self_name) - || is_recursive(else_branch, self_name) - } - SurfaceExpr::Match { - scrutinee, arms, .. - } => { - is_recursive(scrutinee, self_name) - || arms.iter().any(|arm| is_recursive(&arm.body, self_name)) - } - SurfaceExpr::Lambda { body, .. } => is_recursive(body, self_name), - SurfaceExpr::Record { fields, .. } => { - fields.iter().any(|f| is_recursive(&f.value, self_name)) - } - } -} - -/// Partial termination analysis: every recursive self-call's first argument -/// must be `first_param - `. The surface shape is now -/// `SurfaceExpr::Operator { op: ArithmeticOp::Sub, args: [Var(first_param), -/// Literal(Int(k))] }` — the operator dispatch is committed at parse -/// time via `OperatorKind::Arithmetic(ArithmeticOp::Sub)`, so this check -/// is structural rather than a string match against `target == "-"`. -/// -/// **On the remaining string comparisons** (`target == self_name`, -/// `name == first_param`): these are *not* name-based dispatch. Both -/// sides are parser-stage identifier strings from a single parse tree -/// reaching `lower_fn_item_expr_body` together — no declaration table -/// lookup, no cross-module symbol resolution. `self_name` is the fn's -/// declared name and `first_param` is its parameter name, both -/// captured from `SurfaceItem::Fn` immediately above on the call -/// stack. The strings on the other side (`SurfaceExpr::Call.target`, -/// `SurfaceExpr::Var.name`) are raw parser tokens from the body of -/// that same fn. -/// -/// The alternative shape — comparing typed `NodeId`/`PortId` edges — -/// isn't available at M1(2.7) because the `BindNode` that owns this -/// function and its param ports doesn't exist yet: this analysis runs -/// *inside* `lower_fn_item_expr_body`, before the Bind is pushed onto -/// the node graph. A future post-lowering `DescentEvidence` lens -/// would walk the emitted Loop/Transform graph by typed id, but -/// that's a substrate extension, not a bridge to dissolve here. -#[derive(Debug, Clone, Default)] -struct StructuralBindingInfo { - whole_payload_recursive: bool, - recursive_fields: HashSet, -} - -fn bind_named_pattern_fields( - info: &StructuralBindingInfo, - fields: &[SurfacePatternField], - out: &mut HashMap, -) { - for field in fields { - let mut binding_info = StructuralBindingInfo::default(); - if info.recursive_fields.contains(&field.name) { - binding_info.whole_payload_recursive = true; - } - out.insert(field.binding.clone(), binding_info); - } -} - -fn descent_provable( - expr: &SurfaceExpr, - dag: &Dag, - first_param_decl: DeclarationId, - self_name: &str, - first_param: &str, - bindings: &HashMap, -) -> bool { - match expr { - SurfaceExpr::Literal { .. } | SurfaceExpr::Var { .. } | SurfaceExpr::Path { .. } => true, - SurfaceExpr::Call { target, args, .. } => { - if target == self_name { - match args.first() { - None => false, - Some(first_arg) => { - if !is_strictly_smaller(first_arg, first_param, bindings) { - return false; - } - args.iter().skip(1).all(|a| { - descent_provable( - a, - dag, - first_param_decl, - self_name, - first_param, - bindings, - ) - }) - } - } - } else { - args.iter().all(|a| { - descent_provable(a, dag, first_param_decl, self_name, first_param, bindings) - }) - } - } - SurfaceExpr::VariantRecord { fields, .. } => fields.iter().all(|field| { - descent_provable( - &field.value, - dag, - first_param_decl, - self_name, - first_param, - bindings, - ) - }), - SurfaceExpr::Operator { args, .. } => args - .iter() - .all(|a| descent_provable(a, dag, first_param_decl, self_name, first_param, bindings)), - SurfaceExpr::If { - cond, - then_branch, - else_branch, - .. - } => { - descent_provable( - cond, - dag, - first_param_decl, - self_name, - first_param, - bindings, - ) && descent_provable( - then_branch, - dag, - first_param_decl, - self_name, - first_param, - bindings, - ) && descent_provable( - else_branch, - dag, - first_param_decl, - self_name, - first_param, - bindings, - ) - } - SurfaceExpr::Match { - scrutinee, arms, .. - } => { - if !descent_provable( - scrutinee, - dag, - first_param_decl, - self_name, - first_param, - bindings, - ) { - return false; - } - - let scrutinee_is_first_param = matches!( - scrutinee.as_ref(), - SurfaceExpr::Var { name, .. } if name == first_param - ); - arms.iter().all(|arm| { - let mut arm_bindings = bindings.clone(); - if scrutinee_is_first_param { - match &arm.pattern { - SurfacePattern::VariantWith { name, binding, .. } => { - if let Some(info) = - structural_binding_info_for_variant(dag, first_param_decl, name) - { - arm_bindings.insert(binding.clone(), info); - } - } - SurfacePattern::VariantFields { name, fields, .. } => { - if let Some(info) = - structural_binding_info_for_variant(dag, first_param_decl, name) - { - bind_named_pattern_fields(&info, fields, &mut arm_bindings); - } - } - SurfacePattern::BareVariant { .. } => {} - } - } - descent_provable( - &arm.body, - dag, - first_param_decl, - self_name, - first_param, - &arm_bindings, - ) - }) - } - SurfaceExpr::Lambda { body, .. } => descent_provable( - body, - dag, - first_param_decl, - self_name, - first_param, - bindings, - ), - SurfaceExpr::Record { fields, .. } => fields.iter().all(|f| { - descent_provable( - &f.value, - dag, - first_param_decl, - self_name, - first_param, - bindings, - ) - }), - SurfaceExpr::List { elements, .. } => elements.iter().all(|element| { - descent_provable( - element, - dag, - first_param_decl, - self_name, - first_param, - bindings, - ) - }), - } -} - -fn is_strictly_smaller( - expr: &SurfaceExpr, - first_param: &str, - bindings: &HashMap, -) -> bool { - if is_structurally_smaller(expr, bindings) { - return true; - } - let SurfaceExpr::Operator { op, args, .. } = expr else { - return false; - }; - // Structural check — no string match. Subtract is one of the four - // arithmetic variants. - if !matches!(op, OperatorKind::Arithmetic(ArithmeticOp::Sub)) || args.len() != 2 { - return false; - } - let lhs_is_param = matches!( - &args[0], - SurfaceExpr::Var { name, .. } if name == first_param - ); - let rhs_is_positive = matches!( - &args[1], - SurfaceExpr::Literal { - value: SurfaceLiteral::Int(v), .. - } if *v > 0 - ); - lhs_is_param && rhs_is_positive -} - -fn is_structurally_smaller( - expr: &SurfaceExpr, - bindings: &HashMap, -) -> bool { - match expr { - SurfaceExpr::Var { name, .. } => bindings - .get(name) - .is_some_and(|info| info.whole_payload_recursive), - SurfaceExpr::Path { segments, .. } if segments.len() == 2 => bindings - .get(&segments[0]) - .is_some_and(|info| info.recursive_fields.contains(&segments[1])), - _ => false, - } -} - -fn structural_binding_info_for_variant( - dag: &Dag, - first_param_decl: DeclarationId, - variant_name: &str, -) -> Option { - let disj_id = walk_to_disj_decl(dag, first_param_decl)?; - let variant_decl_id = match &dag.declaration(disj_id).connective { - TypeConnective::Disj { variants } => variants - .iter() - .find(|variant| variant.label == variant_name) - .map(|variant| variant.ty)?, - _ => unreachable!("walk_to_disj_decl returned non-Disj"), - }; - let TypeConnective::Conj { children } = &dag.declaration(variant_decl_id).connective else { - return None; - }; - - let mut info = StructuralBindingInfo::default(); - if children.len() == 1 - && children[0].label.as_str() == "_0" - && recursive_decl_equivalent(dag, children[0].ty, first_param_decl) - { - info.whole_payload_recursive = true; - } - for child in children { - if recursive_decl_equivalent(dag, child.ty, first_param_decl) { - info.recursive_fields.insert(child.label.clone()); - } - } - Some(info) -} - -fn recursive_decl_equivalent(dag: &Dag, lhs: DeclarationId, rhs: DeclarationId) -> bool { - canonical_decl_for_descent(dag, lhs) == canonical_decl_for_descent(dag, rhs) -} - -fn canonical_decl_for_descent(dag: &Dag, start: DeclarationId) -> DeclarationId { - let mut current = start; - for _ in 0..32 { - match &dag.declaration(current).connective { - TypeConnective::Instantiation { template, .. } => current = *template, - TypeConnective::Atom(AtomPayload::ResolvedByStructure(next)) - | TypeConnective::Atom(AtomPayload::ResolvedByName(next)) => current = *next, - _ => return current, - } - } - current -} - -#[derive(Clone, Copy)] -struct FunctionSurfaceInfo<'a> { - name: &'a str, - decl_id: DeclarationId, - params: &'a [SurfaceParam], - body: &'a SurfaceExpr, -} - -/// `is_first` must parallel `items` (from [`collect_symbols`]). Only -/// first-authority surface items participate — same filter as -/// [`lower_bodies_phase`]'s body lowering loop — so duplicate `fn` -/// declarations cannot overwrite the mutual-recursion call graph with a -/// later duplicate's body. -fn compute_mutually_recursive( - items: &[SurfaceItem], - dag: &Dag, - symbols: &HashMap, - is_first: &[bool], -) -> MutualRecursionPlans { - debug_assert_eq!(items.len(), is_first.len()); - let mut order: Vec = Vec::new(); - let mut fn_infos: HashMap> = HashMap::new(); - let mut function_symbols: HashMap = HashMap::new(); - for (idx, item) in items.iter().enumerate() { - if !is_first[idx] { - continue; - } - if let SurfaceItem::Fn { - name, params, body, .. - } = item - { - let Some(&decl_id) = symbols.get(name) else { - continue; - }; - order.push(decl_id); - fn_infos.insert( - decl_id, - FunctionSurfaceInfo { - name, - decl_id, - params, - body, - }, - ); - function_symbols.insert(name.clone(), decl_id); - } - } - - let order_index: HashMap = order - .iter() - .enumerate() - .map(|(idx, decl_id)| (*decl_id, idx)) - .collect(); - let mut calls: HashMap> = HashMap::new(); - for decl_id in &order { - let Some(info) = fn_infos.get(decl_id) else { - continue; - }; - let mut callees = HashSet::new(); - let mut shadowed: HashSet = - info.params.iter().map(|param| param.name.clone()).collect(); - collect_recursive_callees(info.body, &function_symbols, &mut shadowed, &mut callees); - let mut sorted_callees: Vec = callees.into_iter().collect(); - sorted_callees.sort_by_key(|callee| order_index.get(callee).copied().unwrap_or(usize::MAX)); - calls.insert(*decl_id, sorted_callees); - } - - let mut plans = MutualRecursionPlans::default(); - for members in strongly_connected_components(&order, &calls) - .into_iter() - .filter(|component| component.len() > 1) - { - if let Some(per_member_positions) = - find_cluster_slot_assignment(&members, dag, &fn_infos, &function_symbols, &calls) - { - let member_names: Vec = members - .iter() - .filter_map(|member| fn_infos.get(member).map(|info| info.name.to_string())) - .collect(); - let member_set: HashSet = members.iter().copied().collect(); - let recursive_edges = members - .iter() - .flat_map(|caller| { - calls - .get(caller) - .into_iter() - .flatten() - .copied() - .filter(|callee| member_set.contains(callee)) - .map(|callee| RecursiveEdge { - caller: *caller, - callee, - }) - }) - .collect(); - let cluster_index = plans.clusters.len(); - for member in &members { - plans.by_member.insert(*member, cluster_index); - } - plans.clusters.push(MutualClusterShape { - members, - member_names, - per_member_positions, - recursive_edges, - }); - } else { - let invalid = InvalidMutualCluster { - members: members - .iter() - .filter_map(|member| fn_infos.get(member).map(|info| info.name.to_string())) - .collect(), - }; - for member in members { - plans.invalid_by_member.insert(member, invalid.clone()); - } - } - } - plans -} - -fn strongly_connected_components( - order: &[DeclarationId], - calls: &HashMap>, -) -> Vec> { - let order_index: HashMap = order - .iter() - .enumerate() - .map(|(idx, decl_id)| (*decl_id, idx)) - .collect(); - let mut visited: HashSet = HashSet::new(); - let mut finish_order: Vec = Vec::new(); - - fn dfs( - node: DeclarationId, - calls: &HashMap>, - visited: &mut HashSet, - finish_order: &mut Vec, - ) { - if !visited.insert(node) { - return; - } - if let Some(callees) = calls.get(&node) { - for callee in callees { - dfs(*callee, calls, visited, finish_order); - } - } - finish_order.push(node); - } - - for decl_id in order { - dfs(*decl_id, calls, &mut visited, &mut finish_order); - } - - let mut reverse: HashMap> = - order.iter().map(|decl_id| (*decl_id, Vec::new())).collect(); - for (caller, callees) in calls { - for callee in callees { - reverse.entry(*callee).or_default().push(*caller); - } - } - - fn dfs_reverse( - node: DeclarationId, - reverse: &HashMap>, - assigned: &mut HashSet, - component: &mut Vec, - ) { - if !assigned.insert(node) { - return; - } - component.push(node); - if let Some(callers) = reverse.get(&node) { - for caller in callers { - dfs_reverse(*caller, reverse, assigned, component); - } - } - } - - let mut assigned: HashSet = HashSet::new(); - let mut components: Vec> = Vec::new(); - for decl_id in finish_order.into_iter().rev() { - if assigned.contains(&decl_id) { - continue; - } - let mut component = Vec::new(); - dfs_reverse(decl_id, &reverse, &mut assigned, &mut component); - component.sort_by_key(|member| order_index.get(member).copied().unwrap_or(usize::MAX)); - components.push(component); - } - components.sort_by_key(|component| { - component - .first() - .and_then(|member| order_index.get(member)) - .copied() - .unwrap_or(usize::MAX) - }); - components -} - -fn find_cluster_slot_assignment( - members: &[DeclarationId], - dag: &Dag, - fn_infos: &HashMap>, - function_symbols: &HashMap, - calls: &HashMap>, -) -> Option> { - if members.iter().any(|member| { - fn_infos - .get(member) - .map(|info| info.params.is_empty()) - .unwrap_or(true) - }) { - return None; - } - - fn search( - member_index: usize, - members: &[DeclarationId], - dag: &Dag, - fn_infos: &HashMap>, - function_symbols: &HashMap, - calls: &HashMap>, - assignment: &mut HashMap, - ) -> Option> { - if member_index == members.len() { - if validate_cluster_slot_assignment( - members, - dag, - fn_infos, - function_symbols, - calls, - assignment, - ) { - return Some(assignment.clone()); - } - return None; - } - - let member = &members[member_index]; - let info = fn_infos.get(member)?; - for slot in 0..info.params.len() { - assignment.insert(*member, slot); - if let Some(found) = search( - member_index + 1, - members, - dag, - fn_infos, - function_symbols, - calls, - assignment, - ) { - return Some(found); - } - } - assignment.remove(member); - None - } - - search( - 0, - members, - dag, - fn_infos, - function_symbols, - calls, - &mut HashMap::new(), - ) -} - -fn validate_cluster_slot_assignment( - members: &[DeclarationId], - dag: &Dag, - fn_infos: &HashMap>, - function_symbols: &HashMap, - calls: &HashMap>, - assignment: &HashMap, -) -> bool { - let cluster_members: HashSet = members.iter().copied().collect(); - members.iter().all(|member| { - let Some(info) = fn_infos.get(member) else { - return false; - }; - let Some(&slot) = assignment.get(member) else { - return false; - }; - let TypeConnective::Arrow { inputs, .. } = &dag.declaration(info.decl_id).connective else { - return false; - }; - let Some(param_decl) = inputs.get(slot).copied() else { - return false; - }; - let Some(param) = info.params.get(slot) else { - return false; - }; - let recursive_callees: HashSet = calls - .get(member) - .into_iter() - .flatten() - .copied() - .filter(|callee| cluster_members.contains(callee)) - .collect(); - let initial_shadowed: HashSet = info - .params - .iter() - .map(|surface_param| surface_param.name.clone()) - .collect(); - ClusterDescentChecker { - dag, - current_param_decl: param_decl, - current_param: ¶m.name, - recursive_callees: &recursive_callees, - assignment, - function_symbols, - } - .expr(info.body, &HashMap::new(), &initial_shadowed) - }) -} - -struct ClusterDescentChecker<'a> { - dag: &'a Dag, - current_param_decl: DeclarationId, - current_param: &'a str, - recursive_callees: &'a HashSet, - assignment: &'a HashMap, - function_symbols: &'a HashMap, -} - -impl ClusterDescentChecker<'_> { - fn expr( - &self, - expr: &SurfaceExpr, - bindings: &HashMap, - shadowed: &HashSet, - ) -> bool { - match expr { - SurfaceExpr::Literal { .. } | SurfaceExpr::Var { .. } | SurfaceExpr::Path { .. } => { - true - } - SurfaceExpr::Call { target, args, .. } => { - if !shadowed.contains(target) { - if let Some(&callee_decl) = self.function_symbols.get(target) { - if self.recursive_callees.contains(&callee_decl) { - let Some(&callee_slot) = self.assignment.get(&callee_decl) else { - return false; - }; - let Some(callee_arg) = args.get(callee_slot) else { - return false; - }; - if !is_strictly_smaller(callee_arg, self.current_param, bindings) { - return false; - } - } - } - } - args.iter().all(|arg| self.expr(arg, bindings, shadowed)) - } - SurfaceExpr::VariantRecord { fields, .. } => fields - .iter() - .all(|field| self.expr(&field.value, bindings, shadowed)), - SurfaceExpr::Operator { args, .. } => { - args.iter().all(|arg| self.expr(arg, bindings, shadowed)) - } - SurfaceExpr::If { - cond, - then_branch, - else_branch, - .. - } => { - self.expr(cond, bindings, shadowed) - && self.expr(then_branch, bindings, shadowed) - && self.expr(else_branch, bindings, shadowed) - } - SurfaceExpr::Match { - scrutinee, arms, .. - } => { - if !self.expr(scrutinee, bindings, shadowed) { - return false; - } - - let scrutinee_is_current_param = matches!( - scrutinee.as_ref(), - SurfaceExpr::Var { name, .. } if name == self.current_param - ); - arms.iter().all(|arm| { - let mut arm_bindings = bindings.clone(); - let mut arm_shadowed = shadowed.clone(); - for binding in pattern_binding_names(&arm.pattern) { - arm_shadowed.insert(binding.to_string()); - } - if scrutinee_is_current_param { - match &arm.pattern { - SurfacePattern::VariantWith { name, binding, .. } => { - if let Some(info) = structural_binding_info_for_variant( - self.dag, - self.current_param_decl, - name, - ) { - arm_bindings.insert(binding.clone(), info); - } - } - SurfacePattern::VariantFields { name, fields, .. } => { - if let Some(info) = structural_binding_info_for_variant( - self.dag, - self.current_param_decl, - name, - ) { - bind_named_pattern_fields(&info, fields, &mut arm_bindings); - } - } - SurfacePattern::BareVariant { .. } => {} - } - } - self.expr(&arm.body, &arm_bindings, &arm_shadowed) - }) - } - SurfaceExpr::Lambda { params, body, .. } => { - let mut lambda_shadowed = shadowed.clone(); - lambda_shadowed.extend(params.iter().cloned()); - self.expr(body, bindings, &lambda_shadowed) - } - SurfaceExpr::Record { fields, .. } => fields - .iter() - .all(|field| self.expr(&field.value, bindings, shadowed)), - SurfaceExpr::List { elements, .. } => elements - .iter() - .all(|element| self.expr(element, bindings, shadowed)), - } - } -} - -fn collect_recursive_callees( - expr: &SurfaceExpr, - function_symbols: &HashMap, - shadowed: &mut HashSet, - out: &mut HashSet, -) { - match expr { - SurfaceExpr::Literal { .. } | SurfaceExpr::Var { .. } | SurfaceExpr::Path { .. } => {} - SurfaceExpr::Call { target, args, .. } => { - if !shadowed.contains(target) { - if let Some(&callee_decl) = function_symbols.get(target) { - out.insert(callee_decl); - } - } - for a in args { - collect_recursive_callees(a, function_symbols, shadowed, out); - } - } - SurfaceExpr::VariantRecord { fields, .. } => { - for field in fields { - collect_recursive_callees(&field.value, function_symbols, shadowed, out); - } - } - SurfaceExpr::Operator { args, .. } => { - for a in args { - collect_recursive_callees(a, function_symbols, shadowed, out); - } - } - SurfaceExpr::If { - cond, - then_branch, - else_branch, - .. - } => { - collect_recursive_callees(cond, function_symbols, shadowed, out); - collect_recursive_callees(then_branch, function_symbols, shadowed, out); - collect_recursive_callees(else_branch, function_symbols, shadowed, out); - } - SurfaceExpr::Match { - scrutinee, arms, .. - } => { - collect_recursive_callees(scrutinee, function_symbols, shadowed, out); - for arm in arms { - let mut arm_shadowed = shadowed.clone(); - for binding in pattern_binding_names(&arm.pattern) { - arm_shadowed.insert(binding.to_string()); - } - collect_recursive_callees(&arm.body, function_symbols, &mut arm_shadowed, out); - } - } - SurfaceExpr::Lambda { params, body, .. } => { - let mut lambda_shadowed = shadowed.clone(); - lambda_shadowed.extend(params.iter().cloned()); - collect_recursive_callees(body, function_symbols, &mut lambda_shadowed, out) - } - SurfaceExpr::Record { fields, .. } => { - for f in fields { - collect_recursive_callees(&f.value, function_symbols, shadowed, out); - } - } - SurfaceExpr::List { elements, .. } => { - for element in elements { - collect_recursive_callees(element, function_symbols, shadowed, out); - } - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::dag::Declaration; - use crate::diagnostics::SourceSpan; - use std::collections::HashMap; - - fn test_span() -> SourceSpan { - SourceSpan::new("lower_decl_ref_test.v3", 0, 0) - } - - fn push_test_declaration( - dag: &mut Dag, - name: &str, - connective: TypeConnective, - meta_tag: Option, - ) -> DeclarationId { - let id = dag.alloc_declaration_id(); - dag.push_declaration(Declaration { - id, - name: Some(name.to_string()), - connective, - type_params: Vec::new(), - meta_tag, - specialization_parent: None, - inhabits: None, - value_body: None, - refinement: None, - span: test_span(), - }); - id - } - - #[test] - fn aliased_expected_type_accepts_same_typed_declaration_reference() { - let mut dag = Dag::new(); - let span = test_span(); - let style_type = push_test_declaration( - &mut dag, - "LocalStyle", - TypeConnective::Conj { - children: Vec::new(), - }, - None, - ); - let aliased_style = push_test_declaration( - &mut dag, - "AliasedLocalStyle", - TypeConnective::Instantiation { - template: style_type, - arguments: Vec::new(), - }, - None, - ); - let style_value = push_test_declaration( - &mut dag, - "local_style_value", - TypeConnective::Instantiation { - template: style_type, - arguments: Vec::new(), - }, - Some(style_type), - ); - let mut symbols = HashMap::new(); - symbols.insert("local_style_value".to_string(), style_value); - - let lowered = lower_structural_field_value( - "local_contract_value", - "style", - &SurfaceExpr::Var { - name: "local_style_value".to_string(), - span: span.clone(), - }, - aliased_style, - &symbols, - &mut dag, - None, - &span, - ); - - assert_eq!( - lowered, - Some(crate::dag::FieldValue::Reference(style_value)), - "alias-wrapped expected types should still accept same-typed declaration refs" - ); - } - - #[test] - fn declaration_ref_resolution_does_not_fall_back_to_global_names() { - let mut dag = Dag::new(); - let global_only = push_test_declaration( - &mut dag, - "global_only", - TypeConnective::Conj { - children: Vec::new(), - }, - None, - ); - - let resolved = resolve_field_value_as_declaration_ref( - &SurfaceExpr::Var { - name: "global_only".to_string(), - span: test_span(), - }, - &HashMap::new(), - &dag, - ); - - assert_eq!( - dag.declaration(global_only).name.as_deref(), - Some("global_only"), - "test setup should create the global declaration" - ); - assert_eq!( - resolved, None, - "declaration-ref lowering must fail closed when the name is not in scope" - ); - } - - #[test] - fn identifier_sweep_marks_name_keyed_resolution_provenance() { - let mut dag = Dag::new(); - let user_start = dag.declarations().len(); - let stub = alloc_identifier_stub(&mut dag, "Int", &test_span()); - - resolve_pending_identifiers_strict(&mut dag, user_start); - - assert!( - matches!( - dag.declaration(stub).connective, - TypeConnective::Atom(AtomPayload::ResolvedByName(_)) - ), - "strict identifier sweep must preserve name-fallback provenance on repaired stubs" - ); - } - - /// Receipt for `Declaration::specialization_parent` on specialized anonymous - /// `Disj` copies: the walker in `emit::rust_target` depends on this edge shape. - #[test] - fn specialize_decl_for_lowering_anonymous_disj_sets_specialization_parent() { - let mut dag = Dag::new(); - let t_param = dag.alloc_declaration_id(); - dag.push_declaration(Declaration { - id: t_param, - name: None, - connective: TypeConnective::Atom(AtomPayload::TypeParam("T".to_string())), - type_params: Vec::new(), - meta_tag: None, - specialization_parent: None, - inhabits: None, - value_body: None, - refinement: None, - span: test_span(), - }); - let int_id = dag.int_shape().expect("bootstrap Int").declaration; - let template_id = push_test_declaration( - &mut dag, - "WithT", - TypeConnective::Disj { - variants: vec![Field { - label: "V".to_string(), - ty: t_param, - }], - }, - None, - ); - let specialized = specialize_decl_for_lowering( - &mut dag, - template_id, - &[TemplateArgument { - parameter: t_param, - value: int_id, - }], - 0, - ); - assert_ne!( - specialized, template_id, - "substituting T into the variant payload should allocate a fresh Disj" - ); - let anon = dag.declaration(specialized); - assert!(anon.name.is_none()); - assert_eq!( - anon.specialization_parent, - Some(template_id), - "Rust emit recovers the template enum via specialization_parent" - ); - let TypeConnective::Disj { variants } = &anon.connective else { - panic!("expected specialized Disj"); - }; - assert_eq!(variants.len(), 1); - assert_eq!(variants[0].ty, int_id); - } -} diff --git a/src/v3/compiler/tests/integration.rs b/src/v3/compiler/tests/integration.rs index dd665fce47b..dfa86c3f382 100644 --- a/src/v3/compiler/tests/integration.rs +++ b/src/v3/compiler/tests/integration.rs @@ -125,8 +125,6 @@ mod sg2_parse_authority_test; mod sg2c1_parse_tables_authority_test; #[path = "integration/sg2c5_soft_keyword_ident_test.rs"] mod sg2c5_soft_keyword_ident_test; -#[path = "integration/sg3_lower_authority_test.rs"] -mod sg3_lower_authority_test; #[path = "integration/sg3_lower_parse_surface_stack_test.rs"] mod sg3_lower_parse_surface_stack_test; #[path = "integration/sg3_surface_reflection_consumer_test.rs"] diff --git a/src/v3/compiler/tests/integration/sg0_census_test.rs b/src/v3/compiler/tests/integration/sg0_census_test.rs index 49d0c2b876b..4c1eb3ed9e7 100644 --- a/src/v3/compiler/tests/integration/sg0_census_test.rs +++ b/src/v3/compiler/tests/integration/sg0_census_test.rs @@ -89,9 +89,11 @@ const CENSUS_ROOT: &str = "src/v3/compiler"; // a bounded SG-0 exception for the merge-blocking Stage 3b receipt, // not a precedent for adding ad hoc integration files. // -// SG-3f-prep (director Option B): `lower.rs` stays on this list — canonical -// lowering remains hand-maintained Rust until `lower.dag` + reflected `Surface*`. -// `regen_lower` + `lower_generated.rs` are prep-only (not `lib.rs` authority). +// T-PB-A-B: `lower.rs` stays on this list — canonical lowering remains +// hand-maintained Rust until `lower.dag` + reflected `Surface*`. The former +// `regen_lower` pass-through scaffold and non-authoritative +// `lower_generated.rs` snapshot were retired so the census no longer preserves +// a generated-looking lower projection that `lib.rs` did not consume. // // Phase 1 Dag builder surface — PR #570 adds one narrow host-side // helper file, `src/dag/builder.rs`, to keep the test-facing graph @@ -144,7 +146,6 @@ const EXPECTED_HAND_AUTHORED_NON_TEST: &[&str] = &[ "src/v3/compiler/build.rs", "src/v3/compiler/src/bin/regen_bootstrap.rs", "src/v3/compiler/src/bin/regen_lens.rs", - "src/v3/compiler/src/bin/regen_lower.rs", "src/v3/compiler/src/bin/regen_parse.rs", "src/v3/compiler/src/bin/regen_parse_tables.rs", "src/v3/compiler/src/bin/regen_tokenize.rs", @@ -237,7 +238,6 @@ const EXPECTED_HAND_AUTHORED_TEST: &[&str] = &[ "src/v3/compiler/tests/integration/sg2_parse_authority_test.rs", "src/v3/compiler/tests/integration/sg2c1_parse_tables_authority_test.rs", "src/v3/compiler/tests/integration/sg2c5_soft_keyword_ident_test.rs", - "src/v3/compiler/tests/integration/sg3_lower_authority_test.rs", "src/v3/compiler/tests/integration/sg3_lower_parse_surface_stack_test.rs", "src/v3/compiler/tests/integration/sg3_surface_reflection_consumer_test.rs", "src/v3/compiler/tests/integration/sg6_hand_authored_census_test.rs", diff --git a/src/v3/compiler/tests/integration/sg3_lower_authority_test.rs b/src/v3/compiler/tests/integration/sg3_lower_authority_test.rs deleted file mode 100644 index 35c71048c36..00000000000 --- a/src/v3/compiler/tests/integration/sg3_lower_authority_test.rs +++ /dev/null @@ -1,44 +0,0 @@ -//! SG-3f-prep: `lower_generated.rs` is the pass-through output of `regen_lower` from -//! canonical `lower.rs` (not imported by `lib.rs`); the snapshot must stay in sync. -//! -//! Hermetic: `regen_lower` is invoked with `--out` under `temp_dir()` so the workspace -//! tree is not rewritten during the test (TESTING.md). - -const CHECKED_IN_GENERATED: &str = include_str!("../../src/lower_generated.rs"); - -#[test] -fn lower_generated_module_matches_regen_snapshot() { - let manifest_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); - let out_path = std::env::temp_dir().join(format!( - "v3_regen_lower_{}_lower_generated.rs", - std::process::id() - )); - let fresh = - std::process::Command::new(std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string())) - .current_dir(&manifest_dir) - .args([ - "run", - "-q", - "-p", - "v3-compiler", - "--bin", - "regen_lower", - "--", - "--out", - ]) - .arg(&out_path) - .output() - .expect("spawn regen_lower"); - assert!( - fresh.status.success(), - "regen_lower failed: {}", - String::from_utf8_lossy(&fresh.stderr) - ); - let regen = std::fs::read_to_string(&out_path).expect("read regenerated lower_generated.rs"); - let _ = std::fs::remove_file(&out_path); - assert_eq!( - CHECKED_IN_GENERATED.trim(), - regen.trim(), - "checked-in lower_generated.rs is stale; run `cargo run -p v3-compiler --bin regen_lower`" - ); -} diff --git a/src/v3/compiler/tests/integration/sg6_hand_authored_census_test.rs b/src/v3/compiler/tests/integration/sg6_hand_authored_census_test.rs index 3208b4b5600..854c886830e 100644 --- a/src/v3/compiler/tests/integration/sg6_hand_authored_census_test.rs +++ b/src/v3/compiler/tests/integration/sg6_hand_authored_census_test.rs @@ -148,10 +148,6 @@ fn sg6_bin_census_is_locked_to_expected_regen_shims() { // tables — not `LensRegistryEntry`-tagged lens declarations), so // for SG-1 it lands as a parallel shim rather than a registry entry. // - // SG-3f-prep receipts `regen_lower.rs`: pass-through from canonical - // `lower.rs` to `lower_generated.rs` (not `lib.rs` authority) until - // `lower.dag` lands (SELF_HOSTING.md §4). - // // Dissolution trigger (SG-2+): unify `regen_tokenize` with the // registry-driven pattern — either extend `regen.dag` to carry a // tokenizer-registry shape that `regen_lens` can dispatch on, or @@ -161,7 +157,6 @@ fn sg6_bin_census_is_locked_to_expected_regen_shims() { let expected: BTreeSet = [ "regen_bootstrap.rs", "regen_lens.rs", - "regen_lower.rs", "regen_parse.rs", // SG-2c-1 grammar-tables prototype: `regen_parse_tables` projects // `src/v3/compiler/parse_tables.dag` into @@ -184,8 +179,7 @@ fn sg6_bin_census_is_locked_to_expected_regen_shims() { assert_eq!( actual, expected, "SG-6 hand-authored bin census changed. The census is \ - `regen_lens` (reads `src/v3/compiler/regen.dag`), `regen_lower` \ - (reads `src/v3/compiler/src/lower.rs`), `regen_parse` \ + `regen_lens` (reads `src/v3/compiler/regen.dag`), `regen_parse` \ (reads `src/v3/std/parse_surface.dag` for Surface carriers), `regen_tokenize` \ (reads `src/v3/compiler/tokenize.dag`), `regen_v3`, and \ `self_host_fixed_point`. Adding a new bin re-introduces a \ From 315eccb7c7bfc1065dbd1f34f724b98063196abe Mon Sep 17 00:00:00 2001 From: Brian Searls Date: Fri, 24 Apr 2026 09:15:40 -0400 Subject: [PATCH 2/2] Lower slow-test exemption ratchet --- scripts/check-test-timeout.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/check-test-timeout.sh b/scripts/check-test-timeout.sh index 905e70f2e94..4d1f73d3c6c 100755 --- a/scripts/check-test-timeout.sh +++ b/scripts/check-test-timeout.sh @@ -46,7 +46,7 @@ # (default scripts/slow-test-exemptions.txt). # TEST_TIMEOUT_MAX_EXEMPTIONS # Ratchet floor for active exemption entries -# (default 43, captured 2026-04-24). Lower this +# (default 42, captured 2026-04-24). Lower this # value in the same PR that removes exemptions. set -euo pipefail @@ -55,7 +55,7 @@ log_file_arg=${1:-} budget_ms=${2:-${TEST_TIMEOUT_MS:-2000}} pkg=${TEST_TIMEOUT_PACKAGE:-v3-compiler} exempt_file=${TEST_TIMEOUT_EXEMPT:-scripts/slow-test-exemptions.txt} -max_exemptions=${TEST_TIMEOUT_MAX_EXEMPTIONS:-43} +max_exemptions=${TEST_TIMEOUT_MAX_EXEMPTIONS:-42} script_dir=$(cd "$(dirname "$0")" && pwd) repo_root=$(cd "$script_dir/.." && pwd)