Skip to content

Commit f482745

Browse files
authored
Rollup merge of #110095 - matthewjasper:ty-utils-diagnostics, r=compiler-errors
Migrate remainder of rustc_ty_utils to `SessionDiagnostic` This moves the remaining errors in `rust_ty_utils` to `SessionsDiagnostic`. r? ``@davidtwco``
2 parents 164d70d + c17a705 commit f482745

File tree

7 files changed

+87
-24
lines changed

7 files changed

+87
-24
lines changed

compiler/rustc_ty_utils/messages.ftl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,13 @@ ty_utils_control_flow_not_supported = control flow is not supported in generic c
4545
ty_utils_inline_asm_not_supported = assembly is not supported in generic constants
4646
4747
ty_utils_operation_not_supported = unsupported operation in generic constants
48+
49+
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,36 @@ pub enum GenericConstantTooComplexSub {
6767
#[label(ty_utils_operation_not_supported)]
6868
OperationNotSupported(#[primary_span] Span),
6969
}
70+
71+
#[derive(Diagnostic)]
72+
#[diag(ty_utils_unexpected_fnptr_associated_item)]
73+
pub struct UnexpectedFnPtrAssociatedItem {
74+
#[primary_span]
75+
pub span: Span,
76+
}
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/instance.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use rustc_span::sym;
88
use rustc_trait_selection::traits;
99
use traits::{translate_substs, Reveal};
1010

11+
use crate::errors::UnexpectedFnPtrAssociatedItem;
12+
1113
fn resolve_instance<'tcx>(
1214
tcx: TyCtxt<'tcx>,
1315
key: ty::ParamEnvAnd<'tcx, (DefId, SubstsRef<'tcx>)>,
@@ -282,10 +284,9 @@ fn resolve_associated_item<'tcx>(
282284
substs: rcvr_substs,
283285
})
284286
} else {
285-
tcx.sess.span_fatal(
286-
tcx.def_span(trait_item_id),
287-
"`FnPtrAddr` trait with unexpected assoc item",
288-
)
287+
tcx.sess.emit_fatal(UnexpectedFnPtrAssociatedItem {
288+
span: tcx.def_span(trait_item_id),
289+
})
289290
}
290291
} else {
291292
None

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;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![feature(repr_simd)]
2+
3+
#[repr(simd)]
4+
struct I64F64(i64, f64);
5+
//~^ ERROR SIMD vector should be homogeneous
6+
7+
static X: I64F64 = I64F64(1, 2.0);
8+
9+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0076]: SIMD vector should be homogeneous
2+
--> $DIR/monomorphize-heterogeneous.rs:4:1
3+
|
4+
LL | struct I64F64(i64, f64);
5+
| ^^^^^^^^^^^^^ SIMD elements must have the same type
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0076`.

0 commit comments

Comments
 (0)