Skip to content

[soundness] f()? on a user function or method that returns bare error passes hyb check, then fails go build (assignment mismatch: 2 variables but f returns 1 value) (E1/E3) #726

Closed

Description

@Morphexe

The smallest .hy program that demonstrates it

package main

fn step() error {
    return nil
}

fn run() error {
    step()?
    return nil
}

fn main() {
    run()?
    fmt.println("ok")
}

Commit: main @ 91222f3e6042def689713806f7e66f1875689f8a. hyb built from that commit (cargo build -p hyb, rust 1.99.0; banner build pins: go1.26.9 / rust 1.99.0). Go 1.26.9, linux/amd64.

$ hyb check try_bare_error_call.hy
try_bare_error_call.hy: OK (0 diagnostics)
$ hyb build try_bare_error_call.hy -o try_bare_error_call
emitted try_bare_error_call.go
go build failed:
# command-line-arguments
out/try_bare_error_call.go:14:18: assignment mismatch: 2 variables but step returns 1 value
out/try_bare_error_call.go:24:19: assignment mismatch: 2 variables but run returns 1 value

What the checker should say

Nothing. The program is valid and should print ok.

  • SPEC-LANGUAGE E1 (line 972): "Functions signal failure by returning error as the final result: (string, error), (total int, err error), or bare error."
  • E3 (line 985) / E4 (line 1009): ? applies to an error-returning call inside a function whose results end in error, or in main. A statement step()? on an error-only function is the simplest legal use. It is the Hybrid spelling of if err := step(); err != nil { return err }.

What it actually said

OK (0 diagnostics), and then the emitted Go does not compile:

func run() error {
	hyT0, hyErr1 := step()        // step returns one value
	if hyErr1 != nil {
		return hyErr1
	}
	hyT0                          // also: `hyT0` evaluated but not used
	return nil
}

main gets the same broken pair inside its E6 wrapper.

The same happens for methods: fn (s *store) save() error + s.save()? emits hyT0, hyErr1 := s.save().

Cause: hy-emit has a dedicated zero-result path for this exact shape (Stmt::Expr → zero_try, and hoist_one_try emits hyErrN := call when call_results(..) == Some((0, true)), compiler/crates/hy-emit/src/stmt.rs around lines 24 and 766). But for user functions call_results reads fn_shapes, which lib.rs (around line 626) fills with f.results.len(). That count includes the trailing error, so fn step() error is recorded as (1, true) and never matches (0, true). The rust-descriptor and curated-method sources use the "non-error results" count, so only user-declared functions and methods hit this. User methods are not in fn_shapes at all (call_results returns None), so they fall through to the two-name form too.

Claimed severity

P2: claim-precision bug (accepted value should have been unproven)

(Accepted-but-unbuildable on a core E1/E3 pattern: any helper fn validate(...) error called with ?. Proposed tier: Medium.)

Anything else

Activity

  1. isHeSatoshi commented on Oct 19, 2026

    @isHeSatoshi
    Contributor

    Opened #791 with both halves of the fix, since your root-cause read was exactly right and the repro was unambiguous.

    Confirming your diagnosis and adding one thing it did not mention: user methods needed a second registry, not just a corrected count. call_results resolves a descriptor member through \lookup_member, but a user struct is not in \SHAPES, so \s.save()?\ found no entry at all rather than a wrong one. I added \method_results\ keyed (receiver struct, method), filled from \ ile.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. It joins through the existing \�ar_struct\ registry.

    Also worth recording: the fixture has to be an ACCEPT, not a REJECT. \hyb check\ reported 0 diagnostics both before and after, so a reject fixture would have been the wrong shape and would have passed for the wrong reason. The real assertion lives at the emitter level (\hy-emit/tests/bare_error_try_lowering.rs, 7 tests) because the corpus harness cannot see a lowering bug behind a clean check — which is exactly how this survived.

    Verified on main @ aa4a9d9: all 4 bare-\error\ shapes (free fn, fn with args, user method, in \main) ACCEPT+FAIL before and ACCEPT+BUILD after; the two-result forms still build, with a test pinning that the value token is NOT collapsed for a call that genuinely has one. Corpus 113 -> 114, 114/114 agreement, differential 76/76 reject and 36/38 accept (baseline).

  2. added 2 commits that reference this issue on Oct 22, 2026
    9eabeff
    7488d0c
  3. airdropia commented on Oct 29, 2026

    @airdropia
    Contributor

    PR #2246 opened: fix(hy-emit): a bare-error result is an error slot, not a value. CI pending.

  4. airdropia commented on Oct 29, 2026

    @airdropia
    Contributor

    PR #2246 rebased onto current main (1f0ff97), now MERGEABLE. The conflict was in hy-emit tests.rs — the test was re-added cleanly without touching main's newer defer tests.

  5. added a commit that references this issue on Oct 30, 2026
    6c5e7af
  6. added a commit that references this issue on Oct 30, 2026
    845e3e9
  7. added a commit that references this issue on Oct 31, 2026
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