Skip to content

[soundness] Channel direction never enforced for close() and ange: <-chan T close and chan<- T range accepted (0 diagnostics) and emit Go that does not compile #94

Closed

Description

@milkingndb-oss

Summary

PR #91 (issue #89) correctly enforces channel DIRECTION at send and receive sites (HYB-E3008). However, two other direction-sensitive operations - close() and for . range - are never checked: the checker accepts programs that emit Go go vet rejects, breaking the accepted-should-emit-buildable-Go contract in exactly the way #89 documented for send/recv.

Repro 1 - close() on a receive-only channel

fn main() {
    c := make(chan int, 1)
    var rc <-chan int = c
    close(rc)
}

hyb check (main 8fb9238 AND fix-89-chan-direction 92f53fb): OK (0 diagnostics).

hyb build --emit-go emits:

package main

func main() {
	c := make(chan int, 1)
	var rc <-chan int = c
	close(rc)
}

go vet on the emitted file:

invalid operation: cannot close receive-only channel rc (variable of type <-chan int)

Go spec: "The channel must be bidirectional or send-only." A <-chan T binding has no send capability, so close is illegal.

Note: defer close(rc) is the same bug (parser wraps it as Expr::Call to close) - also accepted, also invalid.

Repro 2 - range over a send-only channel

fn main() {
    c := make(chan int, 1)
    var sc chan<- int = c
    for v in sc {
        fmt.println(v)
    }
}

hyb check: OK (0 diagnostics).

hyb build --emit-go emits:

package main

import (
	"fmt"
)

func main() {
	c := make(chan int, 1)
	var sc chan<- int = c
	for _, v := range sc {
		fmt.Println(v)
	}
}

go vet:

cannot range over sc (variable of type chan<- int): receive from send-only channel chan<- int

Go spec: range over a channel requires receive capability. A chan<- T binding cannot be ranged over.

Controls (all correctly OK / go-vet-OK)

Suggested fix direction (same shape as #91)

Extend check_chan_dir / add sibling checks:

  1. close-site: after resolving the callee to builtin close, if the argument's declared direction is ChanDir::Recv ? reject (cannot close a receive-only channel). Wire into the statement/expression-statement call path (Stmt::Expr with Expr::Call{Ident("close")}) and Defer (which is also a Call expression).
  2. range-site: in the Stmt::For { ForKind::Iterate } handler, if the iterated expression's declared direction is ChanDir::Send ? reject (cannot range over a send-only channel).

Both are type contracts (like E3008) - managed should NOT suppress them. A new code (e.g. HYB-E3009 or extend E3008 with an op parameter) keeps the registry sorted.

Environment

  • main: 8fb9238
  • fix-89-chan-direction: 92f53fb (send/recv fixed; close/range still open)
  • go1.25.5 local, go vet confirms both rejects

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