Skip to content

Commit ee04311

Browse files
authored
Unrolled build for rust-lang#116056
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
2 parents aadb571 + 861448b commit ee04311

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

compiler/rustc_hir_typeck/src/cast.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,9 @@ impl<'a, 'tcx> CastCheck<'tcx> {
725725
},
726726
// array-ptr-cast
727727
Ptr(mt) => {
728+
if !fcx.type_is_sized_modulo_regions(fcx.param_env, mt.ty) {
729+
return Err(CastError::IllegalCast);
730+
}
728731
self.check_ref_cast(fcx, TypeAndMut { mutbl, ty: inner_ty }, mt)
729732
}
730733
_ => Err(CastError::NonScalar),
@@ -735,15 +738,13 @@ impl<'a, 'tcx> CastCheck<'tcx> {
735738
}
736739
_ => return Err(CastError::NonScalar),
737740
};
738-
739741
if let ty::Adt(adt_def, _) = *self.expr_ty.kind() {
740742
if adt_def.did().krate != LOCAL_CRATE {
741743
if adt_def.variants().iter().any(VariantDef::is_field_list_non_exhaustive) {
742744
return Err(CastError::ForeignNonExhaustiveAdt);
743745
}
744746
}
745747
}
746-
747748
match (t_from, t_cast) {
748749
// These types have invariants! can't cast into them.
749750
(_, Int(CEnum) | FnPtr) => Err(CastError::NonScalar),

tests/ui/cast/unsized-struct-cast.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub struct Data([u8]);
2+
3+
fn main(){
4+
const _: *const Data = &[] as *const Data;
5+
//~^ ERROR: casting `&[_; 0]` as `*const Data` is invalid
6+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0606]: casting `&[_; 0]` as `*const Data` is invalid
2+
--> $DIR/unsized-struct-cast.rs:4:28
3+
|
4+
LL | const _: *const Data = &[] as *const Data;
5+
| ^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0606`.

0 commit comments

Comments
 (0)