Skip to content

Unannotated fn with early 'return <value>' finalizes as Unit return; caller branches on undef — silent runtime trap #653

Closed

Description

@ehartford

Spec reference

  • §9.1 Functions — "Return type only when you return something": an unannotated fn whose body returns a value must infer that value's type.
  • §4.6 Type Inference — bidirectional local inference inside bodies.

Minimal reproduction

fn f(v: i32):
    if v == 3:
        return true
    false

fn main:
    if f(3): print("yes")
    else: print("no")

with build succeeds with zero diagnostics. Running the binary prints nothing and dies with SIGTRAP (rc=133).

Expected vs actual

Expected: f infers -> bool (early return true + tail false), program prints yes.

Actual: Sema finalizes the signature return as Unit. Proven with with analyze on the repro (dirty-tree analyzer build):

row  sema  signature  f  ... return=14        # type 14 = Unit (13 = bool)
row  mir   local      f  ... _0 ty=14 mut=1   # unit return place

LLVM IR (via with ir):

define internal void @f(i32 %0) {
  ...
mir.bb1:
  store i1 true, ptr %.sroa.0.sroa.0   ; early-return value written to a dead temp
  ret void

Caller:

call void @f(i32 3)
switch i32 undef, label %mir.bb3 [ i32 1, label %mir.bb2 ]   ; branches on undef

At -O1 the undef switch traps at runtime.

Trigger matrix (all verified)

Body shape Result
early return true + tail false trap
early return true + explicit return false tail trap
tail true only, no early return ok
same as row 1 but annotated -> bool ok

The trigger is the presence of an early return <value> statement under return-type inference. Loops are irrelevant (first row has none).

Reproduces identically on installed with v0.15.1-gd527f2992 and on the current D7 dirty-tree compiler — a long-standing bug, not introduced by the receiver migration.

Notes

  • Sema.infer_unannotated_function_return_type (src/SemaCheck.w:4810) reads body_return_type_info(body); by inspection saw_value_return/value_type should yield bool here, yet the finalized sig is Unit — the exact wrong line is not yet pinned; needs an LLDB pass on the repro before fixing.
  • Survives checking because if/while condition types are never validated (see companion issue) and no Sema/MIR/LLVM return-consistency audit exists yet (planned with analyze addition).
  • This is the same defect that produced the line_selected -> void -> switch undef failure in the D7 migration repair tool (docs/handoff.md §5); the tool's fix was the -> bool annotation workaround.

Workaround

Annotate the return type explicitly (-> bool).

Activity

  1. ehartford commented on Jul 14, 2026

    @ehartford
    ContributorAuthor

    Fixed in e7ffc47. Root cause: check_return computed the return value's type but check_expr does not self-record bool literals or binary-op results into typed_expr_types, so return-type inference read the value back as 0 and misclassified the value-return as bare — finalizing Unit. Fix records the type at the single choke point in check_return. Verified: repro now prints 'yes'; behavior fixtures behav_infer_return_early_{bool_lit,comparison,arith,logical,bool_explicit_tail} + controls; the mixed bare/value case now correctly errors (err_infer_return_mixed_bare_value). Full gate green incl. fixpoint and a signature-fact flip sweep showing 0 changed inferred returns across compiler sources.

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