Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions compiler/rustc_hir_typeck/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,9 @@ impl<'a, 'tcx> CastCheck<'tcx> {
},
// array-ptr-cast
Ptr(mt) => {
if !fcx.type_is_sized_modulo_regions(fcx.param_env, mt.ty) {
return Err(CastError::IllegalCast);
}
self.check_ref_cast(fcx, TypeAndMut { mutbl, ty: inner_ty }, mt)
}
_ => Err(CastError::NonScalar),
Expand All @@ -735,15 +738,13 @@ impl<'a, 'tcx> CastCheck<'tcx> {
}
_ => return Err(CastError::NonScalar),
};

if let ty::Adt(adt_def, _) = *self.expr_ty.kind() {
if adt_def.did().krate != LOCAL_CRATE {
if adt_def.variants().iter().any(VariantDef::is_field_list_non_exhaustive) {
return Err(CastError::ForeignNonExhaustiveAdt);
}
}
}

match (t_from, t_cast) {
// These types have invariants! can't cast into them.
(_, Int(CEnum) | FnPtr) => Err(CastError::NonScalar),
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/cast/unsized-struct-cast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub struct Data([u8]);

fn main(){
const _: *const Data = &[] as *const Data;
//~^ ERROR: casting `&[_; 0]` as `*const Data` is invalid
}
9 changes: 9 additions & 0 deletions tests/ui/cast/unsized-struct-cast.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0606]: casting `&[_; 0]` as `*const Data` is invalid
--> $DIR/unsized-struct-cast.rs:4:28
|
LL | const _: *const Data = &[] as *const Data;
| ^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

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