# Rustc bug?: generic T is also Box\<dyn T\>

**URL:** <https://internals.rust-lang.org/t/rustc-bug-generic-t-is-also-box-dyn-t/8956>\
**Category:** compiler\
**Created:** [December 4, 2018, 3:33pm UTC](https://internals.rust-lang.org/t/rustc-bug-generic-t-is-also-box-dyn-t/8956 "2018-12-04T15:33:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![derekdreery](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/derekdreery/32/2632_2.png) [@derekdreery](https://internals.rust-lang.org/u/derekdreery)\
**Post date:** [December 4, 2018, 3:33pm UTC](https://internals.rust-lang.org/t/rustc-bug-generic-t-is-also-box-dyn-t/8956/1 "2018-12-04T15:33:31Z")

</div>

The following code fails to compile:

```rust
pub trait ATrait {}

impl<T: ATrait + ?Sized> ATrait for Box<T> {}

impl<'a, T: ATrait + 'a> From<T> for Box<dyn ATrait + 'a> {
    fn from(t: T) -> Box<dyn ATrait + 'a> {
        Box::new(t)
    }
}

```

with the error message

```nohighlight
error[E0119]: conflicting implementations of trait `std::convert::From<std::boxed::Box<(dyn ATrait + 'static)>>` for type `std::boxed::Box<(dyn ATrait + 'static)>`:
 --> box_from_clash.rs:7:1
  |
7 | impl<T: ATrait> From<T> for Box<dyn ATrait> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: conflicting implementation in crate `core`:
          - impl<T> std::convert::From<T> for T;

error: aborting due to previous error

```

but the from impl is converting between different types: it converts T to Box, which are definitely different. Is this a rustc bug, a rustc design choice, or a misunderstanding by me? 🙂

_EDIT_ It’s worth noting that the code will compile with no problems if you remove the `?Sized` bound.

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [December 4, 2018, 3:37pm UTC](https://internals.rust-lang.org/t/rustc-bug-generic-t-is-also-box-dyn-t/8956/2 "2018-12-04T15:37:24Z")

</div>

The `dyn Trait` type is a type that implements `Trait`, so this seems correct to me.

Removing the `?Sized` “fixes” it because `dyn Trait` is an unsized type.

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [December 4, 2018, 3:37pm UTC](https://internals.rust-lang.org/t/rustc-bug-generic-t-is-also-box-dyn-t/8956/3 "2018-12-04T15:37:43Z")

</div>

Consider the following code:

```rust
let boxed: Box<dyn ATrait> = Box::new(SomethingThatImplementsATrait);
let boxed2: Box<dyn ATrait> = boxed.from();

```

In the second line the compiler needs to find an implementation for `<Box<dyn ATrait> as From<Box<dyn ATrait>>>::from`, it can either take the fully generic one from `core`, or, because `Box<dyn ATrait>: ATrait`, the one you have written.

---

<div class="post-metadata">

**Author:** ![derekdreery](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/derekdreery/32/2632_2.png) [@derekdreery](https://internals.rust-lang.org/u/derekdreery)\
**Post date:** [December 4, 2018, 3:39pm UTC](https://internals.rust-lang.org/t/rustc-bug-generic-t-is-also-box-dyn-t/8956/4 "2018-12-04T15:39:29Z")

</div>

> [@Ixrec](#):
>
> The `dyn Trait` type is a type that implements `Trait` , so this seems correct to me.

I'm just confused because one is wrapped in a `Box` and the other isn't. Does the compiler consider `Box<T>` and `T` to be the same type, or more specifically `Box<dyn Trait>` and `T: Trait`?

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [December 4, 2018, 3:56pm UTC](https://internals.rust-lang.org/t/rustc-bug-generic-t-is-also-box-dyn-t/8956/5 "2018-12-04T15:56:11Z")

</div>

In the from impl, what if you replace `T` with `Box<dyn ATrait + 'a>`, then you will have a conflicting impl with `From<T> for T` impl, so the compiler rejects your impl. This is correct and can be fixed by removing the `?Sized` bounds because `dyn Trait: Trait` and is unsized (i.e. doesn’t impl `Sized`) like how @Ixrec said above. This means that it is guaranteed to that `Box<dyn ATrait + 'a>: ATrait` is false, which means it cannot conflict with `From<T> for T` due to the constraint on `T` (`T: ATrait`).

* * *

edit: code blocks for clarity

```rust
impl<U: ATrait + ?Sized> ATrait for Box<U> {}

// replace `T` with `Box<dyn ATrait + 'a>`
// this is valid because `Box<dyn ATrait + 'a>: ATrait` by the first impl
// oh no a conflict with `impl From<T> for T`
impl<'a> From<Box<dyn ATrait + 'a>> for Box<dyn ATrait + 'a> {}

```

```rust
impl<U: ATrait> ATrait for Box<U> {}

// replace `T` with `Box<dyn ATrait + 'a>`
// this is no longer valid because Box<dyn ATrait + 'a> doesn't implement ATrait,
// because `dyn ATrait` is not `Sized`, this means this impl doesn't even exist!
// yay no conflicts
// impl<'a> From<Box<dyn ATrait + 'a>> for Box<dyn ATrait + 'a> {}

```

---

<div class="post-metadata">

**Author:** ![derekdreery](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/derekdreery/32/2632_2.png) [@derekdreery](https://internals.rust-lang.org/u/derekdreery)\
**Post date:** [December 4, 2018, 4:05pm UTC](https://internals.rust-lang.org/t/rustc-bug-generic-t-is-also-box-dyn-t/8956/6 "2018-12-04T16:05:54Z")

</div>

I understand now. It’s a shame that these impls overlap, because they are both bigger than their intersection. This means that I can never implement `impl<U: ATrait + ?Sized> ATrait for Box<U> {}` even though it seems like a desirable thing to have. ☹. Does specialization help with this?

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [December 4, 2018, 4:15pm UTC](https://internals.rust-lang.org/t/rustc-bug-generic-t-is-also-box-dyn-t/8956/7 "2018-12-04T16:15:01Z")

</div>

I just saw the context of where you’re trying this out. Damn, that’s annoying. Unfortunately I don’t know whether specialization can help currently.

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [December 4, 2018, 9:01pm UTC](https://internals.rust-lang.org/t/rustc-bug-generic-t-is-also-box-dyn-t/8956/8 "2018-12-04T21:01:04Z")

</div>

Specialization could help if it gets the lattice rule, but I don’t think that is currently planned.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:31am UTC](https://internals.rust-lang.org/t/rustc-bug-generic-t-is-also-box-dyn-t/8956/9 "2019-03-25T08:31:15Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
