authorgravatar for 51252236+xdBronch@users.noreply.github.comxdBronch <51252236+xdBronch@users.noreply.github.com> 2024-11-18 16:35:45-05:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-11-20 02:09:50+02:00
log5f3a70ed5f5fe21eaaff0a06e95287a13fd2272d
treecbc77e124fb8a6ca599622a09fcb97e7f2f8c3d3
parent865ef245182a14f10e1e79371c80f14e9542c925

Fix peer type resolution with allowzero pointers


2 files changed, 52 insertions(+), 13 deletions(-)

src/Sema.zig+1
......@@ -35076,6 +35076,7 @@ fn resolvePeerTypesInner(
3507635076
3507735077 ptr_info.flags.is_const = ptr_info.flags.is_const or peer_info.flags.is_const;
3507835078 ptr_info.flags.is_volatile = ptr_info.flags.is_volatile or peer_info.flags.is_volatile;
35079 ptr_info.flags.is_allowzero = ptr_info.flags.is_allowzero or peer_info.flags.is_allowzero;
3507935080
3508035081 const peer_sentinel: InternPool.Index = switch (peer_info.flags.size) {
3508135082 .One => switch (ip.indexToKey(peer_info.child)) {
test/behavior/cast.zig+51-13
......@@ -2200,39 +2200,77 @@ test "peer type resolution: pointer attributes are combined correctly" {
22002200 var buf_a align(4) = "foo".*;
22012201 var buf_b align(4) = "bar".*;
22022202 var buf_c align(4) = "baz".*;
2203 var buf_d align(4) = "qux".*;
22032204
22042205 const a: [*:0]align(4) const u8 = &buf_a;
22052206 const b: *align(2) volatile [3:0]u8 = &buf_b;
22062207 const c: [*:0]align(4) u8 = &buf_c;
2207
2208 comptime assert(@TypeOf(a, b, c) == [*:0]align(2) const volatile u8);
2209 comptime assert(@TypeOf(a, c, b) == [*:0]align(2) const volatile u8);
2210 comptime assert(@TypeOf(b, a, c) == [*:0]align(2) const volatile u8);
2211 comptime assert(@TypeOf(b, c, a) == [*:0]align(2) const volatile u8);
2212 comptime assert(@TypeOf(c, a, b) == [*:0]align(2) const volatile u8);
2213 comptime assert(@TypeOf(c, b, a) == [*:0]align(2) const volatile u8);
2208 const d: [*:0]allowzero align(4) u8 = &buf_d;
2209
2210 comptime assert(@TypeOf(a, b, c, d) == [*:0]allowzero align(2) const volatile u8);
2211 comptime assert(@TypeOf(a, b, d, c) == [*:0]allowzero align(2) const volatile u8);
2212 comptime assert(@TypeOf(a, c, b, d) == [*:0]allowzero align(2) const volatile u8);
2213 comptime assert(@TypeOf(a, c, d, b) == [*:0]allowzero align(2) const volatile u8);
2214 comptime assert(@TypeOf(a, d, b, c) == [*:0]allowzero align(2) const volatile u8);
2215 comptime assert(@TypeOf(a, d, c, b) == [*:0]allowzero align(2) const volatile u8);
2216
2217 comptime assert(@TypeOf(b, a, c, d) == [*:0]allowzero align(2) const volatile u8);
2218 comptime assert(@TypeOf(b, a, d, c) == [*:0]allowzero align(2) const volatile u8);
2219 comptime assert(@TypeOf(b, c, a, d) == [*:0]allowzero align(2) const volatile u8);
2220 comptime assert(@TypeOf(b, c, d, a) == [*:0]allowzero align(2) const volatile u8);
2221 comptime assert(@TypeOf(b, d, c, a) == [*:0]allowzero align(2) const volatile u8);
2222 comptime assert(@TypeOf(b, d, a, c) == [*:0]allowzero align(2) const volatile u8);
2223
2224 comptime assert(@TypeOf(c, a, b, d) == [*:0]allowzero align(2) const volatile u8);
2225 comptime assert(@TypeOf(c, a, d, b) == [*:0]allowzero align(2) const volatile u8);
2226 comptime assert(@TypeOf(c, b, a, d) == [*:0]allowzero align(2) const volatile u8);
2227 comptime assert(@TypeOf(c, b, d, a) == [*:0]allowzero align(2) const volatile u8);
2228 comptime assert(@TypeOf(c, d, b, a) == [*:0]allowzero align(2) const volatile u8);
2229 comptime assert(@TypeOf(c, d, a, b) == [*:0]allowzero align(2) const volatile u8);
2230
2231 comptime assert(@TypeOf(d, a, b, c) == [*:0]allowzero align(2) const volatile u8);
2232 comptime assert(@TypeOf(d, a, c, b) == [*:0]allowzero align(2) const volatile u8);
2233 comptime assert(@TypeOf(d, b, a, c) == [*:0]allowzero align(2) const volatile u8);
2234 comptime assert(@TypeOf(d, b, c, a) == [*:0]allowzero align(2) const volatile u8);
2235 comptime assert(@TypeOf(d, c, b, a) == [*:0]allowzero align(2) const volatile u8);
2236 comptime assert(@TypeOf(d, c, a, b) == [*:0]allowzero align(2) const volatile u8);
22142237
22152238 var x: u8 = 0;
22162239 _ = &x;
22172240 const r1 = switch (x) {
22182241 0 => a,
22192242 1 => b,
2220 else => c,
2243 2 => c,
2244 else => d,
22212245 };
22222246 const r2 = switch (x) {
22232247 0 => b,
22242248 1 => a,
2225 else => c,
2249 2 => c,
2250 else => d,
22262251 };
22272252 const r3 = switch (x) {
22282253 0 => c,
22292254 1 => a,
2230 else => b,
2255 2 => b,
2256 else => d,
2257 };
2258 const r4 = switch (x) {
2259 0 => d,
2260 1 => a,
2261 2 => b,
2262 else => c,
22312263 };
22322264
2233 try expectEqualSlices(u8, std.mem.span(@volatileCast(r1)), "foo");
2234 try expectEqualSlices(u8, std.mem.span(@volatileCast(r2)), "bar");
2235 try expectEqualSlices(u8, std.mem.span(@volatileCast(r3)), "baz");
2265 const NonAllowZero = comptime blk: {
2266 var ti = @typeInfo(@TypeOf(r1, r2, r3, r4));
2267 ti.pointer.is_allowzero = false;
2268 break :blk @Type(ti);
2269 };
2270 try expectEqualSlices(u8, std.mem.span(@volatileCast(@as(NonAllowZero, @ptrCast(r1)))), "foo");
2271 try expectEqualSlices(u8, std.mem.span(@volatileCast(@as(NonAllowZero, @ptrCast(r2)))), "bar");
2272 try expectEqualSlices(u8, std.mem.span(@volatileCast(@as(NonAllowZero, @ptrCast(r3)))), "baz");
2273 try expectEqualSlices(u8, std.mem.span(@volatileCast(@as(NonAllowZero, @ptrCast(r4)))), "qux");
22362274}
22372275
22382276test "peer type resolution: arrays of compatible types" {