Skip to content

D43: infer an unannotated return from the tail under one rule; fix #1179, #1180 - #1181

Merged
ehartford merged 5 commits into
mainfrom
d43-tail-inference
Sep 18, 2026
Merged

ehartford merged 5 commits into
mainfrom
d43-tail-inference

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Closes #1178, #1179, #1180.

What

D43 (ruled, docs/decisions.md). A function or closure with no return annotation inherits its tail's type. When the tail is an if, if let or match:

  • a missing arm (no final else, a partial match on bool/enum, or an empty arm else: {} / _ => {}) makes the tail a statement and the function Unit — §9.1 and §9.7 already say such a construct is never a value;
  • otherwise the reaching arms must have one type, which is the return type;
  • otherwise: cannot infer return type: if arms have types i32 and Unit; add -> i32 or -> Unit. The compiler never picks Unit, an arm, or a default.

A single-statement body and a block body now go through one path (Sema.infer_tail_node, handed from the body to its block tail to the branch and its arms). main, @[entry] and test_* functions do not infer.

Mission ¶2 refined to the uniqueness test the guardrail sentence already meant (docs/mission.md, CLAUDE.md, AGENTS.md). Spec §9.1 defines inferred returns and assignment's type; §3.8 cites it. Both wordings blessed verbatim by Eric.

Bugs fixed

Cost, measured

Sweep with a :dev stage1 over the compiler source plus 2,861 files in lib/, tools/, test/, examples/, build/: two functions needed -> Unit, both in src/ (MirCore.mark_place, SemaCheck.demand_generic_iter_next).

Tests

test/behavior/behav_d43_tail_inference.w (every D43 matrix row, both body spellings), test/compile_errors/err_d43_*.w (5), err_fstring_unit_interpolant.w, test/internals/mir_unit_call_argument_test.w (hand-built MIR, 5 cells); behav_index_place_user.w is the regression test for the IndexPlace typing.

Battery (pinned seed v0.15.2.0, WITH=$PWD/src/main)

build rc=0 · :fixpoint rc=0 (stage2 == stage3) · :test rc=0 — 1040 behaviour, 768 compile-error, 210 spec, 56 phase, 17 internals, 16 codegen · :test-green rc=0 · :last-green rc=0 · analyze … audit:all on the D43 fixture: 0 violations. :move-audit/:drop-audit not run: no ownership, drop-scheduling or ABI change.

Follow-ups (in D43, not in this PR)

Demanded-join diagnostic names the Unit arm; lint pub fn without ->; lint an effect-free expression in a discarded position (v == 3 alone on a line); decide whether an assignment arm is ever an implicit result (one of the two swept hits had that shape).

Eric Hartford added 5 commits September 18, 2026 04:22
…sion ¶2

A missing arm forces Unit; mixed written arms are a failure to infer.
Mission ¶2 now states the uniqueness test the guardrail sentence already
meant. Spec wording for §9.1/§3.8 is not yet blessed. #1178 #1179 #1180
A function or closure with no return annotation inherits its tail's type.
A branching tail is Unit when an arm is missing (no else, a partial match on
bool or enum, or an empty arm) and an undemanded join otherwise; written arms
that do not unify are 'cannot infer return type'. A block body hands the role
to its tail, so both body spellings agree.

#1179: parse_if_let desugared a missing else to the literal 0; an unannotated
else-less if let returned a fabricated 0 when the pattern did not match.
#1180: check_fstring accepted a Unit interpolant and codegen formatted the
register (pick printed 67682320); statement-position match kept its first
arm's type across a non-joining arm and MirLower stored Unit into i32.

Sweep with a :dev stage1 over src plus 2,861 files in lib, tools, test,
examples and build: two functions needed -> Unit, both in src.

Open in #1180: the typed MIR validator does not check call argument types.
An omitted -> infers the return type from the body's tail; a branching tail
with a missing arm is Unit, arms that do not have one type cannot be inferred.
§3.8's inferred function return now cites §9.1. Wording blessed verbatim by
Eric 2026-09-18. #1178
Nothing coerces to or from Unit, so a Unit operand is a valid argument only
when the callee's own parameter is Unit. A named callee with a body states its
parameter types; one without a body is a runtime or extern function. The rule
would have caught the f-string that handed fmt_to_str a Unit call result while
--validate-all passed. Tested against hand-built MIR. #1180
fallback_expr_type's NK_INDEX branch asked indexed_element_type, which knows
only built-in containers, and fell through to Unit for a user IndexPlace. The
read local of 'let r = g[0]' and the get/sum temporaries of 'g[i] += x' were
Unit-typed; it ran because codegen used the callee's real return type. The
typed validator's Unit-argument rule rejected it
(behav_index_place_user.w: 'call argument 2 is Unit but the callee parameter
is not'). #1180
@ehartford
ehartford merged commit c1e3292 into main Sep 18, 2026
5 checks passed
@ehartford
ehartford deleted the d43-tail-inference branch September 24, 2026 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unannotated-return tail if/if-let/match: spelling-dependent typing, rejected Unit bodies, and a silent garbage return

1 participant