rust-lang / rust-lang/rust

`DispatchFromDyn` impls don't have their lifetime bounds checked

Open
#148,726 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-dyn-compatibility A-lifetimes C-bug F-arbitrary_self_types F-dispatch_from_dyn fixed-by-next-solver T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

#![feature(dispatch_from_dyn, arbitrary_self_types, unsize)]

use std::{
    marker::Unsize,
    ops::{DispatchFromDyn, Receiver},
};

trait IsStatic {}
impl<'a: 'static> IsStatic for &'a () {}

pub struct Ptr<T: ?Sized>(Box<T>);
impl<T: ?Sized> Receiver for Ptr<T> {
    type Target = T;
}

// This impl never actually has satisfied bounds, but it is used anyway.
// (Deleting this impl causes a compile error)
impl<T: Unsize<U> + ?Sized, U: ?Sized> DispatchFromDyn<Ptr<U>> for Ptr<T> where
    for<'a> &'a (): IsStatic
{
}

pub trait Trait {
    fn method(self: Ptr<Self>);
}

pub fn foo(_: &dyn Trait) {}

I expected the code to cause a compile error, since Ptr does not actually implement DispatchFromDyn (due to the unsatisfied lifetime bound). Instead, the code compiles fine.

Compile error if the DispatchFromDyn impl is deleted
error[E0038]: the trait `Trait` is not dyn compatible
  --> src/lib.rs:20:16
   |
17 |     fn method(self: Ptr<Self>);
   |                     --------- help: consider changing method `method`'s `self` parameter to be `&self`: `&Self`
...
20 | pub fn foo(_: &dyn Trait) {}
   |                ^^^^^^^^^ `Trait` 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:17:21
   |
16 | pub trait Trait {
   |           ----- this trait is not dyn compatible...
17 |     fn method(self: Ptr<Self>);
   |                     ^^^^^^^^^ ...because method `method`'s `self` parameter cannot be dispatched on

For more information about this error, try `rustc --explain E0038`.
Meta

Reproducible on the playground with version 1.93.0-nightly (2025-11-08 fb23dd3c6b120f0d2e55)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the src/lib.rs reproducer from the issue on the reported nightly version and compare behavior with the DispatchFromDyn impl removed. Trace how the compiler checks the impl's lifetime bounds when deciding dyn compatibility. Done means the unsatisfied bound produces the expected compile error rather than allowing the code to compile.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.