Skip to content

Leak: dropping a Box of a recursive enum frees the box but not the Box inside the enum (Box.new(L.Cons(5, Box.new(L.Nil)))) #1456

Closed

Description

@ehartford

Spec: §2 ownership (a value is released by its owner's scope; the mission's "leaking is a defect"). Route: debug allocator.

Dropping a Box of a recursive enum frees the outer box but not the box inside it:

use std.box.Box

enum L:
    Cons(v: i64, next: Box[L])
    Nil

fn main:
    let b = Box.new(L.Cons(5, Box.new(L.Nil)))
    print("made")
$ with run --debug-alloc leak5.w
made
debug-alloc: LEAK addr=… size=32 origin=with_alloc
debug-alloc: leak count=1

The same leak when an owned list is matched and its Box payload is bound by value (match l: L.Cons(v, next) => v over L.Cons(4, Box.new(L.Cons(5, Box.new(L.Nil))))), or moved into a fn take(b: Box[L]). No leak for L.Cons(4, Box.new(L.Nil)) (one level), or when the payload pattern is _ (the enum's own drop glue recurses correctly). So a Box[L] dropped as a Box does not run the boxed L's drop glue.

Expected: leak count=0.

Reproduces on a stage1 of origin/main 1c76062 and on the #1443 branch. Found while writing the #1443 matrix (owned-subject control case).

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