Skip to content

D43: only a body's own tail discards an assignment; an arm keeps its value (#1337) - #1339

Merged
ehartford merged 1 commit into
mainfrom
d43-narrow-assignment-discard
Sep 22, 2026
Merged

ehartford merged 1 commit into
mainfrom
d43-narrow-assignment-discard

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Narrows #1319 per Eric's ruling (2026-09-22): D43 as blessed decides. §9.1 "An assignment place = value is an expression whose type is the type of place; in statement or tail position its value is discarded", read together with D43's pinned test err_d43_cannot_infer_match.w. An assignment is discarded only as a statement or as a function's/closure's OWN body tail (single-statement body or last statement of the body block). As a written arm of a tail if/match — bare or as an arm block's tail — it keeps the place's type and joins under D43.

Fixes #1337.

What changed

Tests (stage1 from :dev)

Open question (for Eric)

fn inc -> i32: g += 1 returns i32.default() (0) per #1319's reading of §9.1 — the body tail is not a join, so D43's "-> i32 makes the tail a demanded join" sentence does not reach it. But after this ruling fn f(p) -> i32: if p: g = 1 else: g = 2 returns the assigned value, while the unbranched -> i32: g = 1 returns 0. Not changed here; flagging the asymmetry.

Battery not yet run (draft).

…keeps its value (D43, #1337)

Eric's ruling (2026-09-22): D43 as blessed decides. §9.1 "an assignment
`place = value` is an expression whose type is the type of `place`; in
statement or tail position its value is discarded", read with D43's pinned
test err_d43_cannot_infer_match.w: "tail position" is a function's or
closure's OWN body tail (single-statement body or last statement of the
body block). A written arm of a tail if/match, bare or as an arm block's
tail, keeps the place's type and joins under D43.

#1319 (980474c) consulted expr_is_assignment at every block tail in
check_block and lower_block_mode, so an arm block whose last statement was
an assignment became Unit. The verdict then depended on statement count:
`if p: seen = 1` and `if p: { seen = 1 }` were "cannot infer", while the
same arm with a preceding `let` inferred Unit (#1337), and
behav_distinct_compat's `check` (both arms end in `x = x + 1`) was
rejected.

Sema now marks the body node (body_tail_block) at the fn body and closure
body; check_block discards a tail only for that block, and
discard_body_tail records each discarded tail. MirLower's lower_tail_expr
and lower_block_mode lower exactly the recorded tails in discard mode, so
Sema and MirLower share one verdict. #1319's #1296 cases (Result[Unit, E]
bodies ending in `x = y?` / `n += 1`, `-> i32` / `-> str` defaults, closure
bodies) and #1298 (with ... as mut bodies) are unchanged.

Tests: err_d43_cannot_infer_if_multi_stmt_arm (the #1337 repro),
err_d43_cannot_infer_match_block_arm and _braced_arm (arm-block spellings
of the pinned test), behav_d43_arm_assignment_keeps_value (arms return the
assigned value; body and closure-body tails are still discarded).

Fixes #1337.
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery (seed-driven, rebased on main, 97f9500): build ok, :fixpoint ok, :test red only on main's known reds — err_no_std_alloc_async_leak, err_no_std_await_leak, err_unit_elision_unconstrained_generic (all fixed by #1340). behav_distinct_compat (main red) is green here.

Note: Eric has since ruled on the -> T body-tail assignment (rule (a): a declared non-Unit return type yields the assigned value; §4.10 narrowed). That lands as a follow-up stacked on this PR once the spec wording is blessed; this PR is correct as far as it goes (arms keep their value).

@ehartford
ehartford marked this pull request as ready for review September 22, 2026 21:11
@ehartford
ehartford merged commit 9364947 into main Sep 22, 2026
@ehartford
ehartford deleted the d43-narrow-assignment-discard branch September 22, 2026 21:55
ehartford pushed a commit that referenced this pull request Sep 23, 2026
… of its place; the implicit default applies only to a Unit tail (§9.1, §4.10)

§9.1: "An assignment `place = value` is an expression whose type is the
type of `place`. In statement position its value is discarded. As the tail
of a body whose declared return type is not `Unit`, the body yields a read
of `place` after the store, under the ordinary copy and move rules."
§4.10: "The implicit default applies only when the tail's own type is
`Unit`. ... The compiler never discards a tail's value to substitute
`T.default()`."

#1319 discarded every body's own assignment tail (SemaCheck
discard_body_tail at the fn body, check_block's body_tail_block tail and
the closure body; MirLower.lower_tail_expr and lower_block_mode lowered the
recorded tails in discard mode), and §4.10 then substituted T.default():
`fn inc -> i32: g += 1` returned 0. Now:

- Sema: discard_body_tail discards only when the body declares no return
  (D43) or `Unit` (body_tail_discards). Otherwise record_tail_reads records
  every assignment the returned value reaches — the body's own tail and,
  through blocks and groupings, the arms of a tail if/match (D43 already
  kept their value): body tail and arm tail are one rule.
  check_tail_read_place holds the read to the ordinary rules: a global that
  needs drop is never moved out of (D52), a field never moves implicitly
  (D32), an element is never moved out of its collection (D27); a whole
  local moves. A mismatched tail is "return type mismatch" at the
  assignment, with the read it yields named.
- The closure body's closure_default_from_side_effect exemption discarded
  a mismatched non-Unit tail whenever the body mutated a capture (and then
  lowered an Option into an i32 slot: invalid MIR); it is removed.
- MirLower: lower_assign(.., read_back) stores as a statement does, then
  read_assigned_place reads the place back once (compound, str append and
  IndexPlace stores evaluate the place once). The arm value is no longer
  the right-hand operand the place already owns (#1339's arm with a str
  local double-freed).
- ComptimeEval: a recorded tail assignment evaluates to the place's value.
- An `unsafe fn` body is its source body inside the parser's unsafe wrapper
  (fn_body_inner); the discard verdict reads the source body.

Consequence the ruling's text decides: `-> Result[Unit, E]` is not `Unit`,
so `mut fn bump() -> Result[Unit, E]: self.n += 1` is a type error again
(#1296's case); err_d60_result_unit_tail pins it.

behav_1296_assignment_tail_discarded pinned #1319's defaults and is
replaced by the D60 fixtures; behav_d43_arm_assignment_keeps_value's body
and closure tails now read their places (50, 11).

A Unit return — declared, or a trait's contract such as Drop.drop —
returns nothing, so no read is recorded under it, whatever a nested block
of the body ends in (behav_d60_unit_return_nested_tail; the spec_ss14_17
mutex/rwlock Drop impls end in an `unsafe:` block assigning a global str).
ehartford added a commit that referenced this pull request Sep 24, 2026
… of its place; the implicit default applies only to a Unit tail (§9.1, §4.10) (#1536)

* D60: a tail assignment under a declared non-Unit return yields a read of its place; the implicit default applies only to a Unit tail (§9.1, §4.10)

§9.1: "An assignment `place = value` is an expression whose type is the
type of `place`. In statement position its value is discarded. As the tail
of a body whose declared return type is not `Unit`, the body yields a read
of `place` after the store, under the ordinary copy and move rules."
§4.10: "The implicit default applies only when the tail's own type is
`Unit`. ... The compiler never discards a tail's value to substitute
`T.default()`."

#1319 discarded every body's own assignment tail (SemaCheck
discard_body_tail at the fn body, check_block's body_tail_block tail and
the closure body; MirLower.lower_tail_expr and lower_block_mode lowered the
recorded tails in discard mode), and §4.10 then substituted T.default():
`fn inc -> i32: g += 1` returned 0. Now:

- Sema: discard_body_tail discards only when the body declares no return
  (D43) or `Unit` (body_tail_discards). Otherwise record_tail_reads records
  every assignment the returned value reaches — the body's own tail and,
  through blocks and groupings, the arms of a tail if/match (D43 already
  kept their value): body tail and arm tail are one rule.
  check_tail_read_place holds the read to the ordinary rules: a global that
  needs drop is never moved out of (D52), a field never moves implicitly
  (D32), an element is never moved out of its collection (D27); a whole
  local moves. A mismatched tail is "return type mismatch" at the
  assignment, with the read it yields named.
- The closure body's closure_default_from_side_effect exemption discarded
  a mismatched non-Unit tail whenever the body mutated a capture (and then
  lowered an Option into an i32 slot: invalid MIR); it is removed.
- MirLower: lower_assign(.., read_back) stores as a statement does, then
  read_assigned_place reads the place back once (compound, str append and
  IndexPlace stores evaluate the place once). The arm value is no longer
  the right-hand operand the place already owns (#1339's arm with a str
  local double-freed).
- ComptimeEval: a recorded tail assignment evaluates to the place's value.
- An `unsafe fn` body is its source body inside the parser's unsafe wrapper
  (fn_body_inner); the discard verdict reads the source body.

Consequence the ruling's text decides: `-> Result[Unit, E]` is not `Unit`,
so `mut fn bump() -> Result[Unit, E]: self.n += 1` is a type error again
(#1296's case); err_d60_result_unit_tail pins it.

behav_1296_assignment_tail_discarded pinned #1319's defaults and is
replaced by the D60 fixtures; behav_d43_arm_assignment_keeps_value's body
and closure tails now read their places (50, 11).

A Unit return — declared, or a trait's contract such as Drop.drop —
returns nothing, so no read is recorded under it, whatever a nested block
of the body ends in (behav_d60_unit_return_nested_tail; the spec_ss14_17
mutex/rwlock Drop impls end in an `unsafe:` block assigning a global str).

* MIR: §4.10's implicit default follows Sema's type of the body, not the lowered operand's; a str's `.len` tail returned 0

§4.10: "The implicit default applies only when the tail's own type is
`Unit`. ... The compiler never discards a tail's value to substitute
`T.default()`."

lower_fn_with_sig chose the default when `operand_type(body_result)` was
Unit. place_local_type has no type for a str's `.len` projection
(struct_field_type of str is 0), so `fn view_len(v: &str) -> i64: v.len`
(std.string) and any `-> i64: v.len` body discarded the value and returned
i64.default(): 0, no diagnostic. The default now follows the body's Sema
type (expr_type, as the closure body already does); a value tail that
lowered to the Unit constant is a loud phase bug, not a fallback. Found by
that check, added at this line for D60: the stage1 self-check tripped it on
view_len.

behav_d60_value_tail_not_defaulted: 5 5 / 5 false (was 0 0 / 0 false).

The same check caught issue155_dead_comptime_error: Sema typed
`comptime_error(..)` as `TypeKind.TY_NEVER as TypeId`, the kind number read
as a type id — usize — so `fn dead_stub() -> i32: comptime_error("..")` had
a usize tail. It is `ty_never` now; after a diverging tail the return slot
is still filled in the block nothing reaches.

---------

Co-authored-by: Eric Hartford <[email protected]>
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.

D43: a multi-statement arm ending in an assignment is typed Unit, so a mixed tail infers instead of 'cannot infer'

1 participant