Skip to content

Commit

Permalink
Unrolled build for rust-lang#116056
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#116056 - ouz-a:wide_ice, r=compiler-errors

Make unsized casts illegal

Weirdly enough this rust-lang#115998 issue seems to exist since Rust 1.0 (couldn't check before that) but it's only recently been noticed. This change makes those casts illegal.

Fixes rust-lang#115998
  • Loading branch information
rust-timer committed Sep 22, 2023
2 parents aadb571 + 861448b commit ee04311
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions compiler/rustc_hir_typeck/src/cast.rs
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
@@ -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
@@ -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`.

0 comments on commit ee04311

Please sign in to comment.