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(...@@ -35076,6 +35076,7 @@ fn resolvePeerTypesInner(
3507635076
35077 ptr_info.flags.is_const = ptr_info.flags.is_const or peer_info.flags.is_const;35077 ptr_info.flags.is_const = ptr_info.flags.is_const or peer_info.flags.is_const;
35078 ptr_info.flags.is_volatile = ptr_info.flags.is_volatile or peer_info.flags.is_volatile;35078 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
35080 const peer_sentinel: InternPool.Index = switch (peer_info.flags.size) {35081 const peer_sentinel: InternPool.Index = switch (peer_info.flags.size) {
35081 .One => switch (ip.indexToKey(peer_info.child)) {35082 .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" {...@@ -2200,39 +2200,77 @@ test "peer type resolution: pointer attributes are combined correctly" {
2200 var buf_a align(4) = "foo".*;2200 var buf_a align(4) = "foo".*;
2201 var buf_b align(4) = "bar".*;2201 var buf_b align(4) = "bar".*;
2202 var buf_c align(4) = "baz".*;2202 var buf_c align(4) = "baz".*;
2203 var buf_d align(4) = "qux".*;
22032204
2204 const a: [*:0]align(4) const u8 = &buf_a;2205 const a: [*:0]align(4) const u8 = &buf_a;
2205 const b: *align(2) volatile [3:0]u8 = &buf_b;2206 const b: *align(2) volatile [3:0]u8 = &buf_b;
2206 const c: [*:0]align(4) u8 = &buf_c;2207 const c: [*:0]align(4) u8 = &buf_c;
22072208 const d: [*:0]allowzero align(4) u8 = &buf_d;
2208 comptime assert(@TypeOf(a, b, c) == [*:0]align(2) const volatile u8);2209
2209 comptime assert(@TypeOf(a, c, b) == [*:0]align(2) const volatile u8);2210 comptime assert(@TypeOf(a, b, c, d) == [*:0]allowzero align(2) const volatile u8);
2210 comptime assert(@TypeOf(b, a, c) == [*:0]align(2) const volatile u8);2211 comptime assert(@TypeOf(a, b, d, c) == [*:0]allowzero align(2) const volatile u8);
2211 comptime assert(@TypeOf(b, c, a) == [*:0]align(2) const volatile u8);2212 comptime assert(@TypeOf(a, c, b, d) == [*:0]allowzero align(2) const volatile u8);
2212 comptime assert(@TypeOf(c, a, b) == [*:0]align(2) const volatile u8);2213 comptime assert(@TypeOf(a, c, d, b) == [*:0]allowzero align(2) const volatile u8);
2213 comptime assert(@TypeOf(c, b, a) == [*:0]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
2215 var x: u8 = 0;2238 var x: u8 = 0;
2216 _ = &x;2239 _ = &x;
2217 const r1 = switch (x) {2240 const r1 = switch (x) {
2218 0 => a,2241 0 => a,
2219 1 => b,2242 1 => b,
2220 else => c,2243 2 => c,
2244 else => d,
2221 };2245 };
2222 const r2 = switch (x) {2246 const r2 = switch (x) {
2223 0 => b,2247 0 => b,
2224 1 => a,2248 1 => a,
2225 else => c,2249 2 => c,
2250 else => d,
2226 };2251 };
2227 const r3 = switch (x) {2252 const r3 = switch (x) {
2228 0 => c,2253 0 => c,
2229 1 => a,2254 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,
2231 };2263 };
22322264
2233 try expectEqualSlices(u8, std.mem.span(@volatileCast(r1)), "foo");2265 const NonAllowZero = comptime blk: {
2234 try expectEqualSlices(u8, std.mem.span(@volatileCast(r2)), "bar");2266 var ti = @typeInfo(@TypeOf(r1, r2, r3, r4));
2235 try expectEqualSlices(u8, std.mem.span(@volatileCast(r3)), "baz");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");
2236}2274}
22372275
2238test "peer type resolution: arrays of compatible types" {2276test "peer type resolution: arrays of compatible types" {