Skip to content

Commit c6b2d0f

Browse files
authored
Unrolled build for rust-lang#135264
Rollup merge of rust-lang#135264 - compiler-errors:layout-propagate-errors, r=oli-obk Consider more erroneous layouts as `LayoutError::ReferencesError` to suppress spurious errors Fixes rust-lang#135208 r? oli-obk
2 parents 93ba568 + b89a6e4 commit c6b2d0f

File tree

6 files changed

+111
-26
lines changed

6 files changed

+111
-26
lines changed

compiler/rustc_ty_utils/src/layout.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,27 @@ fn map_error<'tcx>(
105105
// See `tests/ui/layout/trivial-bounds-sized.rs` for an example.
106106
assert!(field.layout.is_unsized(), "invalid layout error {err:#?}");
107107
if !field.ty.is_sized(cx.tcx(), cx.typing_env) {
108-
cx.tcx().dcx().delayed_bug(format!(
108+
let guar = cx.tcx().dcx().delayed_bug(format!(
109109
"encountered unexpected unsized field in layout of {ty:?}: {field:#?}"
110110
));
111+
LayoutError::ReferencesError(guar)
112+
} else {
113+
LayoutError::Unknown(ty)
111114
}
112-
LayoutError::Unknown(ty)
113115
}
114116
LayoutCalculatorError::EmptyUnion => {
115117
// This is always a compile error.
116-
cx.tcx().dcx().delayed_bug(format!("computed layout of empty union: {ty:?}"));
117-
LayoutError::Unknown(ty)
118+
let guar =
119+
cx.tcx().dcx().delayed_bug(format!("computed layout of empty union: {ty:?}"));
120+
LayoutError::ReferencesError(guar)
118121
}
119122
LayoutCalculatorError::ReprConflict => {
120123
// packed enums are the only known trigger of this, but others might arise
121-
cx.tcx().dcx().delayed_bug(format!("computed impossible repr (packed enum?): {ty:?}"));
122-
LayoutError::Unknown(ty)
124+
let guar = cx
125+
.tcx()
126+
.dcx()
127+
.delayed_bug(format!("computed impossible repr (packed enum?): {ty:?}"));
128+
LayoutError::ReferencesError(guar)
123129
}
124130
};
125131
error(cx, err)
@@ -432,8 +438,10 @@ fn layout_of_uncached<'tcx>(
432438
ty::Adt(def, args) if def.repr().simd() => {
433439
if !def.is_struct() {
434440
// Should have yielded E0517 by now.
435-
tcx.dcx().delayed_bug("#[repr(simd)] was applied to an ADT that is not a struct");
436-
return Err(error(cx, LayoutError::Unknown(ty)));
441+
let guar = tcx
442+
.dcx()
443+
.delayed_bug("#[repr(simd)] was applied to an ADT that is not a struct");
444+
return Err(error(cx, LayoutError::ReferencesError(guar)));
437445
}
438446

439447
let fields = &def.non_enum_variant().fields;
@@ -459,10 +467,10 @@ fn layout_of_uncached<'tcx>(
459467
// (should be caught by typeck)
460468
for fi in fields {
461469
if fi.ty(tcx, args) != f0_ty {
462-
tcx.dcx().delayed_bug(
470+
let guar = tcx.dcx().delayed_bug(
463471
"#[repr(simd)] was applied to an ADT with heterogeneous field type",
464472
);
465-
return Err(error(cx, LayoutError::Unknown(ty)));
473+
return Err(error(cx, LayoutError::ReferencesError(guar)));
466474
}
467475
}
468476

@@ -567,11 +575,11 @@ fn layout_of_uncached<'tcx>(
567575

568576
if def.is_union() {
569577
if def.repr().pack.is_some() && def.repr().align.is_some() {
570-
tcx.dcx().span_delayed_bug(
578+
let guar = tcx.dcx().span_delayed_bug(
571579
tcx.def_span(def.did()),
572580
"union cannot be packed and aligned",
573581
);
574-
return Err(error(cx, LayoutError::Unknown(ty)));
582+
return Err(error(cx, LayoutError::ReferencesError(guar)));
575583
}
576584

577585
return Ok(tcx.mk_layout(
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
union Foo {
2+
a: str,
3+
//~^ ERROR the size for values of type `str` cannot be known at compilation time
4+
//~| ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>`
5+
}
6+
7+
enum Bar {
8+
Boo = {
9+
let _: Option<Foo> = None;
10+
0
11+
},
12+
}
13+
14+
union Foo2 {}
15+
//~^ ERROR unions cannot have zero fields
16+
17+
enum Bar2 {
18+
Boo = {
19+
let _: Option<Foo2> = None;
20+
0
21+
},
22+
}
23+
24+
#[repr(u8, packed)]
25+
//~^ ERROR attribute should be applied to a struct or union
26+
enum Foo3 {
27+
A
28+
}
29+
30+
enum Bar3 {
31+
Boo = {
32+
let _: Option<Foo3> = None;
33+
0
34+
},
35+
}
36+
37+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
error: unions cannot have zero fields
2+
--> $DIR/eval-error.rs:14:1
3+
|
4+
LL | union Foo2 {}
5+
| ^^^^^^^^^^^^^
6+
7+
error[E0517]: attribute should be applied to a struct or union
8+
--> $DIR/eval-error.rs:24:12
9+
|
10+
LL | #[repr(u8, packed)]
11+
| ^^^^^^
12+
LL |
13+
LL | / enum Foo3 {
14+
LL | | A
15+
LL | | }
16+
| |_- not a struct or union
17+
18+
error[E0277]: the size for values of type `str` cannot be known at compilation time
19+
--> $DIR/eval-error.rs:2:8
20+
|
21+
LL | a: str,
22+
| ^^^ doesn't have a size known at compile-time
23+
|
24+
= help: the trait `Sized` is not implemented for `str`
25+
= note: no field of a union may have a dynamically sized type
26+
= help: change the field's type to have a statically known size
27+
help: borrowed types always have a statically known size
28+
|
29+
LL | a: &str,
30+
| +
31+
help: the `Box` type always has a statically known size and allocates its contents in the heap
32+
|
33+
LL | a: Box<str>,
34+
| ++++ +
35+
36+
error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
37+
--> $DIR/eval-error.rs:2:5
38+
|
39+
LL | a: str,
40+
| ^^^^^^
41+
|
42+
= note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
43+
help: wrap the field type in `ManuallyDrop<...>`
44+
|
45+
LL | a: std::mem::ManuallyDrop<str>,
46+
| +++++++++++++++++++++++ +
47+
48+
error: aborting due to 4 previous errors
49+
50+
Some errors have detailed explanations: E0277, E0517, E0740.
51+
For more information about an error, try `rustc --explain E0277`.

tests/ui/layout/base-layout-is-sized-ice-123078.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ struct S {
88
}
99

1010
const C: S = unsafe { std::mem::transmute(()) };
11-
//~^ ERROR cannot transmute between types of different sizes, or dependently-sized types
1211
const _: [(); {
1312
C;
1413
0

tests/ui/layout/base-layout-is-sized-ice-123078.stderr

+2-12
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@ help: the `Box` type always has a statically known size and allocates its conten
1616
LL | a: Box<[u8]>,
1717
| ++++ +
1818

19-
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
20-
--> $DIR/base-layout-is-sized-ice-123078.rs:10:23
21-
|
22-
LL | const C: S = unsafe { std::mem::transmute(()) };
23-
| ^^^^^^^^^^^^^^^^^^^
24-
|
25-
= note: source type: `()` (0 bits)
26-
= note: target type: `S` (size can vary because of [u8])
27-
28-
error: aborting due to 2 previous errors
19+
error: aborting due to 1 previous error
2920

30-
Some errors have detailed explanations: E0277, E0512.
31-
For more information about an error, try `rustc --explain E0277`.
21+
For more information about this error, try `rustc --explain E0277`.

tests/ui/layout/debug.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ LL | type Impossible = (str, str);
590590
= help: the trait `Sized` is not implemented for `str`
591591
= note: only the last element of a tuple may have a dynamically sized type
592592

593-
error: the type `EmptyUnion` has an unknown layout
593+
error: the type has an unknown layout
594594
--> $DIR/debug.rs:83:1
595595
|
596596
LL | union EmptyUnion {}

0 commit comments

Comments
 (0)