Skip to content

Implicit default return: apply only when the body has no explicit value return #382

Closed

Description

@ehartford

Implicit default return: apply only when the body has no explicit value return

Summary

Implicit default return is supposed to fill in a Default value only for bodies that never explicitly produce a value. The compiler currently defaults falling-through functions even after a value-returning path appears elsewhere, so a refactor can silently turn a missing return into 0, false, or another default value. Fix the control-flow gate first, then align the generated default breadth with §4.10.

Spec

docs/with-specification.md:2130-2212 defines implicit default return. The important gate is at docs/with-specification.md:2197-2205: if any path has return expr or a non-Unit tail expression, implicit default return does not apply and a falling-through path is a missing-return error. The same section lists the built-in defaultable cases, including numeric types, bool, str, Option, Vec, HashMap, HashSet, derived Default, and compatible Result.

Current state

  • src/SemaCheck.w:875-881 accepts an explicitly annotated non-void function whose checked body is void whenever type_has_default_value(ret_type) is true. It does not inspect sibling branches for explicit value returns.
  • src/SemaCheck.w:6987-7020 has the same shape for closure return checking: a TY_VOID body is accepted through type_has_default_value with no mixed-body gate.
  • src/SemaCheck.w:8999-9025 already knows a broader set of Default-like types than MIR lowering currently emits.
  • src/MirLower.w:7440-7445 lowers implicit defaults only for void, bool, and integer return types.
  • src/ComptimeEval.w:837-916 has a comptime default path, but it also applies after a CV_VOID body without the mixed-body gate.
  • Existing tests in test/spec/spec_ss04_10_implicit_default_return.w cover positive defaulting cases and missing-Default negative cases, but not the spec's "any explicit value return disables defaulting" rule.

Implementation plan

  1. Add a semantic helper that classifies whether a function or closure body contains any explicit value-producing path: return expr with a non-Unit expression, or a non-Unit tail expression in a branch/block. Use AST layout from AGENTS.md (NK_RETURN.d0, NK_BLOCK.d2, NK_IF_EXPR, NK_MATCH_ARM) instead of guessing fields.
  2. In SemaCheck.w function and closure return checks, when the checked body type is TY_VOID and the declared/inferred return type is non-void, apply implicit default only if that helper returns false. Otherwise emit the normal missing-return diagnostic, not "return type does not implement Default".
  3. Keep the type_has_default_value check for bodies with no explicit value-producing path.
  4. Align MIR and comptime default materialization with the defaultable cases that Sema accepts. If that is too large for one patch, split it explicitly after landing the mixed-body gate so Sema cannot accept values MIR cannot lower.
  5. Audit any implicit default helper duplication between normal MIR and comptime evaluation so both paths reject mixed bodies consistently.

Test plan

  • Add compile-error coverage for:
    • fn f(flag: bool) -> i32: if flag: return 1 falling through on the false path.
    • fn f(flag: bool) -> i32: if flag: 1 where one branch has a non-Unit tail and the other falls through.
    • equivalent closure coverage if closures keep a separate return checker.
  • Keep positive coverage where the body has only Unit statements and falls through to Default.
  • Add at least one non-integer defaulting smoke test for a type Sema already accepts, or file/split the default-breadth follow-up before claiming §4.10 complete.
  • Run with build, with build :fixpoint, and with build :test.

Acceptance criteria

  • A function body with any explicit value return no longer receives an implicit default on another falling-through path.
  • Pure statement-only bodies still receive the correct default when the return type implements Default.
  • Function and closure paths behave the same way.
  • Sema and MIR/comptime default support do not disagree about accepted return types.
  • Tests cover both the mixed-body rejection and the still-legal all-Unit defaulting case.

Dependencies

No hard dependency. If full Default materialization is split out, this issue should block or reference that follow-up because Sema currently accepts more defaultable types than src/MirLower.w:7440-7445 lowers.

Activity

  1. ehartford commented on Jun 11, 2026

    @ehartford
    ContributorAuthor

    Implemented in 09c8ac7. Implicit default return now applies only when the body has no explicit value-producing path; explicit return expressions, branch tails, closures, and comptime functions reject mixed fallthrough/value paths, while effect-only bodies can still default. Added positive and negative coverage, including closure and comptime cases. Verification passed after pulling main: build, fixpoint, full test suite, and test-green.

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