Skip to content

Commit e0d2e20

Browse files
committed
compiletests: cover invalid runtime-array const bitcasts
Add a UI compile-fail test for casting a 3-byte constant allocation to `*const RuntimeArray<u16>`. This exercises the existing trailing-bytes diagnostic for unsupported unsized constants and guards the review concern that this shape must fail instead of silently lowering.
1 parent 1bc950c commit e0d2e20

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// build-fail
2+
3+
use spirv_std::{RuntimeArray, spirv};
4+
5+
// Reinterpreting a 3-byte backing allocation as `RuntimeArray<u16>` leaves
6+
// one trailing byte that cannot form a whole element.
7+
const BYTES: &[u8; 3] = &[1, 2, 3];
8+
const BAD_RUNTIME_ARRAY_PTR: *const RuntimeArray<u16> =
9+
BYTES as *const [u8; 3] as *const RuntimeArray<u16>;
10+
11+
#[inline(never)]
12+
fn use_runtime_array_ptr(ptr: *const RuntimeArray<u16>) -> bool {
13+
!ptr.is_null()
14+
}
15+
16+
#[spirv(fragment)]
17+
pub fn main(output: &mut u32) {
18+
*output = u32::from(use_runtime_array_ptr(BAD_RUNTIME_ARRAY_PTR));
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: unsupported unsized `[u16]` constant with 1 trailing bytes
2+
|
3+
= note: used by unnamed global (%87)
4+
note: used from within `runtime_array_invalid_const_bitcast::main`
5+
--> <$DIR/runtime-array-invalid-const-bitcast.rs>:18:25
6+
|
7+
LL | *output = u32::from(use_runtime_array_ptr(BAD_RUNTIME_ARRAY_PTR));
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9+
note: called by `main`
10+
--> <$DIR/runtime-array-invalid-const-bitcast.rs>:17:8
11+
|
12+
LL | pub fn main(output: &mut u32) {
13+
| ^^^^
14+
15+
error: aborting due to 1 previous error
16+

0 commit comments

Comments
 (0)