Rollup merge of #116056 - ouz-a:wide_ice, r=compiler-errors
Make unsized casts illegal Weirdly enough this https://github.com/rust-lang/rust/issues/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 https://github.com/rust-lang/rust/issues/115998
This commit is contained in:
commit
a38f2309fc
3 changed files with 18 additions and 2 deletions
|
@ -725,6 +725,9 @@ impl<'a, 'tcx> CastCheck<'tcx> {
|
||||||
},
|
},
|
||||||
// array-ptr-cast
|
// array-ptr-cast
|
||||||
Ptr(mt) => {
|
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)
|
self.check_ref_cast(fcx, TypeAndMut { mutbl, ty: inner_ty }, mt)
|
||||||
}
|
}
|
||||||
_ => Err(CastError::NonScalar),
|
_ => Err(CastError::NonScalar),
|
||||||
|
@ -735,7 +738,6 @@ impl<'a, 'tcx> CastCheck<'tcx> {
|
||||||
}
|
}
|
||||||
_ => return Err(CastError::NonScalar),
|
_ => return Err(CastError::NonScalar),
|
||||||
};
|
};
|
||||||
|
|
||||||
if let ty::Adt(adt_def, _) = *self.expr_ty.kind() {
|
if let ty::Adt(adt_def, _) = *self.expr_ty.kind() {
|
||||||
if adt_def.did().krate != LOCAL_CRATE {
|
if adt_def.did().krate != LOCAL_CRATE {
|
||||||
if adt_def.variants().iter().any(VariantDef::is_field_list_non_exhaustive) {
|
if adt_def.variants().iter().any(VariantDef::is_field_list_non_exhaustive) {
|
||||||
|
@ -743,7 +745,6 @@ impl<'a, 'tcx> CastCheck<'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match (t_from, t_cast) {
|
match (t_from, t_cast) {
|
||||||
// These types have invariants! can't cast into them.
|
// These types have invariants! can't cast into them.
|
||||||
(_, Int(CEnum) | FnPtr) => Err(CastError::NonScalar),
|
(_, Int(CEnum) | FnPtr) => Err(CastError::NonScalar),
|
||||||
|
|
6
tests/ui/cast/unsized-struct-cast.rs
Normal file
6
tests/ui/cast/unsized-struct-cast.rs
Normal file
|
@ -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
tests/ui/cast/unsized-struct-cast.stderr
Normal file
9
tests/ui/cast/unsized-struct-cast.stderr
Normal file
|
@ -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`.
|
Loading…
Add table
Add a link
Reference in a new issue