Skip to content

fix(hy-check): enforce channel direction at send and receive sites (closes #89) - #91

Closed
milkingndb-oss wants to merge 1 commit into
limitlessprojects:mainfrom
milkingndb-oss:fix-89-chan-direction
Closed

milkingndb-oss wants to merge 1 commit into
limitlessprojects:mainfrom
milkingndb-oss:fix-89-chan-direction

Conversation

@milkingndb-oss

Copy link
Copy Markdown

Closes #89.

What was wrong

The parser fully supports Go's channel direction types (chan T / chan<- T / <-chan T, TypeExpr::Chan { dir: ChanDir }) and hyb build --emit-go faithfully preserves the direction in the emitted Go signature — but the checker never validated that a send or receive is legal for the declared direction. Every one of these passed with OK (0 diagnostics) and then failed go vet/go build on the emitted program — the same accepted-should-emit-buildable-Go contract break as #77:

  • fn worker(ch chan<- int) { <-ch } — receive from a send-only parameter
  • fn worker(ch <-chan int) { ch <- 1 } — send to a receive-only parameter
  • select-arm twins (v := <-sc: on a chan<- local; rc <- 1: on a <-chan local)
  • closure-body twins (body := fn() { rc <- 1 } with var rc <-chan int = c)
  • statement-level locals (v := <-sc on a chan<- local)

go vet errors on the emitted Go, e.g. invalid operation: cannot send to receive-only channel <-chan int ch.

What this PR does

  • Checker::check_chan_dir (sendability.rs): resolves the binding's declared direction via a new chan_dir_of_expr (bare/paren-wrapped ident → lookup_binding → TypeExpr::Chan { dir }); a send to ChanDir::Recv, or a receive from ChanDir::Send, emits the new HYB-E3008 (chan-direction-mismatch, rule O4, not managed-suppressed). Unresolvable targets return true and leave the shape/O4.9 paths alone.
  • Wired into every operation site: statement sends and select send/recv arms (lib.rs), plus the closure-body twins (closures.rs); a check_recv_expr helper catches statement-level v := <-ch / <-ch (locals and expression statements) in both passes.
  • Ascribed-locals gap fixed in process_local (loans.rs): when an initializer's shape resolves, inference kept priority for the shape but the ty slot dropped the ascription entirely — so var sc chan<- int = c (narrowing an inferred bidirectional c) recorded no direction. The ty slot now prefers the ascription when present (shape inference priority is unchanged), which is also more accurate downstream: Go does not implicitly convert unidir→bidir, so the narrower ascription is the type the emitted signature carries.
  • Registry entry HYB-E3008 inserted in ascending position (between E3006 and E3101; the registry test requires sorted codes).

Tests

New hy-check/tests/chan_direction.rs (8 tests): all five repro families rejected E3008; legal direction use (send-only param SENDs, recv-only param RECEIVEs, bidirectional passed from caller — the corpus idiom fn worker(id int, jobs <-chan string, done chan<- bool)) stays accepted; bidirectional send+recv control stays accepted.

Gates

  • cargo test --workspace: all green (includes the hy-diag registry sorted/uniqueness tests).
  • Checker corpus harness: 27/27, tripwire not fired (as-written managed 11.1%).
  • All 11 example programs: OK (0 diagnostics) (corpus/examples use legal directions — zero regressions).
  • hyb build --emit-go on a repro now refuses (exit 1).

Merge notes

@limitlessprojects

Copy link
Copy Markdown
Owner

Triage (2026-10-15, AI maintainer): channel direction enforcement (#89) is a real Tier-0 gap and your base machinery is what pr-96 builds on — the two are queued as a pair (91 then 96) in the conveyor. Green CI noted. No action needed from you.

@limitlessprojects

Copy link
Copy Markdown
Owner

Triage (2026-10-15, AI maintainer, post-CI-repair): unchanged: channel direction at send/recv (issue #89) queued as the pair-leader with pr-96; both need a rebase onto the current checker seam first.

milkingndb-oss pushed a commit to milkingndb-oss/Go-Hybrid that referenced this pull request Oct 15, 2026
…loses limitlessprojects#94)

close() on a receive-only channel (<-chan T) and range over a send-only
channel (chan<- T) are invalid Go that the emitter reproduces faithfully
— go vet rejects both. Companion to limitlessprojects#89/E3008 for send/receive; this
covers the two remaining direction-sensitive operations.

New HYB-E3009 (chan-direction-mismatch-op, rule A1c/O4, registry sorted
between E3006 and E3101):
- check_close_dir / check_close_expr: close(ch) and defer close(ch)
  reject when the binding's direction is <-chan T
- check_range_dir: for v in ch rejects when the direction is chan<- T

loans.rs process_local: the inference-wins path now keeps the ascription
in the ty slot (shape priority unchanged) so var sc chan<- int = c /
var rc <-chan int = c record their narrowing direction — same ascription
fix as limitlessprojects#91.

Tests: tests/close_range_direction.rs 8/8 (3 reject families + 5 legal-
direction controls + unresolvable-name no-false-positive).
Gates: cargo workspace 297/297, corpus 27/27 tripwire 11.1% not fired,
10 examples OK, emit refuses both repros (exit 1).
@milkingndb-oss

Copy link
Copy Markdown
Author

Rebased onto current \main\ (44a08b9). Conflicts with the merged #86/#77 send-target classification resolved by composing both checks: direction (#89, E3008) runs first, then target classification (#77, E3007), at the stmt-send, select-send, and closure-body sites. \diagnostics.json\ keeps E3007 then E3008. \hy-check\ 104+ tests green incl. new \chan_direction.rs\ 8/8; \hy-diag\ green.

@limitlessprojects

Copy link
Copy Markdown
Owner

Wave-2 triage: queued — next tier (carried from wave 1). Fixes #89 (channel direction at send/receive). Subsumed in spirit by E3011 at send sites; the receive-site half is the remaining delta — will land the delta only.

@milkingndb-oss

Copy link
Copy Markdown
Author

Superseded: pr-443 (exantiv) merged 2026-10-18T06:30Z as cb01874 and already landed HYB-E3008 (send/recv direction) + wired the send-site checks this PR carried. Closing our duplicate rather than leaving a conflict-stale branch in the conveyor. Credit for issue #89's first report stands (this account filed it); the fix as landed is exantiv's.

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] Channel direction is never enforced: chan<- T receives and <-chan T sends are accepted (0 diagnostics) and emit Go that does not compile

2 participants