Skip to content

channel element type is never checked: accepted program emits Go that cannot be built (cannot use 7 as job value in send) #146

Closed

Description

@anYoneo

Summary

Channel direction is enforced (#89/#91, #94/#96), but the channel's element type is never checked. A value of the wrong type goes into (or comes out of) a chan T with zero diagnostics, and the emitted Go cannot be built or run.

struct job { cost int }

fn main() {
    ch := make(chan job, 1)
    ch <- 7
}
$ hyb check x1.hy
x1.hy: OK (0 diagnostics)

$ hyb build x1.hy --emit-go && go build .
./a.out.go:9:8: cannot use 7 (untyped int constant) as job value in send

go vet fails identically — this is not a vet-only nit, the program is simply not buildable.

Why this is a contract breach, not a nicety

The emitter's own contract (and GUARANTEES.md) is that an accepted program builds and runs. Here hyb check says the program is fine and go build says it is not, with no path forward for the author: the diagnostic that would explain the mistake is never produced.

The receive side is symmetric:

struct job { cost int }

fn main() {
    ch := make(chan job, 1)
    ch <- job{cost: 1}
    n int = <-ch
}
$ hyb check y1.hy
y1.hy: OK (0 diagnostics)
$ go build .
./a.out.go:11:11: cannot use <-ch (value of type job) as int value in assignment

Relation to the open PRs

#91 and #96 close the direction rules (chan<- T receiving, <-chan T sending, close/range on receive-only). Those are about which operations are legal on a channel of a given direction. This is about which values are legal in a channel of a given element type — a different axis. Neither PR touches element-type compatibility: with both applied, ch <- 7 on a chan job still emits cannot use 7 … as job value in send. Nothing in the open issue/PR set mentions element types (channel type: 22 hits, all direction; element type: 7 hits, all immutability/alias; no hit for the cannot use … value in send family).

Suggested fix

The checker already carries type information for locals (the make(chan T, n) result is typed), so the work is: record the element type when a channel-typed value is bound, then at each Send/Receive/range/close site compare the value type against that element type, reporting a diagnostic (new code, e.g. HYB-E3011) on mismatch. _-typed/foreign-perimeter values should stay permissive as elsewhere.

This concerns the checker only; no emitter change is needed.

Environment: commit fc74324 (main at the time of writing), Go 1.26.9 (the pinned version).

Activity

  1. changed the title [-]channel element type is never checked: accepted program emits Go that cannot be built ()[/-] [+]channel element type is never checked: accepted program emits Go that cannot be built (cannot use 7 as job value in send)[/+] on Oct 15, 2026
  2. added a commit that references this issue on Oct 15, 2026
  3. added a commit that references this issue on Oct 15, 2026
    c0d9f31
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