Skip to content

add semantic pass to validate inline type assertion field order#78

Merged
cs01 merged 1 commit intomainfrom
worktree-type-assertion-checker
Mar 2, 2026
Merged

add semantic pass to validate inline type assertion field order#78
cs01 merged 1 commit intomainfrom
worktree-type-assertion-checker

Conversation

@cs01
Copy link
Owner

@cs01 cs01 commented Mar 2, 2026

Summary

Add semantic pass to validate inline type assertion field order. Inline assertions like (expr as { type: string; left: Expr; right: Expr }) use their field positions as GEP indices in native code. If those fields don't form a valid prefix of the actual interface in the correct order, GEP reads wrong memory and causes segfaults.

Changes

  • New semantic pass (type-assertion-checker.ts): Validates all inline { } type assertions with 2+ fields. For each assertion, finds matching interfaces and errors if the assertion doesn't form a valid consecutive prefix in exact field order.
  • Integration: Call checkTypeAssertions() in generateParts() before codegen, alongside existing closure and union type checks.
  • Type assertion fixes: Update 4 existing inline assertions to include type field first:
    • handleReplace() string method — regex node assertion
    • resolveMemberAccessType() — type assertion in type inference
    • allocateSetType() — new expression assertion
    • handleExpressionStatement() (native parser) — member/index assignment assertion

Test Plan

  • Run npm test — all tests pass
  • Run npm run verify:quick — self-hosting passes (Stage 2 proves native codegen is correct)
  • Manually verify error message: create a test file with invalid assertion like as { left; right } (missing type), confirm detailed error and hint to fix it

- new src/semantic/type-assertion-checker.ts validates that inline as { } assertions form a valid prefix of a declared interface in correct field order
- fix transformer.ts: as { type, property } on MemberAccessAssignmentNode needed object before property
- fix string-methods.ts: as { pattern, flags } on RegexNode needed type first
- fix type-inference.ts: as { expression, assertedType } on TypeAssertionNode needed type first
- fix variable-allocator.ts: as { className, typeArgs } on NewNode needed type, args before typeArgs
@cs01 cs01 merged commit 0aa9bc4 into main Mar 2, 2026
12 checks passed
@cs01 cs01 deleted the worktree-type-assertion-checker branch March 2, 2026 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant