Repository navigation
D43: only a body's own tail discards an assignment; an arm keeps its value (#1337) - #1339
Merged
Merged
Conversation
…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
force-pushed
the
d43-narrow-assignment-discard
branch
from
September 22, 2026 21:11
b09e25f to
97f9500
Compare
Contributor
Author
|
Battery (seed-driven, rebased on main, 97f9500): build ok, :fixpoint ok, :test red only on main's known reds — Note: Eric has since ruled on the |
ehartford
marked this pull request as ready for review
September 22, 2026 21:11
This was referenced Sep 22, 2026
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]>
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.
Narrows #1319 per Eric's ruling (2026-09-22): D43 as blessed decides. §9.1 "An assignment
place = valueis an expression whose type is the type ofplace; in statement or tail position its value is discarded", read together with D43's pinned testerr_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 tailif/match— bare or as an arm block's tail — it keeps the place's type and joins under D43.Fixes #1337.
What changed
with … as mutbodies are statements (#1298) #1319 askedexpr_is_assignmentat every block tail (check_block,lower_block_mode), so an arm block ending in an assignment became Unit and the verdict depended on statement count (D43: a multi-statement arm ending in an assignment is typed Unit, so a mixed tail infers instead of 'cannot infer' #1337);behav_distinct_compat'scheckwas rejected.body_tail_block) at the fn-body and closure-body sites;check_blockdiscards only that block's tail.discard_body_tailrecords every discarded tail, and MirLower (lower_tail_expr,lower_block_mode) lowers exactly the recorded tails in discard mode — one verdict, owned by Sema.with … as mutbodies are statements (#1298) #1319's real fixes are unchanged: Result[Unit, E] function ending in an assignment gets no implicit Ok(()): "return type mismatch" #1296 (Result[Unit, E]bodies ending inx = y?/n += 1,-> i32/-> strdefaults, closure bodies) and Anifwithoutelseas the last statement of awith ... as mutbody is rejected, though §7.2 discards the body's value #1298 (with … as mutbodies are statements). No Sema: assignment tails are discarded (#1296) andwith … as mutbodies are statements (#1298) #1319 test changed.Tests (stage1 from
:dev)err_d43_cannot_infer_if_multi_stmt_arm(D43: a multi-statement arm ending in an assignment is typed Unit, so a mixed tail infers instead of 'cannot infer' #1337 repro),err_d43_cannot_infer_match_block_arm/_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 still discarded). All three err tests sayokon a post-Sema: assignment tails are discarded (#1296) andwith … as mutbodies are statements (#1298) #1319 compiler and fail as expected here.err_d43_*(pinned tests unchanged),behav_d43_tail_inference,behav_distinct_compat(unchanged; red on post-Sema: assignment tails are discarded (#1296) andwith … as mutbodies are statements (#1298) #1319 main),behav_1296_assignment_tail_discarded,behav_1298_builder_trailing_if,spec_ss07_*,spec_ss09_1*.Open question (for Eric)
fn inc -> i32: g += 1returnsi32.default()(0) per #1319's reading of §9.1 — the body tail is not a join, so D43's "-> i32makes the tail a demanded join" sentence does not reach it. But after this rulingfn f(p) -> i32: if p: g = 1 else: g = 2returns the assigned value, while the unbranched-> i32: g = 1returns 0. Not changed here; flagging the asymmetry.Battery not yet run (draft).