# Why the coerce function don't compile?

**URL:** <https://internals.rust-lang.org/t/why-the-coerce-function-dont-compile/22982>\
**Category:** Uncategorized\
**Created:** [May 24, 2025, 1:28pm UTC](https://internals.rust-lang.org/t/why-the-coerce-function-dont-compile/22982 "2025-05-24T13:28:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![logan272](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/logan272/32/13291_2.png) [@logan272](https://internals.rust-lang.org/u/logan272)\
**Post date:** [May 24, 2025, 1:28pm UTC](https://internals.rust-lang.org/t/why-the-coerce-function-dont-compile/22982/1 "2025-05-24T13:28:11Z")

</div>

```rust
// this compiles 
fn coerce<'a, T: Trait + Sized + 'a>(t: &T) -> &(dyn Trait + 'a) {
    t
}

// this don't compile
fn coerce<'a, T: Trait + ?Sized + 'a>(t: &T) -> &(dyn Trait + 'a) {
    t
}

```

Assume that `T` is `dyn Trait`, then `&T` is `&dyn Trait`, which exactly matches the return type.

I'm confused why Rust compiler refuses to compile the code.

---

<div class="post-metadata">

**Author:** ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)\
**Post date:** [May 24, 2025, 1:40pm UTC](https://internals.rust-lang.org/t/why-the-coerce-function-dont-compile/22982/2 "2025-05-24T13:40:42Z")

</div>

References to `?Sized` types have metadata. References to trait objects also have metadata (the vtable). Therefore, `?Sized` types cannot be converted to trait objects, because there is no room for both the vtable and the original metadata.

---

<div class="post-metadata">

**Author:** ![kpreid](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kpreid/32/8484_2.png) [@kpreid](https://internals.rust-lang.org/u/kpreid)\
**Post date:** [May 24, 2025, 2:04pm UTC](https://internals.rust-lang.org/t/why-the-coerce-function-dont-compile/22982/3 "2025-05-24T14:04:37Z")

</div>

> [@logan272](#):
>
> Assume that `T` is `dyn Trait`, then `&T` is `&dyn Trait`, which exactly matches the return type.

This is true, but in order for `coerce()` to compile, it has to work for _every_ value of `T` that meets the bounds. There are more possible types that are `T: Trait + !Sized` than just `dyn Trait`.

```rust
impl Trait for [i32] {}

impl Trait for dyn OtherTrait {}

struct Generic<T: ?Sized>(T);
impl<T: Trait> Trait for Generic<T> {}

trait SubTrait: Trait {}

```

Given these definitions, `[i32]`, `dyn OtherTrait`, `Generic<dyn Trait>`, and `dyn SubTrait` are all unsized types that implement `Trait`, none of which are equal to `dyn Trait`. And so all of them would need double metadata, which is impossible as @Jules-Bertholet wrote.

It _would_ be useful to be able to write a bound which means “able to be coerced to `dyn Trait`”, but there is no such bound currently, and in particular `T: Trait + ?Sized` isn’t it.

---

<div class="post-metadata">

**Author:** ![logan272](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/logan272/32/13291_2.png) [@logan272](https://internals.rust-lang.org/u/logan272)\
**Post date:** [May 24, 2025, 2:11pm UTC](https://internals.rust-lang.org/t/why-the-coerce-function-dont-compile/22982/4 "2025-05-24T14:11:32Z")

</div>

@kpreidThank you so much! I understand now. You’re a wonderful teacher who can immediately identify exactly where I misunderstood.

---

<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:** [November 15, 2026, 2:11pm UTC](https://internals.rust-lang.org/t/why-the-coerce-function-dont-compile/22982/5 "2026-11-15T14:11:51Z")

</div>

Closed on November 15, 2026, 2:11pm UTC

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