Skip to content

Return-type inference only covers single-expression bodies; 'return <value>' inside an inferred-Unit fn is silently accepted #659

Closed

Description

@ehartford

Spec references

  • §29.13 function forms (docs/with-specification.md:3251): "The return type -> TYPE is omitted when the function returns Unit (void)."
  • §4.6 inference (docs/with-specification.md:2127): "Inside function bodies, types are inferred. At module boundaries, types must be explicit."
  • Mission / AGENTS.md style: "Don't spell types the compiler can infer — omit a return type when the body makes it obvious (fn shout(s: str): s ++ \"!\")."

Facts (verified by running)

The implementation infers the return type for single-expression bodies only, including across module boundaries and for pub methods:

  • pub fn a(x: i32): x < self.n — infers bool, works.
  • Any block body with an early return false and a bool tail expression infers Unit — free functions and methods alike. Callers then see Unit ("argument expression 'a.contains(id0)' has type Unit").
  • The return false inside the inferred-Unit function is silently accepted — no diagnostic — so the error surfaces only at call sites (or as the misleading "return type does not implement Default").

Reduced matrix: scratchpad v_a.w (works) / v_b.w / v_c.w (fail). Real-world instance: src/compiler/foundation/{Arena,SourceMap,Diagnostic}.w were written expecting block-body inference; 6 of 12 test/internals files failed at the baseline (10f7771e) — masked until the spec suite went green because :test stopped at the earlier failure. Fixed for now by annotating the return types.

The fork to settle

  1. Extend inference to block bodies (early returns + tail expression must agree), which the mission text favors — but §4.6's "at module boundaries, types must be explicit" argues the opposite for pub surfaces, and single-expression inference already crosses module boundaries today.
  2. Keep omitted = Unit for block bodies (spec as written) — then the missing diagnostic is the bug: return <value> in a Unit-typed function must be rejected loudly, which would have caught the foundation modules at compile time.

Either ruling needs the diagnostic; the choice between them is a design call (decisions.md material).

Workaround in tree

Explicit -> TYPE annotations added to the affected foundation functions.

Activity

  1. ehartford commented on Jul 14, 2026

    @ehartford
    ContributorAuthor

    Fixed in e7ffc47 — same root cause as #653 (return value type not recorded for bool/binary kinds → inference misreads value-return as bare → Unit → undef branch trap). is_small(3) now prints 'small'. See #653 for the full analysis; fixtures behav_infer_return_early_comparison + behav_infer_return_early_logical cover this shape.

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