summary_generics-limitations-fixed-2026-10-16.md 2.6 KB

Generics — the two limitations, fixed (2026-10-16)

Branch resolve-check, tree clean. Suite 219/219; fixpoint OK (3,714,376 bytes); corpus 372 OK / 0 crash.

1. Arithmetic (and relations) over a type formal

Sum over T (s : T; s := s + n^.val) was 211 at template parse: an unbound formal is an unresolved alias, and ClassOf maps such an alias to ClPtr (the opaque-type rule), so ArithCheck saw a non-numeric operand.

Fix: an unbound type formal already Resolves to InvalidType; the expression checks simply weren't consulting Resolve. ArithCheck, RelCheck, BoolCheck, OrdCheck, InCheck and EqCheck now skip on Resolve(x) = InvalidType (instead of only x = InvalidType), which is exactly what Assignable already did. So a formal — and, consistently, any unresolved type — is treated as unknown and checks are skipped; instances (where the formal is bound to the actual) are checked normally.

2. Procedure-typed formals in a DEFINITION/IMPLEMENTATION pair

A formal typed by an inline PROCEDURE(...) tripped a false 235 ("implementation does not match definition"): each parse builds a fresh proc type, so the def's and impl's proc type have different TypeIndex and the signature snapshot compared them by index.

Fix: FormalOk now compares two FProc formals structurally (same result and parameter types, via ProcTypesOk) — the same treatment open-array formals already had (fresh descriptors never share an index).

3. Bonus: an imported procedure used as a value

Verifying #2 surfaced a third bug: f := Visitor.Add (an imported proc, assigned to a proc-typed variable) stored 0 — LowerProcAddr looked the name up in the proc table under the qualified/alias key. Fix: fall back to SymTab.SymBase(nm) (the base symbol) before giving up. (t_importproc.)

Showcase now exercises all three

showcase23lib gained Sum (arithmetic over T) and ForEach(visit : PROCEDURE(VAR acc : INTEGER; v : T); …) (a procedure parameter), and the program passes the imported Visitor.Add:

list: 5 10 20
sum=35          (Sum over T)
foreach=35      (ForEach + imported visitor procedure)
reversed: 20 10 5

Files: showcase23lib.def/.mod, showcase23int.def/.mod, showcase23vis.def/.mod, showcase23.mod.

Verify

cd .../m2compiler-V3/compiler
./M2 ../stdlib/sysio.def ../stdlib/sysio.mod \
    tests/showcase23lib.def tests/showcase23lib.mod \
    tests/showcase23int.def tests/showcase23int.mod \
    tests/showcase23vis.def tests/showcase23vis.mod tests/showcase23.mod