Repository navigation
Description
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-881accepts an explicitly annotated non-void function whose checked body isvoidwhenevertype_has_default_value(ret_type)is true. It does not inspect sibling branches for explicit value returns.src/SemaCheck.w:6987-7020has the same shape for closure return checking: aTY_VOIDbody is accepted throughtype_has_default_valuewith no mixed-body gate.src/SemaCheck.w:8999-9025already knows a broader set ofDefault-like types than MIR lowering currently emits.src/MirLower.w:7440-7445lowers implicit defaults only forvoid,bool, and integer return types.src/ComptimeEval.w:837-916has a comptime default path, but it also applies after aCV_VOIDbody without the mixed-body gate.- Existing tests in
test/spec/spec_ss04_10_implicit_default_return.wcover positive defaulting cases and missing-Default negative cases, but not the spec's "any explicit value return disables defaulting" rule.
Implementation plan
- Add a semantic helper that classifies whether a function or closure body contains any explicit value-producing path:
return exprwith a non-Unitexpression, or a non-Unittail 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. - In
SemaCheck.wfunction and closure return checks, when the checked body type isTY_VOIDand 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". - Keep the
type_has_default_valuecheck for bodies with no explicit value-producing path. - 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.
- 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 1falling through on the false path.fn f(flag: bool) -> i32: if flag: 1where one branch has a non-Unittail and the other falls through.- equivalent closure coverage if closures keep a separate return checker.
- Keep positive coverage where the body has only
Unitstatements and falls through toDefault. - 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, andwith 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
ehartford commentedon Jun 11, 2026
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.