Skip to content

[soundness] builtin arguments never validated (make size, len/cap/delete/close) and defer accepts non-calls (E2001 registered but never raised) - accepted programs emit Go that fails go vet/build #187

Closed

Description

@milkingndb-oss

Two sub-families of "accepted with 0 diagnostics, emitted Go fails go vet/go build" on current main (c09e992). Both are trivially reachable from ordinary source and neither is covered by any open issue/PR I can find (searched make/builtin/defer/E2003/E2004; #98/#99 are freeze/loan-across-defer - different failure mode; #178 copy-may-share - different).

Sub-family A - builtin argument validation is absent

Every shape below passes hyb check with OK (0 diagnostics) on main c09e992, and the emitted Go fails go build / go vet:

# repro emitted Go go build/vet error
A1 fn main() { c := make(chan int, -1) } c := make(chan int, -1) invalid argument: index -1 (constant of type int) must not be negative
A2 fn main() { c := make(chan int, "x") } c := make(chan int, "x") cannot convert "x" (untyped string constant) to type int
A3 fn main() { s := make([]int, -1) } s := make([]int, -1) invalid argument: index -1 (constant of type int) must not be negative
A4 fn main() { c := make(chan int, 2.5) } c := make(chan int, 2.5) 2.5 (untyped float constant) truncated to int
A5 fn main() { close(5) } close(5) invalid operation: cannot close non-channel 5 (untyped int constant)
A6 fn main() { fmt.println(cap("x")) } fmt.Println(cap("x")) invalid argument: "x" (untyped string constant) for built-in cap
A7 fn main() { fmt.println(len(5)) } fmt.Println(len(5)) invalid argument: 5 (untyped int constant) for built-in len
A8 fn main() { m := map[string]int{"a": 1}; delete(m) } delete(m) not enough arguments in call to delete(m) (expected 2, found 1)

Controls that must KEEP parsing (and do today): make(chan int, n) with an ident size, len("abc") (Go: valid - string has a length), len(m) on maps/slices/chans, delete(m, k), close(ch) on an ident.

The literal-only scope keeps this the same shape as the landed #149/#163 const-arith checks (HYB-E6001/E6002): reject the compile-time-detectable literal misuse; run-time misuse (make(chan int, n) with a variable that happens to be -1) stays a run-time panic, matching Go.

Sub-family B - defer accepts non-call expressions; registry code HYB-E2001 exists but is never raised

diagnostics.json already registers HYB-E2001 defer-non-call ("defer requires a call expression") - but grep shows the code is never constructed anywhere in hy-check (no E2001 in any .rs). So the documented diagnostic has zero enforcement:

Go's grammar requires defer's operand to be a function call (CallExpr); defer ch <- x and defer <-ch are compile errors in Go. Hybrid's own AST documents the intended form (/// defer f.close()?) - Expr::Call (and Try wrapping a call) should be the only accepted shapes.

Suggested fix direction

  1. Wire HYB-E2001 where Stmt::Defer is walked: after unwrapping Paren/Try, the operand must be Expr::Call; otherwise raise the already-registered diagnostic with its existing fix-it ("defer the call itself: defer f.close()?").
  2. New registry code (E2003/E2004 are claimed by open PR fix(hy-check): validate '?' operand error shape and propagation scope (fixes #155) #167; E2005 is free) for builtin-argument validation: an arith.rs-style walker (check_stmt_builtin_call) covering make (arity 1-2; size literal must be a non-negative int - Str/Float/Bool/Char/negative-Int reject), len/cap (arity 1; Int/Float/Bool/Char literal operand reject; len of Str/RawStr stays valid, cap of string rejects), delete (arity exactly 2), close (arity 1; literal operands reject - literals can never be channels), new (literal operand rejects - new takes a type).

Both families match the maintainer's landed pattern for this class (#149?E6001, #148?E6002, #103?while reservation): accepted-but-unbuildable emission gets a check-time rejection with a teachable message.

Repro dir (probe .hy + emitted .go.go side by side): %TEMP%\opencode\hunt\p2-*.hy.

Activity

  1. added a commit that references this issue on Oct 15, 2026
  2. added a commit that references this issue on Oct 16, 2026
    ad9b692
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