Skip to content

Commit 1d679e9

Browse files
committed
consts: diagnose invalid runtime-array const bitcasts
Keep the existing runtime-array reification path for const pointer bitcasts, but report a specific zombie reason when the backing allocation size is not a multiple of the runtime-array element size instead of silently falling through to the generic const_bitcast error.
1 parent e63081b commit 1d679e9

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

crates/rustc_codegen_spirv/src/codegen_cx/constant.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,13 @@ impl ConstCodegenMethods for CodegenCx<'_> {
253253
let alloc_id = prov.alloc_id();
254254
let (base_addr, _base_addr_space) = match self.tcx.global_alloc(alloc_id) {
255255
GlobalAlloc::Memory(alloc) => {
256-
let _pointee = match self.lookup_type(ty) {
257-
SpirvType::Pointer { pointee } => pointee,
256+
match self.lookup_type(ty) {
257+
SpirvType::Pointer { .. } => {}
258258
other => self.tcx.dcx().fatal(format!(
259259
"GlobalAlloc::Memory type not implemented: {}",
260260
other.debug(ty, self)
261261
)),
262-
};
262+
}
263263
let value = self.static_addr_of(alloc, None);
264264
(value, AddressSpace::ZERO)
265265
}
@@ -277,13 +277,13 @@ impl ConstCodegenMethods for CodegenCx<'_> {
277277
}),
278278
)))
279279
.unwrap_memory();
280-
let _pointee = match self.lookup_type(ty) {
281-
SpirvType::Pointer { pointee } => pointee,
280+
match self.lookup_type(ty) {
281+
SpirvType::Pointer { .. } => {}
282282
other => self.tcx.dcx().fatal(format!(
283283
"GlobalAlloc::VTable type not implemented: {}",
284284
other.debug(ty, self)
285285
)),
286-
};
286+
}
287287
let value = self.static_addr_of(alloc, None);
288288
(value, AddressSpace::ZERO)
289289
}
@@ -347,6 +347,7 @@ impl<'tcx> CodegenCx<'tcx> {
347347
self.builder.lookup_const_by_id(pointee)
348348
&& let SpirvType::Pointer { pointee } = self.lookup_type(ty)
349349
{
350+
let mut runtime_array_bitcast_error = None;
350351
let init = self.try_read_from_const_alloc(alloc, pointee).or_else(|| {
351352
match self.lookup_type(pointee) {
352353
// Reify unsized constants through a sized backing array,
@@ -359,6 +360,9 @@ impl<'tcx> CodegenCx<'tcx> {
359360

360361
let alloc_size = alloc.inner().size();
361362
if alloc_size.bytes() % elem_size.bytes() != 0 {
363+
runtime_array_bitcast_error = Some(
364+
"const runtime array backing allocation size is not a multiple of the element size",
365+
);
362366
return None;
363367
}
364368

@@ -378,6 +382,12 @@ impl<'tcx> CodegenCx<'tcx> {
378382
},
379383
);
380384
}
385+
386+
if let Some(reason) = runtime_array_bitcast_error {
387+
let result = val.def_cx(self).with_type(ty);
388+
self.zombie_no_span(result.def_cx(self), reason);
389+
return result;
390+
}
381391
}
382392

383393
if val.ty == ty {

0 commit comments

Comments
 (0)