authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-26 14:47:43-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-02-26 14:47:43-05:00
logb3aa1ab693ac160a07c44f07c7b90577039860a1
tree6c5f4bf623b276963cec98ebbf713d2a752e9e01
parentdb82c1b9820449f2d1e6ef54dd32ec3ffd3c583f
parent315d4e84425ddff888de8d464f657981e4c45da7
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #10994 from Vexu/typeof-scope

stage2: `@TypeOf` improvements

7 files changed, 158 insertions(+), 52 deletions(-)

src/AstGen.zig+41-15
......@@ -2159,6 +2159,7 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
21592159 .negate,
21602160 .negate_wrap,
21612161 .typeof,
2162 .typeof_builtin,
21622163 .xor,
21632164 .optional_type,
21642165 .optional_payload_safe,
......@@ -6875,29 +6876,54 @@ fn typeOf(
68756876 scope: *Scope,
68766877 rl: ResultLoc,
68776878 node: Ast.Node.Index,
6878 params: []const Ast.Node.Index,
6879 args: []const Ast.Node.Index,
68796880) InnerError!Zir.Inst.Ref {
6880 if (params.len < 1) {
6881 if (args.len < 1) {
68816882 return gz.astgen.failNode(node, "expected at least 1 argument, found 0", .{});
68826883 }
6883 if (params.len == 1) {
6884 const expr_result = try reachableExpr(gz, scope, .none, params[0], node);
6885 const result = try gz.addUnNode(.typeof, expr_result, node);
6886 return rvalue(gz, rl, result, node);
6884 const gpa = gz.astgen.gpa;
6885 if (args.len == 1) {
6886 const typeof_inst = try gz.makeBlockInst(.typeof_builtin, node);
6887
6888 var typeof_scope = gz.makeSubBlock(scope);
6889 typeof_scope.force_comptime = false;
6890 defer typeof_scope.unstack();
6891
6892 const ty_expr = try reachableExpr(&typeof_scope, &typeof_scope.base, .none, args[0], node);
6893 if (!gz.refIsNoReturn(ty_expr)) {
6894 _ = try typeof_scope.addBreak(.break_inline, typeof_inst, ty_expr);
6895 }
6896 try typeof_scope.setBlockBody(typeof_inst);
6897
6898 // typeof_scope unstacked now, can add new instructions to gz
6899 try gz.instructions.append(gpa, typeof_inst);
6900 return rvalue(gz, rl, indexToRef(typeof_inst), node);
68876901 }
6902 const payload_size: u32 = std.meta.fields(Zir.Inst.TypeOfPeer).len;
6903 const payload_index = try reserveExtra(gz.astgen, payload_size + args.len);
6904 var args_index = payload_index + payload_size;
6905
6906 const typeof_inst = try gz.addExtendedMultiOpPayloadIndex(.typeof_peer, payload_index, args.len);
68886907
6889 const payload_index = try addExtra(gz.astgen, Zir.Inst.NodeMultiOp{
6908 var typeof_scope = gz.makeSubBlock(scope);
6909 typeof_scope.force_comptime = false;
6910
6911 for (args) |arg, i| {
6912 const param_ref = try reachableExpr(&typeof_scope, &typeof_scope.base, .none, arg, node);
6913 gz.astgen.extra.items[args_index + i] = @enumToInt(param_ref);
6914 }
6915 _ = try typeof_scope.addBreak(.break_inline, refToIndex(typeof_inst).?, .void_value);
6916
6917 const body = typeof_scope.instructionsSlice();
6918 gz.astgen.setExtra(payload_index, Zir.Inst.TypeOfPeer{
6919 .body_len = @intCast(u32, body.len),
6920 .body_index = @intCast(u32, gz.astgen.extra.items.len),
68906921 .src_node = gz.nodeIndexToRelative(node),
68916922 });
6892 var extra_index = try reserveExtra(gz.astgen, params.len);
6893 for (params) |param| {
6894 const param_ref = try reachableExpr(gz, scope, .none, param, node);
6895 gz.astgen.extra.items[extra_index] = @enumToInt(param_ref);
6896 extra_index += 1;
6897 }
6923 try gz.astgen.extra.appendSlice(gpa, body);
6924 typeof_scope.unstack();
68986925
6899 const result = try gz.addExtendedMultiOpPayloadIndex(.typeof_peer, payload_index, params.len);
6900 return rvalue(gz, rl, result, node);
6926 return rvalue(gz, rl, typeof_inst, node);
69016927}
69026928
69036929fn builtinCall(
src/Sema.zig+46-3
......@@ -124,6 +124,7 @@ pub const Block = struct {
124124 runtime_index: u32 = 0,
125125
126126 is_comptime: bool,
127 is_typeof: bool = false,
127128
128129 /// when null, it is determined by build mode, changed by @setRuntimeSafety
129130 want_safety: ?bool = null,
......@@ -181,6 +182,7 @@ pub const Block = struct {
181182 .label = null,
182183 .inlining = parent.inlining,
183184 .is_comptime = parent.is_comptime,
185 .is_typeof = parent.is_typeof,
184186 .runtime_cond = parent.runtime_cond,
185187 .runtime_loop = parent.runtime_loop,
186188 .runtime_index = parent.runtime_index,
......@@ -682,6 +684,7 @@ fn analyzeBodyInner(
682684 .size_of => try sema.zirSizeOf(block, inst),
683685 .bit_size_of => try sema.zirBitSizeOf(block, inst),
684686 .typeof => try sema.zirTypeof(block, inst),
687 .typeof_builtin => try sema.zirTypeofBuiltin(block, inst),
685688 .log2_int_type => try sema.zirLog2IntType(block, inst),
686689 .typeof_log2_int_type => try sema.zirTypeofLog2IntType(block, inst),
687690 .xor => try sema.zirBitwise(block, inst, .xor),
......@@ -10574,6 +10577,29 @@ fn zirTypeof(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
1057410577 return sema.addType(operand_ty);
1057510578}
1057610579
10580fn zirTypeofBuiltin(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
10581 const pl_node = sema.code.instructions.items(.data)[inst].pl_node;
10582 const extra = sema.code.extraData(Zir.Inst.Block, pl_node.payload_index);
10583 const body = sema.code.extra[extra.end..][0..extra.data.body_len];
10584
10585 var child_block: Block = .{
10586 .parent = block,
10587 .sema = sema,
10588 .src_decl = block.src_decl,
10589 .namespace = block.namespace,
10590 .wip_capture_scope = block.wip_capture_scope,
10591 .instructions = .{},
10592 .inlining = block.inlining,
10593 .is_comptime = false,
10594 .is_typeof = true,
10595 };
10596 defer child_block.instructions.deinit(sema.gpa);
10597
10598 const operand = try sema.resolveBody(&child_block, body, inst);
10599 const operand_ty = sema.typeOf(operand);
10600 return sema.addType(operand_ty);
10601}
10602
1057710603fn zirTypeofLog2IntType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1057810604 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
1057910605 const src = inst_data.src();
......@@ -10624,8 +10650,24 @@ fn zirTypeofPeer(
1062410650 const tracy = trace(@src());
1062510651 defer tracy.end();
1062610652
10627 const extra = sema.code.extraData(Zir.Inst.NodeMultiOp, extended.operand);
10653 const extra = sema.code.extraData(Zir.Inst.TypeOfPeer, extended.operand);
1062810654 const src: LazySrcLoc = .{ .node_offset = extra.data.src_node };
10655 const body = sema.code.extra[extra.data.body_index..][0..extra.data.body_len];
10656
10657 var child_block: Block = .{
10658 .parent = block,
10659 .sema = sema,
10660 .src_decl = block.src_decl,
10661 .namespace = block.namespace,
10662 .wip_capture_scope = block.wip_capture_scope,
10663 .instructions = .{},
10664 .inlining = block.inlining,
10665 .is_comptime = false,
10666 .is_typeof = true,
10667 };
10668 defer child_block.instructions.deinit(sema.gpa);
10669 _ = try sema.analyzeBody(&child_block, body);
10670
1062910671 const args = sema.code.refSlice(extra.end, extended.small);
1063010672
1063110673 const inst_list = try sema.gpa.alloc(Air.Inst.Ref, args.len);
......@@ -13385,7 +13427,7 @@ fn zirBuiltinExtern(
1338513427}
1338613428
1338713429fn requireFunctionBlock(sema: *Sema, block: *Block, src: LazySrcLoc) !void {
13388 if (sema.func == null) {
13430 if (sema.func == null and !block.is_typeof) {
1338913431 return sema.fail(block, src, "instruction illegal outside function body", .{});
1339013432 }
1339113433}
......@@ -14152,7 +14194,8 @@ fn fieldCallBind(
1415214194 if (first_param_tag == .var_args_param or
1415314195 first_param_tag == .generic_poison or (
1415414196 first_param_type.zigTypeTag() == .Pointer and
14155 first_param_type.ptrSize() == .One and
14197 (first_param_type.ptrSize() == .One or
14198 first_param_type.ptrSize() == .C) and
1415614199 first_param_type.childType().eql(concrete_ty)))
1415714200 {
1415814201 // zig fmt: on
src/Zir.zig+11
......@@ -547,6 +547,9 @@ pub const Inst = struct {
547547 /// Returns the type of a value.
548548 /// Uses the `un_node` field.
549549 typeof,
550 /// Implements `@TypeOf` for one operand.
551 /// Uses the `pl_node` field.
552 typeof_builtin,
550553 /// Given a value, look at the type of it, which must be an integer type.
551554 /// Returns the integer type for the RHS of a shift operation.
552555 /// Uses the `un_node` field.
......@@ -1067,6 +1070,7 @@ pub const Inst = struct {
10671070 .negate,
10681071 .negate_wrap,
10691072 .typeof,
1073 .typeof_builtin,
10701074 .xor,
10711075 .optional_type,
10721076 .optional_payload_safe,
......@@ -1429,6 +1433,7 @@ pub const Inst = struct {
14291433 .ptr_cast = .pl_node,
14301434 .truncate = .pl_node,
14311435 .align_cast = .pl_node,
1436 .typeof_builtin = .pl_node,
14321437
14331438 .has_decl = .pl_node,
14341439 .has_field = .pl_node,
......@@ -2391,6 +2396,12 @@ pub const Inst = struct {
23912396 };
23922397 };
23932398
2399 pub const TypeOfPeer = struct {
2400 src_node: i32,
2401 body_len: u32,
2402 body_index: u32,
2403 };
2404
23942405 pub const BuiltinCall = struct {
23952406 options: Ref,
23962407 callee: Ref,
src/print_zir.zig+16-3
......@@ -374,6 +374,7 @@ const Writer = struct {
374374 .validate_array_init,
375375 .validate_array_init_comptime,
376376 .c_import,
377 .typeof_builtin,
377378 => try self.writePlNodeBlock(stream, inst),
378379
379380 .condbr,
......@@ -458,9 +459,8 @@ const Writer = struct {
458459 .variable => try self.writeVarExtended(stream, extended),
459460 .alloc => try self.writeAllocExtended(stream, extended),
460461
461 .compile_log,
462 .typeof_peer,
463 => try self.writeNodeMultiOp(stream, extended),
462 .compile_log => try self.writeNodeMultiOp(stream, extended),
463 .typeof_peer => try self.writeTypeofPeer(stream, extended),
464464
465465 .add_with_overflow,
466466 .sub_with_overflow,
......@@ -1966,6 +1966,19 @@ const Writer = struct {
19661966 try self.writeSrc(stream, src);
19671967 }
19681968
1969 fn writeTypeofPeer(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
1970 const extra = self.code.extraData(Zir.Inst.TypeOfPeer, extended.operand);
1971 const body = self.code.extra[extra.data.body_index..][0..extra.data.body_len];
1972 try self.writeBracedBody(stream, body);
1973 try stream.writeAll(",[");
1974 const args = self.code.refSlice(extra.end, extended.small);
1975 for (args) |arg, i| {
1976 if (i != 0) try stream.writeAll(", ");
1977 try self.writeInstRef(stream, arg);
1978 }
1979 try stream.writeAll("])");
1980 }
1981
19691982 fn writeBoolBr(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
19701983 const inst_data = self.code.instructions.items(.data)[inst].bool_br;
19711984 const extra = self.code.extraData(Zir.Inst.Block, inst_data.payload_index);
test/behavior.zig+2-2
......@@ -38,6 +38,7 @@ test {
3838 _ = @import("behavior/bugs/3112.zig");
3939 _ = @import("behavior/bugs/3367.zig");
4040 _ = @import("behavior/bugs/3586.zig");
41 _ = @import("behavior/bugs/4328.zig");
4142 _ = @import("behavior/bugs/4560.zig");
4243 _ = @import("behavior/bugs/4769_a.zig");
4344 _ = @import("behavior/bugs/4769_b.zig");
......@@ -102,6 +103,7 @@ test {
102103 builtin.zig_backend != .stage2_wasm)
103104 {
104105 // Tests that pass for stage1, llvm backend, C backend
106 _ = @import("behavior/bugs/5474.zig");
105107 _ = @import("behavior/bugs/9584.zig");
106108 _ = @import("behavior/bugs/10970.zig");
107109 _ = @import("behavior/cast_int.zig");
......@@ -149,10 +151,8 @@ test {
149151 _ = @import("behavior/bugs/1851.zig");
150152 _ = @import("behavior/bugs/3384.zig");
151153 _ = @import("behavior/bugs/3779.zig");
152 _ = @import("behavior/bugs/4328.zig");
153154 _ = @import("behavior/bugs/5398.zig");
154155 _ = @import("behavior/bugs/5413.zig");
155 _ = @import("behavior/bugs/5474.zig");
156156 _ = @import("behavior/bugs/5487.zig");
157157 _ = @import("behavior/bugs/6456.zig");
158158 _ = @import("behavior/bugs/6781.zig");
test/behavior/bugs/4328.zig+16-7
......@@ -1,4 +1,5 @@
1const expectEqual = @import("std").testing.expectEqual;
1const expect = @import("std").testing.expect;
2const builtin = @import("builtin");
23
34const FILE = extern struct {
45 dummy_field: u8,
......@@ -16,18 +17,20 @@ const S = extern struct {
1617};
1718
1819test "Extern function calls in @TypeOf" {
20 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
21
1922 const Test = struct {
2023 fn test_fn_1(a: anytype, b: anytype) @TypeOf(printf("%d %s\n", a, b)) {
2124 return 0;
2225 }
2326
24 fn test_fn_2(a: anytype) @TypeOf((S{ .state = 0 }).s_do_thing(a)) {
27 fn test_fn_2(s: anytype, a: anytype) @TypeOf(s.s_do_thing(a)) {
2528 return 1;
2629 }
2730
2831 fn doTheTest() !void {
29 try expectEqual(c_int, @TypeOf(test_fn_1(0, 42)));
30 try expectEqual(c_short, @TypeOf(test_fn_2(0)));
32 try expect(@TypeOf(test_fn_1(0, 42)) == c_int);
33 try expect(@TypeOf(test_fn_2(&S{ .state = 1 }, 0)) == c_short);
3134 }
3235 };
3336
......@@ -36,13 +39,15 @@ test "Extern function calls in @TypeOf" {
3639}
3740
3841test "Peer resolution of extern function calls in @TypeOf" {
42 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
43
3944 const Test = struct {
4045 fn test_fn() @TypeOf(ftell(null), fputs(null, null)) {
4146 return 0;
4247 }
4348
4449 fn doTheTest() !void {
45 try expectEqual(c_long, @TypeOf(test_fn()));
50 try expect(@TypeOf(test_fn()) == c_long);
4651 }
4752 };
4853
......@@ -51,6 +56,10 @@ test "Peer resolution of extern function calls in @TypeOf" {
5156}
5257
5358test "Extern function calls, dereferences and field access in @TypeOf" {
59 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
60 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
61 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
62
5463 const Test = struct {
5564 fn test_fn_1(a: c_long) @TypeOf(fopen("test", "r").*) {
5665 _ = a;
......@@ -63,8 +72,8 @@ test "Extern function calls, dereferences and field access in @TypeOf" {
6372 }
6473
6574 fn doTheTest() !void {
66 try expectEqual(FILE, @TypeOf(test_fn_1(0)));
67 try expectEqual(u8, @TypeOf(test_fn_2(0)));
75 try expect(@TypeOf(test_fn_1(0)) == FILE);
76 try expect(@TypeOf(test_fn_2(0)) == u8);
6877 }
6978 };
7079
test/behavior/bugs/5474.zig+26-22
......@@ -1,4 +1,5 @@
11const std = @import("std");
2const builtin = @import("builtin");
23
34// baseline (control) struct with array of scalar
45const Box0 = struct {
......@@ -25,33 +26,36 @@ const Box2 = struct {
2526 };
2627};
2728
28fn doTest() !void {
29 // var
30 {
31 var box0: Box0 = .{ .items = undefined };
32 try std.testing.expect(@typeInfo(@TypeOf(box0.items[0..])).Pointer.is_const == false);
29fn mutable() !void {
30 var box0: Box0 = .{ .items = undefined };
31 try std.testing.expect(@typeInfo(@TypeOf(box0.items[0..])).Pointer.is_const == false);
3332
34 var box1: Box1 = .{ .items = undefined };
35 try std.testing.expect(@typeInfo(@TypeOf(box1.items[0..])).Pointer.is_const == false);
33 var box1: Box1 = .{ .items = undefined };
34 try std.testing.expect(@typeInfo(@TypeOf(box1.items[0..])).Pointer.is_const == false);
3635
37 var box2: Box2 = .{ .items = undefined };
38 try std.testing.expect(@typeInfo(@TypeOf(box2.items[0..])).Pointer.is_const == false);
39 }
36 var box2: Box2 = .{ .items = undefined };
37 try std.testing.expect(@typeInfo(@TypeOf(box2.items[0..])).Pointer.is_const == false);
38}
39
40fn constant() !void {
41 const box0: Box0 = .{ .items = undefined };
42 try std.testing.expect(@typeInfo(@TypeOf(box0.items[0..])).Pointer.is_const == true);
4043
41 // const
42 {
43 const box0: Box0 = .{ .items = undefined };
44 try std.testing.expect(@typeInfo(@TypeOf(box0.items[0..])).Pointer.is_const == true);
44 const box1: Box1 = .{ .items = undefined };
45 try std.testing.expect(@typeInfo(@TypeOf(box1.items[0..])).Pointer.is_const == true);
4546
46 const box1: Box1 = .{ .items = undefined };
47 try std.testing.expect(@typeInfo(@TypeOf(box1.items[0..])).Pointer.is_const == true);
47 const box2: Box2 = .{ .items = undefined };
48 try std.testing.expect(@typeInfo(@TypeOf(box2.items[0..])).Pointer.is_const == true);
49}
4850
49 const box2: Box2 = .{ .items = undefined };
50 try std.testing.expect(@typeInfo(@TypeOf(box2.items[0..])).Pointer.is_const == true);
51 }
51test "pointer-to-array constness for zero-size elements, var" {
52 try mutable();
53 comptime try mutable();
5254}
5355
54test "pointer-to-array constness for zero-size elements" {
55 try doTest();
56 comptime try doTest();
56test "pointer-to-array constness for zero-size elements, const" {
57 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
58
59 try constant();
60 comptime try constant();
5761}