You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
A by-value dyn Trait parameter never drops the value it takes (and the spec does not define one) #1852
§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:
Reject it with a diagnostic that points at &dyn Trait / Box[dyn Trait] (the unsized reading).
Activity
Box[dyn Trait] drops what it holds, exactly once, then frees its cell…
test: dyn parameters that only observe are spelled `&dyn T`, not a by…
Sema: a parameter cannot take `dyn Trait` by value; it is refused whe…
Sema/MirLower/Codegen: a typed binding pattern over a `&dyn T` subjec…
Spec v7.10 (D77): matching on a borrowed trait object — `name: C` bin…