Skip to content

fix(emit/check/syntax): substitute generic struct N5 maps, infer transitive comparable, and lower new(generic[...]) (#744, #722) - #745

Open
sonson0910 wants to merge 1 commit into
limitlessprojects:mainfrom
sonson0910:fix/generic-struct-n5-zeroinit-and-new-722
Open

sonson0910 wants to merge 1 commit into
limitlessprojects:mainfrom
sonson0910:fix/generic-struct-n5-zeroinit-and-new-722

Conversation

@sonson0910

Copy link
Copy Markdown
Contributor

Fixes #744
Fixes #722

Summary

Sweeps the remaining generic-struct and generic-function lowering, parsing, and check-time validation holes noted in #722 / #663:

  1. hy-emit (zeroinit.rs, lib.rs): Generic struct N5 map zero-value substitution (var b box[u8], box[u8]{}, new(box[u8]), nested generic structs):

    • populate_n5_structs now records generic structs alongside their fixed-point comparable parameter constraints ("table[k:comparable,v]").
    • n5_resolve_struct_fields and subst_type_expr substitute concrete type arguments into the declared generic struct fields (including nested generic struct fields, map[K]V, option[T], slices, arrays, pointers, channels, and fn types) so var b box[u8], box[u8]{}, new(box[u8]), and var h holder[i16] allocate Concrete Go map zero values (map[string]uint8{}) instead of skipping N5 or leaving parametric type names unsubstituted.
  2. hy-emit (lib.rs): Fixed-point transitive, receiver-method, constructor, and equality (==/!=/match) comparable constraint inference:

    • Infers comparable constraints transitively across generic structs (struct wrapper[a, b] { inner table[a, b] } -> [a comparable, b any]), through set[t], and through option[t] / [N]t map keys.
    • Infers comparable constraints on generic receiver methods (fn (s *set_buf[x]) unique() map[x]bool -> func (s *set_buf[x]) unique() map[x]bool with set_buf[x comparable]), generic constructors (fn table.new[k, v]() table[k, v] -> [k comparable, v any]), new(table[k, v]), value-typed ==/!= comparisons (fn same[t](x t, y t) bool { return x == y } -> [t comparable]), and non-option match subjects.
  3. hy-syntax (parser.rs) & hy-emit (expr.rs): new(generic[...]) and pointer type args (box[*int]{}):

    • Extends peek_generic_args to recognize balanced type-argument brackets containing *, [, ], <-, fn, (, ), and ,, and allows ) immediately after ] so new(pair[u8, i16]) and box[*int]{} parse as generic type arguments.
    • Updates emit_new to extract the full TypeExpr via Self::new_arg_type_expr and format via self.type_text(&ty) so new(box[u8]) lowers to new(box[uint8]) (or &box[uint8]{...} when N5 applies).
  4. hy-check (buildable.rs): Validate new(T) type arguments and resolve pointer result kind for new(generic[...]):

    • Validates new(T)'s type argument via self.ty(&new_ty) so uninstantiated new(box) and wrong-arity new(pair[int]) fail closed with HYB-E4001.
    • Updates call_result_kinds and new_kind so new(box[int]) resolves to Kind::Ptr(Box::new(Kind::Named("box"))).

Verification

  • cargo test --manifest-path compiler/Cargo.toml --test n5_zero_map_lowering --test resolve_generics_and_const_closures
  • Full workspace cargo test --manifest-path compiler/Cargo.toml passes with 0 failures.

…sitive comparable, and lower new(generic[...])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant