Skip to content

Type AST migration (#140): replace string-based types with structured Type enum#168

Open
frendsick wants to merge 11 commits intomainfrom
feat/type-ast-foundation
Open

Type AST migration (#140): replace string-based types with structured Type enum#168
frendsick wants to merge 11 commits intomainfrom
feat/type-ast-foundation

Conversation

@frendsick
Copy link
Copy Markdown
Owner

Summary

Implements PRD #140. Replaces stringly-typed compiler internals with a structured Type enum (Unknown, Builtin, Generic, Function, TypeVar).

Closes issues #161-#167.

Per-commit breakdown

  • 976a730 Type AST foundation: enum, parse_type_str, Type.format, round-trip tests
  • e7ef718 Migrate Parameter.typ: str → Type
  • a658ef5 Migrate Variable.typ and Member.typ to Type
  • c64f7d1 Migrate Signature.return_types: List[str] → List[Type]
  • 606f79e Migrate TypedStack.types: List[str] → List[Type]
  • 2f59857 Add Type methods: base, type_params, is_type_var, substitute
  • 45f1b12 Fix RPN argument order in parse_type_str (str helpers; see below)
  • 83de95e Remove internal string boundaries at make_named_param / make_signature

Bug discovered and fixed

parse_type_str passed starts_with/ends_with/contains/find arguments in the wrong RPN order, so parse_type_str("fn[T -> U]") returned a Generic{base="fn[T -> U]", params=[]} blob instead of a real Type::Function. Type::format round-tripped it correctly so the bug was masked, but Type::substitute couldn't decompose the blob — broke trait default method synthesis (e.g. Counter.map from Iterable::map). Direct substitute test added in test_type_ast.casa as a regression guard.

Notes on Casa enum move-semantics

Casa enum values (Type, Option[T]) are move-semantic: a named variable is consumed on first method call. resolve_trait_sig therefore uses a format → check → re-parse pattern when it needs to compare a Type against TRAIT_SELF_TYPE and reuse it. signature_matches similarly stays str-based.

Test plan

  • tests/test_compiler.sh < /dev/null — 28/28 pass
  • tests/test_examples.sh — 40/40 pass
  • Self-compilation (compiler builds itself)
  • Fixed-point bootstrap

Introduce BuiltinType, GenericType, FunctionType, and Type enums/structs
in compiler/common.casa. Add parse_type_str (str -> Type) and Type.format
(Type -> str) with round-trip test coverage for builtins, generics,
nested generics, function types, type vars, and named types.
str::starts_with, str::ends_with, str::contains, and str::find take
needle/prefix/suffix as the first parameter. The original calls passed
them as the second, so 'fn[T -> U]' parsed as a single-string Generic
blob (base='fn[T -> U]', params=[]) instead of a real Type::Function.

Type::format round-trips it correctly so the bug stayed undetected, but
Type::substitute cannot decompose the blob, breaking trait default
method synthesis (e.g. Iterable::map for non-generic receivers).
…signature

Final piece of the Type AST migration (#167). make_named_param and
make_signature now take Type / List[Type] directly, eliminating internal
parse_type_str round-trips. apply_type_subs_to_sig and resolve_trait_sig
operate on Type values via Type::substitute and TRAIT_SELF_TYPE checks.

Adds direct test for Type::substitute on Type::Function nodes — the
round-trip tests previously masked decomposition failures.
…_sig

Inline the if/else branches by reassigning typ_str/ret_str when self
matches, then parse once at the end. Same behavior, less repetition.
Eliminate format/parse round-trip on the non-self branch by passing
param.typ and ret as Type values, parsing type_var once up front.
Reduces work proportional to (params + returns) per trait method
synthesis call.
Eliminates three parse_type_str call sites in syntax.casa by parsing
the function-type string once at construction in
build_hidden_trait_methods, then passing Type values through
make_named_param and Variable directly.
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