get rid of an old hack

For structs that cannot be unsized, the layout algorithm sometimes moves
unsized fields to the end of the struct, which circumvented the error
for unexpected unsized fields and returned an unsized layout anyway.

This commit makes it so that the unexpected unsized error is always
returned for structs that cannot be unsized, allowing us to remove an
old hack and fixing some old ICE.
This commit is contained in:
Lukas Markeffsky 2024-09-16 21:37:40 +02:00
parent 3db930a463
commit 20d2414925
6 changed files with 55 additions and 50 deletions

View File

@ -1148,7 +1148,11 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
}
if field.is_unsized() {
if let StructKind::MaybeUnsized = kind {
unsized_field = Some(field);
} else {
return Err(LayoutCalculatorError::UnexpectedUnsized(*field));
}
}
// Invariant: offset < dl.obj_size_bound() <= 1<<61

View File

@ -13,8 +13,7 @@ use rustc_middle::ty::layout::{
};
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::{
self, AdtDef, CoroutineArgsExt, EarlyBinder, FieldDef, GenericArgsRef, Ty, TyCtxt,
TypeVisitableExt,
self, AdtDef, CoroutineArgsExt, EarlyBinder, GenericArgsRef, Ty, TyCtxt, TypeVisitableExt,
};
use rustc_session::{DataTypeKind, FieldInfo, FieldKind, SizeKind, VariantInfo};
use rustc_span::sym;
@ -572,40 +571,6 @@ fn layout_of_uncached<'tcx>(
));
}
let err_if_unsized = |field: &FieldDef, err_msg: &str| {
let field_ty = tcx.type_of(field.did);
let is_unsized = tcx
.try_instantiate_and_normalize_erasing_regions(args, cx.param_env, field_ty)
.map(|f| !f.is_sized(tcx, cx.param_env))
.map_err(|e| {
error(
cx,
LayoutError::NormalizationFailure(field_ty.instantiate_identity(), e),
)
})?;
if is_unsized {
tcx.dcx().span_delayed_bug(tcx.def_span(def.did()), err_msg.to_owned());
Err(error(cx, LayoutError::Unknown(ty)))
} else {
Ok(())
}
};
if def.is_struct() {
if let Some((_, fields_except_last)) =
def.non_enum_variant().fields.raw.split_last()
{
for f in fields_except_last {
err_if_unsized(f, "only the last field of a struct can be unsized")?;
}
}
} else {
for f in def.all_fields() {
err_if_unsized(f, &format!("{}s cannot have unsized fields", def.descr()))?;
}
}
let get_discriminant_type =
|min, max| Integer::repr_discr(tcx, ty, &def.repr(), min, max);
@ -643,6 +608,10 @@ fn layout_of_uncached<'tcx>(
)
.map_err(|err| map_error(cx, ty, err))?;
if !maybe_unsized && layout.is_unsized() {
bug!("got unsized layout for type that cannot be unsized {ty:?}: {layout:#?}");
}
// If the struct tail is sized and can be unsized, check that unsizing doesn't move the fields around.
if cfg!(debug_assertions)
&& maybe_unsized

View File

@ -1,12 +0,0 @@
//@ known-bug: rust-lang/rust#126939
struct MySlice<T>(T);
type MySliceBool = MySlice<[bool]>;
struct P2 {
b: MySliceBool,
}
static CHECK: () = assert!(align_of::<P2>() == 1);
fn main() {}

View File

@ -1,6 +1,10 @@
//@ known-bug: #127737
// issue: #127737
//@ check-pass
//@ compile-flags: -Zmir-opt-level=5 --crate-type lib
//! This test is very similar to `invalid-unsized-const-eval.rs`, but also requires
//! checking for unsized types in the last field of each enum variant.
pub trait TestTrait {
type MyType;
fn func() -> Option<Self>

View File

@ -0,0 +1,17 @@
// issue: rust-lang/rust#126939
//! This used to ICE, because the layout algorithm did not check for unsized types
//! in the struct tail of always-sized types (i.e. those that cannot be unsized)
//! and incorrectly returned an unsized layout.
struct MySlice<T>(T);
type MySliceBool = MySlice<[bool]>;
struct P2 {
b: MySliceBool,
//~^ ERROR: the size for values of type `[bool]` cannot be known at compilation time
}
static CHECK: () = assert!(align_of::<P2>() == 1);
fn main() {}

View File

@ -0,0 +1,23 @@
error[E0277]: the size for values of type `[bool]` cannot be known at compilation time
--> $DIR/invalid-unsized-in-always-sized-tail.rs:11:8
|
LL | b: MySliceBool,
| ^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[bool]`
note: required by an implicit `Sized` bound in `MySlice`
--> $DIR/invalid-unsized-in-always-sized-tail.rs:7:16
|
LL | struct MySlice<T>(T);
| ^ required by the implicit `Sized` requirement on this type parameter in `MySlice`
help: you could relax the implicit `Sized` bound on `T` if it were used through indirection like `&T` or `Box<T>`
--> $DIR/invalid-unsized-in-always-sized-tail.rs:7:16
|
LL | struct MySlice<T>(T);
| ^ - ...if indirection were used here: `Box<T>`
| |
| this could be changed to `T: ?Sized`...
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`.