Skip to content

Commit 2c875a0

Browse files
committed
Unify zero-length and oversized SIMD errors
1 parent f53bf51 commit 2c875a0

File tree

15 files changed

+62
-36
lines changed

15 files changed

+62
-36
lines changed

compiler/rustc_codegen_cranelift/src/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ impl<'tcx> LayoutOfHelpers<'tcx> for FullyMonomorphizedLayoutCx<'tcx> {
440440
#[inline]
441441
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
442442
if let LayoutError::SizeOverflow(_)
443-
| LayoutError::OversizedSimd(_)
443+
| LayoutError::InvalidSimd { .. }
444444
| LayoutError::ReferencesError(_) = err
445445
{
446446
self.0.sess.dcx().span_fatal(span, err.to_string())
@@ -461,7 +461,7 @@ impl<'tcx> FnAbiOfHelpers<'tcx> for FullyMonomorphizedLayoutCx<'tcx> {
461461
span: Span,
462462
fn_abi_request: FnAbiRequest<'tcx>,
463463
) -> ! {
464-
if let FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::OversizedSimd(_, _)) =
464+
if let FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::InvalidSimd { .. }) =
465465
err
466466
{
467467
self.0.sess.dcx().emit_fatal(Spanned { span, node: err })

compiler/rustc_codegen_gcc/src/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ 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>) -> ! {
532532
if let LayoutError::SizeOverflow(_)
533-
| LayoutError::OversizedSimd(_)
533+
| LayoutError::InvalidSimd { .. }
534534
| LayoutError::ReferencesError(_) = err
535535
{
536536
self.tcx.dcx().emit_fatal(respan(span, err.into_diagnostic()))
@@ -548,7 +548,7 @@ impl<'gcc, 'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
548548
span: Span,
549549
fn_abi_request: FnAbiRequest<'tcx>,
550550
) -> ! {
551-
if let FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::OversizedSimd(_, _)) =
551+
if let FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::InvalidSimd { .. }) =
552552
err
553553
{
554554
self.tcx.dcx().emit_fatal(respan(span, err))

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
993993
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
994994
if let LayoutError::SizeOverflow(_)
995995
| LayoutError::ReferencesError(_)
996-
| LayoutError::OversizedSimd(_, _) = err
996+
| LayoutError::InvalidSimd { .. } = err
997997
{
998998
self.tcx.dcx().emit_fatal(Spanned { span, node: err.into_diagnostic() })
999999
} else {
@@ -1014,7 +1014,7 @@ impl<'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
10141014
FnAbiError::Layout(
10151015
LayoutError::SizeOverflow(_)
10161016
| LayoutError::Cycle(_)
1017-
| LayoutError::OversizedSimd(_, _),
1017+
| LayoutError::InvalidSimd { .. },
10181018
) => {
10191019
self.tcx.dcx().emit_fatal(Spanned { span, node: err });
10201020
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/cmse.rs

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

compiler/rustc_middle/messages.ftl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,12 @@ 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 =
101+
middle_layout_simd_too_many =
102102
the SIMD type `{$ty}` has more elements than the limit {$max_lanes}
103103
104+
middle_layout_simd_zero_length =
105+
the SIMD type `{$ty}` has zero elements
106+
104107
middle_layout_too_generic = the type `{$ty}` does not have a fixed layout
105108
106109
middle_layout_unknown =

compiler/rustc_middle/src/error.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,11 @@ 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 },
144+
#[diag(middle_layout_simd_too_many)]
145+
SimdTooManyLanes { ty: Ty<'tcx>, max_lanes: u64 },
146+
147+
#[diag(middle_layout_simd_zero_length)]
148+
SimdZeroLength { ty: Ty<'tcx> },
146149

147150
#[diag(middle_layout_normalization_failure)]
148151
NormalizationFailure { ty: Ty<'tcx>, failure_ty: String },

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,15 @@ impl fmt::Display for ValidityRequirement {
217217
}
218218
}
219219

220+
#[derive(Copy, Clone, Debug, HashStable, TyEncodable, TyDecodable)]
221+
pub enum SimdLayoutError {
222+
/// The vector has 0 lanes.
223+
ZeroLength,
224+
/// The vector has more lanes than supported or permitted by
225+
/// #[rustc_simd_monomorphize_lane_limit].
226+
TooManyLanes(u64),
227+
}
228+
220229
#[derive(Copy, Clone, Debug, HashStable, TyEncodable, TyDecodable)]
221230
pub enum LayoutError<'tcx> {
222231
/// A type doesn't have a sensible layout.
@@ -229,9 +238,8 @@ pub enum LayoutError<'tcx> {
229238
Unknown(Ty<'tcx>),
230239
/// The size of a type exceeds [`TargetDataLayout::obj_size_bound`].
231240
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),
241+
/// A SIMD vector has invalid layout, such as zero-length or too many lanes.
242+
InvalidSimd { ty: Ty<'tcx>, kind: SimdLayoutError },
235243
/// The layout can vary due to a generic parameter.
236244
///
237245
/// Unlike `Unknown`, this variant is a "soft" error and indicates that the layout
@@ -259,7 +267,10 @@ impl<'tcx> LayoutError<'tcx> {
259267
match self {
260268
Unknown(_) => middle_layout_unknown,
261269
SizeOverflow(_) => middle_layout_size_overflow,
262-
OversizedSimd(_, _) => middle_layout_oversized_simd,
270+
InvalidSimd { kind: SimdLayoutError::TooManyLanes(_), .. } => {
271+
middle_layout_simd_too_many
272+
}
273+
InvalidSimd { kind: SimdLayoutError::ZeroLength, .. } => middle_layout_simd_zero_length,
263274
TooGeneric(_) => middle_layout_too_generic,
264275
NormalizationFailure(_, _) => middle_layout_normalization_failure,
265276
Cycle(_) => middle_layout_cycle,
@@ -274,7 +285,10 @@ impl<'tcx> LayoutError<'tcx> {
274285
match self {
275286
Unknown(ty) => E::Unknown { ty },
276287
SizeOverflow(ty) => E::Overflow { ty },
277-
OversizedSimd(ty, max_lanes) => E::OversizedSimd { ty, max_lanes },
288+
InvalidSimd { ty, kind: SimdLayoutError::TooManyLanes(max_lanes) } => {
289+
E::SimdTooManyLanes { ty, max_lanes }
290+
}
291+
InvalidSimd { ty, kind: SimdLayoutError::ZeroLength } => E::SimdZeroLength { ty },
278292
TooGeneric(ty) => E::TooGeneric { ty },
279293
NormalizationFailure(ty, e) => {
280294
E::NormalizationFailure { ty, failure_ty: e.get_type_for_failure() }
@@ -297,9 +311,12 @@ impl<'tcx> fmt::Display for LayoutError<'tcx> {
297311
LayoutError::SizeOverflow(ty) => {
298312
write!(f, "values of the type `{ty}` are too big for the target architecture")
299313
}
300-
LayoutError::OversizedSimd(ty, max_lanes) => {
314+
LayoutError::InvalidSimd { ty, kind: SimdLayoutError::TooManyLanes(max_lanes) } => {
301315
write!(f, "the SIMD type `{ty}` has more elements than the limit {max_lanes}")
302316
}
317+
LayoutError::InvalidSimd { ty, kind: SimdLayoutError::ZeroLength } => {
318+
write!(f, "the SIMD type `{ty}` has zero elements")
319+
}
303320
LayoutError::NormalizationFailure(t, e) => write!(
304321
f,
305322
"unable to determine layout for `{}` because `{}` cannot be normalized",
@@ -381,7 +398,7 @@ impl<'tcx> SizeSkeleton<'tcx> {
381398
e @ LayoutError::Cycle(_)
382399
| e @ LayoutError::Unknown(_)
383400
| e @ LayoutError::SizeOverflow(_)
384-
| e @ LayoutError::OversizedSimd(_, _)
401+
| e @ LayoutError::InvalidSimd { .. }
385402
| e @ LayoutError::NormalizationFailure(..)
386403
| e @ LayoutError::ReferencesError(_),
387404
) => return Err(e),

compiler/rustc_transmute/src/layout/tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ pub(crate) mod rustc {
279279
LayoutError::Unknown(..)
280280
| LayoutError::ReferencesError(..)
281281
| LayoutError::TooGeneric(..)
282+
| LayoutError::InvalidSimd { .. }
282283
| LayoutError::NormalizationFailure(..) => Self::UnknownLayout,
283-
LayoutError::OversizedSimd(..) => Self::UnknownLayout,
284284
LayoutError::SizeOverflow(..) => Self::SizeOverflow,
285285
LayoutError::Cycle(err) => Self::TypeError(*err),
286286
}

compiler/rustc_ty_utils/src/errors.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,6 @@ pub(crate) struct UnexpectedFnPtrAssociatedItem {
7878
pub span: Span,
7979
}
8080

81-
#[derive(Diagnostic)]
82-
#[diag(ty_utils_zero_length_simd_type)]
83-
pub(crate) struct ZeroLengthSimdType<'tcx> {
84-
pub ty: Ty<'tcx>,
85-
}
86-
8781
#[derive(Diagnostic)]
8882
#[diag(ty_utils_non_primitive_simd_type)]
8983
pub(crate) struct NonPrimitiveSimdType<'tcx> {

compiler/rustc_ty_utils/src/layout.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_index::IndexVec;
1313
use rustc_middle::bug;
1414
use rustc_middle::query::Providers;
1515
use rustc_middle::ty::layout::{
16-
FloatExt, HasTyCtxt, IntegerExt, LayoutCx, LayoutError, LayoutOf, TyAndLayout,
16+
FloatExt, HasTyCtxt, IntegerExt, LayoutCx, LayoutError, LayoutOf, SimdLayoutError, TyAndLayout,
1717
};
1818
use rustc_middle::ty::print::with_no_trimmed_paths;
1919
use rustc_middle::ty::{
@@ -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, ZeroLengthSimdType};
27+
use crate::errors::NonPrimitiveSimdType;
2828

2929
mod invariant;
3030

@@ -122,11 +122,11 @@ fn map_error<'tcx>(
122122
}
123123
LayoutCalculatorError::ZeroLengthSimdType => {
124124
// Can't be caught in typeck if the array length is generic.
125-
cx.tcx().dcx().emit_fatal(ZeroLengthSimdType { ty })
125+
LayoutError::InvalidSimd { ty, kind: SimdLayoutError::ZeroLength }
126126
}
127127
LayoutCalculatorError::OversizedSimdType { max_lanes } => {
128128
// Can't be caught in typeck if the array length is generic.
129-
LayoutError::OversizedSimd(ty, max_lanes)
129+
LayoutError::InvalidSimd { ty, kind: SimdLayoutError::TooManyLanes(max_lanes) }
130130
}
131131
LayoutCalculatorError::NonPrimitiveSimdType(field) => {
132132
// This error isn't caught in typeck, e.g., if

0 commit comments

Comments
 (0)