Skip to content

Commit 997c3f4

Browse files
Add TooGeneric variant to LayoutError and emit Unknown one
- `check-pass` test for a MRE of #135020 - fail test for #135138 - switch to `TooGeneric` for checking CMSE fn signatures - switch to `TooGeneric` for compute `SizeSkeleton` (for transmute) - fix broken tests
1 parent 341f603 commit 997c3f4

32 files changed

+243
-79
lines changed

compiler/rustc_const_eval/src/const_eval/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ where
140140
// Don't emit a new diagnostic for these errors, they are already reported elsewhere or
141141
// should remain silent.
142142
err_inval!(AlreadyReported(info)) => ErrorHandled::Reported(info, span),
143-
err_inval!(Layout(LayoutError::Unknown(_))) | err_inval!(TooGeneric) => {
143+
err_inval!(Layout(LayoutError::TooGeneric(_))) | err_inval!(TooGeneric) => {
144144
ErrorHandled::TooGeneric(span)
145145
}
146146
err_inval!(Layout(LayoutError::ReferencesError(guar))) => {

compiler/rustc_hir_analysis/src/hir_ty_lowering/cmse.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ fn should_emit_generic_error<'tcx>(abi: ExternAbi, layout_err: &'tcx LayoutError
201201
use LayoutError::*;
202202

203203
match layout_err {
204-
Unknown(ty) => {
204+
TooGeneric(ty) => {
205205
match abi {
206206
ExternAbi::CCmseNonSecureCall => {
207207
// prevent double reporting of this error
@@ -211,7 +211,11 @@ fn should_emit_generic_error<'tcx>(abi: ExternAbi, layout_err: &'tcx LayoutError
211211
_ => bug!("invalid ABI: {abi}"),
212212
}
213213
}
214-
SizeOverflow(..) | NormalizationFailure(..) | ReferencesError(..) | Cycle(..) => {
214+
Unknown(..)
215+
| SizeOverflow(..)
216+
| NormalizationFailure(..)
217+
| ReferencesError(..)
218+
| Cycle(..) => {
215219
false // not our job to report these
216220
}
217221
}

compiler/rustc_middle/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,14 @@ middle_strict_coherence_needs_negative_coherence =
100100
to use `strict_coherence` on this trait, the `with_negative_coherence` feature must be enabled
101101
.label = due to this attribute
102102
103+
middle_too_generic = `{$ty}` does not have a fixed size
104+
103105
middle_type_length_limit = reached the type-length limit while instantiating `{$shrunk}`
104106
105107
middle_unknown_layout =
106108
the type `{$ty}` has an unknown layout
107109
108110
middle_values_too_big =
109111
values of the type `{$ty}` are too big for the target architecture
112+
110113
middle_written_to_path = the full type name has been written to '{$path}'

compiler/rustc_middle/src/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ pub enum LayoutError<'tcx> {
129129
#[diag(middle_unknown_layout)]
130130
Unknown { ty: Ty<'tcx> },
131131

132+
#[diag(middle_too_generic)]
133+
TooGeneric { ty: Ty<'tcx> },
134+
132135
#[diag(middle_values_too_big)]
133136
Overflow { ty: Ty<'tcx> },
134137

compiler/rustc_middle/src/ty/layout.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ impl fmt::Display for ValidityRequirement {
231231
pub enum LayoutError<'tcx> {
232232
Unknown(Ty<'tcx>),
233233
SizeOverflow(Ty<'tcx>),
234+
TooGeneric(Ty<'tcx>),
234235
NormalizationFailure(Ty<'tcx>, NormalizationError<'tcx>),
235236
ReferencesError(ErrorGuaranteed),
236237
Cycle(ErrorGuaranteed),
@@ -244,6 +245,7 @@ impl<'tcx> LayoutError<'tcx> {
244245
match self {
245246
Unknown(_) => middle_unknown_layout,
246247
SizeOverflow(_) => middle_values_too_big,
248+
TooGeneric(_) => middle_too_generic,
247249
NormalizationFailure(_, _) => middle_cannot_be_normalized,
248250
Cycle(_) => middle_cycle,
249251
ReferencesError(_) => middle_layout_references_error,
@@ -257,6 +259,7 @@ impl<'tcx> LayoutError<'tcx> {
257259
match self {
258260
Unknown(ty) => E::Unknown { ty },
259261
SizeOverflow(ty) => E::Overflow { ty },
262+
TooGeneric(ty) => E::TooGeneric { ty },
260263
NormalizationFailure(ty, e) => {
261264
E::NormalizationFailure { ty, failure_ty: e.get_type_for_failure() }
262265
}
@@ -272,6 +275,9 @@ impl<'tcx> fmt::Display for LayoutError<'tcx> {
272275
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
273276
match *self {
274277
LayoutError::Unknown(ty) => write!(f, "the type `{ty}` has an unknown layout"),
278+
LayoutError::TooGeneric(ty) => {
279+
write!(f, "`{ty}` does not have a fixed size")
280+
}
275281
LayoutError::SizeOverflow(ty) => {
276282
write!(f, "values of the type `{ty}` are too big for the target architecture")
277283
}
@@ -350,10 +356,11 @@ impl<'tcx> SizeSkeleton<'tcx> {
350356
return Err(tcx.arena.alloc(LayoutError::Unknown(ty)));
351357
}
352358
}
353-
Err(err @ LayoutError::Unknown(_)) => err,
359+
Err(err @ LayoutError::TooGeneric(_)) => err,
354360
// We can't extract SizeSkeleton info from other layout errors
355361
Err(
356362
e @ LayoutError::Cycle(_)
363+
| e @ LayoutError::Unknown(_)
357364
| e @ LayoutError::SizeOverflow(_)
358365
| e @ LayoutError::NormalizationFailure(..)
359366
| e @ LayoutError::ReferencesError(_),

compiler/rustc_transmute/src/layout/tree.rs

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ pub(crate) mod rustc {
197197
match err {
198198
LayoutError::Unknown(..)
199199
| LayoutError::ReferencesError(..)
200+
| LayoutError::TooGeneric(..)
200201
| LayoutError::NormalizationFailure(..) => Self::UnknownLayout,
201202
LayoutError::SizeOverflow(..) => Self::SizeOverflow,
202203
LayoutError::Cycle(err) => Self::TypeError(*err),

compiler/rustc_ty_utils/src/layout.rs

+35-15
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,12 @@ fn map_error<'tcx>(
104104
// This is sometimes not a compile error if there are trivially false where clauses.
105105
// See `tests/ui/layout/trivial-bounds-sized.rs` for an example.
106106
assert!(field.layout.is_unsized(), "invalid layout error {err:#?}");
107-
if !field.ty.is_sized(cx.tcx(), cx.typing_env) {
108-
let guar = cx.tcx().dcx().delayed_bug(format!(
107+
if cx.typing_env.param_env.caller_bounds().is_empty() {
108+
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)
114111
}
112+
LayoutError::Unknown(ty)
115113
}
116114
LayoutCalculatorError::EmptyUnion => {
117115
// This is always a compile error.
@@ -319,17 +317,32 @@ fn layout_of_uncached<'tcx>(
319317
}
320318

321319
// Arrays and slices.
322-
ty::Array(element, mut count) => {
323-
if count.has_aliases() {
324-
count = tcx.normalize_erasing_regions(cx.typing_env, count);
325-
if count.has_aliases() {
326-
return Err(error(cx, LayoutError::Unknown(ty)));
320+
ty::Array(element, count) => {
321+
let count = match count.kind() {
322+
ty::ConstKind::Value(_, valtree) => valtree
323+
.try_to_target_usize(tcx)
324+
.ok_or_else(|| error(cx, LayoutError::Unknown(ty)))?,
325+
ty::ConstKind::Error(guar) => {
326+
return Err(error(cx, LayoutError::ReferencesError(guar)));
327327
}
328-
}
328+
ty::ConstKind::Param(_) | ty::ConstKind::Expr(_) => {
329+
if !count.has_param() {
330+
bug!("no generic type found in count of array type: {ty:?}");
331+
}
332+
return Err(error(cx, LayoutError::TooGeneric(ty)));
333+
}
334+
ty::ConstKind::Unevaluated(_) => {
335+
if !count.has_param() {
336+
return Err(error(cx, LayoutError::Unknown(ty)));
337+
} else {
338+
return Err(error(cx, LayoutError::TooGeneric(ty)));
339+
}
340+
}
341+
ty::ConstKind::Infer(_)
342+
| ty::ConstKind::Bound(..)
343+
| ty::ConstKind::Placeholder(_) => bug!("unexpected count in array type: {ty:?}"),
344+
};
329345

330-
let count = count
331-
.try_to_target_usize(tcx)
332-
.ok_or_else(|| error(cx, LayoutError::Unknown(ty)))?;
333346
let element = cx.layout_of(element)?;
334347
let size = element
335348
.size
@@ -687,6 +700,9 @@ fn layout_of_uncached<'tcx>(
687700

688701
// Types with no meaningful known layout.
689702
ty::Alias(..) => {
703+
if ty.has_param() {
704+
return Err(error(cx, LayoutError::TooGeneric(ty)));
705+
}
690706
// NOTE(eddyb) `layout_of` query should've normalized these away,
691707
// if that was possible, so there's no reason to try again here.
692708
return Err(error(cx, LayoutError::Unknown(ty)));
@@ -696,7 +712,11 @@ fn layout_of_uncached<'tcx>(
696712
bug!("Layout::compute: unexpected type `{}`", ty)
697713
}
698714

699-
ty::Placeholder(..) | ty::Param(_) => {
715+
ty::Param(_) => {
716+
return Err(error(cx, LayoutError::TooGeneric(ty)));
717+
}
718+
719+
ty::Placeholder(..) => {
700720
return Err(error(cx, LayoutError::Unknown(ty)));
701721
}
702722
})

src/librustdoc/html/templates/type_layout.html

+11-5
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ <h2 id="layout" class="section-header"> {# #}
2828
{% endfor %}
2929
</ul>
3030
{% endif %}
31-
{# This kind of layout error can occur with valid code, e.g. if you try to
32-
get the layout of a generic type such as `Vec<T>`. #}
31+
{# This kind of layout error can occur with valid code, for example
32+
if there are trivial bounds: `struct Foo(str, str) where str: Sized;`. #}
3333
{% when Err(LayoutError::Unknown(_)) %}
3434
<p> {# #}
35-
<strong>Note:</strong> Unable to compute type layout, {#+ #}
36-
possibly due to this type having generic parameters. {#+ #}
37-
Layout can only be computed for concrete, fully-instantiated types. {# #}
35+
<strong>Note:</strong> Unable to compute type layout. {# #}
3836
</p>
3937
{# This kind of error probably can't happen with valid code, but we don't
4038
want to panic and prevent the docs from building, so we just let the
@@ -44,6 +42,14 @@ <h2 id="layout" class="section-header"> {# #}
4442
<strong>Note:</strong> Encountered an error during type layout; {#+ #}
4543
the type was too big. {# #}
4644
</p>
45+
{# This kind of layout error can occur with valid code, e.g. if you try to
46+
get the layout of a generic type such as `Vec<T>`. #}
47+
{% when Err(LayoutError::TooGeneric(_)) %}
48+
<p> {# #}
49+
<strong>Note:</strong> Unable to compute type layout, {#+ #}
50+
possibly due to this type having generic parameters. {#+ #}
51+
Layout can only be computed for concrete, fully-instantiated types. {# #}
52+
</p>
4753
{% when Err(LayoutError::ReferencesError(_)) %}
4854
<p> {# #}
4955
<strong>Note:</strong> Encountered an error during type layout; {#+ #}

tests/crashes/135020.rs

-11
This file was deleted.

tests/rustdoc/type-layout.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub struct Y(u8);
3737
pub struct Z;
3838

3939
// We can't compute layout for generic types.
40-
//@ hasraw type_layout/struct.Generic.html 'Unable to compute type layout, possibly due to this type having generic parameters'
40+
//@ hasraw type_layout/struct.Generic.html 'Encountered an error during type layout; the type was too generic.'
4141
//@ !hasraw - 'Size: '
4242
pub struct Generic<T>(T);
4343

@@ -91,3 +91,9 @@ pub enum Uninhabited {}
9191
//@ hasraw type_layout/struct.Uninhabited2.html 'Size: '
9292
//@ hasraw - '8 bytes (<a href="https://doc.rust-lang.org/stable/reference/glossary.html#uninhabited">uninhabited</a>)'
9393
pub struct Uninhabited2(std::convert::Infallible, u64);
94+
95+
pub trait Project { type Assoc; }
96+
// We can't compute layout. A `LayoutError::Unknown` is returned.
97+
//@ hasraw type_layout/struct.Unknown.html 'Unable to compute type layout.'
98+
//@ !hasraw - 'Size: '
99+
pub struct Unknown(<() as Project>::Assoc) where for<'a> (): Project;

tests/ui/const-generics/transmute_no_gate.stderr

+18-18
Original file line numberDiff line numberDiff line change
@@ -4,80 +4,80 @@ error[E0512]: cannot transmute between types of different sizes, or dependently-
44
LL | std::mem::transmute(v)
55
| ^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: source type: `[[u32; H]; W]` (this type does not have a fixed size)
8-
= note: target type: `[[u32; W]; H]` (this type does not have a fixed size)
7+
= note: source type: `[[u32; H]; W]` (`[[u32; H]; W]` does not have a fixed size)
8+
= note: target type: `[[u32; W]; H]` (`[[u32; W]; H]` does not have a fixed size)
99

1010
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
1111
--> $DIR/transmute_no_gate.rs:20:5
1212
|
1313
LL | std::mem::transmute(v)
1414
| ^^^^^^^^^^^^^^^^^^^
1515
|
16-
= note: source type: `[[u32; H]; W]` (this type does not have a fixed size)
17-
= note: target type: `[u32; W * H]` (this type does not have a fixed size)
16+
= note: source type: `[[u32; H]; W]` (`[[u32; H]; W]` does not have a fixed size)
17+
= note: target type: `[u32; W * H]` (`[u32; W * H]` does not have a fixed size)
1818

1919
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
2020
--> $DIR/transmute_no_gate.rs:27:5
2121
|
2222
LL | std::mem::transmute(v)
2323
| ^^^^^^^^^^^^^^^^^^^
2424
|
25-
= note: source type: `[u32; H*W]` (this type does not have a fixed size)
26-
= note: target type: `[[u32; W]; H]` (this type does not have a fixed size)
25+
= note: source type: `[u32; H*W]` (`[u32; H*W]` does not have a fixed size)
26+
= note: target type: `[[u32; W]; H]` (`[[u32; W]; H]` does not have a fixed size)
2727

2828
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
2929
--> $DIR/transmute_no_gate.rs:36:5
3030
|
3131
LL | std::mem::transmute(v)
3232
| ^^^^^^^^^^^^^^^^^^^
3333
|
34-
= note: source type: `[[[u32; D]; H]; W]` (this type does not have a fixed size)
35-
= note: target type: `[u32; D * W * H]` (this type does not have a fixed size)
34+
= note: source type: `[[[u32; D]; H]; W]` (`[[[u32; D]; H]; W]` does not have a fixed size)
35+
= note: target type: `[u32; D * W * H]` (`[u32; D * W * H]` does not have a fixed size)
3636

3737
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
3838
--> $DIR/transmute_no_gate.rs:45:5
3939
|
4040
LL | std::mem::transmute(v)
4141
| ^^^^^^^^^^^^^^^^^^^
4242
|
43-
= note: source type: `[[[u32; D]; H]; W]` (this type does not have a fixed size)
44-
= note: target type: `[[u32; D * W]; H]` (this type does not have a fixed size)
43+
= note: source type: `[[[u32; D]; H]; W]` (`[[[u32; D]; H]; W]` does not have a fixed size)
44+
= note: target type: `[[u32; D * W]; H]` (`[[u32; D * W]; H]` does not have a fixed size)
4545

4646
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
4747
--> $DIR/transmute_no_gate.rs:52:5
4848
|
4949
LL | std::mem::transmute(v)
5050
| ^^^^^^^^^^^^^^^^^^^
5151
|
52-
= note: source type: `[u16; L]` (this type does not have a fixed size)
53-
= note: target type: `[u8; L * 2]` (this type does not have a fixed size)
52+
= note: source type: `[u16; L]` (`[u16; L]` does not have a fixed size)
53+
= note: target type: `[u8; L * 2]` (`[u8; L * 2]` does not have a fixed size)
5454

5555
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
5656
--> $DIR/transmute_no_gate.rs:59:5
5757
|
5858
LL | std::mem::transmute(v)
5959
| ^^^^^^^^^^^^^^^^^^^
6060
|
61-
= note: source type: `[u8; L * 2]` (this type does not have a fixed size)
62-
= note: target type: `[u16; L]` (this type does not have a fixed size)
61+
= note: source type: `[u8; L * 2]` (`[u8; L * 2]` does not have a fixed size)
62+
= note: target type: `[u16; L]` (`[u16; L]` does not have a fixed size)
6363

6464
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
6565
--> $DIR/transmute_no_gate.rs:66:5
6666
|
6767
LL | std::mem::transmute(v)
6868
| ^^^^^^^^^^^^^^^^^^^
6969
|
70-
= note: source type: `[u8; L]` (this type does not have a fixed size)
71-
= note: target type: `[[u8; 1]; L]` (this type does not have a fixed size)
70+
= note: source type: `[u8; L]` (`[u8; L]` does not have a fixed size)
71+
= note: target type: `[[u8; 1]; L]` (`[[u8; 1]; L]` does not have a fixed size)
7272

7373
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
7474
--> $DIR/transmute_no_gate.rs:75:5
7575
|
7676
LL | std::mem::transmute(v)
7777
| ^^^^^^^^^^^^^^^^^^^
7878
|
79-
= note: source type: `[[u32; 2 * H]; W + W]` (this type does not have a fixed size)
80-
= note: target type: `[[u32; W + W]; 2 * H]` (this type does not have a fixed size)
79+
= note: source type: `[[u32; 2 * H]; W + W]` (`[[u32; 2 * H]; W + W]` does not have a fixed size)
80+
= note: target type: `[[u32; W + W]; 2 * H]` (`[[u32; W + W]; 2 * H]` does not have a fixed size)
8181

8282
error: aborting due to 9 previous errors
8383

tests/ui/enum-discriminant/eval-error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ union Foo {
66

77
enum Bar {
88
Boo = {
9-
let _: Option<Foo> = None;
9+
let _: Option<Foo> = None; //~ ERROR evaluation of constant value failed
1010
0
1111
},
1212
}

tests/ui/enum-discriminant/eval-error.stderr

+9-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ help: wrap the field type in `ManuallyDrop<...>`
4545
LL | a: std::mem::ManuallyDrop<str>,
4646
| +++++++++++++++++++++++ +
4747

48-
error: aborting due to 4 previous errors
48+
error[E0080]: evaluation of constant value failed
49+
--> $DIR/eval-error.rs:9:30
50+
|
51+
LL | let _: Option<Foo> = None;
52+
| ^^^^ the type `Foo` has an unknown layout
53+
54+
error: aborting due to 5 previous errors
4955

50-
Some errors have detailed explanations: E0277, E0517, E0740.
51-
For more information about an error, try `rustc --explain E0277`.
56+
Some errors have detailed explanations: E0080, E0277, E0517, E0740.
57+
For more information about an error, try `rustc --explain E0080`.

tests/ui/infinite/infinite-instantiation-struct-tail-ice-114484.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl<T, const L: u8> VirtualWrapper<T, L> {
2424
impl<T: MyTrait + 'static, const L: u8> MyTrait for VirtualWrapper<T, L> {
2525
fn virtualize(&self) -> &dyn MyTrait {
2626
unsafe { virtualize_my_trait(L, self) }
27-
// unsafe { virtualize_my_trait(L, &self.0) } // <-- this code fixes the problem
27+
// unsafe { virtualize_my_trait(L, &self.0) } // <-- this code fixes the problem
2828
}
2929
}
3030

0 commit comments

Comments
 (0)