You can instantiate a dyn Trait that doesn't implement Trait #88904
Labels
A-trait-objects
Area: trait objects, vtable layout
C-bug
Category: This is a bug.
T-types
Relevant to the types team, which will review and decide on the PR/issue.
I tried this code:
I expected to see this happen: The unsized coercion is denied (but see discussion below).
Instead, this happened: The unsized coercion happened and we end up with a
dyn Trait
which does not implementTrait
.Meta
This works on any stable version of Rust.
Notes
The failure to meet the trait bound is caught when you actually try to use the
dyn Trait
, at least trivially. I haven't found a way to weaponize this, but I haven't put any effort into doing so either.dyn Trait
implementsTrait
if youimpl<'a> Marker<'a> for &'a (dyn Trait + 'static) {}
in the example. However, the non-super-trait bound need not be a marker trait (it can have non-defaulted methods).Related: Implied Bounds, #20671
Discussion
I'm filing this as a bug because the assertion that
dyn Trait: Trait
always holds has been expressed as a belief by Rust team members, e.g.There are some possibilities in line with this assertion:
dyn Trait
However, another possibility is to generalize RFC 2027 and just accept the current behavior:
dyn Trait
is always a valid type, as per RFC 2027where Self: Sized
)dyn Trait
just doesn't implementTrait
)dyn Trait
, even if it doesn't implementTrait
Trait
methods if it doesn't implementTrait
I'm sure there are a lot of subtleties and backwards-compatibility concerns in any of the possible approaches.
The text was updated successfully, but these errors were encountered: