Skip to content

Port type registry to std/rt (Wave 1 unit 4) - #10

Merged
CierCier merged 1 commit into
mainfrom
std/rt-types-port
Jul 20, 2026
Merged

CierCier merged 1 commit into
mainfrom
std/rt-types-port

Conversation

@CierCier

@CierCier CierCier commented Jul 20, 2026 •

Copy link
Copy Markdown
Owner

Summary

Wave 1 unit 4 of docs/runtime-migration.md.

  • std/rt/types.ag + std/rt/type_info.ag — TypeId, 15 builtins, TypeKind + payload structs, TypeRegistry with name lookup, structural interning for vec<T>/optional<T>/fn(...), struct/enum definition with validation and discriminant autofill
  • agc/src/parser/prt_parser.rs — recognize pointer declarations whose base type is not yet in known_type_names (common after import inlining)
  • Minor std hygiene: drop redundant abort() extern redecls in map/vec/string/slice (same ambiguity class as unit 5's typeck fix)
  • tests/rt_types_test.ag — 103 cases porting registry semantics

Test plan

Greptile Summary

This PR ports the Silver runtime type registry to pure Silver (std/rt/type_info.ag and std/rt/types.ag), consolidates the redundant abort() extern declarations into std/mem/alloc.ag, extends the PRT parser to recognize pointer-declaration syntax for types not yet in known_type_names, and adds a 103-case test suite validating the registry semantics.

  • std/rt/type_info.ag — TypeId newtype (15 builtin fixed indices), TYPE_KIND discriminants, aggregate payload structs (Field, StructType, EnumVariant, EnumType, FunctionType), the flat Type registry entry, and small numeric-to-string helpers used when building display names.
  • std/rt/types.ag — TypeRegistry with name-indexed HashMap, structural-interning caches for vec<T> / optional<T> / fn(...), struct/enum definition with O(n²) duplicate validation, and a TypeResult result-pattern type mirroring the Rust TypeError variants.
  • agc/src/parser/prt_parser.rs — new heuristic branch in classify_identifier_as_type_start for Name *+ ident (= | ; | ,) when Name is absent from known_type_names.

Confidence Score: 4/5

Safe to merge. The new registry code is well-structured and the manual memory management is consistent throughout; the one correctness gap is an extreme edge case that the test suite does not exercise and is unlikely to arise in practice.

The two new std/rt files are large and introduce careful manual drop discipline that is correct in all tested paths. The only defect found is in define_enum_autofill_discriminants: the overflow guard fires on any variant whose discriminant equals i64 max, including when that variant is the last one and no actual overflow would occur. The test suite does not cover a single-variant-at-max enum, so the regression is latent. The parser heuristic and abort() consolidation changes are both narrowly scoped and look correct.

std/rt/types.ag — specifically the overflow guard in define_enum_autofill_discriminants and the matching test coverage in tests/rt_types_test.ag.

Important Files Changed

Filename Overview
std/rt/types.ag New TypeRegistry implementation: name-indexed HashMap, structural interning for vec/optional/fn, struct/enum definition with validation. Memory ownership is carefully managed throughout; one over-eager overflow guard in define_enum_autofill_discriminants rejects valid enums where the last variant has discriminant RT_I64_MAX.
std/rt/type_info.ag New type descriptor data model: TypeId newtype, TYPE_KIND enum, payload structs (Field, StructType, EnumVariant, EnumType, FunctionType), flat Type struct, and numeric helpers. Drop implementations are consistent with Vec's non-element-dropping semantics. No issues found.
agc/src/parser/prt_parser.rs Adds a heuristic branch to classify_identifier_as_type_start for pointer-declaration syntax where the base type is not yet in known_type_names. The lookahead tokens (=, ;, ,) correctly exclude function-call positions. Multi-star pointer support works. Worth verifying with a dedicated parser unit test for Name **+ ident = ... shapes.
tests/rt_types_test.ag 103 test cases covering builtins, vec/optional interning, struct definition, enum auto/explicit/autofill discriminants, and function types. The autofill overflow test uses a 2-variant enum, leaving the single-variant-at-RT_I64_MAX edge case (which the current implementation incorrectly rejects) uncovered.
std/mem/alloc.ag Adds void abort() to the alloc extern block, consolidating the declaration that was previously duplicated across map, vec, string, and slice modules.
std/slice.ag Removes the local abort() extern redeclaration and replaces it with import std.mem.alloc, which now carries the canonical declaration. No behavioral change.
std/map.ag Removes the local abort() extern redeclaration. Already imports std.mem.alloc, which now provides it.
std/mem/vec.ag Removes the local abort() extern redeclaration. Already imports std.mem.alloc, which now provides it.
std/string.ag Removes the local abort() extern redeclaration. Already imports std.mem.alloc, which now provides it.

Class Diagram

%%{init: {'theme': 'neutral'}}%%
classDiagram
    class TypeRegistry {
        +Vec~Type~ types
        +HashMap~str_u32~ by_name
        +HashMap~u32_u32~ vec_cache
        +HashMap~u32_u32~ optional_cache
        +HashMap~str_u32~ fn_cache
        +Vec~String~ fn_cache_keys
        +new() TypeRegistry
        +get(TypeId) Type*
        +name_of(TypeId) str
        +lookup(str) TypeId
        +vec_of(TypeId) TypeResult
        +optional_of(TypeId) TypeResult
        +function(Vec~TypeId~, TypeId) TypeResult
        +define_struct(str, Vec~FieldDef~) TypeResult
        +define_enum(str, Vec~VariantDef~) TypeResult
        +define_enum_with_discriminants(str, Vec~VariantDef~) TypeResult
        +define_enum_autofill_discriminants(str, Vec~VariantDef~) TypeResult
        +enum_variant_index(TypeId, str) i64
        +enum_variant(TypeId, i64) EnumVariant*
        +enum_variant_by_discriminant(TypeId, i64) i64
    }
    class Type {
        +TypeId id
        +String name
        +i32 kind
        +u16 bits
        +bool is_signed
        +TypeId elem
        +StructType struct_data
        +EnumType enum_data
        +FunctionType fn_data
        +new(String, i32) Type
    }
    class TypeId {
        +u32 raw
        +from_raw(u32) TypeId
        +none() TypeId
        +is_none() bool
        +eq(TypeId) bool
    }
    class TypeResult {
        +bool ok
        +TypeId id
        +i32 err
        +String msg
        +ok_id(TypeId) TypeResult
        +error(i32, String) TypeResult
        +unwrap() TypeId
    }
    class StructType { +Vec~Field~ fields }
    class EnumType { +Vec~EnumVariant~ variants }
    class FunctionType { +Vec~TypeId~ params\n+TypeId ret }
    class Field { +String name\n+TypeId ty }
    class EnumVariant { +String name\n+i64 discriminant\n+Vec~TypeId~ payload }
    class FieldDef { +str name\n+TypeId ty }
    class VariantDef { +str name\n+i64 discriminant\n+bool has_discriminant\n+Vec~TypeId~ payload }
    TypeRegistry "1" --> "N" Type : types[]
    Type --> StructType : struct_data
    Type --> EnumType : enum_data
    Type --> FunctionType : fn_data
    StructType "1" --> "N" Field : fields[]
    EnumType "1" --> "N" EnumVariant : variants[]
    TypeRegistry ..> TypeResult : returns
    TypeRegistry ..> FieldDef : consumes
    TypeRegistry ..> VariantDef : consumes
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
classDiagram
    class TypeRegistry {
        +Vec~Type~ types
        +HashMap~str_u32~ by_name
        +HashMap~u32_u32~ vec_cache
        +HashMap~u32_u32~ optional_cache
        +HashMap~str_u32~ fn_cache
        +Vec~String~ fn_cache_keys
        +new() TypeRegistry
        +get(TypeId) Type*
        +name_of(TypeId) str
        +lookup(str) TypeId
        +vec_of(TypeId) TypeResult
        +optional_of(TypeId) TypeResult
        +function(Vec~TypeId~, TypeId) TypeResult
        +define_struct(str, Vec~FieldDef~) TypeResult
        +define_enum(str, Vec~VariantDef~) TypeResult
        +define_enum_with_discriminants(str, Vec~VariantDef~) TypeResult
        +define_enum_autofill_discriminants(str, Vec~VariantDef~) TypeResult
        +enum_variant_index(TypeId, str) i64
        +enum_variant(TypeId, i64) EnumVariant*
        +enum_variant_by_discriminant(TypeId, i64) i64
    }
    class Type {
        +TypeId id
        +String name
        +i32 kind
        +u16 bits
        +bool is_signed
        +TypeId elem
        +StructType struct_data
        +EnumType enum_data
        +FunctionType fn_data
        +new(String, i32) Type
    }
    class TypeId {
        +u32 raw
        +from_raw(u32) TypeId
        +none() TypeId
        +is_none() bool
        +eq(TypeId) bool
    }
    class TypeResult {
        +bool ok
        +TypeId id
        +i32 err
        +String msg
        +ok_id(TypeId) TypeResult
        +error(i32, String) TypeResult
        +unwrap() TypeId
    }
    class StructType { +Vec~Field~ fields }
    class EnumType { +Vec~EnumVariant~ variants }
    class FunctionType { +Vec~TypeId~ params\n+TypeId ret }
    class Field { +String name\n+TypeId ty }
    class EnumVariant { +String name\n+i64 discriminant\n+Vec~TypeId~ payload }
    class FieldDef { +str name\n+TypeId ty }
    class VariantDef { +str name\n+i64 discriminant\n+bool has_discriminant\n+Vec~TypeId~ payload }
    TypeRegistry "1" --> "N" Type : types[]
    Type --> StructType : struct_data
    Type --> EnumType : enum_data
    Type --> FunctionType : fn_data
    StructType "1" --> "N" Field : fields[]
    EnumType "1" --> "N" EnumVariant : variants[]
    TypeRegistry ..> TypeResult : returns
    TypeRegistry ..> FieldDef : consumes
    TypeRegistry ..> VariantDef : consumes
Loading

Comments Outside Diff (1)

  1. std/rt/types.ag, line 857-864 (link)

    P2 Overflow guard fires one iteration too early. disc == RT_I64_MAX is checked for the current variant before next = disc + 1 is computed, so any variant whose discriminant equals RT_I64_MAX is rejected — even when it is the last variant in the list and next is never used. An enum registered via define_enum_autofill_discriminants with a single explicitly-discriminated variant at i64 max (or any trailing explicitly-discriminated variant at that value) would incorrectly return DISCRIMINANT_OVERFLOW. The guard should only trigger when a subsequent variant actually requires the overflowed autofill value — the simplest correction is to move the check to after next is assigned and only when there are remaining variants whose has_discriminant is false, or to restructure so that overflow is detected at the iteration that would consume the bad next.

Reviews (1): Last reviewed commit: "Port TypeRegistry to Silver and fix poin..." | Re-trigger Greptile

Greptile also left 1 inline comment on this PR.

Context used:

  • Context used - You are an expert systems programmer, compiler eng... (source)

Add std/rt/types.ag and std/rt/type_info.ag (TypeId, TypeKind, TypeRegistry
with name lookup, vec/optional/fn interning, struct/enum definition and
discriminant autofill). Parser treats Name*+ ident (=|;|,) as a
declaration so pointer locals work when the base type was import-inlined.
Remove redundant abort() externs from a few std modules to avoid
ambiguous-overload noise when co-imported.

Co-Authored-By: Claude Fable 5 <[email protected]>
Copilot AI review requested due to automatic review settings July 20, 2026 16:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Comment on lines +631 to +652
// `Name *+ ident (= | ; | ,)` — pointer declaration whose base type
// is not in known_type_names (e.g. a struct inlined from an import).
// A bare product expression statement (`a * b;`) has no effect in
// Silver, so treating this shape as a declaration is safe.
if matches!(
tokens.get(index + 1).map(|next| &next.kind),
Some(Token::Star)
) {
let mut cursor = index + 1;
while matches!(tokens.get(cursor).map(|next| &next.kind), Some(Token::Star)) {
cursor += 1;
}
if matches!(
tokens.get(cursor).map(|next| &next.kind),
Some(Token::Identifier(_))
) && matches!(
tokens.get(cursor + 1).map(|next| &next.kind),
Some(Token::Assign | Token::Semicolon | Token::Comma)
) {
return true;
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Pointer-declaration heuristic test coverage gap. The outer if checks that tokens[index + 1] is Token::Star before entering the cursor loop — the new block correctly walks stars before the variable name, complementing the existing handler (lines 616–628) that walks stars after it. Both paths are needed, but it is worth confirming the case Name **+ ident (= | ; | ,) is exercised by the parser unit tests, since rt_types_test.ag defines no double-pointer types through the registry.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@CierCier
CierCier merged commit 42e6128 into main Jul 20, 2026
1 check passed
@CierCier
CierCier deleted the std/rt-types-port branch July 21, 2026 10:16
CierCier added a commit that referenced this pull request Aug 1, 2026
Port type registry to std/rt (Wave 1 unit 4)
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.

2 participants