Skip to content

An assignment used as a value outside a tail duplicates its non-Copy value: double free / wrong value (§9.1, D60) #1479

Closed

Description

@ehartford

Found implementing D60 (§9.1 tail assignments). D60 rules the value of an assignment in statement position (discarded) and as the tail of a body with a declared non-Unit return (a read of the place after the store, under the ordinary copy and move rules). An assignment used as a value anywhere else still lowers to its right-hand operand (MirLower.lower_expr, the NK_ASSIGN arm: "the expression value of lhs = rhs is rhs"), which the place has already taken. For a type that is not Copy that is a second owner.

Reproduced identically on origin/main 5848ae9 (stage1) and on the D60 branch, run --debug-alloc:

fn compute(s: &str): s.clone() ++ "!"
fn main:
    var s = "".clone()
    let t = (s = compute("x"))
    print(t)
    print(s)

prints x! twice, then debug-alloc: DOUBLE FREE ... origin=unknown.

    let t = if p: s = compute("y") else: s = compute("n")

same double free (an arm of a non-tail value if; D43 keeps the arm's value).

    var a = "".clone()
    var b = "".clone()
    a = b = compute("x")
    print(a)   // prints an empty line
    print(b)   // x!

wrong value, no allocator complaint: a receives the blanked operand.

i32 spellings are correct (a = b = 5 gives 5 and 5).

Expected: one owner. Under the D60 reading the value is a read of the place, so let t = (s = compute("x")) moves s out (a later print(s) is use-after-move), and a = b = compute("x") moves b into a. Whether that reading extends beyond the tail is a spec question (§9.1 now defines statement and tail positions only); until it is ruled, the compiler should at least not mint a second owner. The D60 PR adds the machinery this would reuse: Sema.tail_read_assigns / check_tail_read_place and MirBuilder.lower_assign(.., read_back) / read_assigned_place.

Activity

  1. ehartford commented on Sep 27, 2026

    @ehartford
    ContributorAuthor

    Ruled by Eric (D73, spec v7.5, PR #1784): an assignment expression's value is a read of the place after the store — C11 §6.5.16's rule under With's view semantics (a view; Copy copies, an owned demand on a non-Copy view is refused). D60's tail rule stands. Implementation on the batch-2 ownership layer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions