authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-05-27 16:20:49-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-05-27 16:20:49-04:00
log4994ac18e4b540ceaac450db552749c7ac559396
treeb2ca9e9824f28dee0cb6ec1eb1cebc94d3335672
parentb3672e073819fdb5c2d21c0b50127c1bfcdc9b3f
parent989c0f55e8bbc790dcdd742ddea3d18c1194eab5
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11733 from Vexu/stage2

more misc stage2 fixes

7 files changed, 141 insertions(+), 4 deletions(-)

src/AstGen.zig+1-1
......@@ -9915,7 +9915,7 @@ const GenZir = struct {
99159915 .inferred_ptr => |ptr| {
99169916 gz.rl_ty_inst = .none;
99179917 gz.rl_ptr = ptr;
9918 gz.break_result_loc = .{ .block_ptr = gz };
9918 gz.break_result_loc = parent_rl;
99199919 },
99209920
99219921 .block_ptr => |parent_block_scope| {
src/Sema.zig+67-3
......@@ -2881,6 +2881,28 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
28812881
28822882 if (var_is_mut) {
28832883 try sema.validateVarType(block, ty_src, final_elem_ty, false);
2884
2885 // The value might have been bitcasted into a comptime only
2886 // pointer type such as `*@Type(.EnumLiteral)` so we must now
2887 // update all the stores to not give backends invalid AIR.
2888
2889 var air_tags = sema.air_instructions.items(.tag);
2890 var air_data = sema.air_instructions.items(.data);
2891 var peer_inst_index: usize = 0;
2892 var i = ptr_inst;
2893 while (i < air_tags.len and peer_inst_index < peer_inst_list.len) : (i += 1) {
2894 if (air_tags[i] != .store) continue;
2895 if (air_data[i].bin_op.rhs == peer_inst_list[peer_inst_index]) {
2896 peer_inst_index += 1;
2897 _ = (try sema.resolveMaybeUndefVal(block, .unneeded, air_data[i].bin_op.rhs)) orelse continue;
2898 const coerced_val = try sema.coerce(block, final_elem_ty, air_data[i].bin_op.rhs, .unneeded);
2899 air_tags = sema.air_instructions.items(.tag);
2900 air_data = sema.air_instructions.items(.data);
2901
2902 air_data[i].bin_op.lhs = ptr;
2903 air_data[i].bin_op.rhs = coerced_val;
2904 }
2905 }
28842906 } else ct: {
28852907 // Detect if the value is comptime known. In such case, the
28862908 // last 3 AIR instructions of the block will look like this:
......@@ -5814,8 +5836,10 @@ fn zirArrayType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
58145836 defer tracy.end();
58155837
58165838 const bin_inst = sema.code.instructions.items(.data)[inst].bin;
5817 const len = try sema.resolveInt(block, .unneeded, bin_inst.lhs, Type.usize);
5818 const elem_type = try sema.resolveType(block, .unneeded, bin_inst.rhs);
5839 const len_src = sema.src; // TODO better source location
5840 const elem_src = sema.src; // TODO better source location
5841 const len = try sema.resolveInt(block, len_src, bin_inst.lhs, Type.usize);
5842 const elem_type = try sema.resolveType(block, elem_src, bin_inst.rhs);
58195843 const array_ty = try Type.array(sema.arena, len, null, elem_type, sema.mod);
58205844
58215845 return sema.addType(array_ty);
......@@ -7307,9 +7331,11 @@ fn zirElemVal(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
73077331 defer tracy.end();
73087332
73097333 const bin_inst = sema.code.instructions.items(.data)[inst].bin;
7334 const src = sema.src; // TODO better source location
7335 const elem_index_src = sema.src; // TODO better source location
73107336 const array = try sema.resolveInst(bin_inst.lhs);
73117337 const elem_index = try sema.resolveInst(bin_inst.rhs);
7312 return sema.elemVal(block, sema.src, array, elem_index, sema.src);
7338 return sema.elemVal(block, src, array, elem_index, elem_index_src);
73137339}
73147340
73157341fn zirElemValNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -18591,6 +18617,25 @@ fn elemValArray(
1859118617 }
1859218618 }
1859318619
18620 const valid_rt = try sema.validateRunTimeType(block, elem_index_src, elem_ty, false);
18621 if (!valid_rt) {
18622 const msg = msg: {
18623 const msg = try sema.errMsg(
18624 block,
18625 elem_index_src,
18626 "values of type '{}' must be comptime known, but index value is runtime known",
18627 .{array_ty.fmt(sema.mod)},
18628 );
18629 errdefer msg.destroy(sema.gpa);
18630
18631 const src_decl = sema.mod.declPtr(block.src_decl);
18632 try sema.explainWhyTypeIsComptime(block, elem_index_src, msg, array_src.toSrcLoc(src_decl), array_ty);
18633
18634 break :msg msg;
18635 };
18636 return sema.failWithOwnedErrorMsg(block, msg);
18637 }
18638
1859418639 const runtime_src = if (maybe_undef_array_val != null) elem_index_src else array_src;
1859518640 try sema.requireRuntimeBlock(block, runtime_src);
1859618641 if (block.wantSafety()) {
......@@ -18646,6 +18691,25 @@ fn elemPtrArray(
1864618691 }
1864718692 }
1864818693
18694 const valid_rt = try sema.validateRunTimeType(block, elem_index_src, array_ty.elemType2(), false);
18695 if (!valid_rt) {
18696 const msg = msg: {
18697 const msg = try sema.errMsg(
18698 block,
18699 elem_index_src,
18700 "values of type '{}' must be comptime known, but index value is runtime known",
18701 .{array_ty.fmt(sema.mod)},
18702 );
18703 errdefer msg.destroy(sema.gpa);
18704
18705 const src_decl = sema.mod.declPtr(block.src_decl);
18706 try sema.explainWhyTypeIsComptime(block, elem_index_src, msg, array_ptr_src.toSrcLoc(src_decl), array_ty);
18707
18708 break :msg msg;
18709 };
18710 return sema.failWithOwnedErrorMsg(block, msg);
18711 }
18712
1864918713 const runtime_src = if (maybe_undef_array_ptr_val != null) elem_index_src else array_ptr_src;
1865018714 try sema.requireRuntimeBlock(block, runtime_src);
1865118715 if (block.wantSafety()) {
test/behavior.zig+1
......@@ -80,6 +80,7 @@ test {
8080 _ = @import("behavior/bugs/11159.zig");
8181 _ = @import("behavior/bugs/11162.zig");
8282 _ = @import("behavior/bugs/11165.zig");
83 _ = @import("behavior/bugs/11179.zig");
8384 _ = @import("behavior/bugs/11181.zig");
8485 _ = @import("behavior/bugs/11213.zig");
8586 _ = @import("behavior/byteswap.zig");
test/behavior/basic.zig+12
......@@ -933,3 +933,15 @@ test "try in labeled block doesn't cast to wrong type" {
933933 };
934934 _ = s;
935935}
936
937test "comptime int in switch in catch is casted to correct inferred type" {
938 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
939 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
940
941 var a: error{ A, B }!u64 = 0;
942 var b = a catch |err| switch (err) {
943 error.A => 0,
944 else => unreachable,
945 };
946 _ = b;
947}
test/behavior/bugs/11179.zig created+18
......@@ -0,0 +1,18 @@
1const std = @import("std");
2const Type = std.builtin.Type;
3
4test "Tuple" {
5 const fields_list = fields(@TypeOf(.{}));
6 if (fields_list.len != 0)
7 @compileError("Argument count mismatch");
8}
9
10pub fn fields(comptime T: type) switch (@typeInfo(T)) {
11 .Struct => []const Type.StructField,
12 else => unreachable,
13} {
14 return switch (@typeInfo(T)) {
15 .Struct => |info| info.fields,
16 else => unreachable,
17 };
18}
test/cases/compile_errors/invalid_array_elem_ty.zig created+11
......@@ -0,0 +1,11 @@
1pub fn S() type {
2 return struct {};
3}
4pub export fn entry() void {
5 _ = [0]S;
6}
7
8// error
9// backend=stage2,llvm
10//
11// :4:1: error: expected type, found fn() type
test/cases/compile_errors/runtime_indexing_comptime_array.zig created+31
......@@ -0,0 +1,31 @@
1fn foo() void {}
2fn bar() void {}
3
4pub export fn entry1() void {
5 const TestFn = fn () void;
6 const test_fns = [_]TestFn{ foo, bar };
7 for (test_fns) |testFn| {
8 testFn();
9 }
10}
11pub export fn entry2() void {
12 const TestFn = fn () void;
13 const test_fns = [_]TestFn{ foo, bar };
14 var i: usize = 0;
15 _ = test_fns[i];
16}
17pub export fn entry3() void {
18 const TestFn = fn () void;
19 const test_fns = [_]TestFn{ foo, bar };
20 var i: usize = 0;
21 _ = &test_fns[i];
22}
23// error
24// backend=stage2,llvm
25//
26// :6:33: error: values of type '[2]fn() callconv(.C) void' must be comptime known, but index value is runtime known
27// :6:33: note: use '*const fn() callconv(.C) void' for a function pointer type
28// :13:33: error: values of type '[2]fn() callconv(.C) void' must be comptime known, but index value is runtime known
29// :13:33: note: use '*const fn() callconv(.C) void' for a function pointer type
30// :19:33: error: values of type '[2]fn() callconv(.C) void' must be comptime known, but index value is runtime known
31// :19:33: note: use '*const fn() callconv(.C) void' for a function pointer type