Skip to content

Commit cbab2f0

Browse files
Rollup merge of rust-lang#151495 - enthropy7:fix-simd-zero-length-extern-static, r=JonathanBrouwer
Fix ICE when using zero-length SIMD type in extern static before my fix using a zero-length SIMD type in an extern static would cause an internal compiler error. now it properly shows a diagnostic error instead of panicking. it was because `LayoutError::InvalidSimd` wasn't handled in `check_static_inhabited` and fell through to a generic `delayed_bug`. i added handling for `InvalidSimd` in `check_static_inhabited` (similar to `SizeOverflow`): when a SIMD type has an invalid layout, we call `emit_err` with `Spanned` to emit a normal error instead of an ICE. compiler now emits a clear error `"the SIMD type Simd<u8, 0> has zero elements"` with the correct span on the type, matching expected compiler behavior. fixes rust-lang#151451
2 parents a2c53d6 + b970366 commit cbab2f0

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc_middle::ty::{
2424
TypeVisitable, TypeVisitableExt, fold_regions,
2525
};
2626
use rustc_session::lint::builtin::UNINHABITED_STATIC;
27+
use rustc_span::source_map::Spanned;
2728
use rustc_target::spec::{AbiMap, AbiMapping};
2829
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
2930
use rustc_trait_selection::error_reporting::traits::on_unimplemented::OnUnimplementedDirective;
@@ -192,6 +193,12 @@ fn check_static_inhabited(tcx: TyCtxt<'_>, def_id: LocalDefId) {
192193
tcx.dcx().emit_err(errors::TooLargeStatic { span });
193194
return;
194195
}
196+
// SIMD types with invalid layout (e.g., zero-length) should emit an error
197+
Err(e @ LayoutError::InvalidSimd { .. }) => {
198+
let ty_span = tcx.ty_span(def_id);
199+
tcx.dcx().emit_err(Spanned { span: ty_span, node: e.into_diagnostic() });
200+
return;
201+
}
195202
// Generic statics are rejected, but we still reach this case.
196203
Err(e) => {
197204
tcx.dcx().span_delayed_bug(span, format!("{e:?}"));
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(repr_simd)]
2+
3+
#[repr(simd)]
4+
struct Simd<T, const N: usize>([T; N]);
5+
6+
unsafe extern "C" {
7+
static VAR: Simd<u8, 0>;
8+
//~^ ERROR the SIMD type `Simd<u8, 0>` has zero elements
9+
static VAR2: Simd<u8, 1_000_000>;
10+
//~^ ERROR the SIMD type `Simd<u8, 1000000>` has more elements than the limit 32768
11+
}
12+
13+
fn main() {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: the SIMD type `Simd<u8, 0>` has zero elements
2+
--> $DIR/extern-static-zero-length.rs:7:17
3+
|
4+
LL | static VAR: Simd<u8, 0>;
5+
| ^^^^^^^^^^^
6+
7+
error: the SIMD type `Simd<u8, 1000000>` has more elements than the limit 32768
8+
--> $DIR/extern-static-zero-length.rs:9:18
9+
|
10+
LL | static VAR2: Simd<u8, 1_000_000>;
11+
| ^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+

0 commit comments

Comments
 (0)