Skip to content

Commit a468da9

Browse files
committed
Add a check for reprs that could change the ABI
disallow `#[repr(C)] and `#[repr(packed)]` on structs implementing DispatchFromDyn because they will change the ABI from Scalar/ScalarPair to Aggregrate, resulting in an ICE during object-safety checks or codegen
1 parent 3db2203 commit a468da9

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/librustc_typeck/coherence/builtin.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ fn visit_implementation_of_dispatch_from_dyn<'a, 'tcx>(
223223
return
224224
}
225225

226+
if def_a.repr.c() || def_a.repr.packed() {
227+
create_err(
228+
"structs implementing `DispatchFromDyn` may not have \
229+
`#[repr(packed)]` or `#[repr(C)]`"
230+
).emit();
231+
}
232+
226233
let fields = &def_a.non_enum_variant().fields;
227234

228235
let coerced_fields = fields.iter().filter_map(|field| {

src/test/ui/invalid_dispatch_from_dyn_impls.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,12 @@ struct NothingToCoerce<T: ?Sized> {
4141
impl<T: ?Sized, U: ?Sized> DispatchFromDyn<NothingToCoerce<T>> for NothingToCoerce<U> {}
4242
//~^ ERROR [E0378]
4343

44+
#[repr(C)]
45+
struct HasReprC<T: ?Sized>(Box<T>);
46+
47+
impl<T: ?Sized, U: ?Sized> DispatchFromDyn<HasReprC<U>> for HasReprC<T>
48+
where
49+
T: Unsize<U>,
50+
{} //~^^^ ERROR [E0378]
51+
4452
fn main() {}

src/test/ui/invalid_dispatch_from_dyn_impls.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ error[E0378]: the trait `DispatchFromDyn` may only be implemented for a coercion
2727
LL | impl<T: ?Sized, U: ?Sized> DispatchFromDyn<NothingToCoerce<T>> for NothingToCoerce<U> {}
2828
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2929

30-
error: aborting due to 3 previous errors
30+
error[E0378]: structs implementing `DispatchFromDyn` may not have `#[repr(packed)]` or `#[repr(C)]`
31+
--> $DIR/invalid_dispatch_from_dyn_impls.rs:47:1
32+
|
33+
LL | / impl<T: ?Sized, U: ?Sized> DispatchFromDyn<HasReprC<U>> for HasReprC<T>
34+
LL | | where
35+
LL | | T: Unsize<U>,
36+
LL | | {} //~^^^ ERROR [E0378]
37+
| |__^
38+
39+
error: aborting due to 4 previous errors
3140

3241
For more information about this error, try `rustc --explain E0378`.

0 commit comments

Comments
 (0)