authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-06-01 02:19:40+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-06-01 13:01:39+03:00
log9431100736abe63c361506411a40d143b23091d9
treebdea60c2a28a88a9b49472ec7b31507d3fb22b87
parent3c4e7abfbff9fbdb967308659951ca091b346bac

Sema: apply previous changes to `validateUnionInit`


3 files changed, 65 insertions(+), 5 deletions(-)

lib/std/math/big/int.zig+2-3
......@@ -2063,9 +2063,8 @@ pub const Const = struct {
20632063 // This is the inverse of calcDivLimbsBufferLen
20642064 const available_len = (limbs.len / 3) - 2;
20652065
2066 // TODO https://github.com/ziglang/zig/issues/11439
2067 const biggest = comptime Const{
2068 .limbs = &([1]Limb{math.maxInt(Limb)} ** available_len),
2066 const biggest: Const = .{
2067 .limbs = &([1]Limb{comptime math.maxInt(Limb)} ** available_len),
20692068 .positive = false,
20702069 };
20712070 var buf: [biggest.sizeInBaseUpperBound(radix)]u8 = undefined;
src/Sema.zig+15-2
......@@ -3215,7 +3215,9 @@ fn validateUnionInit(
32153215 return sema.failWithOwnedErrorMsg(block, msg);
32163216 }
32173217
3218 if (is_comptime or block.is_comptime) {
3218 if ((is_comptime or block.is_comptime) and
3219 (try sema.resolveDefinedValue(block, init_src, union_ptr)) != null)
3220 {
32193221 // In this case, comptime machinery already did everything. No work to do here.
32203222 return;
32213223 }
......@@ -3261,7 +3263,18 @@ fn validateUnionInit(
32613263 if (store_inst == field_ptr_air_inst) break;
32623264 if (air_tags[store_inst] != .store) continue;
32633265 const bin_op = air_datas[store_inst].bin_op;
3264 if (bin_op.lhs != field_ptr_air_ref) continue;
3266 var lhs = bin_op.lhs;
3267 if (Air.refToIndex(lhs)) |lhs_index| {
3268 if (air_tags[lhs_index] == .bitcast) {
3269 lhs = air_datas[lhs_index].ty_op.operand;
3270 block_index -= 1;
3271 }
3272 }
3273 if (lhs != field_ptr_air_ref) continue;
3274 while (block_index > 0) : (block_index -= 1) {
3275 const block_inst = block.instructions.items[block_index - 1];
3276 if (air_tags[block_inst] != .dbg_stmt) break;
3277 }
32653278 if (block_index > 0 and
32663279 field_ptr_air_inst == block.instructions.items[block_index - 1])
32673280 {
test/behavior/basic.zig+48
......@@ -1005,3 +1005,51 @@ test "generic function uses return type of other generic function" {
10051005 };
10061006 try std.testing.expect(S.call(S.func, .{@as(u8, 1)}) == 1);
10071007}
1008
1009test "const alloc with comptime known initializer is made comptime known" {
1010 const S = struct {
1011 a: bool,
1012 b: [2]u8,
1013 };
1014 {
1015 const s: S = .{
1016 .a = false,
1017 .b = .{ 1, 2 },
1018 };
1019 if (s.a) @compileError("bad");
1020 }
1021 {
1022 const s: S = .{
1023 .a = false,
1024 .b = [2]u8{ 1, 2 },
1025 };
1026 if (s.a) @compileError("bad");
1027 }
1028 {
1029 const s: S = comptime .{
1030 .a = false,
1031 .b = .{ 1, 2 },
1032 };
1033 if (s.a) @compileError("bad");
1034 }
1035 {
1036 const Const = struct {
1037 limbs: []const usize,
1038 positive: bool,
1039 };
1040 const biggest: Const = .{
1041 .limbs = &([1]usize{comptime std.math.maxInt(usize)} ** 128),
1042 .positive = false,
1043 };
1044 if (biggest.positive) @compileError("bad");
1045 }
1046 {
1047 const U = union(enum) {
1048 a: usize,
1049 };
1050 const u: U = .{
1051 .a = comptime std.math.maxInt(usize),
1052 };
1053 if (u.a == 0) @compileError("bad");
1054 }
1055}