Skip to content

[type-system] eliminate silent-default dispatch and raw string array checks #710

@cs01

Description

@cs01

Problem

Two related anti-patterns produce silent-wrong-code bugs:

1. Silent-default dispatch (~15 functions)

Functions that map TS types → LLVM types use if/else chains ending with return "i8*" as a silent default. When a new type variant is added, these functions silently return a wrong pointer type instead of crashing.

Files with return "i8*" fallthrough defaults:

  • src/codegen/types/objects/class.tsfieldToLlvmType, methodReturnTypeToLlvm, fieldTypeToLlvm, fieldTypeToLlvmPrimitive
  • src/codegen/expressions/access/member.tsresolveFieldLlvmType
  • src/codegen/expressions/calls.ts — field type resolution
  • src/codegen/statements/control-flow.ts — switch discriminant type resolution
  • src/codegen/types/interface-struct-generator.ts — interface field type mapping
  • src/codegen/infrastructure/generator-context.ts — type fallback

Fix: Convert all dispatch to switch + throw on default.

2. Raw endsWith("[]") array type checks

Scattered inline string checks like if (t.endsWith("[]")) that manually re-derive whether something is a number/string/boolean/object array.

Fix: Replace with classifyArray()ArrayKind enum + isAnyArrayTsType() guard checks.

Progress

Guard checks (endsWith → isAnyArrayTsType) — boolean guards only

Type-mapping sites (endsWith → classifyArray+arrayKindToLlvm) — changes return values

  • class.ts methodReturnTypeToLlvm — exact equivalent
  • class.ts fieldTypeToLlvm — exact equivalent
  • class.ts fieldToLlvmType (2 sites) — has latent ordering bug, behavioral change
  • calls.ts getFieldLlvmType — behavioral change (object[] was %Array*, should be %ObjectArray*)
  • member.ts resolveFieldLlvmType (2 sites) — behavioral change
  • member.ts (4 type-map sites) — behavioral change
  • llvm-generator.ts (2 sites) — behavioral change
  • ⚠️ These must be migrated ALL AT ONCE to avoid type inconsistencies that break self-hosting (learned from PR [type-system] add ArrayKind enum, migrate 25 sites from raw string checks #711 Stage 1→2 failure)

Silent-default dispatch — not started

  • class.ts fieldToLlvmType / fieldTypeToLlvmPrimitive
  • member.ts resolveFieldLlvmType
  • calls.ts field type resolution
  • control-flow.ts switch discriminant
  • interface-struct-generator.ts (4 sites)
  • generator-context.ts type fallback

Infrastructure

  • ArrayKind enum + classifyArray + arrayKindToLlvm in type-system.ts
  • isAnyArrayTsType() centralized guard check
  • isObjectArrayTsType() rewritten as classifyArray(t) === ArrayKind_Object
  • CLAUDE.md "Discriminated Dispatch — No Silent Defaults" rule

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions