Skip to content

Commit e9481bc

Browse files
committed
Sema: fix illegal multi level pointer coercions
Removes an incorrect check that allowed pointers to mutable pointers to coerce to any other pointer to a mutable pointer.
1 parent 2e6f7d3 commit e9481bc

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/Sema.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30179,7 +30179,7 @@ fn coerceInMemoryAllowedPtrs(
3017930179
src_src,
3018030180
null,
3018130181
);
30182-
if (child != .ok and !dest_is_mut) allow: {
30182+
if (child != .ok) allow: {
3018330183
// As a special case, we also allow coercing `*[n:s]T` to `*[n]T`, akin to dropping the sentinel from a slice.
3018430184
// `*[n:s]T` cannot coerce in memory to `*[n]T` since they have different sizes.
3018530185
if (src_child.zigTypeTag(zcu) == .array and dest_child.zigTypeTag(zcu) == .array and
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
export fn entry1() void {
2+
const p: **u32 = undefined;
3+
const q: **i32 = p;
4+
_ = q;
5+
}
6+
7+
export fn entry2() void {
8+
const p: [*]*u32 = undefined;
9+
const q: [*]*i32 = p;
10+
_ = q;
11+
}
12+
13+
export fn entry3() void {
14+
const p: []*u32 = undefined;
15+
const q: []*i32 = p;
16+
_ = q;
17+
}
18+
19+
export fn entry4() void {
20+
const p: [*c]*u32 = undefined;
21+
const q: [*c]*i32 = p;
22+
_ = q;
23+
}
24+
25+
// error
26+
//
27+
// :3:22: error: expected type '**i32', found '**u32'
28+
// :3:22: note: pointer type child '*u32' cannot cast into pointer type child '*i32'
29+
// :3:22: note: pointer type child 'u32' cannot cast into pointer type child 'i32'
30+
// :3:22: note: signed 32-bit int cannot represent all possible unsigned 32-bit values
31+
// :9:24: error: expected type '[*]*i32', found '[*]*u32'
32+
// :9:24: note: pointer type child '*u32' cannot cast into pointer type child '*i32'
33+
// :9:24: note: pointer type child 'u32' cannot cast into pointer type child 'i32'
34+
// :9:24: note: signed 32-bit int cannot represent all possible unsigned 32-bit values
35+
// :15:23: error: expected type '[]*i32', found '[]*u32'
36+
// :15:23: note: pointer type child '*u32' cannot cast into pointer type child '*i32'
37+
// :15:23: note: pointer type child 'u32' cannot cast into pointer type child 'i32'
38+
// :15:23: note: signed 32-bit int cannot represent all possible unsigned 32-bit values
39+
// :21:25: error: expected type '[*c]*i32', found '[*c]*u32'
40+
// :21:25: note: pointer type child '*u32' cannot cast into pointer type child '*i32'
41+
// :21:25: note: pointer type child 'u32' cannot cast into pointer type child 'i32'
42+
// :21:25: note: signed 32-bit int cannot represent all possible unsigned 32-bit values

0 commit comments

Comments
 (0)