Skip to content

CoercePointee doesn't support trait bounds on the smart pointer #138172

Closed
@Darksonn

Description

@Darksonn

I tried this code:

#![feature(derive_coerce_pointee)]
#![feature(arbitrary_self_types)]
use std::ops::Deref;
use std::marker::CoercePointee;
use std::sync::Arc;

trait MyTrait {}

#[derive(CoercePointee)]
#[repr(transparent)]
struct MyArc<T>
where
    T: MyTrait + ?Sized,
{
    inner: Arc<T>
}

impl<T: MyTrait + ?Sized> Deref for MyArc<T> {
    type Target = T;
    fn deref(&self) -> &T {
        &self.inner
    }
}

trait MyOtherTrait: MyTrait {
    fn foo(self: MyArc<Self>);
}

fn test(_: MyArc<dyn MyOtherTrait>) {}

I expected to see this happen: It compiles.
Instead, this happened:

error[E0038]: the trait `MyOtherTrait` is not dyn compatible
  --> src/lib.rs:29:18
   |
26 |     fn foo(self: MyArc<Self>);
   |                  ----------- help: consider changing method `foo`'s `self` parameter to be `&self`: `&Self`
...
29 | fn test(_: MyArc<dyn MyOtherTrait>) {}
   |                  ^^^^^^^^^^^^^^^^ `MyOtherTrait` is not dyn compatible
   |
note: for a trait to be dyn compatible it needs to allow building a vtable
      for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
  --> src/lib.rs:26:18
   |
25 | trait MyOtherTrait: MyTrait {
   |       ------------ this trait is not dyn compatible...
26 |     fn foo(self: MyArc<Self>);
   |                  ^^^^^^^^^^^ ...because method `foo`'s `self` parameter cannot be dispatched on

Metadata

Metadata

Labels

C-bugCategory: This is a bug.F-derive_coerce_pointeeFeature: RFC 3621's oft-renamed implementationT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions