Fix ICE when using zero-length SIMD type in extern static

Previously, using a zero-length SIMD type in an extern static would
cause an internal compiler error. Now it properly emits a diagnostic
error instead of panicking.
This commit is contained in:
enthropy7 2026-01-22 18:14:19 +03:00
parent 838db25382
commit b97036628f
No known key found for this signature in database
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