Skip to content

Struct-literal field init does not consume a by-value param (spurious drop) #599

Closed

Description

@ehartford

Summary

Initializing a struct-literal field from a by-value parameter does not consume (move) that parameter. The parameter is then dropped at the enclosing function's scope end even though its value was moved into the returned/escaping struct — a spurious drop, and a double-drop hazard.

Repro

var TR = 0
type G { id: i32 }
impl Drop for G:
    fn drop(move self: Self): TR = TR + 1
type Cell[T] { value: T }
fn box_it[T](value: T) -> Cell[T]:
    Cell { value: value }      // value moved into the returned Cell...
fn main:
    let c = box_it(G { id: 5 })
    print(f"TR={TR}")          // prints 1 — value was dropped despite escaping in c

Contrast: passing the same value as a function-call argument (e.g. Box.new(value)) consumes it correctly (no drop). The difference is that call args go through consume_moved_operand (src/MirLower.w) but struct-literal field operands do not.

Impact

A generic "wrapper" function that stores its argument in a struct field and returns/escapes it drops the argument early. Discovered while implementing std.ffi.box_ctx (#436); worked around by routing through Box.new(value) (a call arg, which consumes correctly).

Activity

  1. ehartford commented on Jul 2, 2026

    @ehartford
    ContributorAuthor

    Closing as fixed on current HEAD (v0.15.1-g5b599d18e): the repro now prints TR=0 — a struct-literal field init consumes the by-value param (§3.8/§2.4 move semantics), no spurious drop. Fixed by the aggregate-construction move work in the #605/#606 campaign (aggregate literals MOVE Drop values; conservative consume machinery since refined by A6/A7/A8).

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