Repository navigation
Description
[soundness] ? is never checked against its operand — ? on option-returning, int-returning, and closure-local calls is accepted with 0 diagnostics and emits non-compiling Go (E4/T20 type error unimplemented; closure ? binds to the wrong scope)
Revision (git rev-parse HEAD)
f99d6d0b37722de206015be9e1c59b04e827c794 (origin/main, 2026-10-15 04:45 UTC; hyb built from this exact tree).
Summary
SPEC-LANGUAGE E4 pins two type errors for the try operator:
"
?on an expression that is not an error-returning call is a type error;?onoptionis a type error (T20) — absence and failure stay distinct."
And E3b pins where a ? may propagate:
"Where the propagation cannot land (no error result, not main, not an E11 handler), it is a compile error — never a silent swallow."
None of this is checked. In hy-check, Expr::Try exists only as a transparent operand-walk (shape.rs:155: Expr::Move {..} | Expr::Try { operand, .. } | Expr::Paren {..} => self.shape_of_expr(st, operand)) — the checker never inspects whether the operand returns error at all, whether the enclosing function (or closure) can host the propagation, or whether an option result was confused with a failure. ? is not a checker special case in the sense the spec means; it is a no-op case.
Consequences, all reproduced on main @ f99d6d0 with OK (0 diagnostics):
?on anoption-returning call (the T20 type error): accepted; the emitter hoists a two-name binding from a one-result call → emitted Go fails withassignment mismatch.?on a plainint-returning call (the E4 "not an error-returning call" type error): same — accepted, non-compiling Go.?inside a closure whose results do not end inerror(the E3b "compile error — never a silent swallow" case): accepted, and the emitter binds the propagation to the enclosing main's exit contract, violating E4's lexical rule ("binds to the closure's own results, not the enclosing function's") — before the emitted Go fails to compile anyway.?in adeferargument: the checker accepts; the emitter refuses ("?outside a binding position") — a check→emit gap.
The four smallest .hy programs that demonstrate it
R1 — ? on an option-returning call (T20 type error; checker: OK)
// try_r1_option.hy — OK (0 diagnostics) on main @ f99d6d0
fn find(k string) option[int] {
if k == "a" {
return some(1)
}
return none
}
fn main() {
v := find("a")?
fmt.println(v)
}hyb check → OK (0 diagnostics). hyb build --emit-go emits (t6-out.go):
hyT0, hyErr0 := find("a") // line 28go build → .\t6-out.go:28:16: assignment mismatch: 2 variables but find returns 1 value.
The emitter's hoist_one_try (hy-emit/src/stmt.rs) only special-cases call_results == Some((0, true)) (error-only calls); everything else — including a 1-result, no-error call — gets the hyT, hyErr := two-name form.
R2 — ? on a plain int-returning call (E4 type error; checker: OK)
// try_r2_int.hy — OK (0 diagnostics) on main @ f99d6d0
fn five() int {
return 5
}
fn main() {
x := five()?
fmt.println(x)
}hyb check → OK (0 diagnostics). go build on the emission → .\t7-out.go:15:16: assignment mismatch: 2 variables but five returns 1 value.
R3 — ? in a closure without an error result (E3b compile error missing; propagation binds to the wrong scope)
// try_r3_closure.hy — OK (0 diagnostics) on main @ f99d6d0
fn five() int {
return 5
}
fn main() {
f := fn() {
x := five()?
fmt.println(x)
}
f()
}hyb check → OK (0 diagnostics). Emitted (t8-out.go, abridged):
hyErr := func() (hyErr error) {
f := func() {
x, hyErr0 := five() // line 16: assignment mismatch
if hyErr0 != nil {
hyErr = hyErr0 // the closure's "?" assigns the OUTER main-exit error
return
}
fmt.Println(x)
}
f()
return
}()
if hyErr != nil { ...; os.Exit(1) }Two violations in one emission: the closure's ? is wired into main's exit contract (E4 says the closure's own results govern — it has none, so E3b demands a compile error), and the emitted Go does not compile (.\t8-out.go:16:17: assignment mismatch: 2 variables but five returns 1 value).
R4 — ? in a defer argument (checker: OK; emitter: unsupported)
// try_r4_defer.hy — OK (0 diagnostics) on main @ f99d6d0
fn cleanup() error {
return nil
}
fn main() {
defer cleanup()?
fmt.println("done")
}hyb check → OK (0 diagnostics). hyb build → error [emit]: emitter (MVP-0): not yet supported: \?` outside a binding position — E3 desugars binding-position and hoisted-expression `?` only. Same check→emit gap shape as #103, on the ?`/defer pair.
What the checker should say
- R1:
HYB-E…: \?` on an option result — absence and failure are distinct (T20); use match/if-ok or an (T, error) call`. - R2:
HYB-E…: \?` on a call that does not return error (E4)`. - R3:
HYB-E…: \?` in a closure whose results do not end in error — the propagation has nowhere to land (E3b)`. - R4: a defer-specific diagnostic (or an explicit not-yet-supported error at check time), so
hyb checkandhyb buildagree.
What it actually said
try_r1_option.hy: OK (0 diagnostics)
try_r2_int.hy: OK (0 diagnostics)
try_r3_closure.hy: OK (0 diagnostics)
try_r4_defer.hy: OK (0 diagnostics)
Root cause
hy-checkhas no?-legality pass.Expr::Tryappears only in operand-walk positions:shape.rs:155(shape),walk.rs:327/488/548(uses/calls/paths),closures.rs:479(fn-lit collection),sendability.rs:191catch-all (result-shape classification). Nothing asks the one question E4/T20 require: does this call returnerror, and can this site propagate it? The fn table (self.fns) already carries result signatures —call_shapeconsults it for shapes — so the legality check has the data it needs.- The emitter guesses instead.
hoist_one_trykeys offcall_resultsalone:(0, true)→ error-only form; anything else →hyT, hyErr := call. A one-result, no-error call silently takes the two-name form and emits Go that cannot compile.propagate_errorthen picks a landing site by emission context (handler_writer→in_main_closure→cur_fn_error_returns), which is how R3's closure?ends up bound to main's exit contract. - SPEC E3's claim that "the checker expands this before loan checking" does not match the implementation — the desugar lives in the emitter (
try_tempshoisting,hy-emit/src/lib.rs:354,hy-emit/src/stmt.rs). The checker never sees the hoisted bindings or the early-return propagation paths.
Suggested fix
- In
hy-check, validate everyExpr::Trysite: resolve the operand's call signature fromself.fns(and method/builtin tables); reject when the call does not end inerror(E4), when the result isoption[T](T20), and when the enclosing function/closure has noerrorresult and is notmain/an E11 handler (E3b). All four repros above must REJECT athyb checktime. - Until the check exists, make
hoist_one_tryfail closed whencall_resultsreports a non-error shape (Some((n, false))) instead of emitting the two-name form. - Regression tests: R1-R4 reject; example 02/05's legitimate
?uses keep passing.
Relation to known issues / PRs
- [soundness] Unbound
match some(x)pattern bindings,some(m)/opt.unwrap()alias drops, and ignoredDecl::TypeAliasbypass A1c/A1b and race with 0 diagnostics #66 / PR fix(hy-check): resolve option some/unwrap/match bindings and type aliases (#66) #67 — disjoint: those fixsome(m)/.unwrap()/.or()call-shape inference,Decl::TypeAliastable skips, and match-pattern binding shadowing (race paths). This report is about the legality of?itself — which the other report explicitly does not touch. - while cond { } silently accepted as ident+expr+block - emitted Go fails go vet (undefined: while); reserve while like mut #103 (while accepted, emitted Go fails go vet) — same gap shape (checker accepts what the toolchain rejects); R4 is the
?-in-defer instance. R1-R3 are worse than while cond { } silently accepted as ident+expr+block - emitted Go fails go vet (undefined: while); reserve while like mut #103's shape: the checker misses a spec-pinned type error, and R3 additionally mis-scopes the propagation. - SPEC-LANGUAGE.md §6.1 (E3, E3b, E4), §2.6 (T20) — the normative text this report tests.
Claimed severity
Medium (BOUNTY.md: "a guarantee weaker than documented, or a misleading pass"). Four spec-pinned rejections never fire; three of the four programs emit non-compiling Go from a 0-diagnostic pass, and R3's emitted code — had the operand returned (int, error) — would have quietly rerouted a closure's error into main's exit path, a semantic change E4 forbids. Not a data race, but it voids the absence/failure distinction the error model is built on.
Report prepared with AI assistance (WorkBuddy agent); checker verdicts reproduced mechanically against main @ f99d6d0 — hyb check and hyb build --emit-go + go build on each .hy file quoted above.
Activity
fix(hy-check): implement the E4 / T20 / E3b rules for the try operator
Leimiu-G commentedon Oct 16, 2026
Follow-up: the fix is up as #288.
For this report's R4 (
defer cleanup()?), the checker now reports thespec-mandated HYB-E2001 — SPEC-LANGUAGE §7 P1: "
deferof a non-callexpression is a compile error (
HYB-E2001)" — at the operand's own span,instead of passing the program (0 diagnostics) and letting the emitter fail
late with the unrelated "
?outside a binding position" message.The same PR also covers the sibling case in the same family: a
?inside adeferred call's arguments (
defer f(g?)) hit the identical check→emit gapeven though P1 evaluates a deferred call's arguments at defer time. It is now
hoisted to a binding before the
defer.While wiring E2001 up, two spots turned out to contradict the rule and only
became user-visible once the diagnostic actually fires: the registry fix-it
and the
DeferAST doc comment both showeddefer f.close()?— the very shapethe rule rejects. Both corrected to
defer f.close().Verified:
cargo test --workspace(rustc 1.99.0, x86_64-pc-windows-gnu) →hy-check 105/105, hy-emit 111/111, all other suites green. The only failures
are the
hyb generatetests, and only because this box has no Go toolchain(
gofmt: program not found).R1/R2/R3 from the report (the
TypeShapeerasure family) are untouched here —they need the raw
TypeExprat the?site and are a deeper change; notclaimed by this PR.
check: reject non-call `defer` operands (HYB-E2001); hoist `?` in def…
check: defer-operand rule with closure wiring; emit: hoist arg-? and …
fix: resolve hy-check: `?` operator never validated against operand o…
gelismarvel commentedon Oct 17, 2026
/claim #155
Fixes #155
Submitting verified fix PR for #155.
?operator never validated against operand or enclosing scope (E4, T20, E3b) + closure error scoping in hy-emit (#155) #469 (fix: resolve hy-check:?operator never validated against operand or enclosing scope (E4, T20, E3b) + closure error scoping in hy-emit (#155) #469)fix(hy-check,hy-emit): enforce ? operator legality (E4, T20, E3b) and…
fix(hy-check,hy-emit): enforce ? operator legality (E4, T20, E3b) and…
fix(hy-check,hy-emit): enforce ? operator legality (E4, T20, E3b) and…
fix(hy-check,hy-emit): enforce ? operator legality (E4, T20, E3b) and…
Merge origin/main into fix/issue-155-real-patch
Merge origin/main into fix/issue-155-real-patch
emitter(try): a bare-`error` call under `?` lowers to a one-name assi…
emitter(try): a bare-`error` call under `?` lowers to a one-name assi…
fix(hy-check, hy-emit): validate '?' operand error shape and propagat…