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__castApply 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 whetherori_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 atori_ir/src/builtin_constants/protocol/mod.rs. - For each builtin, identify the lowering site in
ori_arc/src/lower/calls/that emits theApplynode. Document file:line. - Verify which builtins appear inside generic bodies (function bodies whose signature contains generic type params):
__cast(viaascast in generic body),__index(viaxs[i]in generic body),__iter_next(via iterator chains). - Document the gap: builtin Apply targets do not have a
FunctionSigregistered infunction_sigs/impl_sigs/import_sigs, socollect_mono_functionsskips them silently. - Subsection close-out (05.1) — MANDATORY before §05.2:
- Inventory documented in section body; gap classification clear.
- Run
/improve-toolingretrospectively 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_functionsto acceptbuiltin_sigs: &[(Name, FunctionSig)]as a fourth lookup source. Follow the §02.1 pattern (fourth lookup table built alongsidefunction_sigs,impl_sigs,import_sigs). - In
ori_ir::builtin_constants::protocol/mod.rs(canonical builtin-sig list source per §05.1 inventory), expose abuiltin_sigs()function returning the canonical(Name, FunctionSig)list for all protocol builtins. EachFunctionSigcarries the generic type params for the builtin (e.g.,__cast<Src, Dst>has two type params). - Wire
builtin_sigs()into the call site incompile_common.rsandcodegen_pipeline/mod.rs(per §02.2 driver wiring pattern). - JIT call-site update —
compiler_repo/compiler/ori_llvm/src/evaluator/compile.rs:230— change thecollect_mono_functionscall to pass&builtin_sigs(NOT&[]) for the newbuiltin_sigsparameter. JIT also runs builtin Apply through the same dispatch — cross-module imports stay AOT-only (import_sigsremains&[]at JIT site per §02 contract), but builtin Apply is universal to both backends percanon.md §1Evaluator 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) ANDori_arc/src/lower/collections/mod.rs:320-331(lower_cast), ensure builtin Apply lowering emits a correspondingMonoInstancefor 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 beforecollect_mono_functionsruns. 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_leakpasses. -
_two_borrow_applies_*and_mixed_apply_*variants pass. - No regression:
cargo test --release -p ori_llvm --test aotwhole-suite green. - Run
/improve-toolingretrospectively on §05.2. - Run
/sync-claudeon §05.2 —collect_mono_functionssignature extended again; checkcompiler.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:
| Builtin | Source type | Target type | Generic context | Test name |
|---|---|---|---|---|
__cast (as) | int | float | generic @id<T>(xs: [T]) body | test_cast_int_to_float_in_generic |
__cast (as) | float | int | generic body | test_cast_float_to_int_in_generic |
__cast (as) | int | byte | generic body | test_cast_int_to_byte_in_generic |
__cast (as) | str.len() return | int | generic body (canonical BUG-04-114 repro) | test_cast_str_len_in_generic |
__index ([i]) | [T] | T | generic body | test_index_list_in_generic |
__iter_next | Iterator<T> | Option<T> | generic body | test_iter_next_in_generic |
__collect_set | iterator | Set<T> | generic body | test_collect_set_in_generic |
- Author all 7 matrix tests.
- Negative pin:
test_cast_unsupported_type_combo_emits_typeck_error—bool as float(or similar invalidascombo) 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-toolingretrospectively 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-reviewclean across reviewer set. -
/impl-hygiene-reviewclean after TPR. -
python -m scripts.plan_corpus check plans/aot-mono-completeness/section-05-builtin-apply-mono.mdexit 0. -
cargo stgreen fortests/spec/conversions/+tests/spec/iterators/+tests/spec/generics/corpus (builtin Apply targets__cast,__iter_next) — §05 wiresbuiltin_sigsinto 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 Criteriacargo stregression-guard row + section frontmattersuccess_criteriarow 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.