Skip to content

fix(hy-check): instantiate generic type parameters on call result shapes and types (#73) - #408

Closed
gelismarvel wants to merge 2 commits into
limitlessprojects:mainfrom
gelismarvel:fix/issue-73-generic-call-shape-freeze
Closed

gelismarvel wants to merge 2 commits into
limitlessprojects:mainfrom
gelismarvel:fix/issue-73-generic-call-shape-freeze

Conversation

@gelismarvel

Copy link
Copy Markdown
Contributor

Fixes #73
Participates in #5

Root Cause Analysis (RCA)

In compiler/crates/hy-check/src/shape.rs, call_shape, ty_of_expr, multi_value_shapes, and multi_value_tys resolved the declared return type of top-level functions (self.fns) directly through shape_of_type(&r.ty) without instantiating generic type parameters (FnDecl::type_params) against call-site arguments (or explicit [T, ...] type arguments).

Because any generic type parameter t fell through named_shape_depth to TypeShape::Unknown (and remained an un-substituted TypeExpr::Named { name: "t" } in ty_of_expr), generic call results lost their concrete reference/container shape and source type:

  1. Transitive copy / re-alias after a generic call (m2 := id(m); m3 := m2): m2 received TypeShape::Unknown and missed record_alias (other_shape.is_reference_alias() == false). A subsequent copy m3 := m2 then skipped both record_may_share (binding_may_share("m2") == false on Unknown) and record_alias, leaving m3 completely unfrozen after task worker(move m) (0 diagnostics, runtime fatal error: concurrent map writes).
  2. Projection / indexing through a generic call result (h2 := id(h1); inner := h2.m): h2 received TypeShape::Unknown instead of TypeShape::Struct(Some("holder")), so inner := h2.m resolved to Unknown and skipped record_may_share (shape_may_share(&h2.shape) == false), allowing inner["k"] = 2 after task worker(move h1) with 0 diagnostics.
  3. Generic container return shapes (fn id_slice[t any](s []t) []t, fn wrap_slice[t any](x t) []t): Return type []t resolved to TypeShape::Slice(Box::new(TypeShape::Unknown)), erasing element shapes needed by record_copy_may_share (copy(dst, src) skipped linking dst and src) and by the closure-container spawn guard (shape_carries_closure).
  4. Owned move chains through generic helpers (m2 := id(move m); task worker(move m2)): Because m2 degraded to TypeShape::Unknown, honest owned move chains were falsely rejected with HYB-E3005.

Changes

  • compiler/crates/hy-check/src/shape.rs:
    • Added shape_of_type_with_params, infer_fn_type_params, match_param_shape, match_param_ty, and substitute_type_params to unify FnDecl::type_params against call-site argument shapes/types (and explicit folded [T, ...] type arguments) and substitute them into single-return (call_shape, ty_of_expr) and multi-return (multi_value_shapes, multi_value_tys) result types.
    • Added 5 regression unit tests covering transitive copies of id(m), struct field projections of id(h1), copy(dst, src) on generic slice results, closure-carrying slices returned by generic helpers, and the positive control (m2 := id(move m); task worker(move m2)).

Verification

  • cargo test --manifest-path compiler/Cargo.toml -p hy-check: 126/126 unit tests PASS + 86/86 integration tests PASS (0 failed).

@G0st13

G0st13 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Reviewed the patch. The core idea — threading a type_params: HashMap<String, TypeShape> through shape_of_type_with_params and binding it from both the explicit callee[...] arguments and the actual argument shapes — is the right shape for this, and match_param_shape falling back to argument-driven inference means a failed explicit parse degrades rather than misfires. Direction is good.

One thing that should be fixed before merge: the diff re-encodes existing comment text.

I checked the file at 3119f67 directly (not the rendered diff), and compiler/crates/hy-check/src/shape.rs now carries mojibake where em-dashes used to be:

//! shape.rs 窶・the lightweight TypeShape inference (NOT a type system).

The original U+2014 — has become U+7ABA U+30FB (窶・). Counting the bytes in the file at your head SHA:

  • U+30FB — 21 occurrences
  • U+7ABA — 20 occurrences
  • U+2014 — 0 occurrences left

So roughly 20 lines of pre-existing documentation were rewritten by an encoding round-trip. It is not a correctness problem, but it inflates the diff with changes that have nothing to do with #73 and makes the real hunks harder to review — and once merged the corruption is in main. Restoring the original comment bytes (or re-saving the file as UTF-8 without the cp932-style round trip) should make those lines disappear from the diff entirely.

A smaller note on split_top_level_type_args. Parsing explicit type arguments out of the rendered callee name is forced by the AST folding generics into names, so I understand why it is done this way. The bracket-depth walk covers nesting and trim() covers spacing, so same[map[string]int] and same[ map[string]int ] should both land. Worth one parser-level test on the spaced/nested spelling, since that is where a string-based parse would break first if the folding ever changes — and if it breaks, the fallback silently switches to argument-driven inference rather than erroring, which is the right failure mode but easy to miss.

For what it is worth I did the same check I ran on #410: your branch builds, and the real question for #73 is whether the generic instantiation now freezes correctly (t := same(same(s)), w := wrap[map[string]int]{items: m}). I have not run those specifically against your head — happy to, if it is useful.

@gelismarvel

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review @G0st13! Addressed both points in e91032b:

  1. Restored original UTF-8 comment bytes in compiler/crates/hy-check/src/shape.rs: all 21 pre-existing em-dash (—) / arrow (→) comment lines now match main byte-for-byte and have been removed from the diff.
  2. Added split_top_level_type_args_and_chained_generic_calls_freeze_issue_73: pins Checker::split_top_level_type_args on spaced and nested generic argument lists (" map[string]int " and " map[string][]int , chan map[string]bool ") and verifies end-to-end freeze (HYB-E3003) on chained generic calls (t := same(same(s))).

@limitlessprojects

Copy link
Copy Markdown
Owner

Validation (agent session-15/2-a3, worktree = PR head merged with main @ c53978b): VALIDATED — READY TO LAND.

Status note: the two headline repros from #73 (m2 := id(m) generic identity, h2 := alias_holder(h1) non-generic struct copy) are already rejected on current main with HYB-E3003 — the landed call-arg share machinery covers the direct forms. But the deeper gaps this PR's type-param instantiation closes are still live, and I reproduced all of the interesting ones:

  1. Transitive copy after a generic call — m2 := id(m); m3 := m2, spawn move m, write m3: main 0 diagnostics → PR rejects HYB-E3003.
  2. Projection through a generic call result — h2 := id(h1); inner := h2.m, spawn move h1, write inner: main 0 diagnostics → PR rejects HYB-E3003.
  3. False reject fixed (precision, participates in Bounty track: hunt accepted-but-unsound programs at the task boundary #5) — the honest owned move chain m2 := id(move m); task worker(move m2) is falsely rejected on main with HYB-E3005 (m2 cannot be proven safe across tasks — its type did not resolve); this PR accepts it.

Negative controls unchanged: non-generic id(m) still rejects E3003 on both, and the direct issue repros stay rejected on the PR — no loosening.

Battery on the merged worktree: cargo test --workspace 600 passed / 0 failed (main 595 + 5 new shape.rs regression tests), tools/corpus_harness.py 52/52.

One observation for the author/maintainer: the false-reject family in (3) is worth a corpus fixture — "checker cannot verify because the type did not resolve" is exactly the class of fail-closed-but-wrong rejection that drives users away from generic helpers. Thanks @exantiv — substituting FnDecl::type_params from call-site shapes (with explicit [T, ...] support) is the right layer for this; the diff stays inside shape.rs which keeps the blast radius small.

@gelismarvel

Copy link
Copy Markdown
Contributor Author

Thank you for validating @limitlessprojects! Happy to add a corpus fixture for the generic owned-move chain (m2 := id(move m); task worker(move m2)) if helpful on follow-up.

@limitlessprojects

Copy link
Copy Markdown
Owner

Closing as same-fix with credit - maintainer re-verification on current main (0c0ef74, after #312/#319/#234 landed): your transitive-call shape (m3 := m2 := id(m)) and the projection shape (inner := b.m) both reject E3003 on main now, and the false-reject case (m2 := id(move m); task worker(move m2)) ACCEPTS on main - the E3005 fail-closed was refined by later landings. All three of this PR's behaviors are covered; the generic-parameter instantiation machinery you built remains a solid reference for the future generics work.

@gelismarvel

Copy link
Copy Markdown
Contributor Author

Thank you for the credit @limitlessprojects! Quick heads-up from testing against current main (a6a2bc8 / 0c0ef74): the non-generic fn id(x map[string]int) variants are indeed covered on main, but when id has a generic type parameter (fn id[t any](x t) t or fn wrap_slice[t any](x t) []t), shape_of_type(&r.ty) on main still returns TypeShape::Struct { name: "t", fields: [] } (a value struct with no reference fields), so all four generic cases are still live on a6a2bc8:

  1. Transitive copy after generic call (fn id[t any](x t) t):

    m := map[string]int{"k": 0}
    m2 := id(m)   // main gives m2 shape Struct("t", [])
    m3 := m2      // treated as value-struct copy -> no alias/share edge
    task worker(move m)
    m3["k"] = 2
    • main (a6a2bc8): 0 diagnostics (ACCEPT)
    • This PR (01949ee): HYB-E3003 (REJECT)
  2. Projection through generic call result (fn id[t any](x t) t on holder):

    h1 := holder{m: map[string]int{"k": 0}}
    h2 := id(h1)
    inner := h2.m
    task worker(move h1)
    inner["k"] = 2
    • main (a6a2bc8): 0 diagnostics (ACCEPT)
    • This PR (01949ee): HYB-E3003 (REJECT)
  3. Closure-carrying slice returned by generic helper (fn wrap_slice[t any](x t) []t):

    • main (a6a2bc8): 0 diagnostics (ACCEPT) on fs := wrap_slice(f); task run_all(move fs)
    • This PR (01949ee): HYB-E3001 (REJECT)
  4. Generic owned-move chain (m2 := id(move m); task worker(move m2) with fn id[t any](x t) t):

    • main (a6a2bc8): HYB-E3005 (FALSE REJECT) (m2.ty remains uninstantiated Named("t"))
    • This PR (01949ee): 0 diagnostics (ACCEPT)

I rebased the branch cleanly onto a6a2bc8 (01949ee, 132/132 hy-check unit tests + 55/55 corpus passing) in case you would like to reopen and land the generic type-param instantiation on main!

dancerdan15 added a commit to dancerdan15/Go-Hybrid that referenced this pull request Oct 23, 2026
…xit outer aliases

Instantiate generic function and method return shapes and TypeExprs from call-site arguments across single-value and multi-value calls so shallow-copied references, struct field projections, slice element copies, and closure slices cannot escape O5.2 freeze tracking.

Also preserve surviving outer alias and self-shadow links across FnState::scope_pop and clear alias chains across loop back-edge share groups.

Fixes limitlessprojects#73
Fixes limitlessprojects#313
Sibling-take credit: @Visional-Dev (limitlessprojects#408), @Andrrewss (limitlessprojects#401)
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.

[soundness] Call results that shallow-copy or launder argument storage escape the O5.2 freeze (h2 := alias_holder(h1); generic id(m))

3 participants