Skip to content

hy-check: check_loop_backedge misses loop-carried moves on shadowed pre_moved bindings and LX14 continue loop / break loop inside nested finite for loops (go run -race) #739

Open

Description

@c-viper-work

Summary

Checker::check_loop_backedge (compiler/crates/hy-check/src/freeze.rs, compiler/crates/hy-check/src/lib.rs, and compiler/crates/hy-check/src/closures.rs) has three soundness bypasses where loop-carried ownership moves (O5.1 / O5.2 / O13) pass hyb check with 0 diagnostics and trigger real go run -race data races (exit code 66) at runtime:

  1. Gap 1 (lib.rs + closures.rs + freeze.rs — pre_moved and reported_backedge track bare variable names HashSet<String> instead of (scope_depth, name)):

    • At loop entry (lib.rs:472–478 and closures.rs:971–977), pre_moved collects bare binding names k.clone() across st.env and st.scopes without their scope depth (0 for st.env, i + 1 for st.scopes[i]).
    • When an outer binding m (e.g. in st.env, depth 0) is moved before an enclosing scope (if, block { ... }, outer loop, or closure) that shadows m with a fresh binding m := map[string]int{...} (st.scopes[d], depth d + 1 <= outer_depth), pre_moved contains "m".
    • When the inner shadowed m (or an alias of the same name) is moved inside a subsequent loop (for i in 0..2 { m["main"] = i; task worker(move m, done) }), check_loop_backedge (freeze.rs:639) and has_continue_after_loop_move (freeze.rs:801) see pre_moved.contains("m") == true, clear b.moved = None on the shadowed binding, and skip the back-edge check completely.
    • Similarly, reported_backedge tracks bare String instead of (usize, String), so reporting a back-edge diagnostic on an earlier binding suppresses back-edge reporting on a distinct shadowed binding of the same name in another scope.
  2. Gap 2 (freeze.rs — Labeled continue loop (SPEC LX14) inside a nested finite for loop (ForKind::Range, ForKind::Iterate, ForKind::While) bypasses stmt_has_continue_for_loop, stmt_can_reach_backedge, and stmt_move_can_reach_backedge):

    • By SPEC LX14 (and hy-emit/src/stmt.rs:1674–1675, 1932–1938), labeled continue loop (Stmt::Continue { label: Some(_) }) binds ONLY to the nearest enclosing loop (ForKind::Infinite), skipping any intermediate for (Range, Iterate, While).
    • However, in freeze.rs:
      • stmt_has_continue_for_loop (freeze.rs:1245–1247) unconditionally returns false on Stmt::For { .. }, missing continue loop inside a nested finite for when checking an enclosing loop.
      • stmt_can_reach_backedge (freeze.rs:884–886) returns after_reach directly on a finite For, ignoring that its body can jump directly to the enclosing loop back-edge via continue loop even when after_reach is false (before trailing break or return).
      • stmt_move_can_reach_backedge (freeze.rs:987–1000) overwrites continue_reach with loop_exits, losing labeled_continue_reach for continue loop inside a finite For.
  3. Gap 3 (freeze.rs — Labeled break loop (SPEC LX14) inside a nested finite for loop enclosed in an inner loop bypasses stmt_can_reach_backedge and stmt_move_can_reach_backedge):

    • By SPEC LX14, labeled break loop (Stmt::Break { label: Some(_) }) binds to the nearest enclosing loop (ForKind::Infinite), skipping any intermediate finite for and select.
    • Because stmt_can_reach_backedge returns after_reach on any finite For and stmt_move_can_reach_backedge overwrites loop_break_reach with after_reach on any For, an inner loop { for j in 0..1 { if step == 0 { step = 1; break loop } }; return } followed in the outer loop by if step == 1 { continue }; break is misclassified as unable to reach the outer loop back-edge.

Minimal Reproducers & go run -race Verification

Reproducer 1 (Gap 1: Shadowed pre_moved binding moved inside loop)

package main

fn worker(m map[string]int, done chan bool) {
    for i in 0..1000 {
        m["w"] = i
    }
    done <- true
}

fn main() {
    m := map[string]int{"outer": 0}
    done := make(chan bool, 3)
    task worker(move m, done)
    if true {
        m := map[string]int{"inner": 1}
        for i in 0..2 {
            m["main"] = i
            task worker(move m, done)
        }
    }
    <-done
    <-done
    <-done
}
  • Actual on main (aa4a9d9): hyb check -> OK (0 diagnostics), go run -race -> WARNING: DATA RACE (exit code 66) between main and both spawned worker goroutines on the inner map m.
  • Expected: Reject with HYB-E3003 (m is used after ownership was moved away).

Reproducer 2 (Gap 2: continue loop inside nested finite for before trailing break)

package main

fn worker(m map[string]int, done chan bool) {
    for i in 0..1000 {
        m["w"] = i
    }
    done <- true
}

fn main() {
    m := map[string]int{"init": 0}
    done := make(chan bool, 2)
    step := 0
    loop {
        m["main"] = step
        task worker(move m, done)
        for j in 0..1 {
            if step == 0 {
                step = 1
                continue loop
            }
        }
        break
    }
    <-done
    <-done
}
  • Actual on main (aa4a9d9): hyb check -> OK (0 diagnostics), go run -race -> WARNING: DATA RACE (exit code 66).
  • Expected: Reject with HYB-E3003.

Reproducer 3 (Gap 3: break loop inside nested finite for exiting inner loop to outer continue)

package main

fn worker(m map[string]int, done chan bool) {
    for i in 0..1000 {
        m["w"] = i
    }
    done <- true
}

fn main() {
    m := map[string]int{"init": 0}
    done := make(chan bool, 2)
    step := 0
    loop {
        m["main"] = step
        task worker(move m, done)
        loop {
            for j in 0..1 {
                if step == 0 {
                    step = 1
                    break loop
                }
            }
            return
        }
        if step == 1 {
            continue
        }
        break
    }
    <-done
    <-done
}
  • Actual on main (aa4a9d9): hyb check -> OK (0 diagnostics), go run -race -> WARNING: DATA RACE (exit code 66).
  • Expected: Reject with HYB-E3003.

Payout Wallet

  • USDC / ETH (Base / Ethereum): 0x3018577736ebcdd3ddca916Bef44e7dC6E27e33E

Activity

  1. dancerdan15 commented on Oct 23, 2026

    @dancerdan15

    /attempt #739

  2. added a commit that references this issue on Nov 15, 2026
    749c140
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions