Rollup merge of #148638 - chenyukang:yukang-fix-148634-repr-simd-enum-ice, r=Kivooeo,lcnr

Fix ICE for repr simd on non struct

Fixes rust-lang/rust#148634

The ICE happened because
https://github.com/rust-lang/rust/blob/995c11894fdabe1c630694254de756f82389c6cf/compiler/rustc_middle/src/ty/mod.rs#L1531

will always set `IS_SIMD` according to `get_all_attrs`, and since we already report error `attribute should be applied to a struct`, it's OK to bypass here.
This commit is contained in:
Stuart Cook
2025-11-14 19:57:06 +11:00
committed by GitHub
3 changed files with 51 additions and 1 deletions
+13 -1
View File
@@ -7,7 +7,7 @@
use rustc_middle::ty::{self, Article, FloatTy, IntTy, Ty, TyCtxt, TypeVisitableExt, UintTy};
use rustc_session::lint;
use rustc_span::def_id::LocalDefId;
use rustc_span::{Span, Symbol, sym};
use rustc_span::{ErrorGuaranteed, Span, Symbol, sym};
use rustc_target::asm::{
InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType, ModifierInfo,
};
@@ -27,6 +27,7 @@ enum NonAsmTypeReason<'tcx> {
InvalidElement(DefId, Ty<'tcx>),
NotSizedPtr(Ty<'tcx>),
EmptySIMDArray(Ty<'tcx>),
Tainted(ErrorGuaranteed),
}
impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
@@ -93,6 +94,14 @@ fn get_asm_ty(
}
}
ty::Adt(adt, args) if adt.repr().simd() => {
if !adt.is_struct() {
let guar = self.fcx.dcx().span_delayed_bug(
span,
format!("repr(simd) should only be used on structs, got {}", adt.descr()),
);
return Err(NonAsmTypeReason::Tainted(guar));
}
let fields = &adt.non_enum_variant().fields;
if fields.is_empty() {
return Err(NonAsmTypeReason::EmptySIMDArray(ty));
@@ -234,6 +243,9 @@ fn check_asm_operand_type(
let msg = format!("use of empty SIMD vector `{ty}`");
self.fcx.dcx().struct_span_err(expr.span, msg).emit();
}
NonAsmTypeReason::Tainted(_error_guard) => {
// An error has already been reported.
}
}
return None;
}
@@ -0,0 +1,16 @@
//@ needs-asm-support
#![feature(repr_simd)]
use std::arch::asm;
#[repr(simd)]
//~^ ERROR attribute should be applied to a struct
//~| ERROR unsupported representation for zero-variant enum
enum Es {}
fn main() {
unsafe {
let mut x: Es;
asm!("{}", out(reg) x);
}
}
@@ -0,0 +1,22 @@
error[E0517]: attribute should be applied to a struct
--> $DIR/invalid-repr-simd-on-enum-148634.rs:6:8
|
LL | #[repr(simd)]
| ^^^^
...
LL | enum Es {}
| ---------- not a struct
error[E0084]: unsupported representation for zero-variant enum
--> $DIR/invalid-repr-simd-on-enum-148634.rs:6:8
|
LL | #[repr(simd)]
| ^^^^
...
LL | enum Es {}
| ------- zero-variant enum
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0084, E0517.
For more information about an error, try `rustc --explain E0084`.