Skip to content

Commit 20d2414

Browse files
author
Lukas Markeffsky
committed
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.
1 parent 3db930a commit 20d2414

File tree

6 files changed

+55
-50
lines changed

6 files changed

+55
-50
lines changed

compiler/rustc_abi/src/layout.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,11 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
11481148
}
11491149

11501150
if field.is_unsized() {
1151-
unsized_field = Some(field);
1151+
if let StructKind::MaybeUnsized = kind {
1152+
unsized_field = Some(field);
1153+
} else {
1154+
return Err(LayoutCalculatorError::UnexpectedUnsized(*field));
1155+
}
11521156
}
11531157

11541158
// Invariant: offset < dl.obj_size_bound() <= 1<<61

compiler/rustc_ty_utils/src/layout.rs

+5-36
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use rustc_middle::ty::layout::{
1313
};
1414
use rustc_middle::ty::print::with_no_trimmed_paths;
1515
use rustc_middle::ty::{
16-
self, AdtDef, CoroutineArgsExt, EarlyBinder, FieldDef, GenericArgsRef, Ty, TyCtxt,
17-
TypeVisitableExt,
16+
self, AdtDef, CoroutineArgsExt, EarlyBinder, GenericArgsRef, Ty, TyCtxt, TypeVisitableExt,
1817
};
1918
use rustc_session::{DataTypeKind, FieldInfo, FieldKind, SizeKind, VariantInfo};
2019
use rustc_span::sym;
@@ -572,40 +571,6 @@ fn layout_of_uncached<'tcx>(
572571
));
573572
}
574573

575-
let err_if_unsized = |field: &FieldDef, err_msg: &str| {
576-
let field_ty = tcx.type_of(field.did);
577-
let is_unsized = tcx
578-
.try_instantiate_and_normalize_erasing_regions(args, cx.param_env, field_ty)
579-
.map(|f| !f.is_sized(tcx, cx.param_env))
580-
.map_err(|e| {
581-
error(
582-
cx,
583-
LayoutError::NormalizationFailure(field_ty.instantiate_identity(), e),
584-
)
585-
})?;
586-
587-
if is_unsized {
588-
tcx.dcx().span_delayed_bug(tcx.def_span(def.did()), err_msg.to_owned());
589-
Err(error(cx, LayoutError::Unknown(ty)))
590-
} else {
591-
Ok(())
592-
}
593-
};
594-
595-
if def.is_struct() {
596-
if let Some((_, fields_except_last)) =
597-
def.non_enum_variant().fields.raw.split_last()
598-
{
599-
for f in fields_except_last {
600-
err_if_unsized(f, "only the last field of a struct can be unsized")?;
601-
}
602-
}
603-
} else {
604-
for f in def.all_fields() {
605-
err_if_unsized(f, &format!("{}s cannot have unsized fields", def.descr()))?;
606-
}
607-
}
608-
609574
let get_discriminant_type =
610575
|min, max| Integer::repr_discr(tcx, ty, &def.repr(), min, max);
611576

@@ -643,6 +608,10 @@ fn layout_of_uncached<'tcx>(
643608
)
644609
.map_err(|err| map_error(cx, ty, err))?;
645610

611+
if !maybe_unsized && layout.is_unsized() {
612+
bug!("got unsized layout for type that cannot be unsized {ty:?}: {layout:#?}");
613+
}
614+
646615
// If the struct tail is sized and can be unsized, check that unsizing doesn't move the fields around.
647616
if cfg!(debug_assertions)
648617
&& maybe_unsized

tests/crashes/126939.rs

-12
This file was deleted.

tests/crashes/127737.rs tests/ui/layout/invalid-unsized-const-prop.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
//@ known-bug: #127737
1+
// issue: #127737
2+
//@ check-pass
23
//@ compile-flags: -Zmir-opt-level=5 --crate-type lib
34

5+
//! This test is very similar to `invalid-unsized-const-eval.rs`, but also requires
6+
//! checking for unsized types in the last field of each enum variant.
7+
48
pub trait TestTrait {
59
type MyType;
610
fn func() -> Option<Self>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// issue: rust-lang/rust#126939
2+
3+
//! This used to ICE, because the layout algorithm did not check for unsized types
4+
//! in the struct tail of always-sized types (i.e. those that cannot be unsized)
5+
//! and incorrectly returned an unsized layout.
6+
7+
struct MySlice<T>(T);
8+
type MySliceBool = MySlice<[bool]>;
9+
10+
struct P2 {
11+
b: MySliceBool,
12+
//~^ ERROR: the size for values of type `[bool]` cannot be known at compilation time
13+
}
14+
15+
static CHECK: () = assert!(align_of::<P2>() == 1);
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0277]: the size for values of type `[bool]` cannot be known at compilation time
2+
--> $DIR/invalid-unsized-in-always-sized-tail.rs:11:8
3+
|
4+
LL | b: MySliceBool,
5+
| ^^^^^^^^^^^ doesn't have a size known at compile-time
6+
|
7+
= help: the trait `Sized` is not implemented for `[bool]`
8+
note: required by an implicit `Sized` bound in `MySlice`
9+
--> $DIR/invalid-unsized-in-always-sized-tail.rs:7:16
10+
|
11+
LL | struct MySlice<T>(T);
12+
| ^ required by the implicit `Sized` requirement on this type parameter in `MySlice`
13+
help: you could relax the implicit `Sized` bound on `T` if it were used through indirection like `&T` or `Box<T>`
14+
--> $DIR/invalid-unsized-in-always-sized-tail.rs:7:16
15+
|
16+
LL | struct MySlice<T>(T);
17+
| ^ - ...if indirection were used here: `Box<T>`
18+
| |
19+
| this could be changed to `T: ?Sized`...
20+
21+
error: aborting due to 1 previous error
22+
23+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)