Skip to content

Commit

Permalink
Add a check for reprs that could change the ABI
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mikeyhew committed Nov 1, 2018
1 parent 3db2203 commit a468da9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/librustc_typeck/coherence/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/invalid_dispatch_from_dyn_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
11 changes: 10 additions & 1 deletion src/test/ui/invalid_dispatch_from_dyn_impls.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

0 comments on commit a468da9

Please sign in to comment.