Repository navigation
D43: infer an unannotated return from the tail under one rule; fix #1179, #1180 - #1181
Merged
Merged
Conversation
added 5 commits
September 18, 2026 04:22
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 anif,if letormatch:else, a partial match on bool/enum, or an empty armelse: {}/_ => {}) makes the tail a statement and the functionUnit— §9.1 and §9.7 already say such a construct is never a value;cannot infer return type: if arms have types i32 and Unit; add -> i32 or -> Unit. The compiler never picksUnit, 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]andtest_*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
else: 0for a missing else #1179:parse_if_letdesugared a missingelseto the literal0. An unannotatedif let …: x = vfunction inferredi32and returned a fabricated0when the pattern did not match.check_fstringaccepted aUnitinterpolant and codegen formatted the register (print(f"{pick(true)}")printed67682320). Nowcannot interpolate a Unit value.matchkept its first arm's type across a non-joining arm; MirLower stored aUnitarm into ani32place (invalid MIR).Unitargument is valid only when the callee's own parameter isUnit. On its first battery it found a second real defect:fallback_expr_typetyped a subscript on a userIndexPlaceasUnit, solet r = g[0]and the temporaries ofg[i] += xwereUnit-typed (it ran only because codegen used the callee's return type). Fixed by typing the subscript as whatgetreturns.Cost, measured
Sweep with a
:devstage1 over the compiler source plus 2,861 files inlib/,tools/,test/,examples/,build/: two functions needed-> Unit, both insrc/(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.wis the regression test for theIndexPlacetyping.Battery (pinned seed v0.15.2.0,
WITH=$PWD/src/main)buildrc=0 ·:fixpointrc=0 (stage2 == stage3) ·:testrc=0 — 1040 behaviour, 768 compile-error, 210 spec, 56 phase, 17 internals, 16 codegen ·:test-greenrc=0 ·:last-greenrc=0 ·analyze … audit:allon the D43 fixture: 0 violations.:move-audit/:drop-auditnot run: no ownership, drop-scheduling or ABI change.Follow-ups (in D43, not in this PR)
Demanded-join diagnostic names the
Unitarm; lintpub fnwithout->; lint an effect-free expression in a discarded position (v == 3alone on a line); decide whether an assignment arm is ever an implicit result (one of the two swept hits had that shape).