Skip to content

Don't ICE on ZST union #780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions crates/rustc_codegen_spirv/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,22 +500,36 @@ fn dig_scalar_pointee<'tcx>(
}

fn trans_aggregate<'tcx>(cx: &CodegenCx<'tcx>, span: Span, ty: TyAndLayout<'tcx>) -> Word {
fn create_zst<'tcx>(cx: &CodegenCx<'tcx>, span: Span, ty: TyAndLayout<'tcx>) -> Word {
SpirvType::Adt {
def_id: def_id_for_spirv_type_adt(ty),
size: Some(Size::ZERO),
align: Align::from_bytes(0).unwrap(),
field_types: Vec::new(),
field_offsets: Vec::new(),
field_names: None,
}
.def_with_name(cx, span, TyLayoutNameKey::from(ty))
}
match ty.fields {
FieldsShape::Primitive => span_bug!(
span,
"trans_aggregate called for FieldsShape::Primitive layout {:#?}",
ty
),
FieldsShape::Union(_) => {
assert_ne!(ty.size.bytes(), 0, "{:#?}", ty);
assert!(!ty.is_unsized(), "{:#?}", ty);
let byte = SpirvType::Integer(8, false).def(span, cx);
let count = cx.constant_u32(span, ty.size.bytes() as u32);
SpirvType::Array {
element: byte,
count,
if ty.size.bytes() == 0 {
create_zst(cx, span, ty)
} else {
let byte = SpirvType::Integer(8, false).def(span, cx);
let count = cx.constant_u32(span, ty.size.bytes() as u32);
SpirvType::Array {
element: byte,
count,
}
.def(span, cx)
}
.def(span, cx)
}
FieldsShape::Array { stride, count } => {
let element_type = trans_type_impl(cx, span, ty.field(cx, 0), false);
Expand All @@ -529,15 +543,7 @@ fn trans_aggregate<'tcx>(cx: &CodegenCx<'tcx>, span: Span, ty: TyAndLayout<'tcx>
.def(span, cx)
} else if count == 0 {
// spir-v doesn't support zero-sized arrays
SpirvType::Adt {
def_id: def_id_for_spirv_type_adt(ty),
size: Some(Size::ZERO),
align: Align::from_bytes(0).unwrap(),
field_types: Vec::new(),
field_offsets: Vec::new(),
field_names: None,
}
.def_with_name(cx, span, TyLayoutNameKey::from(ty))
create_zst(cx, span, ty)
} else {
let count_const = cx.constant_u32(span, count as u32);
let element_spv = cx.lookup_type(element_type);
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/lang/issue-415.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Test that zero sized unions don't ICE (even if unions are generally not supported yet)
// build-pass

use spirv_std as _;

union U {
a: (),
}

#[spirv(fragment)]
pub fn main() {
let _u = U { a: () };
}