`DispatchFromDyn` impls don't have their lifetime bounds checked
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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