Skip to content

A by-value dyn Trait parameter never drops the value it takes (and the spec does not define one) #1852

Closed

Description

@ehartford

Spec

§11.3 and §3.9 define &dyn Trait and Box[dyn Trait]; nothing defines a by-value dyn Trait parameter. §3.8: a plain T parameter consumes. §2.4: an owned value is dropped exactly once.

Repro

trait Named:
    fn name(self: &Self) -> i32

type Tok:
    n: i32

impl Named for Tok:
    fn name(self: &Self) -> i32: self.n

impl Drop for Tok:
    move fn drop(): print(f"drop {self.n}")

fn describe(s: dyn Named) -> i32: s.name()

fn main:
    let t = Tok { n: 1 }
    print(f"{describe(t)}")
    print("end")

with run --debug-alloc (installed main, and fix-globals stage1 with #1847's fix): prints 1, end, leak count=0. drop 1 never prints: neither the callee (which took t by value) nor the caller drops the Tok. It shows no leak only because Tok owns no heap memory; a dyn value holding a str would leak it.

Question for a ruling

Is a by-value dyn Trait parameter a With type at all? The compiler accepts it, but the spec never defines it. Two options:

Found while fixing #1847.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions