Skip to content

Commit be6a7fb

Browse files
authored
Unrolled build for #154935
Rollup merge of #154935 - Darksonn:dyn-trait-dyn-compat, r=scottmcm Add Sized supertrait for CoerceUnsized and DispatchFromDyn This is part of #149094. I did not include `Receiver` because I think it is correct to allow non-sized receivers.
2 parents 2f201bc + 46a84f6 commit be6a7fb

4 files changed

Lines changed: 62 additions & 2 deletions

File tree

library/core/src/ops/unsize.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::marker::{PointeeSized, Unsize};
3333
/// [nomicon-coerce]: ../../nomicon/coercions.html
3434
#[unstable(feature = "coerce_unsized", issue = "18598")]
3535
#[lang = "coerce_unsized"]
36-
pub trait CoerceUnsized<T: PointeeSized> {
36+
pub trait CoerceUnsized<T: PointeeSized>: Sized {
3737
// Empty.
3838
}
3939

@@ -116,7 +116,7 @@ impl<T: PointeeSized + Unsize<U>, U: PointeeSized> CoerceUnsized<*const U> for *
116116
/// [^1]: Formerly known as *object safety*.
117117
#[unstable(feature = "dispatch_from_dyn", issue = "none")]
118118
#[lang = "dispatch_from_dyn"]
119-
pub trait DispatchFromDyn<T> {
119+
pub trait DispatchFromDyn<T>: Sized {
120120
// Empty.
121121
}
122122

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ run-pass
2+
#![feature(arbitrary_self_types)]
3+
4+
use std::ops::Receiver;
5+
6+
trait Trait {
7+
fn foo(self: &dyn Receiver<Target=Self>);
8+
}
9+
10+
struct Thing;
11+
impl Trait for Thing {
12+
fn foo(self: &dyn Receiver<Target=Self>) {
13+
println!("huh???");
14+
}
15+
}
16+
17+
fn main() {
18+
let x = Box::new(Thing);
19+
let y: &dyn Receiver<Target=Thing> = &x;
20+
y.foo();
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Regression test for:
2+
// https://github.com/rust-lang/rust/issues/149094#issuecomment-4191071539
3+
#![feature(coerce_unsized, unsized_fn_params)]
4+
#![expect(internal_features)]
5+
6+
use std::ops::CoerceUnsized;
7+
8+
pub trait Trait {}
9+
10+
pub fn foo(x: dyn CoerceUnsized<*const dyn Trait>) -> *const dyn Trait {
11+
//~^ ERROR: the trait `CoerceUnsized` is not dyn compatible [E0038]
12+
x
13+
//~^ ERROR: the size for values of type `(dyn CoerceUnsized<*const (dyn Trait + 'static)> + 'static)` cannot be known at compilation time [E0277]
14+
}
15+
16+
fn main() {}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0038]: the trait `CoerceUnsized` is not dyn compatible
2+
--> $DIR/dyn-coerce-unsized-ice.rs:10:15
3+
|
4+
LL | pub fn foo(x: dyn CoerceUnsized<*const dyn Trait>) -> *const dyn Trait {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `CoerceUnsized` is not dyn compatible
6+
|
7+
= note: the trait is not dyn compatible because it requires `Self: Sized`
8+
= note: for a trait to be dyn compatible it needs to allow building a vtable
9+
for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
10+
11+
error[E0277]: the size for values of type `(dyn CoerceUnsized<*const (dyn Trait + 'static)> + 'static)` cannot be known at compilation time
12+
--> $DIR/dyn-coerce-unsized-ice.rs:12:5
13+
|
14+
LL | x
15+
| ^ doesn't have a size known at compile-time
16+
|
17+
= help: the trait `Sized` is not implemented for `(dyn CoerceUnsized<*const (dyn Trait + 'static)> + 'static)`
18+
= note: required for the cast from `(dyn CoerceUnsized<*const (dyn Trait + 'static)> + 'static)` to `*const (dyn Trait + 'static)`
19+
20+
error: aborting due to 2 previous errors
21+
22+
Some errors have detailed explanations: E0038, E0277.
23+
For more information about an error, try `rustc --explain E0038`.

0 commit comments

Comments
 (0)