Skip to content

Commit 21b3cf9

Browse files
InKryptionVexu
authored andcommitted
Add error for bad cast from *T to *[n]T
Casting `*T` to `*[1]T` should still work, but every other length will now be a compiler error instead of a potential OOB access.
1 parent fafb1a5 commit 21b3cf9

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export fn entry0(single: *u32) void {
2+
_ = @as(*const [0]u32, single);
3+
}
4+
export fn entry1(single: *u32) void {
5+
_ = @as(*const [1]u32, single);
6+
}
7+
export fn entry2(single: *u32) void {
8+
_ = @as(*const [2]u32, single);
9+
}
10+
11+
// error
12+
// backend=stage2
13+
// target=native
14+
//
15+
// :2:28: error: expected type '*const [0]u32', found '*u32'
16+
// :2:28: note: pointer type child 'u32' cannot cast into pointer type child '[0]u32'
17+
// :8:28: error: expected type '*const [2]u32', found '*u32'
18+
// :8:28: note: pointer type child 'u32' cannot cast into pointer type child '[2]u32'

0 commit comments

Comments
 (0)