Skip to content

Commit af8bf6f

Browse files
eddybFirestar99
authored andcommitted
constant: avoid div-by-0 due to ZST arrays/slices.
1 parent 4df52fa commit af8bf6f

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

crates/rustc_codegen_spirv/src/codegen_cx/constant.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,10 @@ impl<'tcx> CodegenCx<'tcx> {
573573
SpirvType::Array { count, .. } => {
574574
u64::try_from(self.builder.lookup_const_scalar(count).unwrap()).unwrap()
575575
}
576-
SpirvType::RuntimeArray { .. } => {
577-
(alloc.inner().size() - offset).bytes() / stride.bytes()
578-
}
576+
SpirvType::RuntimeArray { .. } => (alloc.inner().size() - offset)
577+
.bytes()
578+
.checked_div(stride.bytes())
579+
.unwrap_or(0),
579580
_ => unreachable!(),
580581
};
581582

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: unsupported unsized `[struct () { }]` constant
2+
|
3+
= note: used by unnamed global (%68)
4+
= note: used by unnamed global (%69)
5+
note: used from within `size_of_val_unsized::main`
6+
--> <$DIR/size_of_val_unsized.rs>:5:12
7+
|
8+
LL | *out = core::mem::size_of_val(
9+
| ____________^
10+
LL | | const {
11+
LL | | struct S<T: ?Sized>(T);
12+
LL | | &S([]) as &S<[()]>
13+
LL | | },
14+
LL | | );
15+
| |_____^
16+
note: called by `main`
17+
--> <$DIR/size_of_val_unsized.rs>:4:8
18+
|
19+
LL | pub fn main(out: &mut usize) {
20+
| ^^^^
21+
22+
error: aborting due to 1 previous error
23+

0 commit comments

Comments
 (0)