diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs index c37611ab2ee7..f2b06e1cde71 100644 --- a/compiler/rustc_hir_analysis/src/check/check.rs +++ b/compiler/rustc_hir_analysis/src/check/check.rs @@ -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:?}")); diff --git a/tests/ui/simd/extern-static-zero-length.rs b/tests/ui/simd/extern-static-zero-length.rs new file mode 100644 index 000000000000..5ca7b63fc523 --- /dev/null +++ b/tests/ui/simd/extern-static-zero-length.rs @@ -0,0 +1,13 @@ +#![feature(repr_simd)] + +#[repr(simd)] +struct Simd([T; N]); + +unsafe extern "C" { + static VAR: Simd; + //~^ ERROR the SIMD type `Simd` has zero elements + static VAR2: Simd; + //~^ ERROR the SIMD type `Simd` has more elements than the limit 32768 +} + +fn main() {} diff --git a/tests/ui/simd/extern-static-zero-length.stderr b/tests/ui/simd/extern-static-zero-length.stderr new file mode 100644 index 000000000000..49c05f27d8c5 --- /dev/null +++ b/tests/ui/simd/extern-static-zero-length.stderr @@ -0,0 +1,14 @@ +error: the SIMD type `Simd` has zero elements + --> $DIR/extern-static-zero-length.rs:7:17 + | +LL | static VAR: Simd; + | ^^^^^^^^^^^ + +error: the SIMD type `Simd` has more elements than the limit 32768 + --> $DIR/extern-static-zero-length.rs:9:18 + | +LL | static VAR2: Simd; + | ^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors +