Skip to content

Commit 5310c03

Browse files
KivooeoKivooeo
authored andcommitted
moved renamed docs formatted stderr | invalid_dispatch_from_dyn_impls.rs
1 parent 4e6bd62 commit 5310c03

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed
Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,71 @@
1+
//! Test various invalid implementations of DispatchFromDyn trait.
2+
//!
3+
//! DispatchFromDyn is a special trait used by the compiler for object-safe dynamic dispatch.
4+
//! This checks that the compiler correctly rejects invalid implementations:
5+
//! - Structs with extra non-coercible fields
6+
//! - Structs with multiple pointer fields
7+
//! - Structs with no coercible fields
8+
//! - Structs with repr(C) or other incompatible representations
9+
//! - Structs with over-aligned fields
10+
111
#![feature(unsize, dispatch_from_dyn)]
212

3-
use std::{
4-
ops::DispatchFromDyn,
5-
marker::{Unsize, PhantomData},
6-
};
13+
use std::marker::{PhantomData, Unsize};
14+
use std::ops::DispatchFromDyn;
715

16+
// Error: Extra field prevents DispatchFromDyn
817
struct WrapperWithExtraField<T>(T, i32);
918

1019
impl<T, U> DispatchFromDyn<WrapperWithExtraField<U>> for WrapperWithExtraField<T>
1120
//~^ ERROR [E0378]
1221
where
13-
T: DispatchFromDyn<U>,
14-
{}
15-
22+
T: DispatchFromDyn<U>
23+
{
24+
}
1625

17-
struct MultiplePointers<T: ?Sized>{
26+
// Error: Multiple pointer fields create ambiguous coercion
27+
struct MultiplePointers<T: ?Sized> {
1828
ptr1: *const T,
1929
ptr2: *const T,
2030
}
2131

2232
impl<T: ?Sized, U: ?Sized> DispatchFromDyn<MultiplePointers<U>> for MultiplePointers<T>
2333
//~^ ERROR implementing `DispatchFromDyn` does not allow multiple fields to be coerced
2434
where
25-
T: Unsize<U>,
26-
{}
27-
35+
T: Unsize<U>
36+
{
37+
}
2838

39+
// Error: No coercible fields (only PhantomData)
2940
struct NothingToCoerce<T: ?Sized> {
3041
data: PhantomData<T>,
3142
}
3243

3344
impl<T: ?Sized, U: ?Sized> DispatchFromDyn<NothingToCoerce<T>> for NothingToCoerce<U> {}
3445
//~^ ERROR implementing `DispatchFromDyn` requires a field to be coerced
3546

47+
// Error: repr(C) is incompatible with DispatchFromDyn
3648
#[repr(C)]
3749
struct HasReprC<T: ?Sized>(Box<T>);
3850

3951
impl<T: ?Sized, U: ?Sized> DispatchFromDyn<HasReprC<U>> for HasReprC<T>
4052
//~^ ERROR [E0378]
4153
where
42-
T: Unsize<U>,
43-
{}
54+
T: Unsize<U>
55+
{
56+
}
4457

58+
// Error: Over-aligned fields are incompatible
4559
#[repr(align(64))]
4660
struct OverAlignedZst;
61+
4762
struct OverAligned<T: ?Sized>(Box<T>, OverAlignedZst);
4863

4964
impl<T: ?Sized, U: ?Sized> DispatchFromDyn<OverAligned<U>> for OverAligned<T>
5065
//~^ ERROR [E0378]
51-
where
52-
T: Unsize<U>,
53-
{}
66+
where
67+
T: Unsize<U>
68+
{
69+
}
5470

5571
fn main() {}

tests/ui/invalid_dispatch_from_dyn_impls.stderr renamed to tests/ui/traits/dispatch-from-dyn-invalid-impls.stderr

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
11
error[E0378]: the trait `DispatchFromDyn` may only be implemented for structs containing the field being coerced, ZST fields with 1 byte alignment that don't mention type/const generics, and nothing else
2-
--> $DIR/invalid_dispatch_from_dyn_impls.rs:10:1
2+
--> $DIR/dispatch-from-dyn-invalid-impls.rs:19:1
33
|
44
LL | / impl<T, U> DispatchFromDyn<WrapperWithExtraField<U>> for WrapperWithExtraField<T>
55
LL | |
66
LL | | where
7-
LL | | T: DispatchFromDyn<U>,
8-
| |__________________________^
7+
LL | | T: DispatchFromDyn<U>
8+
| |_________________________^
99
|
1010
= note: extra field `1` of type `i32` is not allowed
1111

1212
error[E0375]: implementing `DispatchFromDyn` does not allow multiple fields to be coerced
13-
--> $DIR/invalid_dispatch_from_dyn_impls.rs:22:1
13+
--> $DIR/dispatch-from-dyn-invalid-impls.rs:32:1
1414
|
1515
LL | / impl<T: ?Sized, U: ?Sized> DispatchFromDyn<MultiplePointers<U>> for MultiplePointers<T>
1616
LL | |
1717
LL | | where
18-
LL | | T: Unsize<U>,
19-
| |_________________^
18+
LL | | T: Unsize<U>
19+
| |________________^
2020
|
2121
note: the trait `DispatchFromDyn` may only be implemented when a single field is being coerced
22-
--> $DIR/invalid_dispatch_from_dyn_impls.rs:18:5
22+
--> $DIR/dispatch-from-dyn-invalid-impls.rs:28:5
2323
|
2424
LL | ptr1: *const T,
2525
| ^^^^^^^^^^^^^^
2626
LL | ptr2: *const T,
2727
| ^^^^^^^^^^^^^^
2828

2929
error[E0374]: implementing `DispatchFromDyn` requires a field to be coerced
30-
--> $DIR/invalid_dispatch_from_dyn_impls.rs:33:1
30+
--> $DIR/dispatch-from-dyn-invalid-impls.rs:44:1
3131
|
3232
LL | impl<T: ?Sized, U: ?Sized> DispatchFromDyn<NothingToCoerce<T>> for NothingToCoerce<U> {}
3333
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3434
|
3535
= note: expected a single field to be coerced, none found
3636

3737
error[E0378]: structs implementing `DispatchFromDyn` may not have `#[repr(packed)]` or `#[repr(C)]`
38-
--> $DIR/invalid_dispatch_from_dyn_impls.rs:39:1
38+
--> $DIR/dispatch-from-dyn-invalid-impls.rs:51:1
3939
|
4040
LL | / impl<T: ?Sized, U: ?Sized> DispatchFromDyn<HasReprC<U>> for HasReprC<T>
4141
LL | |
4242
LL | | where
43-
LL | | T: Unsize<U>,
44-
| |_________________^
43+
LL | | T: Unsize<U>
44+
| |________________^
4545

4646
error[E0378]: the trait `DispatchFromDyn` may only be implemented for structs containing the field being coerced, ZST fields with 1 byte alignment that don't mention type/const generics, and nothing else
47-
--> $DIR/invalid_dispatch_from_dyn_impls.rs:49:1
47+
--> $DIR/dispatch-from-dyn-invalid-impls.rs:64:1
4848
|
4949
LL | / impl<T: ?Sized, U: ?Sized> DispatchFromDyn<OverAligned<U>> for OverAligned<T>
5050
LL | |
51-
LL | | where
52-
LL | | T: Unsize<U>,
53-
| |_____________________^
51+
LL | | where
52+
LL | | T: Unsize<U>
53+
| |________________^
5454
|
5555
= note: extra field `1` of type `OverAlignedZst` is not allowed
5656

0 commit comments

Comments
 (0)