0%

Section 05: Builtin Apply Mono in Generic Context (BUG-04-114)

Status: Not Started Goal: Close the builtin-Apply-in-generic-body gap. Calls like xs.len() as int inside generic functions emit __cast Apply nodes whose mono instances are registered alongside user-function Apply.

Context: Per §01.3 BUG-04-114 entry, the symptom is unresolved function __cast in apply — missing mono instance?, LLVM module verification fails BEFORE realize phase runs. Distinct from §02 (cross-module), §03 (inherent methods), §04 (complex generic receivers): this is about builtin Apply targets — compiler-internal protocol functions (per arc.md §Protocol Builtins) that appear as Apply callees in ARC IR. The mono pipeline does not register them today because function_sigs / impl_sigs / import_sigs are all user-function tables.

Reference implementations:

  • Lean 4 compiler/IR/Borrow.lean — builtin RC operations registered alongside user-function RC; same dispatch mechanism.
  • Roc crates/compiler/gen_llvm/src/llvm/build.rs — builtin Apply targets enumerated via a static table; mono dispatch consults it as a fourth lookup source.

Depends on: §01 (cite §01.3 BUG-04-114), §02 (collect_mono_functions stable after §02.1), §04 (dedup predicate over compound generic args lands first — both §04 and §05 touch ori_types::check::mod.rs:341-407; §05’s builtin Apply MonoInstance registration consumes the §04 dedup invariant to avoid spurious collisions). Per gemini blind-spot 2026-05-16 §1 — serialized after §04 in the Section Dependency Graph at 00-overview.md.

Intelligence Reconnaissance

Queries planned:

  • scripts/intel-query.sh --human callers "__cast" --repo ori — every site that constructs __cast Apply nodes.
  • scripts/intel-query.sh --human file-symbols "compiler/ori_arc/src/lower/calls" --repo ori — builtin Apply lowering surface (per BUG-04-114 subsystem).
  • scripts/intel-query.sh --human file-symbols "compiler/ori_arc/src/mono" --repo ori — verify whether ori_arc/mono/ exists; bug entry suggests it does.
  • scripts/intel-query.sh --human similar "builtin_apply mono" --repo lean4,roc --limit 3 — cross-language patterns.

Results summary (≤500 chars, recorded 2026-05-14) [ori]: TO BE POPULATED at section start by running the queries above. Scaffold authored 2026-05-14 — queries deferred to execution time when ori_arc/lower/calls + ori_arc/mono surfaces are current.


05.1 Builtin Apply emission inventory in generic bodies

File(s): compiler_repo/compiler/ori_arc/src/lower/calls/, compiler_repo/compiler/ori_ir/src/builtin_constants/protocol/mod.rs

  • Enumerate every protocol builtin variant per arc.md §Protocol Builtins table: Index (__index), Iter (iter), IterNext (__iter_next), IterDrop (ori_iter_drop), CollectSet (__collect_set), plus __cast. Verify the canonical list at ori_ir/src/builtin_constants/protocol/mod.rs.
  • For each builtin, identify the lowering site in ori_arc/src/lower/calls/ that emits the Apply node. Document file:line.
  • Verify which builtins appear inside generic bodies (function bodies whose signature contains generic type params): __cast (via as cast in generic body), __index (via xs[i] in generic body), __iter_next (via iterator chains).
  • Document the gap: builtin Apply targets do not have a FunctionSig registered in function_sigs / impl_sigs / import_sigs, so collect_mono_functions skips them silently.
  • Subsection close-out (05.1) — MANDATORY before §05.2:
    • Inventory documented in section body; gap classification clear.
    • Run /improve-tooling retrospectively on §05.1 (read-only investigation).
    • Run compiler_repo/diagnostics/repo-hygiene.sh --check.

05.2 MonoInstance registration for builtin Apply

File(s): compiler_repo/compiler/ori_llvm/src/monomorphize/mod.rs (add builtin_sigs: parameter), compiler_repo/compiler/ori_arc/src/lower/calls/, compiler_repo/compiler/ori_ir/src/builtin_constants/protocol/mod.rs

Per §01 plan-local invariant MONO-REG (corroborated by codegen-rules.md §AB-6): every Apply / Invoke carrier in realized ARC IR SHALL have a registered MonoFunction.

  • Extend collect_mono_functions to accept builtin_sigs: &[(Name, FunctionSig)] as a fourth lookup source. Follow the §02.1 pattern (fourth lookup table built alongside function_sigs, impl_sigs, import_sigs).
  • In ori_ir::builtin_constants::protocol/mod.rs (canonical builtin-sig list source per §05.1 inventory), expose a builtin_sigs() function returning the canonical (Name, FunctionSig) list for all protocol builtins. Each FunctionSig carries the generic type params for the builtin (e.g., __cast<Src, Dst> has two type params).
  • Wire builtin_sigs() into the call site in compile_common.rs and codegen_pipeline/mod.rs (per §02.2 driver wiring pattern).
  • JIT call-site update — compiler_repo/compiler/ori_llvm/src/evaluator/compile.rs:230 — change the collect_mono_functions call to pass &builtin_sigs (NOT &[]) for the new builtin_sigs parameter. JIT also runs builtin Apply through the same dispatch — cross-module imports stay AOT-only (import_sigs remains &[] at JIT site per §02 contract), but builtin Apply is universal to both backends per canon.md §1 Evaluator parallel-row dual-execution parity. Per gemini blind-spot 2026-05-16 §2 + §00-overview §Extension-Surface Map row §05.
  • In ori_arc/src/lower/calls/{mod.rs,lambda.rs} (verified surface per §05.1 inventory) AND ori_arc/src/lower/collections/mod.rs:320-331 (lower_cast), ensure builtin Apply lowering emits a corresponding MonoInstance for each call site (the typeck-side emission may not produce these — builtin Apply nodes are inserted at ARC lowering, AFTER typeck). If typeck doesn’t see them, the registration MUST happen during ARC lowering before collect_mono_functions runs. Verify the pipeline order and add registration at the correct phase.
  • Subsection close-out (05.2) — MANDATORY before §05.3:
    • cargo test --release -p ori_llvm --test aot generics::test_borrow_list_int_one_borrow_apply_then_return_no_leak passes.
    • _two_borrow_applies_* and _mixed_apply_* variants pass.
    • No regression: cargo test --release -p ori_llvm --test aot whole-suite green.
    • Run /improve-tooling retrospectively on §05.2.
    • Run /sync-claude on §05.2 — collect_mono_functions signature extended again; check compiler.md, llvm.md, arc.md §Protocol Builtins for mention of the four-table dispatch.
    • Run compiler_repo/diagnostics/repo-hygiene.sh --check.

05.3 Matrix tests — builtin Apply in generic context

File(s): compiler_repo/compiler/ori_llvm/tests/aot/generics.rs, compiler_repo/compiler/ori_llvm/tests/aot/builtin_apply_in_generic.rs (new)

Matrix:

BuiltinSource typeTarget typeGeneric contextTest name
__cast (as)intfloatgeneric @id<T>(xs: [T]) bodytest_cast_int_to_float_in_generic
__cast (as)floatintgeneric bodytest_cast_float_to_int_in_generic
__cast (as)intbytegeneric bodytest_cast_int_to_byte_in_generic
__cast (as)str.len() returnintgeneric body (canonical BUG-04-114 repro)test_cast_str_len_in_generic
__index ([i])[T]Tgeneric bodytest_index_list_in_generic
__iter_nextIterator<T>Option<T>generic bodytest_iter_next_in_generic
__collect_setiteratorSet<T>generic bodytest_collect_set_in_generic
  • Author all 7 matrix tests.
  • Negative pin: test_cast_unsupported_type_combo_emits_typeck_errorbool as float (or similar invalid as combo) produces clean E2xxx, not E5001 codegen crash.
  • Subsection close-out (05.3)status: complete:
    • All 7 matrix tests + 1 negative pin pass.
    • timeout 150 diagnostics/dual-exec-verify.sh tests/aot/builtin_apply_in_generic.rs — parity holds.
    • Run /improve-tooling retrospectively on §05.3.
    • Run compiler_repo/diagnostics/repo-hygiene.sh --check.

05.R Third Party Review Findings

Populated by /tpr-review at §05.N.


05.N Completion Checklist

  • All 05.1, 05.2, 05.3 subsections status: complete.
  • /tpr-review clean across reviewer set.
  • /impl-hygiene-review clean after TPR.
  • python -m scripts.plan_corpus check plans/aot-mono-completeness/section-05-builtin-apply-mono.md exit 0.
  • cargo st green for tests/spec/conversions/ + tests/spec/iterators/ + tests/spec/generics/ corpus (builtin Apply targets __cast, __iter_next) — §05 wires builtin_sigs into both AOT (codegen_pipeline/mod.rs:138) and JIT (evaluator/compile.rs:230) call sites, so dual-execution parity requires spec-test coverage on both backends; per overview Mission Success Criteria cargo st regression-guard row + section frontmatter success_criteria row 5.
  • BUG-04-114 tracker entry annotated <!-- delivered-by: plans/aot-mono-completeness/section-05-builtin-apply-mono.md -->.
  • Section frontmatter flipped to status: complete, reviewed: true.