WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 237f4ec

Browse files
committed
location assignment: checked multiply and expect with messages
1 parent 90486d0 commit 237f4ec

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

crates/rustc_codegen_spirv/src/spirv_type.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,19 @@ impl SpirvType<'_> {
293293
}
294294
locations
295295
}
296-
Self::Matrix { element, count } => cx.lookup_type(element).location_size(cx)? * count,
296+
Self::Matrix { element, count } => cx
297+
.lookup_type(element)
298+
.location_size(cx)?
299+
.checked_mul(count)
300+
.expect("overflow"),
297301
Self::Array { element, count } => {
298-
let count = cx.builder.lookup_const_scalar(count).unwrap();
299-
let count: u32 = count.try_into().unwrap();
300-
cx.lookup_type(element).location_size(cx)? * count
302+
let element = cx.lookup_type(element).location_size(cx)?;
303+
let count = cx
304+
.builder
305+
.lookup_const_scalar(count)
306+
.and_then(|c| u32::try_from(c).ok())
307+
.expect("SpirvType::Array.count to be a u32 constant");
308+
element.checked_mul(count).expect("overflow")
301309
}
302310
_ => return None,
303311
};
@@ -313,11 +321,19 @@ impl SpirvType<'_> {
313321
Self::Integer(width, _) | Self::Float(width) => Size::from_bits(width),
314322
Self::Adt { size, .. } => size?,
315323
Self::Vector { size, .. } => size,
316-
Self::Matrix { element, count } => cx.lookup_type(element).sizeof(cx)? * count as u64,
324+
Self::Matrix { element, count } => cx
325+
.lookup_type(element)
326+
.sizeof(cx)?
327+
.checked_mul(count as u64, cx)
328+
.expect("overflow"),
317329
Self::Array { element, count } => {
318-
let count = cx.builder.lookup_const_scalar(count).unwrap();
319-
let count = count.try_into().unwrap();
320-
cx.lookup_type(element).sizeof(cx)? * count
330+
let element = cx.lookup_type(element).sizeof(cx)?;
331+
let count = cx
332+
.builder
333+
.lookup_const_scalar(count)
334+
.and_then(|c| u64::try_from(c).ok())
335+
.expect("SpirvType::Array.count to be a u32 constant");
336+
element.checked_mul(count, cx).expect("overflow")
321337
}
322338
Self::Pointer { .. } => cx.tcx.data_layout.pointer_size,
323339
Self::Image { .. }

0 commit comments

Comments
 (0)