Repository navigation
Port type registry to std/rt (Wave 1 unit 4) - #10
Merged
Merged
Conversation
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]>
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; | ||
| } | ||
| } |
Contributor
There was a problem hiding this comment.
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
added a commit
that referenced
this pull request
Aug 1, 2026
Port type registry to std/rt (Wave 1 unit 4)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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,TypeRegistrywith name lookup, structural interning forvec<T>/optional<T>/fn(...), struct/enum definition with validation and discriminant autofillagc/src/parser/prt_parser.rs— recognize pointer declarations whose base type is not yet inknown_type_names(common after import inlining)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 semanticsTest plan
agc tests/rt_types_test.ag(with parser patch) → 103/103 PASScargo test -p agcfor parser unit testsGreptile Summary
This PR ports the Silver runtime type registry to pure Silver (
std/rt/type_info.agandstd/rt/types.ag), consolidates the redundantabort()extern declarations intostd/mem/alloc.ag, extends the PRT parser to recognize pointer-declaration syntax for types not yet inknown_type_names, and adds a 103-case test suite validating the registry semantics.std/rt/type_info.ag—TypeIdnewtype (15 builtin fixed indices),TYPE_KINDdiscriminants, aggregate payload structs (Field,StructType,EnumVariant,EnumType,FunctionType), the flatTyperegistry entry, and small numeric-to-string helpers used when building display names.std/rt/types.ag—TypeRegistrywith name-indexedHashMap, structural-interning caches forvec<T>/optional<T>/fn(...), struct/enum definition with O(n²) duplicate validation, and aTypeResultresult-pattern type mirroring the RustTypeErrorvariants.agc/src/parser/prt_parser.rs— new heuristic branch inclassify_identifier_as_type_startforName *+ ident (= | ; | ,)whenNameis absent fromknown_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/rtfiles are large and introduce careful manual drop discipline that is correct in all tested paths. The only defect found is indefine_enum_autofill_discriminants: the overflow guard fires on any variant whose discriminant equalsi64max, 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 andabort()consolidation changes are both narrowly scoped and look correct.std/rt/types.ag — specifically the overflow guard in
define_enum_autofill_discriminantsand the matching test coverage in tests/rt_types_test.ag.Important Files Changed
define_enum_autofill_discriminantsrejects valid enums where the last variant has discriminant RT_I64_MAX.classify_identifier_as_type_startfor pointer-declaration syntax where the base type is not yet inknown_type_names. The lookahead tokens (=, ;, ,) correctly exclude function-call positions. Multi-star pointer support works. Worth verifying with a dedicated parser unit test forName **+ ident = ...shapes.void abort()to the alloc extern block, consolidating the declaration that was previously duplicated across map, vec, string, and slice modules.abort()extern redeclaration and replaces it withimport std.mem.alloc, which now carries the canonical declaration. No behavioral change.abort()extern redeclaration. Already importsstd.mem.alloc, which now provides it.abort()extern redeclaration. Already importsstd.mem.alloc, which now provides it.abort()extern redeclaration. Already importsstd.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%%{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 : consumesComments Outside Diff (1)
std/rt/types.ag, line 857-864 (link)disc == RT_I64_MAXis checked for the current variant beforenext = disc + 1is computed, so any variant whose discriminant equalsRT_I64_MAXis rejected — even when it is the last variant in the list andnextis never used. An enum registered viadefine_enum_autofill_discriminantswith a single explicitly-discriminated variant ati64max (or any trailing explicitly-discriminated variant at that value) would incorrectly returnDISCRIMINANT_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 afternextis assigned and only when there are remaining variants whosehas_discriminantis false, or to restructure so that overflow is detected at the iteration that would consume the badnext.Reviews (1): Last reviewed commit: "Port TypeRegistry to Silver and fix poin..." | Re-trigger Greptile
Context used: