authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-08-08 22:51:08+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-08 18:28:39-07:00
logd769fd0102dee7ea24f957c3c96e2336d1c18839
tree789f5b8f67ced1bf2da811f9c7b961b02f00a133
parent3afc4dff302a85e2f13e0e43a2258d1b16dd4730

stage2: pass anon name strategy to reify


6 files changed, 78 insertions(+), 20 deletions(-)

src/AstGen.zig+24-2
...@@ -2454,7 +2454,6 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As...@@ -2454,7 +2454,6 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
2454 .trunc,2454 .trunc,
2455 .round,2455 .round,
2456 .tag_name,2456 .tag_name,
2457 .reify,
2458 .type_name,2457 .type_name,
2459 .frame_type,2458 .frame_type,
2460 .frame_size,2459 .frame_size,
...@@ -7553,7 +7552,6 @@ fn builtinCall(...@@ -7553,7 +7552,6 @@ fn builtinCall(
7553 .trunc => return simpleUnOp(gz, scope, rl, node, .none, params[0], .trunc),7552 .trunc => return simpleUnOp(gz, scope, rl, node, .none, params[0], .trunc),
7554 .round => return simpleUnOp(gz, scope, rl, node, .none, params[0], .round),7553 .round => return simpleUnOp(gz, scope, rl, node, .none, params[0], .round),
7555 .tag_name => return simpleUnOp(gz, scope, rl, node, .none, params[0], .tag_name),7554 .tag_name => return simpleUnOp(gz, scope, rl, node, .none, params[0], .tag_name),
7556 .Type => return simpleUnOp(gz, scope, rl, node, .{ .coerced_ty = .type_info_type }, params[0], .reify),
7557 .type_name => return simpleUnOp(gz, scope, rl, node, .none, params[0], .type_name),7555 .type_name => return simpleUnOp(gz, scope, rl, node, .none, params[0], .type_name),
7558 .Frame => return simpleUnOp(gz, scope, rl, node, .none, params[0], .frame_type),7556 .Frame => return simpleUnOp(gz, scope, rl, node, .none, params[0], .frame_type),
7559 .frame_size => return simpleUnOp(gz, scope, rl, node, .none, params[0], .frame_size),7557 .frame_size => return simpleUnOp(gz, scope, rl, node, .none, params[0], .frame_size),
...@@ -7568,6 +7566,30 @@ fn builtinCall(...@@ -7568,6 +7566,30 @@ fn builtinCall(
7568 .truncate => return typeCast(gz, scope, rl, node, params[0], params[1], .truncate),7566 .truncate => return typeCast(gz, scope, rl, node, params[0], params[1], .truncate),
7569 // zig fmt: on7567 // zig fmt: on
75707568
7569 .Type => {
7570 const operand = try expr(gz, scope, .{ .coerced_ty = .type_info_type }, params[0]);
7571
7572 const gpa = gz.astgen.gpa;
7573
7574 try gz.instructions.ensureUnusedCapacity(gpa, 1);
7575 try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1);
7576
7577 const payload_index = try gz.astgen.addExtra(Zir.Inst.UnNode{
7578 .node = gz.nodeIndexToRelative(node),
7579 .operand = operand,
7580 });
7581 const new_index = @intCast(Zir.Inst.Index, gz.astgen.instructions.len);
7582 gz.astgen.instructions.appendAssumeCapacity(.{
7583 .tag = .extended,
7584 .data = .{ .extended = .{
7585 .opcode = .reify,
7586 .small = @enumToInt(gz.anon_name_strategy),
7587 .operand = payload_index,
7588 } },
7589 });
7590 gz.instructions.appendAssumeCapacity(new_index);
7591 return indexToRef(new_index);
7592 },
7571 .panic => {7593 .panic => {
7572 try emitDbgNode(gz, node);7594 try emitDbgNode(gz, node);
7573 return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], if (gz.force_comptime) .panic_comptime else .panic);7595 return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], if (gz.force_comptime) .panic_comptime else .panic);
src/Autodoc.zig+1-1
...@@ -1220,7 +1220,6 @@ fn walkInstruction(...@@ -1220,7 +1220,6 @@ fn walkInstruction(
1220 .trunc,1220 .trunc,
1221 .round,1221 .round,
1222 .tag_name,1222 .tag_name,
1223 .reify,
1224 .type_name,1223 .type_name,
1225 .frame_type,1224 .frame_type,
1226 .frame_size,1225 .frame_size,
...@@ -2605,6 +2604,7 @@ fn walkInstruction(...@@ -2605,6 +2604,7 @@ fn walkInstruction(
2605 },2604 },
2606 .error_to_int,2605 .error_to_int,
2607 .int_to_error,2606 .int_to_error,
2607 .reify,
2608 => {2608 => {
2609 const extra = file.zir.extraData(Zir.Inst.UnNode, extended.operand).data;2609 const extra = file.zir.extraData(Zir.Inst.UnNode, extended.operand).data;
2610 const bin_index = self.exprs.items.len;2610 const bin_index = self.exprs.items.len;
src/Sema.zig+13-11
...@@ -816,7 +816,6 @@ fn analyzeBodyInner(...@@ -816,7 +816,6 @@ fn analyzeBodyInner(
816 .embed_file => try sema.zirEmbedFile(block, inst),816 .embed_file => try sema.zirEmbedFile(block, inst),
817 .error_name => try sema.zirErrorName(block, inst),817 .error_name => try sema.zirErrorName(block, inst),
818 .tag_name => try sema.zirTagName(block, inst),818 .tag_name => try sema.zirTagName(block, inst),
819 .reify => try sema.zirReify(block, inst),
820 .type_name => try sema.zirTypeName(block, inst),819 .type_name => try sema.zirTypeName(block, inst),
821 .frame_type => try sema.zirFrameType(block, inst),820 .frame_type => try sema.zirFrameType(block, inst),
822 .frame_size => try sema.zirFrameSize(block, inst),821 .frame_size => try sema.zirFrameSize(block, inst),
...@@ -951,6 +950,7 @@ fn analyzeBodyInner(...@@ -951,6 +950,7 @@ fn analyzeBodyInner(
951 .select => try sema.zirSelect( block, extended),950 .select => try sema.zirSelect( block, extended),
952 .error_to_int => try sema.zirErrorToInt( block, extended),951 .error_to_int => try sema.zirErrorToInt( block, extended),
953 .int_to_error => try sema.zirIntToError( block, extended),952 .int_to_error => try sema.zirIntToError( block, extended),
953 .reify => try sema.zirReify( block, extended, inst),
954 // zig fmt: on954 // zig fmt: on
955 .fence => {955 .fence => {
956 try sema.zirFence(block, extended);956 try sema.zirFence(block, extended);
...@@ -16023,13 +16023,14 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -16023,13 +16023,14 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
16023 return block.addUnOp(.tag_name, casted_operand);16023 return block.addUnOp(.tag_name, casted_operand);
16024}16024}
1602516025
16026fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {16026fn zirReify(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
16027 const mod = sema.mod;16027 const mod = sema.mod;
16028 const inst_data = sema.code.instructions.items(.data)[inst].un_node;16028 const name_strategy = @intToEnum(Zir.Inst.NameStrategy, extended.small);
16029 const src = inst_data.src();16029 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
16030 const src = LazySrcLoc.nodeOffset(extra.node);
16030 const type_info_ty = try sema.resolveBuiltinTypeFields(block, src, "Type");16031 const type_info_ty = try sema.resolveBuiltinTypeFields(block, src, "Type");
16031 const uncasted_operand = try sema.resolveInst(inst_data.operand);16032 const uncasted_operand = try sema.resolveInst(extra.operand);
16032 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };16033 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
16033 const type_info = try sema.coerce(block, type_info_ty, uncasted_operand, operand_src);16034 const type_info = try sema.coerce(block, type_info_ty, uncasted_operand, operand_src);
16034 const val = try sema.resolveConstValue(block, operand_src, type_info, "operand to @Type must be comptime known");16035 const val = try sema.resolveConstValue(block, operand_src, type_info, "operand to @Type must be comptime known");
16035 const union_val = val.cast(Value.Payload.Union).?.data;16036 const union_val = val.cast(Value.Payload.Union).?.data;
...@@ -16289,7 +16290,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I...@@ -16289,7 +16290,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
16289 return if (is_tuple_val.toBool())16290 return if (is_tuple_val.toBool())
16290 try sema.reifyTuple(block, src, fields_val)16291 try sema.reifyTuple(block, src, fields_val)
16291 else16292 else
16292 try sema.reifyStruct(block, inst, src, layout_val, fields_val);16293 try sema.reifyStruct(block, inst, src, layout_val, fields_val, name_strategy);
16293 },16294 },
16294 .Enum => {16295 .Enum => {
16295 const struct_val = union_val.val.castTag(.aggregate).?.data;16296 const struct_val = union_val.val.castTag(.aggregate).?.data;
...@@ -16338,7 +16339,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I...@@ -16338,7 +16339,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
16338 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{16339 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{
16339 .ty = Type.type,16340 .ty = Type.type,
16340 .val = enum_val,16341 .val = enum_val,
16341 }, .anon, "enum", null);16342 }, name_strategy, "enum", inst);
16342 const new_decl = mod.declPtr(new_decl_index);16343 const new_decl = mod.declPtr(new_decl_index);
16343 new_decl.owns_tv = true;16344 new_decl.owns_tv = true;
16344 errdefer mod.abortAnonDecl(new_decl_index);16345 errdefer mod.abortAnonDecl(new_decl_index);
...@@ -16435,7 +16436,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I...@@ -16435,7 +16436,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
16435 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{16436 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{
16436 .ty = Type.type,16437 .ty = Type.type,
16437 .val = opaque_val,16438 .val = opaque_val,
16438 }, .anon, "opaque", null);16439 }, name_strategy, "opaque", inst);
16439 const new_decl = mod.declPtr(new_decl_index);16440 const new_decl = mod.declPtr(new_decl_index);
16440 new_decl.owns_tv = true;16441 new_decl.owns_tv = true;
16441 errdefer mod.abortAnonDecl(new_decl_index);16442 errdefer mod.abortAnonDecl(new_decl_index);
...@@ -16494,7 +16495,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I...@@ -16494,7 +16495,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
16494 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{16495 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{
16495 .ty = Type.type,16496 .ty = Type.type,
16496 .val = new_union_val,16497 .val = new_union_val,
16497 }, .anon, "union", null);16498 }, name_strategy, "union", inst);
16498 const new_decl = mod.declPtr(new_decl_index);16499 const new_decl = mod.declPtr(new_decl_index);
16499 new_decl.owns_tv = true;16500 new_decl.owns_tv = true;
16500 errdefer mod.abortAnonDecl(new_decl_index);16501 errdefer mod.abortAnonDecl(new_decl_index);
...@@ -16787,6 +16788,7 @@ fn reifyStruct(...@@ -16787,6 +16788,7 @@ fn reifyStruct(
16787 src: LazySrcLoc,16788 src: LazySrcLoc,
16788 layout_val: Value,16789 layout_val: Value,
16789 fields_val: Value,16790 fields_val: Value,
16791 name_strategy: Zir.Inst.NameStrategy,
16790) CompileError!Air.Inst.Ref {16792) CompileError!Air.Inst.Ref {
16791 var new_decl_arena = std.heap.ArenaAllocator.init(sema.gpa);16793 var new_decl_arena = std.heap.ArenaAllocator.init(sema.gpa);
16792 errdefer new_decl_arena.deinit();16794 errdefer new_decl_arena.deinit();
...@@ -16799,7 +16801,7 @@ fn reifyStruct(...@@ -16799,7 +16801,7 @@ fn reifyStruct(
16799 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{16801 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{
16800 .ty = Type.type,16802 .ty = Type.type,
16801 .val = new_struct_val,16803 .val = new_struct_val,
16802 }, .anon, "struct", null);16804 }, name_strategy, "struct", inst);
16803 const new_decl = mod.declPtr(new_decl_index);16805 const new_decl = mod.declPtr(new_decl_index);
16804 new_decl.owns_tv = true;16806 new_decl.owns_tv = true;
16805 errdefer mod.abortAnonDecl(new_decl_index);16807 errdefer mod.abortAnonDecl(new_decl_index);
src/Zir.zig+4-5
...@@ -839,8 +839,6 @@ pub const Inst = struct {...@@ -839,8 +839,6 @@ pub const Inst = struct {
839 round,839 round,
840 /// Implement builtin `@tagName`. Uses `un_node`.840 /// Implement builtin `@tagName`. Uses `un_node`.
841 tag_name,841 tag_name,
842 /// Implement builtin `@Type`. Uses `un_node`.
843 reify,
844 /// Implement builtin `@typeName`. Uses `un_node`.842 /// Implement builtin `@typeName`. Uses `un_node`.
845 type_name,843 type_name,
846 /// Implement builtin `@Frame`. Uses `un_node`.844 /// Implement builtin `@Frame`. Uses `un_node`.
...@@ -1197,7 +1195,6 @@ pub const Inst = struct {...@@ -1197,7 +1195,6 @@ pub const Inst = struct {
1197 .trunc,1195 .trunc,
1198 .round,1196 .round,
1199 .tag_name,1197 .tag_name,
1200 .reify,
1201 .type_name,1198 .type_name,
1202 .frame_type,1199 .frame_type,
1203 .frame_size,1200 .frame_size,
...@@ -1484,7 +1481,6 @@ pub const Inst = struct {...@@ -1484,7 +1481,6 @@ pub const Inst = struct {
1484 .trunc,1481 .trunc,
1485 .round,1482 .round,
1486 .tag_name,1483 .tag_name,
1487 .reify,
1488 .type_name,1484 .type_name,
1489 .frame_type,1485 .frame_type,
1490 .frame_size,1486 .frame_size,
...@@ -1759,7 +1755,6 @@ pub const Inst = struct {...@@ -1759,7 +1755,6 @@ pub const Inst = struct {
1759 .trunc = .un_node,1755 .trunc = .un_node,
1760 .round = .un_node,1756 .round = .un_node,
1761 .tag_name = .un_node,1757 .tag_name = .un_node,
1762 .reify = .un_node,
1763 .type_name = .un_node,1758 .type_name = .un_node,
1764 .frame_type = .un_node,1759 .frame_type = .un_node,
1765 .frame_size = .un_node,1760 .frame_size = .un_node,
...@@ -1980,6 +1975,10 @@ pub const Inst = struct {...@@ -1980,6 +1975,10 @@ pub const Inst = struct {
1980 /// Implement builtin `@intToError`.1975 /// Implement builtin `@intToError`.
1981 /// `operand` is payload index to `UnNode`.1976 /// `operand` is payload index to `UnNode`.
1982 int_to_error,1977 int_to_error,
1978 /// Implement builtin `@Type`.
1979 /// `operand` is payload index to `UnNode`.
1980 /// `small` contains `NameStrategy
1981 reify,
19831982
1984 pub const InstData = struct {1983 pub const InstData = struct {
1985 opcode: Extended,1984 opcode: Extended,
src/print_zir.zig+1-1
...@@ -214,7 +214,6 @@ const Writer = struct {...@@ -214,7 +214,6 @@ const Writer = struct {
214 .trunc,214 .trunc,
215 .round,215 .round,
216 .tag_name,216 .tag_name,
217 .reify,
218 .type_name,217 .type_name,
219 .frame_type,218 .frame_type,
220 .frame_size,219 .frame_size,
...@@ -500,6 +499,7 @@ const Writer = struct {...@@ -500,6 +499,7 @@ const Writer = struct {
500 .wasm_memory_size,499 .wasm_memory_size,
501 .error_to_int,500 .error_to_int,
502 .int_to_error,501 .int_to_error,
502 .reify,
503 => {503 => {
504 const inst_data = self.code.extraData(Zir.Inst.UnNode, extended.operand).data;504 const inst_data = self.code.extraData(Zir.Inst.UnNode, extended.operand).data;
505 const src = LazySrcLoc.nodeOffset(inst_data.node);505 const src = LazySrcLoc.nodeOffset(inst_data.node);
test/cases/compile_errors/reify_type_for_tagged_union_with_extra_union_field.zig created+35
...@@ -0,0 +1,35 @@
1const Tag = @Type(.{
2 .Enum = .{
3 .layout = .Auto,
4 .tag_type = u1,
5 .fields = &.{
6 .{ .name = "signed", .value = 0 },
7 .{ .name = "unsigned", .value = 1 },
8 },
9 .decls = &.{},
10 .is_exhaustive = true,
11 },
12});
13const Tagged = @Type(.{
14 .Union = .{
15 .layout = .Auto,
16 .tag_type = Tag,
17 .fields = &.{
18 .{ .name = "signed", .field_type = i32, .alignment = @alignOf(i32) },
19 .{ .name = "unsigned", .field_type = u32, .alignment = @alignOf(u32) },
20 .{ .name = "arst", .field_type = f32, .alignment = @alignOf(f32) },
21 },
22 .decls = &.{},
23 },
24});
25export fn entry() void {
26 var tagged = Tagged{ .signed = -1 };
27 tagged = .{ .unsigned = 1 };
28}
29
30// error
31// backend=stage2
32// target=native
33//
34// :13:16: error: no field named 'arst' in enum 'tmp.Tag'
35// :1:13: note: enum declared here