Skip to content

Commit f53bf51

Browse files
committed
Improve oversized SIMD layout error
1 parent 83ca8cd commit f53bf51

File tree

18 files changed

+67
-31
lines changed

18 files changed

+67
-31
lines changed

compiler/rustc_codegen_cranelift/src/common.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,10 @@ pub(crate) struct FullyMonomorphizedLayoutCx<'tcx>(pub(crate) TyCtxt<'tcx>);
439439
impl<'tcx> LayoutOfHelpers<'tcx> for FullyMonomorphizedLayoutCx<'tcx> {
440440
#[inline]
441441
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
442-
if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
442+
if let LayoutError::SizeOverflow(_)
443+
| LayoutError::OversizedSimd(_)
444+
| LayoutError::ReferencesError(_) = err
445+
{
443446
self.0.sess.dcx().span_fatal(span, err.to_string())
444447
} else {
445448
self.0
@@ -458,7 +461,9 @@ impl<'tcx> FnAbiOfHelpers<'tcx> for FullyMonomorphizedLayoutCx<'tcx> {
458461
span: Span,
459462
fn_abi_request: FnAbiRequest<'tcx>,
460463
) -> ! {
461-
if let FnAbiError::Layout(LayoutError::SizeOverflow(_)) = err {
464+
if let FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::OversizedSimd(_, _)) =
465+
err
466+
{
462467
self.0.sess.dcx().emit_fatal(Spanned { span, node: err })
463468
} else {
464469
match fn_abi_request {

compiler/rustc_codegen_gcc/src/context.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,10 @@ impl<'gcc, 'tcx> HasX86AbiOpt for CodegenCx<'gcc, 'tcx> {
529529
impl<'gcc, 'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
530530
#[inline]
531531
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
532-
if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
532+
if let LayoutError::SizeOverflow(_)
533+
| LayoutError::OversizedSimd(_)
534+
| LayoutError::ReferencesError(_) = err
535+
{
533536
self.tcx.dcx().emit_fatal(respan(span, err.into_diagnostic()))
534537
} else {
535538
self.tcx.dcx().emit_fatal(ssa_errors::FailedToGetLayout { span, ty, err })
@@ -545,7 +548,9 @@ impl<'gcc, 'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
545548
span: Span,
546549
fn_abi_request: FnAbiRequest<'tcx>,
547550
) -> ! {
548-
if let FnAbiError::Layout(LayoutError::SizeOverflow(_)) = err {
551+
if let FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::OversizedSimd(_, _)) =
552+
err
553+
{
549554
self.tcx.dcx().emit_fatal(respan(span, err))
550555
} else {
551556
match fn_abi_request {

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,10 @@ impl<'tcx, 'll> HasTypingEnv<'tcx> for CodegenCx<'ll, 'tcx> {
991991
impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
992992
#[inline]
993993
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
994-
if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
994+
if let LayoutError::SizeOverflow(_)
995+
| LayoutError::ReferencesError(_)
996+
| LayoutError::OversizedSimd(_, _) = err
997+
{
995998
self.tcx.dcx().emit_fatal(Spanned { span, node: err.into_diagnostic() })
996999
} else {
9971000
self.tcx.dcx().emit_fatal(ssa_errors::FailedToGetLayout { span, ty, err })
@@ -1008,7 +1011,11 @@ impl<'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
10081011
fn_abi_request: FnAbiRequest<'tcx>,
10091012
) -> ! {
10101013
match err {
1011-
FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::Cycle(_)) => {
1014+
FnAbiError::Layout(
1015+
LayoutError::SizeOverflow(_)
1016+
| LayoutError::Cycle(_)
1017+
| LayoutError::OversizedSimd(_, _),
1018+
) => {
10121019
self.tcx.dcx().emit_fatal(Spanned { span, node: err });
10131020
}
10141021
_ => match fn_abi_request {

compiler/rustc_hir_analysis/src/hir_ty_lowering/cmse.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ fn should_emit_generic_error<'tcx>(abi: ExternAbi, layout_err: &'tcx LayoutError
213213
}
214214
Unknown(..)
215215
| SizeOverflow(..)
216+
| OversizedSimd(..)
216217
| NormalizationFailure(..)
217218
| ReferencesError(..)
218219
| Cycle(..) => {

compiler/rustc_middle/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ middle_layout_references_error =
9898
middle_layout_size_overflow =
9999
values of the type `{$ty}` are too big for the target architecture
100100
101+
middle_layout_oversized_simd =
102+
the SIMD type `{$ty}` has more elements than the limit {$max_lanes}
103+
101104
middle_layout_too_generic = the type `{$ty}` does not have a fixed layout
102105
103106
middle_layout_unknown =

compiler/rustc_middle/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ pub enum LayoutError<'tcx> {
141141
#[diag(middle_layout_size_overflow)]
142142
Overflow { ty: Ty<'tcx> },
143143

144+
#[diag(middle_layout_oversized_simd)]
145+
OversizedSimd { ty: Ty<'tcx>, max_lanes: u64 },
146+
144147
#[diag(middle_layout_normalization_failure)]
145148
NormalizationFailure { ty: Ty<'tcx>, failure_ty: String },
146149

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ pub enum LayoutError<'tcx> {
229229
Unknown(Ty<'tcx>),
230230
/// The size of a type exceeds [`TargetDataLayout::obj_size_bound`].
231231
SizeOverflow(Ty<'tcx>),
232+
/// The size of a SIMD type exceeds either the max SIMD size, or the limit
233+
/// configured by #[rustc_simd_monomorphize_lane_limit].
234+
OversizedSimd(Ty<'tcx>, u64),
232235
/// The layout can vary due to a generic parameter.
233236
///
234237
/// Unlike `Unknown`, this variant is a "soft" error and indicates that the layout
@@ -256,6 +259,7 @@ impl<'tcx> LayoutError<'tcx> {
256259
match self {
257260
Unknown(_) => middle_layout_unknown,
258261
SizeOverflow(_) => middle_layout_size_overflow,
262+
OversizedSimd(_, _) => middle_layout_oversized_simd,
259263
TooGeneric(_) => middle_layout_too_generic,
260264
NormalizationFailure(_, _) => middle_layout_normalization_failure,
261265
Cycle(_) => middle_layout_cycle,
@@ -270,6 +274,7 @@ impl<'tcx> LayoutError<'tcx> {
270274
match self {
271275
Unknown(ty) => E::Unknown { ty },
272276
SizeOverflow(ty) => E::Overflow { ty },
277+
OversizedSimd(ty, max_lanes) => E::OversizedSimd { ty, max_lanes },
273278
TooGeneric(ty) => E::TooGeneric { ty },
274279
NormalizationFailure(ty, e) => {
275280
E::NormalizationFailure { ty, failure_ty: e.get_type_for_failure() }
@@ -292,6 +297,9 @@ impl<'tcx> fmt::Display for LayoutError<'tcx> {
292297
LayoutError::SizeOverflow(ty) => {
293298
write!(f, "values of the type `{ty}` are too big for the target architecture")
294299
}
300+
LayoutError::OversizedSimd(ty, max_lanes) => {
301+
write!(f, "the SIMD type `{ty}` has more elements than the limit {max_lanes}")
302+
}
295303
LayoutError::NormalizationFailure(t, e) => write!(
296304
f,
297305
"unable to determine layout for `{}` because `{}` cannot be normalized",
@@ -373,6 +381,7 @@ impl<'tcx> SizeSkeleton<'tcx> {
373381
e @ LayoutError::Cycle(_)
374382
| e @ LayoutError::Unknown(_)
375383
| e @ LayoutError::SizeOverflow(_)
384+
| e @ LayoutError::OversizedSimd(_, _)
376385
| e @ LayoutError::NormalizationFailure(..)
377386
| e @ LayoutError::ReferencesError(_),
378387
) => return Err(e),

compiler/rustc_transmute/src/layout/tree.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ pub(crate) mod rustc {
280280
| LayoutError::ReferencesError(..)
281281
| LayoutError::TooGeneric(..)
282282
| LayoutError::NormalizationFailure(..) => Self::UnknownLayout,
283+
LayoutError::OversizedSimd(..) => Self::UnknownLayout,
283284
LayoutError::SizeOverflow(..) => Self::SizeOverflow,
284285
LayoutError::Cycle(err) => Self::TypeError(*err),
285286
}

compiler/rustc_ty_utils/src/errors.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,6 @@ pub(crate) struct ZeroLengthSimdType<'tcx> {
8484
pub ty: Ty<'tcx>,
8585
}
8686

87-
#[derive(Diagnostic)]
88-
#[diag(ty_utils_oversized_simd_type)]
89-
pub(crate) struct OversizedSimdType<'tcx> {
90-
pub ty: Ty<'tcx>,
91-
pub max_lanes: u64,
92-
}
93-
9487
#[derive(Diagnostic)]
9588
#[diag(ty_utils_non_primitive_simd_type)]
9689
pub(crate) struct NonPrimitiveSimdType<'tcx> {

compiler/rustc_ty_utils/src/layout.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_span::{Symbol, sym};
2424
use tracing::{debug, instrument};
2525
use {rustc_abi as abi, rustc_hir as hir};
2626

27-
use crate::errors::{NonPrimitiveSimdType, OversizedSimdType, ZeroLengthSimdType};
27+
use crate::errors::{NonPrimitiveSimdType, ZeroLengthSimdType};
2828

2929
mod invariant;
3030

@@ -126,7 +126,7 @@ fn map_error<'tcx>(
126126
}
127127
LayoutCalculatorError::OversizedSimdType { max_lanes } => {
128128
// Can't be caught in typeck if the array length is generic.
129-
cx.tcx().dcx().emit_fatal(OversizedSimdType { ty, max_lanes })
129+
LayoutError::OversizedSimd(ty, max_lanes)
130130
}
131131
LayoutCalculatorError::NonPrimitiveSimdType(field) => {
132132
// This error isn't caught in typeck, e.g., if

0 commit comments

Comments
 (0)