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
This commit is contained in:
Michael Hewson 2018-10-26 01:09:33 -04:00
parent 3db22039dc
commit a468da9cfb
3 changed files with 25 additions and 1 deletions

View file

@ -223,6 +223,13 @@ fn visit_implementation_of_dispatch_from_dyn<'a, 'tcx>(
return
}
if def_a.repr.c() || def_a.repr.packed() {
create_err(
"structs implementing `DispatchFromDyn` may not have \
`#[repr(packed)]` or `#[repr(C)]`"
).emit();
}
let fields = &def_a.non_enum_variant().fields;
let coerced_fields = fields.iter().filter_map(|field| {

View file

@ -41,4 +41,12 @@ struct NothingToCoerce<T: ?Sized> {
impl<T: ?Sized, U: ?Sized> DispatchFromDyn<NothingToCoerce<T>> for NothingToCoerce<U> {}
//~^ ERROR [E0378]
#[repr(C)]
struct HasReprC<T: ?Sized>(Box<T>);
impl<T: ?Sized, U: ?Sized> DispatchFromDyn<HasReprC<U>> for HasReprC<T>
where
T: Unsize<U>,
{} //~^^^ ERROR [E0378]
fn main() {}

View file

@ -27,6 +27,15 @@ error[E0378]: the trait `DispatchFromDyn` may only be implemented for a coercion
LL | impl<T: ?Sized, U: ?Sized> DispatchFromDyn<NothingToCoerce<T>> for NothingToCoerce<U> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 3 previous errors
error[E0378]: structs implementing `DispatchFromDyn` may not have `#[repr(packed)]` or `#[repr(C)]`
--> $DIR/invalid_dispatch_from_dyn_impls.rs:47:1
|
LL | / impl<T: ?Sized, U: ?Sized> DispatchFromDyn<HasReprC<U>> for HasReprC<T>
LL | | where
LL | | T: Unsize<U>,
LL | | {} //~^^^ ERROR [E0378]
| |__^
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0378`.