Skip to content

Commit 804e938

Browse files
committed
Move SIMD layout errors to SessionDiagnostic
1 parent f7f2eb3 commit 804e938

File tree

4 files changed

+55
-20
lines changed

4 files changed

+55
-20
lines changed

compiler/rustc_ty_utils/messages.ftl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,11 @@ ty_utils_inline_asm_not_supported = assembly is not supported in generic constan
4747
ty_utils_operation_not_supported = unsupported operation in generic constants
4848
4949
ty_utils_unexpected_fnptr_associated_item = `FnPtr` trait with unexpected associated item
50+
51+
ty_utils_zero_length_simd_type = monomorphising SIMD type `{$ty}` of zero length
52+
53+
ty_utils_multiple_array_fields_simd_type = monomorphising SIMD type `{$ty}` with more than one array field
54+
55+
ty_utils_oversized_simd_type = monomorphising SIMD type `{$ty}` of length greater than {$max_lanes}
56+
57+
ty_utils_non_primative_simd_type = monomorphising SIMD type `{$ty}` with a non-primitive-scalar (integer/float/pointer) element type `{$e_ty}`

compiler/rustc_ty_utils/src/errors.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,29 @@ pub struct UnexpectedFnPtrAssociatedItem {
7474
#[primary_span]
7575
pub span: Span,
7676
}
77+
78+
#[derive(Diagnostic)]
79+
#[diag(ty_utils_zero_length_simd_type)]
80+
pub struct ZeroLengthSimdType<'tcx> {
81+
pub ty: Ty<'tcx>,
82+
}
83+
84+
#[derive(Diagnostic)]
85+
#[diag(ty_utils_multiple_array_fields_simd_type)]
86+
pub struct MultipleArrayFieldsSimdType<'tcx> {
87+
pub ty: Ty<'tcx>,
88+
}
89+
90+
#[derive(Diagnostic)]
91+
#[diag(ty_utils_oversized_simd_type)]
92+
pub struct OversizedSimdType<'tcx> {
93+
pub ty: Ty<'tcx>,
94+
pub max_lanes: u64,
95+
}
96+
97+
#[derive(Diagnostic)]
98+
#[diag(ty_utils_non_primative_simd_type)]
99+
pub struct NonPrimitiveSimdType<'tcx> {
100+
pub ty: Ty<'tcx>,
101+
pub e_ty: Ty<'tcx>,
102+
}

compiler/rustc_ty_utils/src/layout.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ use rustc_target::abi::*;
1717
use std::fmt::Debug;
1818
use std::iter;
1919

20+
use crate::errors::{
21+
MultipleArrayFieldsSimdType, NonPrimitiveSimdType, OversizedSimdType, ZeroLengthSimdType,
22+
};
2023
use crate::layout_sanity_check::sanity_check_layout;
2124

2225
pub fn provide(providers: &mut ty::query::Providers) {
@@ -294,6 +297,8 @@ fn layout_of_uncached<'tcx>(
294297
return Err(LayoutError::Unknown(ty));
295298
}
296299

300+
let fields = &def.non_enum_variant().fields;
301+
297302
// Supported SIMD vectors are homogeneous ADTs with at least one field:
298303
//
299304
// * #[repr(simd)] struct S(T, T, T, T);
@@ -304,18 +309,22 @@ fn layout_of_uncached<'tcx>(
304309

305310
// SIMD vectors with zero fields are not supported.
306311
// (should be caught by typeck)
307-
if def.non_enum_variant().fields.is_empty() {
308-
tcx.sess.fatal(&format!("monomorphising SIMD type `{}` of zero length", ty));
312+
if fields.is_empty() {
313+
tcx.sess.emit_fatal(ZeroLengthSimdType { ty })
309314
}
310315

311316
// Type of the first ADT field:
312-
let f0_ty = def.non_enum_variant().fields[FieldIdx::from_u32(0)].ty(tcx, substs);
317+
let f0_ty = fields[FieldIdx::from_u32(0)].ty(tcx, substs);
313318

314319
// Heterogeneous SIMD vectors are not supported:
315320
// (should be caught by typeck)
316-
for fi in &def.non_enum_variant().fields {
321+
for fi in fields {
317322
if fi.ty(tcx, substs) != f0_ty {
318-
tcx.sess.fatal(&format!("monomorphising heterogeneous SIMD type `{}`", ty));
323+
tcx.sess.delay_span_bug(
324+
DUMMY_SP,
325+
"#[repr(simd)] was applied to an ADT with hetrogeneous field type",
326+
);
327+
return Err(LayoutError::Unknown(ty));
319328
}
320329
}
321330

@@ -330,12 +339,9 @@ fn layout_of_uncached<'tcx>(
330339
// First ADT field is an array:
331340

332341
// SIMD vectors with multiple array fields are not supported:
333-
// (should be caught by typeck)
342+
// Can't be caught by typeck with a generic simd type.
334343
if def.non_enum_variant().fields.len() != 1 {
335-
tcx.sess.fatal(&format!(
336-
"monomorphising SIMD type `{}` with more than one array field",
337-
ty
338-
));
344+
tcx.sess.emit_fatal(MultipleArrayFieldsSimdType { ty });
339345
}
340346

341347
// Extract the number of elements from the layout of the array field:
@@ -355,24 +361,17 @@ fn layout_of_uncached<'tcx>(
355361
//
356362
// Can't be caught in typeck if the array length is generic.
357363
if e_len == 0 {
358-
tcx.sess.fatal(&format!("monomorphising SIMD type `{}` of zero length", ty));
364+
tcx.sess.emit_fatal(ZeroLengthSimdType { ty });
359365
} else if e_len > MAX_SIMD_LANES {
360-
tcx.sess.fatal(&format!(
361-
"monomorphising SIMD type `{}` of length greater than {}",
362-
ty, MAX_SIMD_LANES,
363-
));
366+
tcx.sess.emit_fatal(OversizedSimdType { ty, max_lanes: MAX_SIMD_LANES });
364367
}
365368

366369
// Compute the ABI of the element type:
367370
let e_ly = cx.layout_of(e_ty)?;
368371
let Abi::Scalar(e_abi) = e_ly.abi else {
369372
// This error isn't caught in typeck, e.g., if
370373
// the element type of the vector is generic.
371-
tcx.sess.fatal(&format!(
372-
"monomorphising SIMD type `{}` with a non-primitive-scalar \
373-
(integer/float/pointer) element type `{}`",
374-
ty, e_ty
375-
))
374+
tcx.sess.emit_fatal(NonPrimitiveSimdType {ty, e_ty });
376375
};
377376

378377
// Compute the size and alignment of the vector:

compiler/rustc_ty_utils/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#![feature(never_type)]
1111
#![feature(box_patterns)]
1212
#![recursion_limit = "256"]
13+
#![deny(rustc::untranslatable_diagnostic)]
14+
#![deny(rustc::diagnostic_outside_of_impl)]
1315

1416
#[macro_use]
1517
extern crate rustc_middle;

0 commit comments

Comments
 (0)