Repository navigation
fix: resolve hy-check: ? operator never validated against operand or enclosing scope (E4, T20, E3b) + closure error scoping in hy-emit (#155) - #469
Conversation
124143e to
b6164da
Compare
|
Thanks for the thorough write-up — the Two notes on the defer interaction, one of which I'd consider blocking: 1.
fn load() (int, error) { return 1, nil }
fn close_it(n int) error { return nil }
fn run() error {
defer close_it(load()?) // argument `?` — legitimately hoistable
return nil
}Argument-position
Your tests cover the outer form ( 2. The PR edits the E2001 fixit away from - "defer the call itself: `defer f.close()?` instead of `defer f.close`"
+ "defer the call itself: `defer f.close()` instead of `defer f.close`"If the ruling is "no On the outer form itself: rejecting |
|
Thanks @Leimiu-G for the spot-on review! Rebased onto latest
|
b6164da to
65c229f
Compare
|
Thanks for adopting the split — narrowing HYB-E2001 to Two gaps this branch still has — both covered by #288 v2 (in flight; the wave-2 integration bar is addressed) — flagging so the two PRs land as complements rather than duplicates: 1. A parenthesized defer operand emits invalid Go. The defer (fmt.println("done"))lowers to 2. One policy heads-up, since the two PRs now collide on test expectations: the wave-2 triage on #288 recorded that main intentionally treats the Try-wrapping-call defer operand as the documented legal shape at check time ("builtins.rs unwraps Try today"), and called an outright reject "a policy change vs landed code". Your E2001 on the direct operand is the buildable resolution of R4 — an outermost Two smaller notes:
|
65c229f to
b147f14
Compare
|
Thanks @Leimiu-G — updated in �147f14 so the two PRs stay completely aligned:
|
|
Confirmed on Two small hardening notes for the triage pass — defense-in-depth rather than correctness, since the checker covers both paths today:
Either way this is in good shape — thanks again for the collaboration. |
… closure error scoping (limitlessprojects#155)
b147f14 to
005a65d
Compare
|
Thanks @Leimiu-G! Applied both defense-in-depth hardening updates and rebased onto latest
Verified |
Integrate limitlessprojects#288 (HYB-E2001 defer-operand, single owner in builtins.rs): take main's defer emission in hy-emit/stmt.rs; ignore the new SelectArmKind::Send is_move field in try_check.rs; remove try_check's own defer-E2001 arm and the duplicate check_stmt_defer_call in the closure walk, since defer f()? is now the documented legal shape; drop the three limitlessprojects#155 tests that asserted the superseded behaviour (defer_operand.rs covers it).
? operator never validated against operand or enclosing scope (E4, T20, E3b, defer E3) + closure error scoping in hy-emit (#155)? operator never validated against operand or enclosing scope (E4, T20, E3b) + closure error scoping in hy-emit (#155)
Resolve hy-emit/expr.rs conflict with pr-536: keep limitlessprojects#155's closure state restores (in_main_closure, cur_fn_error_returns) alongside main's restore_scope and deferred body_res? (replacing the old bindings.retain).
…gnment (limitlessprojects#726) SPEC E1: "Functions signal failure by returning `error` as the final result: `(string, error)`, `(total int, err error)`, **or bare `error`**." SPEC E3/E4: `?` applies to an error-returning call inside a function whose results end in `error`, or in `main`. So `step()?` on a `fn step() error` is the simplest legal use — the Hybrid spelling of `if err := step(); err != nil { return err }`. `hyb check` accepted it correctly. The LOWERING was wrong: ```go func run() error { hyT0, hyErr1 := step() // step returns ONE value if hyErr1 != nil { return hyErr1 } hyT0 // and: evaluated but not used return nil } ``` Two causes, both in how the `?` hoist learns a call's arity: 1. `fn_shapes` stored `results.len()`, which INCLUDES the trailing `error`, so `fn step() error` recorded `(1, true)` and never matched the dedicated zero-value path (`(0, true)`). The descriptor tables spell the count the other way — `serde_json.value.set` is `(0, true)`, it returns only an error — so only user-declared functions hit this. Now stores the value-result count, `results.len() - usize::from(err)`. 2. A method on a USER struct was in no table at all, so `s.save()?` found no entry and fell through to the two-name form. Added `method_results`, keyed `(receiver struct name, method name)`, filled from `file.fns()` — a method is a `Decl::Fn` with a receiver, so it was already in that iterator and only needed the receiver's struct name to become findable. `call_results` joins it through the existing `var_struct` registry, which already maps a binding to its struct name. Verified against main @ aa4a9d9: - 4 bare-`error` shapes (free fn, fn with args, user method, in `main`): `hyb check` ACCEPT + `go build` FAILS before, ACCEPT + BUILDS after - the two-result forms (`(int, error)`, named `(n int, err error)`) still build, and `two_result_call_under_try_keeps_its_value_temp` pins that the fix did NOT collapse the value token for a call that genuinely has one - corpus 113 -> 114 (1 ACCEPT fixture), verdict agreement 114/114, tripwire not fired - differential reject-side 76/76, accept-side 36/38 (the 2 are the known cgo-bridge programs, unchanged from baseline) - managed gate PASS, test registration OK, conflict markers none, census OK - 7 new tests in `hy-emit/tests/bare_error_try_lowering.rs`; hy-emit and hy-check suites green The fixture is deliberately an ACCEPT, not a REJECT: the checker was right to accept, and it reported 0 diagnostics both before and after the fix, so a reject fixture would have been the wrong shape and would have passed for the wrong reason. That is also why the real assertion lives at the emitter level — the harness alone cannot see a lowering bug behind a clean check, which is exactly how this survived. Not a duplicate of limitlessprojects#155 / limitlessprojects#166 / limitlessprojects#469: those cover `?` on a call that does NOT return `error` (option/int results, closures with no error result), which should be REJECTED. Those are still open and unlanded. This change is about lowering a call that is already legal — it adds no new rejection, so the two do not interact. Refs limitlessprojects#726.
…gnment (limitlessprojects#726) SPEC E1: "Functions signal failure by returning `error` as the final result: `(string, error)`, `(total int, err error)`, **or bare `error`**." SPEC E3/E4: `?` applies to an error-returning call inside a function whose results end in `error`, or in `main`. So `step()?` on a `fn step() error` is the simplest legal use — the Hybrid spelling of `if err := step(); err != nil { return err }`. `hyb check` accepted it correctly. The LOWERING was wrong: ```go func run() error { hyT0, hyErr1 := step() // step returns ONE value if hyErr1 != nil { return hyErr1 } hyT0 // and: evaluated but not used return nil } ``` Two causes, both in how the `?` hoist learns a call's arity: 1. `fn_shapes` stored `results.len()`, which INCLUDES the trailing `error`, so `fn step() error` recorded `(1, true)` and never matched the dedicated zero-value path (`(0, true)`). The descriptor tables spell the count the other way — `serde_json.value.set` is `(0, true)`, it returns only an error — so only user-declared functions hit this. Now stores the value-result count, `results.len() - usize::from(err)`. 2. A method on a USER struct was in no table at all, so `s.save()?` found no entry and fell through to the two-name form. Added `method_results`, keyed `(receiver struct name, method name)`, filled from `file.fns()` — a method is a `Decl::Fn` with a receiver, so it was already in that iterator and only needed the receiver's struct name to become findable. `call_results` joins it through the existing `var_struct` registry, which already maps a binding to its struct name. Verified against main @ aa4a9d9: - 4 bare-`error` shapes (free fn, fn with args, user method, in `main`): `hyb check` ACCEPT + `go build` FAILS before, ACCEPT + BUILDS after - the two-result forms (`(int, error)`, named `(n int, err error)`) still build, and `two_result_call_under_try_keeps_its_value_temp` pins that the fix did NOT collapse the value token for a call that genuinely has one - corpus 113 -> 114 (1 ACCEPT fixture), verdict agreement 114/114, tripwire not fired - differential reject-side 76/76, accept-side 36/38 (the 2 are the known cgo-bridge programs, unchanged from baseline) - managed gate PASS, test registration OK, conflict markers none, census OK - 7 new tests in `hy-emit/tests/bare_error_try_lowering.rs`; hy-emit and hy-check suites green The fixture is deliberately an ACCEPT, not a REJECT: the checker was right to accept, and it reported 0 diagnostics both before and after the fix, so a reject fixture would have been the wrong shape and would have passed for the wrong reason. That is also why the real assertion lives at the emitter level — the harness alone cannot see a lowering bug behind a clean check, which is exactly how this survived. Not a duplicate of limitlessprojects#155 / limitlessprojects#166 / limitlessprojects#469: those cover `?` on a call that does NOT return `error` (option/int results, closures with no error result), which should be REJECTED. Those are still open and unlanded. This change is about lowering a call that is already legal — it adds no new rejection, so the two do not interact. Refs limitlessprojects#726.
Root Cause Analysis (
#155)hy-checknever validatedExpr::Tryagainst its operand or enclosing scope (E4,T20,E3b):Expr::Trywas only walked transparently as its inner operand, so:?on anoption[T]result (v := find("a")?) was accepted with0diagnostics (T20violation: absence and failure stay distinct) and emitted Go that failedgo build(assignment mismatch: 2 variables but find returns 1 value).?on a call returning a non-error type (x := five()?,fmt.println("hi")?,serde_json.new_object()?) or a shadowed non-function local (run := 42; run()?) was accepted with0diagnostics (E4violation).?inside a function or closure whose declared results do not end inerror(and is neithermainnor anE11http.handlerconversion target) was accepted with0diagnostics (E3bviolation).hy-emitleakedin_main_closureandcur_fn_error_returnsinto nestedExpr::FnLitclosures:Expr::FnLitincompiler/crates/hy-emit/src/expr.rs,self.in_main_closureandself.cur_fn_error_returnswere not scoped to the closure's own declaredresults, causing closures insidemain()or an error-returning function to inherit the outer function's error return target instead of propagating via the closure's own(..., error)signature (or refusing when the closure has noerrorreturn).Fix Summary (Reconciled with
#288onmain)compiler/crates/hy-diag/data/diagnostics.json):HYB-E4003(try-operand-not-error, ruleE4),HYB-E4004(try-on-option, ruleT20), andHYB-E4005(try-cannot-propagate, ruleE3b) in theE400xrange (avoiding anyE200xnumbering collision).HYB-E2001ownership stays solely inbuiltins.rsas established by#288.hy-checkTry-Legality Pass (compiler/crates/hy-check/src/try_check.rs,compiler/crates/hy-check/src/lib.rs):try_check::check_tries_in_fnwith lexical block/closure scope tracking and type-alias resolution:HYB-E4004(rule: T20) when?is applied to anoption-returning call (some,none, user fn/method/closure returningoption[T]).HYB-E4003(rule: E4) when?is applied to a non-call or a call known not to returnerror(user fn/method/closure, non-function local binding, builtin, or foreign package/handle method).HYB-E4005(rule: E3b) when?is used inside a function or closure that cannot propagate errors (neithermain, nor ending inerror, nor anE11http.handler(...)closure).hy-emitClosure Error-Scoping & Non-Error Call Guards (compiler/crates/hy-emit/src/expr.rs,compiler/crates/hy-emit/src/stmt.rs):self.in_main_closureandself.cur_fn_error_returnsaroundExpr::FnLitemission, computingcur_fn_error_returnsfrom the closure's own declaredresults.hoist_one_tryandemit_try_defineagainst calls whosecall_resultsshape hashas_error == false.Verification
cargo test -p hy-diag -p hy-syntax -p hy-check -p hy-emit: 888/888 passed (0warnings,0failures)