Phase 2: Typechecker + Borrow Implementation Plan¶
For agentic workers: REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development or superpowers:executing-plans.
Goal: Reject ill-typed programs; attach types to AST; enforce lexical borrow rules and raises effect tracking.
Architecture: li_types owns TypeCtx, unification for int literals, borrow state per scope in Borrowck.
Tech Stack: indexmap, rustc-hash
Depends on: Phase 1
Blocks: Phase 3
Proof gaps (Doc-c): G-vc · G-bnd · G-def · G-math-syn
Type system (v1)¶
| Type | Notes |
|---|---|
int, uint, wrapping_int, float64, bool, unit, string | int default |
array[N, T] | N const usize |
Option[T] | no null |
enum Name | user enums |
object Name | product types |
proc(Args) -> Ret | effect !raises set on decl |
Rules: - Literal 42 → int; suffix 42u → uint - + on two int → int; int + float → error - Index a[i] requires i: int and bounds proof for literal i - while body requires raises Loop or raises IO on enclosing proc - echo requires raises IO
Task 1: Type representation¶
Files: - Create: crates/li_types/src/ty.rs - Create: crates/li_types/src/context.rs
-
enum Type { Int, Uint, WrappingInt, Float64, Bool, Unit, String, Array { len: u64, elem: Box<Type> }, Option(Box<Type>), Enum(EnumId), Object(ObjectId), Proc { .. } }(C++:TypeExpr+typecheck.cpp) -
TypeCtx::define_type,lookup,define_proc(C++:TypecheckStateintypecheck.cpp)
Task 2: Typecheck expressions and stmts¶
Files: - Create: crates/li_types/src/check.rs - Test: crates/li_types/tests/check_fib.rs
-
check_module(&Module) -> Result<TypedModule, Vec<TypeError>>(C++:typecheck_module) - Return typed wrapper nodes or side table
node_id → Type(C++:TypecheckResult+ AST type fields)
Task 3: Borrow checker (lexical)¶
Files: - Create: crates/li_types/src/borrow.rs - Test: crates/li_types/tests/borrow_errors.rs
- Track
Owned | BorrowImm | BorrowMutper local (borrowck.cpp) - Reject: use after move, two
mutborrows, mut while imm borrow live (li-tests/borrow/) - v1: no references in struct fields yet
Task 4: Scientific error fixtures¶
Files: - Create: tests/fixtures/bad_array_index.li - Create: tests/fixtures/bad_numeric_mix.li - Create: tests/fixtures/bad_overflow_mode.li
- Tests assert compile error messages mention dimension / type mismatch (
li-tests/typecheck/bad_*.li)
Task 5: CLI lic check¶
Files: - Modify: crates/lic/src/main.rs
-
lic check file.li— parse + typecheck, exit 1 on errors (compiler/lic/main.cpp)
Phase 2 exit gate¶
-
fib.litypechecks (li-tests/typecheck/fib.li) - All
bad_*.lifail with expected errors - Borrow double-mut test fails cleanly (
li-tests/borrow/double_mut.li)