authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-29 10:30:10+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-30 00:17:49+03:00
logf43ea43ac920d3fbd629175e4e7fbe4309c6eab5
treed0edfe385cedffe26a15c325421c5029a1081977
parent4fc2acdaa4f2b649b17ddf958d2608abc4787a4e

stage2: fix hashing of struct values

Closes #12279

2 files changed, 23 insertions(+), 15 deletions(-)

src/value.zig+3-15
...@@ -2292,25 +2292,13 @@ pub const Value = extern union {...@@ -2292,25 +2292,13 @@ pub const Value = extern union {
2292 }2292 }
2293 },2293 },
2294 .Struct => {2294 .Struct => {
2295 if (ty.isTupleOrAnonStruct()) {
2296 const fields = ty.tupleFields();
2297 for (fields.values) |field_val, i| {
2298 field_val.hash(fields.types[i], hasher, mod);
2299 }
2300 return;
2301 }
2302 const fields = ty.structFields().values();
2303 if (fields.len == 0) return;
2304 switch (val.tag()) {2295 switch (val.tag()) {
2305 .empty_struct_value => {2296 .empty_struct_value => {},
2306 for (fields) |field| {
2307 field.default_val.hash(field.ty, hasher, mod);
2308 }
2309 },
2310 .aggregate => {2297 .aggregate => {
2311 const field_values = val.castTag(.aggregate).?.data;2298 const field_values = val.castTag(.aggregate).?.data;
2312 for (field_values) |field_val, i| {2299 for (field_values) |field_val, i| {
2313 field_val.hash(fields[i].ty, hasher, mod);2300 const field_ty = ty.structFieldType(i);
2301 field_val.hash(field_ty, hasher, mod);
2314 }2302 }
2315 },2303 },
2316 else => unreachable,2304 else => unreachable,
test/behavior/tuple.zig+20
...@@ -255,3 +255,23 @@ test "initializing anon struct with mixed comptime-runtime fields" {...@@ -255,3 +255,23 @@ test "initializing anon struct with mixed comptime-runtime fields" {
255 var a: T = .{ .foo = -1234, .bar = x + 1 };255 var a: T = .{ .foo = -1234, .bar = x + 1 };
256 _ = a;256 _ = a;
257}257}
258
259test "tuple in tuple passed to generic function" {
260 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
261 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
262 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
263 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
264
265 const S = struct {
266 fn pair(x: f32, y: f32) std.meta.Tuple(&.{ f32, f32 }) {
267 return .{ x, y };
268 }
269
270 fn foo(x: anytype) !void {
271 try expect(x[0][0] == 1.5);
272 try expect(x[0][1] == 2.5);
273 }
274 };
275 const x = comptime S.pair(1.5, 2.5);
276 try S.foo(.{x});
277}