authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-17 16:39:44+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-19 01:38:19+03:00
log4aaff75c8191b9cff40d3423f44bdb831a88d89b
treedb731a48944773e52472cbbfe239e9ab01d8a3ed
parent6582896ee00cd6b05b2f0a1d845b4a77d6bf7c25

Sema: resolve tuple default values before hashing

Closes #12488

3 files changed, 43 insertions(+), 0 deletions(-)

src/Sema.zig+29
...@@ -6622,6 +6622,11 @@ fn instantiateGenericCall(...@@ -6622,6 +6622,11 @@ fn instantiateGenericCall(
6622 }6622 }
66236623
6624 const arg_ty = sema.typeOf(uncasted_args[i]);6624 const arg_ty = sema.typeOf(uncasted_args[i]);
6625 if (is_comptime or is_anytype) {
6626 // Tuple default values are a part of the type and need to be
6627 // resolved to hash the type.
6628 try sema.resolveTupleLazyValues(block, call_src, arg_ty);
6629 }
66256630
6626 if (is_comptime) {6631 if (is_comptime) {
6627 const arg_val = sema.analyzeGenericCallArgVal(block, .unneeded, uncasted_args[i]) catch |err| switch (err) {6632 const arg_val = sema.analyzeGenericCallArgVal(block, .unneeded, uncasted_args[i]) catch |err| switch (err) {
...@@ -6997,6 +7002,16 @@ fn instantiateGenericCall(...@@ -6997,6 +7002,16 @@ fn instantiateGenericCall(
6997 return result;7002 return result;
6998}7003}
69997004
7005fn resolveTupleLazyValues(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!void {
7006 if (!ty.isTuple()) return;
7007 const tuple = ty.tupleFields();
7008 for (tuple.values) |field_val, i| {
7009 try sema.resolveTupleLazyValues(block, src, tuple.types[i]);
7010 if (field_val.tag() == .unreachable_value) continue;
7011 try sema.resolveLazyValue(block, src, field_val);
7012 }
7013}
7014
7000fn emitDbgInline(7015fn emitDbgInline(
7001 sema: *Sema,7016 sema: *Sema,
7002 block: *Block,7017 block: *Block,
...@@ -28606,6 +28621,20 @@ fn resolveLazyValue(...@@ -28606,6 +28621,20 @@ fn resolveLazyValue(
28606 const ty = val.castTag(.lazy_size).?.data;28621 const ty = val.castTag(.lazy_size).?.data;
28607 return sema.resolveTypeLayout(block, src, ty);28622 return sema.resolveTypeLayout(block, src, ty);
28608 },28623 },
28624 .comptime_field_ptr => {
28625 const field_ptr = val.castTag(.comptime_field_ptr).?.data;
28626 return sema.resolveLazyValue(block, src, field_ptr.field_val);
28627 },
28628 .@"union" => {
28629 const union_val = val.castTag(.@"union").?.data;
28630 return sema.resolveLazyValue(block, src, union_val.val);
28631 },
28632 .aggregate => {
28633 const aggregate = val.castTag(.aggregate).?.data;
28634 for (aggregate) |elem_val| {
28635 try sema.resolveLazyValue(block, src, elem_val);
28636 }
28637 },
28609 else => return,28638 else => return,
28610 }28639 }
28611}28640}
test/behavior.zig+1
...@@ -88,6 +88,7 @@ test {...@@ -88,6 +88,7 @@ test {
88 _ = @import("behavior/bugs/12033.zig");88 _ = @import("behavior/bugs/12033.zig");
89 _ = @import("behavior/bugs/12430.zig");89 _ = @import("behavior/bugs/12430.zig");
90 _ = @import("behavior/bugs/12486.zig");90 _ = @import("behavior/bugs/12486.zig");
91 _ = @import("behavior/bugs/12488.zig");
91 _ = @import("behavior/bugs/12551.zig");92 _ = @import("behavior/bugs/12551.zig");
92 _ = @import("behavior/bugs/12644.zig");93 _ = @import("behavior/bugs/12644.zig");
93 _ = @import("behavior/bugs/12680.zig");94 _ = @import("behavior/bugs/12680.zig");
test/behavior/bugs/12488.zig created+13
...@@ -0,0 +1,13 @@
1const expect = @import("std").testing.expect;
2
3const A = struct {
4 a: u32,
5};
6
7fn foo(comptime a: anytype) !void {
8 try expect(a[0][0] == @sizeOf(A));
9}
10
11test {
12 try foo(.{[_]usize{@sizeOf(A)}});
13}