Rollup merge of #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/rust#151451
This commit is contained in:
Jonathan Brouwer 2026-01-22 20:42:11 +01:00 committed by GitHub
commit cbab2f0237
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 34 additions and 0 deletions

View file

@ -24,6 +24,7 @@ use rustc_middle::ty::{
TypeVisitable, TypeVisitableExt, fold_regions,
};
use rustc_session::lint::builtin::UNINHABITED_STATIC;
use rustc_span::source_map::Spanned;
use rustc_target::spec::{AbiMap, AbiMapping};
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
use rustc_trait_selection::error_reporting::traits::on_unimplemented::OnUnimplementedDirective;
@ -192,6 +193,12 @@ fn check_static_inhabited(tcx: TyCtxt<'_>, def_id: LocalDefId) {
tcx.dcx().emit_err(errors::TooLargeStatic { span });
return;
}
// SIMD types with invalid layout (e.g., zero-length) should emit an error
Err(e @ LayoutError::InvalidSimd { .. }) => {
let ty_span = tcx.ty_span(def_id);
tcx.dcx().emit_err(Spanned { span: ty_span, node: e.into_diagnostic() });
return;
}
// Generic statics are rejected, but we still reach this case.
Err(e) => {
tcx.dcx().span_delayed_bug(span, format!("{e:?}"));

View file

@ -0,0 +1,13 @@
#![feature(repr_simd)]
#[repr(simd)]
struct Simd<T, const N: usize>([T; N]);
unsafe extern "C" {
static VAR: Simd<u8, 0>;
//~^ ERROR the SIMD type `Simd<u8, 0>` has zero elements
static VAR2: Simd<u8, 1_000_000>;
//~^ ERROR the SIMD type `Simd<u8, 1000000>` has more elements than the limit 32768
}
fn main() {}

View file

@ -0,0 +1,14 @@
error: the SIMD type `Simd<u8, 0>` has zero elements
--> $DIR/extern-static-zero-length.rs:7:17
|
LL | static VAR: Simd<u8, 0>;
| ^^^^^^^^^^^
error: the SIMD type `Simd<u8, 1000000>` has more elements than the limit 32768
--> $DIR/extern-static-zero-length.rs:9:18
|
LL | static VAR2: Simd<u8, 1_000_000>;
| ^^^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors