Skip to content

P0 (hy-check): generic struct TypeShape erasure drops type arguments in shape_may_share, shape_holds_closure, and field_borrow_kind — bypassing O5.2/O5.1/T22/A5/O2 (HYB-E3003/E3004/E3006/E3001/E3104) with confirmed go run -race data races #775

Open

Description

The smallest .hy program that demonstrates it

// Commit: aa4a9d9 (latest main)
// Command: hyb check repro.hy && hyb build repro.hy
// Original Finder & Bounty Claimant: @huynhnhatquandongnai-source
// Payout Wallet (USDC/USDT - BEP20/EVM): 0xb9f3765aeb1974c27a193463f655a1a57b3aec94

struct box[t] {
v t
}

fn worker(b box[[]int], done chan bool) {
b.v[0] = 1
done <- true
}

fn main() {
done := make(chan bool, 1)
b1 := box[[]int]{v: []int{0}}
b2 := b1
task worker(move b1, done)
b2.v[0] = 2
<-done
}

What the checker should say

Reject with HYB-E3003 (tier0_rule: "A1b", O5.2 — write through a live alias of a moved value: b2 is a shallow copy of b1 := box[[]int]{v: []int{0}} sharing the underlying []int backing array, so moving b1 into task worker(move b1, done) must freeze b2 in main).

Negative Control (Monomorphic equivalent is correctly rejected with HYB-E3003):
struct int_box { v []int }
fn worker(b int_box, done chan bool) { b.v[0] = 1; done <- true }
fn main() {
done := make(chan bool, 1)
b1 := int_box{v: []int{0}}
b2 := b1
task worker(move b1, done)
b2.v[0] = 2
<-done
}

Additional P0 Soundness Bypasses from the Same Root Cause (All Accepted with 0 Diagnostics & Race at Runtime):

  1. O5.1 Non-owned parameter move bypass (HYB-E3004): Copying local_b := param_b (where param_b: box[[]int]) drops non_owned_origin and share_edge, allowing task worker(move local_b, done) while caller mutates b.v[0].
  2. T22 Deep const immutability bypass (HYB-E3006): Copying c := k from const k = box[[]int]{v: []int{0}} drops shadowed_const and const_write_root, allowing concurrent tasks to mutate c.v[0] on the shared const slice.
  3. A5 / O4 Closure-in-generic-struct move bypass (HYB-E3001): b := box[fn()]{v: fn() { shared = 1 }}; task run_box(move b, done) is accepted with 0 diagnostics, racing on captured local shared.
  4. O12 / O2 Borrow-in-generic-struct & loan-escape bypass (HYB-E3103 / HYB-E3104): fn escape() box[*node] { n := node{x: 1}; b := box[*node]{v: &n}; return b } is accepted with 0 diagnostics, escaping stack borrow &n.

What it actually said

OK (0 diagnostics) — Verdict::Accept. Compiles cleanly under hyb build / go build and immediately triggers WARNING: DATA RACE under go run -race between worker writing b.v[0] = 1 and hyMain writing b2.v[0] = 2.

Claimed severity

P0 — data race reachable in the checked subset

Anything else

Root Cause in hy-check

Commit 4a17528 (#722) enabled generic struct declarations (struct box[t] { v t }) to emit valid Go (type box[t any] struct { v t }) and added type-argument substitution (OpKind::Struct(Some(full_name)), struct_type_arg_map, kind_of_ty_with_subst) only inside compiler/crates/hy-check/src/buildable.rs:1231-1390.

However, the Tier-0 ownership, sendability, freeze, loan, and const-immutability passes in shape.rs, sendability.rs, and loans.rs still strip generic type arguments:

  1. compiler/crates/hy-check/src/shape.rs:80-84 (Checker::named_shape_depth) & shape.rs:269-275:
    Calls Self::base_named_type_str(name) and returns TypeShape::Struct(Some("box")), discarding [[]int].
  2. compiler/crates/hy-check/src/shape.rs:116-143 (Checker::shape_may_share_at):
    Looks up self.structs.get("box"), whose uninstantiated field type is TypeExpr::Named { name: "t" }. self.shape_of_type(ty) returns TypeShape::Unknown, so shape_may_share_at returns false for every generic struct whose reference-bearing field comes from a type parameter t.
  3. compiler/crates/hy-check/src/sendability.rs:157-212 (shape_holds_closure & shape_holds_unmovable_leaf) and compiler/crates/hy-check/src/loans.rs:2006-2042, 7760-7860 (field_borrow_kind & expr_has_borrow_shape):
    All evaluate uninstantiated field type "t" -> TypeShape::Unknown -> false.

Original Reporter & Bounty Claimant: @huynhnhatquandongnai-source
Payout Wallet (USDC/USDT — BNB Smart Chain BEP20 / EVM): 0xb9f3765aeb1974c27a193463f655a1a57b3aec94
(Note: Per BOUNTY.md rules, the first reporter of this unique root cause holds the bounty claim. I am also preparing the regression test and fix PR under @huynhnhatquandongnai-source.)

Activity

  1. added
    soundness
    on Oct 19, 2026
  2. sonson0910 commented on Oct 19, 2026

    @sonson0910
    Contributor

    Prior-Art & First-Reporter Notice (Duplicate of #105 / PR #106, filed 97.5 hours earlier)

    For maintainer triage reference (@limitlessprojects), the root cause and soundness surface reported here — TypeShape generic type-argument erasure on generic structs (box[T], lru_cache[K, V]) across shape.rs, sendability.rs, and loans.rs — was originally reported and fixed 4 days (97.5 hours) earlier by @sonson0910 in:

    PR #106 already implements Checker::resolved_struct_fields, struct_fields_for_name, and shape_field_borrow_kind across shape.rs, sendability.rs, and loans.rs, rejecting all repros (HYB-E3003, HYB-E3004, HYB-E3006, HYB-E3001/HYB-E3002, HYB-E3102, HYB-E3104, HYB-E4001, and HYB-E4002) with regression tests in compiler/crates/hy-check/src/lib.rs.

  3. huynhnhatquandongnai-source commented on Oct 19, 2026

    @huynhnhatquandongnai-source
    Author

    Fix PR Submitted: #792 (Covers emit_guard.rs, closures.rs, and freeze.rs surfaces omitted by #106)

    Fix PR: #792 (2f5fb14, MERGEABLE: true)

    For maintainer triage (@limitlessprojects): in addition to shape.rs, sendability.rs, and loans.rs, PR #792 also closes the remaining generic-struct TypeShape soundness & completeness gaps in emit_guard.rs, closures.rs, and freeze.rs that were not touched by PR #106:

    1. emit_guard.rs (check_type_expr_with_state — O12 / A6 HYB-E3103): Rejects generic struct instantiations carrying non-atomic pointers or borrow handles (box[*int]{v: &x}, box[serde_json.view]) with HYB-E3103, which otherwise slip past check_struct_decls because the uninstantiated declaration type box[T any] struct { v T } has TypeShape::Unknown for T at struct-decl check time.
    2. emit_guard.rs (incomparable_rec & Expr::StructLit — B2 HYB-E6003 & HYB-E6002): Substitutes generic type parameters when checking struct comparability (box[[]int]{} == box[[]int]{}) and constant integer overflow on generic struct literal fields (box[int8]{v: 200}).
    3. closures.rs (A6 HYB-E3103 inside closure bodies): Uses recursive ck.field_borrow_kind(&shape) on field assignments inside closure bodies.
    4. freeze.rs (shape_is_proven_pure_value): Normalizes Self::base_named_type_str(sname) when checking generic struct declarations in shape_is_proven_pure_value.

    Author / Payout Attribution:

    • GitHub: @huynhnhatquandongnai-source
    • Wallet (USDC / USDT — BEP20 / EVM): 0xb9f3765aeb1974c27a193463f655a1a57b3aec94
  4. sonson0910 commented on Oct 19, 2026

    @sonson0910
    Contributor

    Duplicate / Copied PR & Regression Notice — Reported & Fixed 99 Hours Earlier in #105 / PR #106

    For maintainer triage: this PR (#792, opened 2026-10-19T05:09:20Z) is a duplicate of our earlier issue and pull request opened more than 4 days (99 hours 16 minutes) earlier, and was opened 80 minutes after we posted a prior-art link to PR #106 on #775:

    1. Original First-Reporter Issue: #105 — opened 2026-10-15T01:51:53Z (99 hours 17 minutes before #792).
    2. Original Companion Pull Request: PR #106 (fix/generic-struct-and-set-soundness) — opened 2026-10-15T01:52:46Z (99 hours 16 minutes before #792).
    3. Prior-Art Comment Posted on #775: #775 (comment) — posted 2026-10-19T03:48:53Z (80 minutes before #792 was opened).

    1. Regression Introduced by #792 (tests/const_overflow.rs::type_alias_struct_literal_overflow_is_rejected Fails)

    In compiler/crates/hy-check/src/emit_guard.rs:413-419, #792 deleted the while let Some(TypeExpr::Named { name: target, .. }) = self.type_aliases.get(&sname) { sname = target.clone(); } loop before calling self.struct_fields_for_sname(&sname). Because struct_fields_for_sname only looks up self.structs.get(sname), any struct literal constructed through a type alias (type MyLimit = Limit; MyLimit{limit: 300}) returns None and breaks existing test tests/const_overflow.rs::type_alias_struct_literal_overflow_is_rejected:

    ---- type_alias_struct_literal_overflow_is_rejected stdout ----
    thread 'type_alias_struct_literal_overflow_is_rejected' panicked at crates/hy-check/tests/const_overflow.rs:624:5:
    expected HYB-E6002 for aliased struct literal limit: 300, got []
    

    2. Direct Code Overlap & Strict Superset Comparison (PR #106 vs #792)

    • Core mechanism in #792 was already implemented in PR #106 on Oct 15:
      • Preserving the full instantiated generic struct name in TypeShape::Struct(Some(name.to_string())) in shape.rs:231 instead of stripping [T1, T2].
      • Substituting Decl::Struct.type_params across field types via struct_fields_for_name / parse_generic_type_args / substitute_type_params in sendability.rs and shape.rs.
      • Recursive struct-field borrow inspection (shape_field_borrow_kind / emit_struct_field_borrow_diag) and generic receiver base-name stripping across loans.rs, closures.rs, emit_guard.rs (while preserving self.type_aliases resolution so const_overflow passes 94/94), and freeze.rs.
    • What PR #106 additionally covers that #792 omits:
      • Prelude set[T] sendability and share-edge tracking (HYB-E3001 / HYB-E3003),
      • Generic function call type-argument inference and return-shape substitution (resolve_hy_fn_for_call),
      • Multi-parameter generic struct instantiation (Pair[K, V]), chained type aliases to generic structs (type Alias = Box[map[string]int]), and nested generic struct fields (Outer[Inner[[]int]]),
      • Rebased cleanly on current main with 100% of unit and integration tests passing (including tests/const_overflow.rs 94/94 ok and 27 dedicated generic-struct/set regression tests).

    Recommend closing #792 and #775 as duplicates of #105 / PR #106.

  5. Dimank92 commented on Oct 26, 2026

    @Dimank92
    Contributor

    Opened PR #1806 to fix this issue by preserving full generic type arguments in TypeShape::Struct and substituting type parameters during struct field shape lookups.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    soundness

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions