rust icon indicating copy to clipboard operation
rust copied to clipboard

Allow dropping `dyn Trait` principal

Open Jules-Bertholet opened this issue 1 year ago • 26 comments
trafficstars

Redux of #114679, fixes #126313

This allows the following examples to compile:

trait Trait: Send {}
fn foo(x: &dyn Trait) -> &dyn Send { x }
trait Trait {}
fn foo(x: &(dyn Trait + Send)) -> &dyn Send { x }

This makes the language more consistent, as we already allow:

trait Trait {}
fn foo(x: &(dyn Trait + Send)) -> &dyn Trait { x }

The PR includes a test case, in tests/ui/traits/dyn-drop-principal.rs.

Documentation

dyn upcasting coercions, of any kind, are currently entirely undocumented in the reference. A future reference PR should address this.

@rustbot label T-lang T-types needs-fcp A-coercions A-trait-objects

Jules-Bertholet avatar Jun 19 '24 03:06 Jules-Bertholet