authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-08-12 10:28:26-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-08-12 10:28:26-07:00
loge4d05358e18a49c80079ead3bb31966cea564f87
tree5991f6c784c4bdd67477f00d1d2cf84131f8a5ba
parent5e0107fbce8f33f84af232c3edc912a81615175f
parent09b0070e876e11da0a5291106a115f66b548790b
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #16787 from jacobly0/as-srclocs

AstGen: create more `@as` with source locations

6 files changed, 526 insertions(+), 369 deletions(-)

src/AstGen.zig+393-348
......@@ -70,10 +70,10 @@ fn addExtra(astgen: *AstGen, extra: anytype) Allocator.Error!u32 {
7070
7171fn addExtraAssumeCapacity(astgen: *AstGen, extra: anytype) u32 {
7272 const fields = std.meta.fields(@TypeOf(extra));
73 const result = @as(u32, @intCast(astgen.extra.items.len));
73 const extra_index: u32 = @intCast(astgen.extra.items.len);
7474 astgen.extra.items.len += fields.len;
75 setExtra(astgen, result, extra);
76 return result;
75 setExtra(astgen, extra_index, extra);
76 return extra_index;
7777}
7878
7979fn setExtra(astgen: *AstGen, index: usize, extra: anytype) void {
......@@ -83,11 +83,12 @@ fn setExtra(astgen: *AstGen, index: usize, extra: anytype) void {
8383 astgen.extra.items[i] = switch (field.type) {
8484 u32 => @field(extra, field.name),
8585 Zir.Inst.Ref => @intFromEnum(@field(extra, field.name)),
86 i32 => @as(u32, @bitCast(@field(extra, field.name))),
87 Zir.Inst.Call.Flags => @as(u32, @bitCast(@field(extra, field.name))),
88 Zir.Inst.BuiltinCall.Flags => @as(u32, @bitCast(@field(extra, field.name))),
89 Zir.Inst.SwitchBlock.Bits => @as(u32, @bitCast(@field(extra, field.name))),
90 Zir.Inst.FuncFancy.Bits => @as(u32, @bitCast(@field(extra, field.name))),
86 i32,
87 Zir.Inst.Call.Flags,
88 Zir.Inst.BuiltinCall.Flags,
89 Zir.Inst.SwitchBlock.Bits,
90 Zir.Inst.FuncFancy.Bits,
91 => @bitCast(@field(extra, field.name)),
9192 else => @compileError("bad field type"),
9293 };
9394 i += 1;
......@@ -95,19 +96,17 @@ fn setExtra(astgen: *AstGen, index: usize, extra: anytype) void {
9596}
9697
9798fn reserveExtra(astgen: *AstGen, size: usize) Allocator.Error!u32 {
98 const result = @as(u32, @intCast(astgen.extra.items.len));
99 try astgen.extra.resize(astgen.gpa, result + size);
100 return result;
99 const extra_index: u32 = @intCast(astgen.extra.items.len);
100 try astgen.extra.resize(astgen.gpa, extra_index + size);
101 return extra_index;
101102}
102103
103104fn appendRefs(astgen: *AstGen, refs: []const Zir.Inst.Ref) !void {
104 const coerced = @as([]const u32, @ptrCast(refs));
105 return astgen.extra.appendSlice(astgen.gpa, coerced);
105 return astgen.extra.appendSlice(astgen.gpa, @ptrCast(refs));
106106}
107107
108108fn appendRefsAssumeCapacity(astgen: *AstGen, refs: []const Zir.Inst.Ref) void {
109 const coerced = @as([]const u32, @ptrCast(refs));
110 astgen.extra.appendSliceAssumeCapacity(coerced);
109 astgen.extra.appendSliceAssumeCapacity(@ptrCast(refs));
111110}
112111
113112pub fn generate(gpa: Allocator, tree: Ast) Allocator.Error!Zir {
......@@ -176,7 +175,7 @@ pub fn generate(gpa: Allocator, tree: Ast) Allocator.Error!Zir {
176175 @typeInfo(Zir.Inst.CompileErrors.Item).Struct.fields.len);
177176
178177 astgen.extra.items[err_index] = astgen.addExtraAssumeCapacity(Zir.Inst.CompileErrors{
179 .items_len = @as(u32, @intCast(astgen.compile_errors.items.len)),
178 .items_len = @intCast(astgen.compile_errors.items.len),
180179 });
181180
182181 for (astgen.compile_errors.items) |item| {
......@@ -192,7 +191,7 @@ pub fn generate(gpa: Allocator, tree: Ast) Allocator.Error!Zir {
192191 astgen.imports.count() * @typeInfo(Zir.Inst.Imports.Item).Struct.fields.len);
193192
194193 astgen.extra.items[imports_index] = astgen.addExtraAssumeCapacity(Zir.Inst.Imports{
195 .imports_len = @as(u32, @intCast(astgen.imports.count())),
194 .imports_len = @intCast(astgen.imports.count()),
196195 });
197196
198197 var it = astgen.imports.iterator();
......@@ -1334,7 +1333,7 @@ fn fnProtoExpr(
13341333 var param_gz = block_scope.makeSubBlock(scope);
13351334 defer param_gz.unstack();
13361335 const param_type = try expr(&param_gz, scope, coerced_type_ri, param_type_node);
1337 const param_inst_expected = @as(u32, @intCast(astgen.instructions.len + 1));
1336 const param_inst_expected: u32 = @intCast(astgen.instructions.len + 1);
13381337 _ = try param_gz.addBreakWithSrcNode(.break_inline, param_inst_expected, param_type, param_type_node);
13391338 const main_tokens = tree.nodes.items(.main_token);
13401339 const name_token = param.name_token orelse main_tokens[param_type_node];
......@@ -1468,7 +1467,7 @@ fn arrayInitExpr(
14681467 const array_type_inst = try typeExpr(gz, scope, array_init.ast.type_expr);
14691468 _ = try gz.addPlNode(.validate_array_init_ty, node, Zir.Inst.ArrayInit{
14701469 .ty = array_type_inst,
1471 .init_count = @as(u32, @intCast(array_init.ast.elements.len)),
1470 .init_count = @intCast(array_init.ast.elements.len),
14721471 });
14731472 break :inst .{
14741473 .array = array_type_inst,
......@@ -1552,7 +1551,7 @@ fn arrayInitExprRlNone(
15521551 const astgen = gz.astgen;
15531552
15541553 const payload_index = try addExtra(astgen, Zir.Inst.MultiOp{
1555 .operands_len = @as(u32, @intCast(elements.len)),
1554 .operands_len = @intCast(elements.len),
15561555 });
15571556 var extra_index = try reserveExtra(astgen, elements.len);
15581557
......@@ -1577,7 +1576,7 @@ fn arrayInitExprInner(
15771576
15781577 const len = elements.len + @intFromBool(array_ty_inst != .none);
15791578 const payload_index = try addExtra(astgen, Zir.Inst.MultiOp{
1580 .operands_len = @as(u32, @intCast(len)),
1579 .operands_len = @intCast(len),
15811580 });
15821581 var extra_index = try reserveExtra(astgen, len);
15831582 if (array_ty_inst != .none) {
......@@ -1593,7 +1592,7 @@ fn arrayInitExprInner(
15931592 .tag = .elem_type_index,
15941593 .data = .{ .bin = .{
15951594 .lhs = array_ty_inst,
1596 .rhs = @as(Zir.Inst.Ref, @enumFromInt(i)),
1595 .rhs = @enumFromInt(i),
15971596 } },
15981597 });
15991598 break :ri ResultInfo{ .rl = .{ .coerced_ty = ty_expr } };
......@@ -1638,14 +1637,14 @@ fn arrayInitExprRlPtrInner(
16381637 const astgen = gz.astgen;
16391638
16401639 const payload_index = try addExtra(astgen, Zir.Inst.Block{
1641 .body_len = @as(u32, @intCast(elements.len)),
1640 .body_len = @intCast(elements.len),
16421641 });
16431642 var extra_index = try reserveExtra(astgen, elements.len);
16441643
16451644 for (elements, 0..) |elem_init, i| {
16461645 const elem_ptr = try gz.addPlNode(.elem_ptr_imm, elem_init, Zir.Inst.ElemPtrImm{
16471646 .ptr = result_ptr,
1648 .index = @as(u32, @intCast(i)),
1647 .index = @intCast(i),
16491648 });
16501649 astgen.extra.items[extra_index] = refToIndex(elem_ptr).?;
16511650 extra_index += 1;
......@@ -1797,7 +1796,7 @@ fn structInitExprRlNone(
17971796 const tree = astgen.tree;
17981797
17991798 const payload_index = try addExtra(astgen, Zir.Inst.StructInitAnon{
1800 .fields_len = @as(u32, @intCast(struct_init.ast.fields.len)),
1799 .fields_len = @intCast(struct_init.ast.fields.len),
18011800 });
18021801 const field_size = @typeInfo(Zir.Inst.StructInitAnon.Item).Struct.fields.len;
18031802 var extra_index: usize = try reserveExtra(astgen, struct_init.ast.fields.len * field_size);
......@@ -1855,7 +1854,7 @@ fn structInitExprRlPtrInner(
18551854 const tree = astgen.tree;
18561855
18571856 const payload_index = try addExtra(astgen, Zir.Inst.Block{
1858 .body_len = @as(u32, @intCast(struct_init.ast.fields.len)),
1857 .body_len = @intCast(struct_init.ast.fields.len),
18591858 });
18601859 var extra_index = try reserveExtra(astgen, struct_init.ast.fields.len);
18611860
......@@ -1887,7 +1886,7 @@ fn structInitExprRlTy(
18871886 const tree = astgen.tree;
18881887
18891888 const payload_index = try addExtra(astgen, Zir.Inst.StructInit{
1890 .fields_len = @as(u32, @intCast(struct_init.ast.fields.len)),
1889 .fields_len = @intCast(struct_init.ast.fields.len),
18911890 });
18921891 const field_size = @typeInfo(Zir.Inst.StructInit.Item).Struct.fields.len;
18931892 var extra_index: usize = try reserveExtra(astgen, struct_init.ast.fields.len * field_size);
......@@ -2126,7 +2125,7 @@ fn breakExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index) Inn
21262125 }
21272126
21282127 const operand = try reachableExpr(parent_gz, parent_scope, block_gz.break_result_info, rhs, node);
2129 const search_index = @as(Zir.Inst.Index, @intCast(astgen.instructions.len));
2128 const search_index: Zir.Inst.Index = @intCast(astgen.instructions.len);
21302129
21312130 try genDefers(parent_gz, scope, parent_scope, .normal_only);
21322131
......@@ -2373,8 +2372,8 @@ fn labeledBlockExpr(
23732372 try astgen.appendErrorTok(label_token, "unused block label", .{});
23742373 }
23752374
2376 const zir_datas = gz.astgen.instructions.items(.data);
2377 const zir_tags = gz.astgen.instructions.items(.tag);
2375 const zir_datas = astgen.instructions.items(.data);
2376 const zir_tags = astgen.instructions.items(.tag);
23782377 const strat = ri.rl.strategy(&block_scope);
23792378 switch (strat.tag) {
23802379 .break_void => {
......@@ -2396,6 +2395,10 @@ fn labeledBlockExpr(
23962395 // it as the break operand.
23972396 // This corresponds to similar code in `setCondBrPayloadElideBlockStorePtr`.
23982397 if (block_scope.rl_ty_inst != .none) {
2398 try astgen.extra.ensureUnusedCapacity(
2399 astgen.gpa,
2400 @typeInfo(Zir.Inst.As).Struct.fields.len * block_scope.labeled_breaks.items.len,
2401 );
23992402 for (block_scope.labeled_breaks.items) |br| {
24002403 // We expect the `store_to_block_ptr` to be created between 1-3 instructions
24012404 // prior to the break.
......@@ -2404,12 +2407,28 @@ fn labeledBlockExpr(
24042407 if (zir_tags[search_index] == .store_to_block_ptr and
24052408 zir_datas[search_index].bin.lhs == block_scope.rl_ptr)
24062409 {
2407 zir_tags[search_index] = .as;
2408 zir_datas[search_index].bin = .{
2409 .lhs = block_scope.rl_ty_inst,
2410 .rhs = zir_datas[br.br].@"break".operand,
2411 };
2412 zir_datas[br.br].@"break".operand = indexToRef(search_index);
2410 const break_data = &zir_datas[br.br].@"break";
2411 const break_src: i32 = @bitCast(astgen.extra.items[
2412 break_data.payload_index +
2413 std.meta.fieldIndex(Zir.Inst.Break, "operand_src_node").?
2414 ]);
2415 if (break_src == Zir.Inst.Break.no_src_node) {
2416 zir_tags[search_index] = .as;
2417 zir_datas[search_index].bin = .{
2418 .lhs = block_scope.rl_ty_inst,
2419 .rhs = break_data.operand,
2420 };
2421 } else {
2422 zir_tags[search_index] = .as_node;
2423 zir_datas[search_index] = .{ .pl_node = .{
2424 .src_node = break_src,
2425 .payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.As{
2426 .dest_type = block_scope.rl_ty_inst,
2427 .operand = break_data.operand,
2428 }),
2429 } };
2430 }
2431 break_data.operand = indexToRef(search_index);
24132432 break;
24142433 }
24152434 } else unreachable;
......@@ -2530,19 +2549,21 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
25302549 // For some instructions, modify the zir data
25312550 // so we can avoid a separate ensure_result_used instruction.
25322551 .call, .field_call => {
2533 const extra_index = gz.astgen.instructions.items(.data)[inst].pl_node.payload_index;
2534 const slot = &gz.astgen.extra.items[extra_index];
2535 var flags = @as(Zir.Inst.Call.Flags, @bitCast(slot.*));
2552 const break_extra = gz.astgen.instructions.items(.data)[inst].pl_node.payload_index;
2553 comptime assert(std.meta.fieldIndex(Zir.Inst.Call, "flags") ==
2554 std.meta.fieldIndex(Zir.Inst.FieldCall, "flags"));
2555 const flags: *Zir.Inst.Call.Flags = @ptrCast(&gz.astgen.extra.items[
2556 break_extra + std.meta.fieldIndex(Zir.Inst.Call, "flags").?
2557 ]);
25362558 flags.ensure_result_used = true;
2537 slot.* = @as(u32, @bitCast(flags));
25382559 break :b true;
25392560 },
25402561 .builtin_call => {
2541 const extra_index = gz.astgen.instructions.items(.data)[inst].pl_node.payload_index;
2542 const slot = &gz.astgen.extra.items[extra_index];
2543 var flags = @as(Zir.Inst.BuiltinCall.Flags, @bitCast(slot.*));
2562 const break_extra = gz.astgen.instructions.items(.data)[inst].pl_node.payload_index;
2563 const flags: *Zir.Inst.BuiltinCall.Flags = @ptrCast(&gz.astgen.extra.items[
2564 break_extra + std.meta.fieldIndex(Zir.Inst.BuiltinCall, "flags").?
2565 ]);
25442566 flags.ensure_result_used = true;
2545 slot.* = @as(u32, @bitCast(flags));
25462567 break :b true;
25472568 },
25482569
......@@ -2920,7 +2941,7 @@ fn genDefers(
29202941 .index = defer_scope.index,
29212942 .len = defer_scope.len,
29222943 });
2923 const new_index = @as(Zir.Inst.Index, @intCast(gz.astgen.instructions.len));
2944 const new_index: Zir.Inst.Index = @intCast(gz.astgen.instructions.len);
29242945 gz.astgen.instructions.appendAssumeCapacity(.{
29252946 .tag = .defer_err_code,
29262947 .data = .{ .defer_err_code = .{
......@@ -2999,7 +3020,7 @@ fn deferStmt(
29993020 const sub_scope = if (!have_err_code) &defer_gen.base else blk: {
30003021 try gz.addDbgBlockBegin();
30013022 const ident_name = try gz.astgen.identAsString(payload_token);
3002 remapped_err_code = @as(Zir.Inst.Index, @intCast(gz.astgen.instructions.len));
3023 remapped_err_code = @intCast(gz.astgen.instructions.len);
30033024 try gz.astgen.instructions.append(gz.astgen.gpa, .{
30043025 .tag = .extended,
30053026 .data = .{ .extended = .{
......@@ -3039,7 +3060,7 @@ fn deferStmt(
30393060 break :blk gz.astgen.countBodyLenAfterFixups(body) + refs;
30403061 };
30413062
3042 const index = @as(u32, @intCast(gz.astgen.extra.items.len));
3063 const index: u32 = @intCast(gz.astgen.extra.items.len);
30433064 try gz.astgen.extra.ensureUnusedCapacity(gz.astgen.gpa, body_len);
30443065 if (have_err_code) {
30453066 if (gz.astgen.ref_table.fetchRemove(remapped_err_code)) |kv| {
......@@ -3580,7 +3601,7 @@ fn ptrType(
35803601 gz.astgen.extra.appendAssumeCapacity(@intFromEnum(bit_end_ref));
35813602 }
35823603
3583 const new_index = @as(Zir.Inst.Index, @intCast(gz.astgen.instructions.len));
3604 const new_index: Zir.Inst.Index = @intCast(gz.astgen.instructions.len);
35843605 const result = indexToRef(new_index);
35853606 gz.astgen.instructions.appendAssumeCapacity(.{ .tag = .ptr_type, .data = .{
35863607 .ptr_type = .{
......@@ -3671,7 +3692,7 @@ const WipMembers = struct {
36713692 const max_decl_size = 11;
36723693
36733694 fn init(gpa: Allocator, payload: *ArrayListUnmanaged(u32), decl_count: u32, field_count: u32, comptime bits_per_field: u32, comptime max_field_size: u32) Allocator.Error!Self {
3674 const payload_top = @as(u32, @intCast(payload.items.len));
3695 const payload_top: u32 = @intCast(payload.items.len);
36753696 const decls_start = payload_top + (decl_count + decls_per_u32 - 1) / decls_per_u32;
36763697 const field_bits_start = decls_start + decl_count * max_decl_size;
36773698 const fields_start = field_bits_start + if (bits_per_field > 0) blk: {
......@@ -3726,7 +3747,7 @@ const WipMembers = struct {
37263747 fn appendToDeclSlice(self: *Self, data: []const u32) void {
37273748 assert(self.decls_end + data.len <= self.field_bits_start);
37283749 @memcpy(self.payload.items[self.decls_end..][0..data.len], data);
3729 self.decls_end += @as(u32, @intCast(data.len));
3750 self.decls_end += @intCast(data.len);
37303751 }
37313752
37323753 fn appendToField(self: *Self, data: u32) void {
......@@ -3739,14 +3760,14 @@ const WipMembers = struct {
37393760 const empty_decl_slots = decls_per_u32 - (self.decl_index % decls_per_u32);
37403761 if (self.decl_index > 0 and empty_decl_slots < decls_per_u32) {
37413762 const index = self.payload_top + self.decl_index / decls_per_u32;
3742 self.payload.items[index] >>= @as(u5, @intCast(empty_decl_slots * bits_per_decl));
3763 self.payload.items[index] >>= @intCast(empty_decl_slots * bits_per_decl);
37433764 }
37443765 if (bits_per_field > 0) {
37453766 const fields_per_u32 = 32 / bits_per_field;
37463767 const empty_field_slots = fields_per_u32 - (self.field_index % fields_per_u32);
37473768 if (self.field_index > 0 and empty_field_slots < fields_per_u32) {
37483769 const index = self.field_bits_start + self.field_index / fields_per_u32;
3749 self.payload.items[index] >>= @as(u5, @intCast(empty_field_slots * bits_per_field));
3770 self.payload.items[index] >>= @intCast(empty_field_slots * bits_per_field);
37503771 }
37513772 }
37523773 }
......@@ -3908,7 +3929,7 @@ fn fnDecl(
39083929 var param_gz = decl_gz.makeSubBlock(scope);
39093930 defer param_gz.unstack();
39103931 const param_type = try expr(&param_gz, params_scope, coerced_type_ri, param_type_node);
3911 const param_inst_expected = @as(u32, @intCast(astgen.instructions.len + 1));
3932 const param_inst_expected: u32 = @intCast(astgen.instructions.len + 1);
39123933 _ = try param_gz.addBreakWithSrcNode(.break_inline, param_inst_expected, param_type, param_type_node);
39133934
39143935 const main_tokens = tree.nodes.items(.main_token);
......@@ -4122,9 +4143,8 @@ fn fnDecl(
41224143 try decl_gz.setBlockBody(block_inst);
41234144
41244145 {
4125 const contents_hash = std.zig.hashSrc(tree.getNodeSource(decl_node));
4126 const casted = @as([4]u32, @bitCast(contents_hash));
4127 wip_members.appendToDeclSlice(&casted);
4146 const contents_hash align(@alignOf(u32)) = std.zig.hashSrc(tree.getNodeSource(decl_node));
4147 wip_members.appendToDeclSlice(std.mem.bytesAsSlice(u32, &contents_hash));
41284148 }
41294149 {
41304150 const line_delta = decl_gz.decl_line - gz.decl_line;
......@@ -4273,9 +4293,8 @@ fn globalVarDecl(
42734293 try block_scope.setBlockBody(block_inst);
42744294
42754295 {
4276 const contents_hash = std.zig.hashSrc(tree.getNodeSource(node));
4277 const casted = @as([4]u32, @bitCast(contents_hash));
4278 wip_members.appendToDeclSlice(&casted);
4296 const contents_hash align(@alignOf(u32)) = std.zig.hashSrc(tree.getNodeSource(node));
4297 wip_members.appendToDeclSlice(std.mem.bytesAsSlice(u32, &contents_hash));
42794298 }
42804299 {
42814300 const line_delta = block_scope.decl_line - gz.decl_line;
......@@ -4328,9 +4347,8 @@ fn comptimeDecl(
43284347 try decl_block.setBlockBody(block_inst);
43294348
43304349 {
4331 const contents_hash = std.zig.hashSrc(tree.getNodeSource(node));
4332 const casted = @as([4]u32, @bitCast(contents_hash));
4333 wip_members.appendToDeclSlice(&casted);
4350 const contents_hash align(@alignOf(u32)) = std.zig.hashSrc(tree.getNodeSource(node));
4351 wip_members.appendToDeclSlice(std.mem.bytesAsSlice(u32, &contents_hash));
43344352 }
43354353 {
43364354 const line_delta = decl_block.decl_line - gz.decl_line;
......@@ -4380,9 +4398,8 @@ fn usingnamespaceDecl(
43804398 try decl_block.setBlockBody(block_inst);
43814399
43824400 {
4383 const contents_hash = std.zig.hashSrc(tree.getNodeSource(node));
4384 const casted = @as([4]u32, @bitCast(contents_hash));
4385 wip_members.appendToDeclSlice(&casted);
4401 const contents_hash align(@alignOf(u32)) = std.zig.hashSrc(tree.getNodeSource(node));
4402 wip_members.appendToDeclSlice(std.mem.bytesAsSlice(u32, &contents_hash));
43864403 }
43874404 {
43884405 const line_delta = decl_block.decl_line - gz.decl_line;
......@@ -4567,9 +4584,8 @@ fn testDecl(
45674584 try decl_block.setBlockBody(block_inst);
45684585
45694586 {
4570 const contents_hash = std.zig.hashSrc(tree.getNodeSource(node));
4571 const casted = @as([4]u32, @bitCast(contents_hash));
4572 wip_members.appendToDeclSlice(&casted);
4587 const contents_hash align(@alignOf(u32)) = std.zig.hashSrc(tree.getNodeSource(node));
4588 wip_members.appendToDeclSlice(std.mem.bytesAsSlice(u32, &contents_hash));
45734589 }
45744590 {
45754591 const line_delta = decl_block.decl_line - gz.decl_line;
......@@ -4668,7 +4684,7 @@ fn structDeclInner(
46684684 };
46694685
46704686 const decl_count = try astgen.scanDecls(&namespace, container_decl.ast.members);
4671 const field_count = @as(u32, @intCast(container_decl.ast.members.len - decl_count));
4687 const field_count: u32 = @intCast(container_decl.ast.members.len - decl_count);
46724688
46734689 const bits_per_field = 4;
46744690 const max_field_size = 5;
......@@ -4781,7 +4797,7 @@ fn structDeclInner(
47814797 const old_scratch_len = astgen.scratch.items.len;
47824798 try astgen.scratch.ensureUnusedCapacity(gpa, countBodyLenAfterFixups(astgen, body));
47834799 appendBodyWithFixupsArrayList(astgen, &astgen.scratch, body);
4784 wip_members.appendToField(@as(u32, @intCast(astgen.scratch.items.len - old_scratch_len)));
4800 wip_members.appendToField(@intCast(astgen.scratch.items.len - old_scratch_len));
47854801 block_scope.instructions.items.len = block_scope.instructions_top;
47864802 } else {
47874803 wip_members.appendToField(@intFromEnum(field_type));
......@@ -4799,7 +4815,7 @@ fn structDeclInner(
47994815 const old_scratch_len = astgen.scratch.items.len;
48004816 try astgen.scratch.ensureUnusedCapacity(gpa, countBodyLenAfterFixups(astgen, body));
48014817 appendBodyWithFixupsArrayList(astgen, &astgen.scratch, body);
4802 wip_members.appendToField(@as(u32, @intCast(astgen.scratch.items.len - old_scratch_len)));
4818 wip_members.appendToField(@intCast(astgen.scratch.items.len - old_scratch_len));
48034819 block_scope.instructions.items.len = block_scope.instructions_top;
48044820 }
48054821
......@@ -4814,7 +4830,7 @@ fn structDeclInner(
48144830 const old_scratch_len = astgen.scratch.items.len;
48154831 try astgen.scratch.ensureUnusedCapacity(gpa, countBodyLenAfterFixups(astgen, body));
48164832 appendBodyWithFixupsArrayList(astgen, &astgen.scratch, body);
4817 wip_members.appendToField(@as(u32, @intCast(astgen.scratch.items.len - old_scratch_len)));
4833 wip_members.appendToField(@intCast(astgen.scratch.items.len - old_scratch_len));
48184834 block_scope.instructions.items.len = block_scope.instructions_top;
48194835 } else if (member.comptime_token) |comptime_token| {
48204836 return astgen.failTok(comptime_token, "comptime field without default initialization value", .{});
......@@ -4827,7 +4843,7 @@ fn structDeclInner(
48274843 .fields_len = field_count,
48284844 .decls_len = decl_count,
48294845 .backing_int_ref = backing_int_ref,
4830 .backing_int_body_len = @as(u32, @intCast(backing_int_body_len)),
4846 .backing_int_body_len = @intCast(backing_int_body_len),
48314847 .known_non_opv = known_non_opv,
48324848 .known_comptime_only = known_comptime_only,
48334849 .is_tuple = is_tuple,
......@@ -4887,7 +4903,7 @@ fn unionDeclInner(
48874903 defer block_scope.unstack();
48884904
48894905 const decl_count = try astgen.scanDecls(&namespace, members);
4890 const field_count = @as(u32, @intCast(members.len - decl_count));
4906 const field_count: u32 = @intCast(members.len - decl_count);
48914907
48924908 if (layout != .Auto and (auto_enum_tok != null or arg_node != 0)) {
48934909 const layout_str = if (layout == .Extern) "extern" else "packed";
......@@ -5182,7 +5198,7 @@ fn containerDecl(
51825198
51835199 const bits_per_field = 1;
51845200 const max_field_size = 3;
5185 var wip_members = try WipMembers.init(gpa, &astgen.scratch, @as(u32, @intCast(counts.decls)), @as(u32, @intCast(counts.total_fields)), bits_per_field, max_field_size);
5201 var wip_members = try WipMembers.init(gpa, &astgen.scratch, @intCast(counts.decls), @intCast(counts.total_fields), bits_per_field, max_field_size);
51865202 defer wip_members.deinit();
51875203
51885204 for (container_decl.ast.members) |member_node| {
......@@ -5240,8 +5256,8 @@ fn containerDecl(
52405256 .nonexhaustive = nonexhaustive,
52415257 .tag_type = arg_inst,
52425258 .body_len = body_len,
5243 .fields_len = @as(u32, @intCast(counts.total_fields)),
5244 .decls_len = @as(u32, @intCast(counts.decls)),
5259 .fields_len = @intCast(counts.total_fields),
5260 .decls_len = @intCast(counts.decls),
52455261 });
52465262
52475263 wip_members.finishBits(bits_per_field);
......@@ -5431,7 +5447,7 @@ fn errorSetDecl(gz: *GenZir, ri: ResultInfo, node: Ast.Node.Index) InnerError!Zi
54315447 }
54325448
54335449 setExtra(astgen, payload_index, Zir.Inst.ErrorSetDecl{
5434 .fields_len = @as(u32, @intCast(fields_len)),
5450 .fields_len = @intCast(fields_len),
54355451 });
54365452 const result = try gz.addPlNodePayloadIndex(.error_set_decl, node, payload_index);
54375453 return rvalue(gz, ri, result, node);
......@@ -5657,7 +5673,7 @@ fn finishThenElseBlock(
56575673 0;
56585674
56595675 if (strat.elide_store_to_block_ptr_instructions) {
5660 try setCondBrPayloadElideBlockStorePtr(condbr, cond, then_scope, then_break, else_scope, else_break, block_scope.rl_ptr);
5676 try setCondBrPayloadElideBlockStorePtr(condbr, cond, then_scope, then_break, then_src_node, else_scope, else_break, else_src_node, block_scope.rl_ptr);
56615677 } else {
56625678 try setCondBrPayload(condbr, cond, then_scope, then_break, else_scope, else_break);
56635679 }
......@@ -6082,8 +6098,10 @@ fn setCondBrPayloadElideBlockStorePtr(
60826098 cond: Zir.Inst.Ref,
60836099 then_scope: *GenZir,
60846100 then_break: Zir.Inst.Index,
6101 then_src_node: Ast.Node.Index,
60856102 else_scope: *GenZir,
60866103 else_break: Zir.Inst.Index,
6104 else_src_node: Ast.Node.Index,
60876105 block_ptr: Zir.Inst.Ref,
60886106) !void {
60896107 defer then_scope.unstack();
......@@ -6097,39 +6115,36 @@ fn setCondBrPayloadElideBlockStorePtr(
60976115 const else_body_len = astgen.countBodyLenAfterFixups(else_body) + @intFromBool(has_else_break);
60986116 try astgen.extra.ensureUnusedCapacity(
60996117 astgen.gpa,
6100 @typeInfo(Zir.Inst.CondBr).Struct.fields.len + then_body_len + else_body_len,
6118 @typeInfo(Zir.Inst.CondBr).Struct.fields.len + then_body_len + else_body_len +
6119 @typeInfo(Zir.Inst.As).Struct.fields.len * 2,
61016120 );
61026121
61036122 const zir_tags = astgen.instructions.items(.tag);
61046123 const zir_datas = astgen.instructions.items(.data);
61056124
6106 const condbr_pl = astgen.addExtraAssumeCapacity(Zir.Inst.CondBr{
6125 const condbr_extra = astgen.addExtraAssumeCapacity(Zir.Inst.CondBr{
61076126 .condition = cond,
61086127 .then_body_len = then_body_len,
61096128 .else_body_len = else_body_len,
61106129 });
6111 zir_datas[condbr].pl_node.payload_index = condbr_pl;
6112 const then_body_len_index = condbr_pl + 1;
6113 const else_body_len_index = condbr_pl + 2;
6130 zir_datas[condbr].pl_node.payload_index = condbr_extra;
61146131
61156132 // The break instructions need to have their operands coerced if the
61166133 // switch's result location is a `ty`. In this case we overwrite the
61176134 // `store_to_block_ptr` instruction with an `as` instruction and repurpose
61186135 // it as the break operand.
61196136 // This corresponds to similar code in `labeledBlockExpr`.
6137 var then_as_inst: Zir.Inst.Index = 0;
61206138 for (then_body) |src_inst| {
61216139 if (zir_tags[src_inst] == .store_to_block_ptr and
61226140 zir_datas[src_inst].bin.lhs == block_ptr)
61236141 {
61246142 if (then_scope.rl_ty_inst != .none and has_then_break) {
6125 zir_tags[src_inst] = .as;
6126 zir_datas[src_inst].bin = .{
6127 .lhs = then_scope.rl_ty_inst,
6128 .rhs = zir_datas[then_break].@"break".operand,
6129 };
6130 zir_datas[then_break].@"break".operand = indexToRef(src_inst);
6143 then_as_inst = src_inst;
61316144 } else {
6132 astgen.extra.items[then_body_len_index] -= 1;
6145 astgen.extra.items[
6146 condbr_extra + std.meta.fieldIndex(Zir.Inst.CondBr, "then_body_len").?
6147 ] -= 1;
61336148 continue;
61346149 }
61356150 }
......@@ -6137,25 +6152,47 @@ fn setCondBrPayloadElideBlockStorePtr(
61376152 }
61386153 if (has_then_break) astgen.extra.appendAssumeCapacity(then_break);
61396154
6155 var else_as_inst: Zir.Inst.Index = 0;
61406156 for (else_body) |src_inst| {
61416157 if (zir_tags[src_inst] == .store_to_block_ptr and
61426158 zir_datas[src_inst].bin.lhs == block_ptr)
61436159 {
61446160 if (else_scope.rl_ty_inst != .none and has_else_break) {
6145 zir_tags[src_inst] = .as;
6146 zir_datas[src_inst].bin = .{
6147 .lhs = else_scope.rl_ty_inst,
6148 .rhs = zir_datas[else_break].@"break".operand,
6149 };
6150 zir_datas[else_break].@"break".operand = indexToRef(src_inst);
6161 else_as_inst = src_inst;
61516162 } else {
6152 astgen.extra.items[else_body_len_index] -= 1;
6163 astgen.extra.items[
6164 condbr_extra + std.meta.fieldIndex(Zir.Inst.CondBr, "else_body_len").?
6165 ] -= 1;
61536166 continue;
61546167 }
61556168 }
61566169 appendPossiblyRefdBodyInst(astgen, &astgen.extra, src_inst);
61576170 }
61586171 if (has_else_break) astgen.extra.appendAssumeCapacity(else_break);
6172
6173 if (then_as_inst != 0) {
6174 zir_tags[then_as_inst] = .as_node;
6175 zir_datas[then_as_inst] = .{ .pl_node = .{
6176 .src_node = then_scope.nodeIndexToRelative(then_src_node),
6177 .payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.As{
6178 .dest_type = then_scope.rl_ty_inst,
6179 .operand = zir_datas[then_break].@"break".operand,
6180 }),
6181 } };
6182 zir_datas[then_break].@"break".operand = indexToRef(then_as_inst);
6183 }
6184
6185 if (else_as_inst != 0) {
6186 zir_tags[else_as_inst] = .as_node;
6187 zir_datas[else_as_inst] = .{ .pl_node = .{
6188 .src_node = else_scope.nodeIndexToRelative(else_src_node),
6189 .payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.As{
6190 .dest_type = else_scope.rl_ty_inst,
6191 .operand = zir_datas[else_break].@"break".operand,
6192 }),
6193 } };
6194 zir_datas[else_break].@"break".operand = indexToRef(else_as_inst);
6195 }
61596196}
61606197
61616198fn whileExpr(
......@@ -6313,10 +6350,10 @@ fn whileExpr(
63136350 loop_scope.break_block = loop_block;
63146351 loop_scope.continue_block = continue_block;
63156352 if (while_full.label_token) |label_token| {
6316 loop_scope.label = @as(?GenZir.Label, GenZir.Label{
6353 loop_scope.label = .{
63176354 .token = label_token,
63186355 .block_inst = loop_block,
6319 });
6356 };
63206357 }
63216358
63226359 // done adding instructions to loop_scope, can now stack then_scope
......@@ -6929,7 +6966,7 @@ fn switchExpr(
69296966
69306967 // If any prong has an inline tag capture, allocate a shared dummy instruction for it
69316968 const tag_inst = if (any_has_tag_capture) tag_inst: {
6932 const inst = @as(Zir.Inst.Index, @intCast(astgen.instructions.len));
6969 const inst: Zir.Inst.Index = @intCast(astgen.instructions.len);
69336970 try astgen.instructions.append(astgen.gpa, .{
69346971 .tag = .extended,
69356972 .data = .{ .extended = .{
......@@ -7022,7 +7059,7 @@ fn switchExpr(
70227059 break :blk &tag_scope.base;
70237060 };
70247061
7025 const header_index = @as(u32, @intCast(payloads.items.len));
7062 const header_index: u32 = @intCast(payloads.items.len);
70267063 const body_len_index = if (is_multi_case) blk: {
70277064 payloads.items[multi_case_table + multi_case_index] = header_index;
70287065 multi_case_index += 1;
......@@ -7112,12 +7149,12 @@ fn switchExpr(
71127149 };
71137150 const body_len = refs_len + astgen.countBodyLenAfterFixups(case_slice);
71147151 try payloads.ensureUnusedCapacity(gpa, body_len);
7115 payloads.items[body_len_index] = @as(u32, @bitCast(Zir.Inst.SwitchBlock.ProngInfo{
7116 .body_len = @as(u28, @intCast(body_len)),
7152 payloads.items[body_len_index] = @bitCast(Zir.Inst.SwitchBlock.ProngInfo{
7153 .body_len = @intCast(body_len),
71177154 .capture = capture,
71187155 .is_inline = case.inline_token != null,
71197156 .has_tag_capture = has_tag_capture,
7120 }));
7157 });
71217158 if (astgen.ref_table.fetchRemove(switch_block)) |kv| {
71227159 appendPossiblyRefdBodyInst(astgen, payloads, kv.value);
71237160 }
......@@ -7135,7 +7172,8 @@ fn switchExpr(
71357172 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.SwitchBlock).Struct.fields.len +
71367173 @intFromBool(multi_cases_len != 0) +
71377174 @intFromBool(any_has_tag_capture) +
7138 payloads.items.len - case_table_end);
7175 payloads.items.len - case_table_end +
7176 (case_table_end - case_table_start) * @typeInfo(Zir.Inst.As).Struct.fields.len);
71397177
71407178 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.SwitchBlock{
71417179 .operand = raw_operand,
......@@ -7144,7 +7182,7 @@ fn switchExpr(
71447182 .has_else = special_prong == .@"else",
71457183 .has_under = special_prong == .under,
71467184 .any_has_tag_capture = any_has_tag_capture,
7147 .scalar_cases_len = @as(Zir.Inst.SwitchBlock.Bits.ScalarCasesLen, @intCast(scalar_cases_len)),
7185 .scalar_cases_len = @intCast(scalar_cases_len),
71487186 },
71497187 });
71507188
......@@ -7162,82 +7200,108 @@ fn switchExpr(
71627200 zir_datas[switch_block].pl_node.payload_index = payload_index;
71637201
71647202 const strat = ri.rl.strategy(&block_scope);
7165 for (payloads.items[case_table_start..case_table_end], 0..) |start_index, i| {
7166 var body_len_index = start_index;
7167 var end_index = start_index;
7168 const table_index = case_table_start + i;
7169 if (table_index < scalar_case_table) {
7170 end_index += 1;
7171 } else if (table_index < multi_case_table) {
7172 body_len_index += 1;
7173 end_index += 2;
7174 } else {
7175 body_len_index += 2;
7176 const items_len = payloads.items[start_index];
7177 const ranges_len = payloads.items[start_index + 1];
7178 end_index += 3 + items_len + 2 * ranges_len;
7179 }
7180
7181 const body_len = @as(Zir.Inst.SwitchBlock.ProngInfo, @bitCast(payloads.items[body_len_index])).body_len;
7182 end_index += body_len;
7183
7184 switch (strat.tag) {
7185 .break_operand => blk: {
7186 // Switch expressions return `true` for `nodeMayNeedMemoryLocation` thus
7187 // `elide_store_to_block_ptr_instructions` will either be true,
7188 // or all prongs are noreturn.
7189 if (!strat.elide_store_to_block_ptr_instructions)
7190 break :blk;
7191
7192 // There will necessarily be a store_to_block_ptr for
7193 // all prongs, except for prongs that ended with a noreturn instruction.
7194 // Elide all the `store_to_block_ptr` instructions.
7195
7196 // The break instructions need to have their operands coerced if the
7197 // switch's result location is a `ty`. In this case we overwrite the
7198 // `store_to_block_ptr` instruction with an `as` instruction and repurpose
7199 // it as the break operand.
7200 if (body_len < 2)
7201 break :blk;
7202
7203 var store_index = end_index - 2;
7204 while (true) : (store_index -= 1) switch (zir_tags[payloads.items[store_index]]) {
7205 .dbg_block_end, .dbg_block_begin, .dbg_stmt, .dbg_var_val, .dbg_var_ptr => {},
7206 else => break,
7207 };
7208 const store_inst = payloads.items[store_index];
7209 if (zir_tags[store_inst] != .store_to_block_ptr or
7210 zir_datas[store_inst].bin.lhs != block_scope.rl_ptr)
7211 break :blk;
7212 const break_inst = payloads.items[end_index - 1];
7213 if (block_scope.rl_ty_inst != .none) {
7214 zir_tags[store_inst] = .as;
7215 zir_datas[store_inst].bin = .{
7216 .lhs = block_scope.rl_ty_inst,
7217 .rhs = zir_datas[break_inst].@"break".operand,
7203 inline for (.{ .body, .breaks }) |pass| {
7204 for (payloads.items[case_table_start..case_table_end], 0..) |start_index, i| {
7205 var body_len_index = start_index;
7206 var end_index = start_index;
7207 const table_index = case_table_start + i;
7208 if (table_index < scalar_case_table) {
7209 end_index += 1;
7210 } else if (table_index < multi_case_table) {
7211 body_len_index += 1;
7212 end_index += 2;
7213 } else {
7214 body_len_index += 2;
7215 const items_len = payloads.items[start_index];
7216 const ranges_len = payloads.items[start_index + 1];
7217 end_index += 3 + items_len + 2 * ranges_len;
7218 }
7219
7220 const prong_info: Zir.Inst.SwitchBlock.ProngInfo = @bitCast(payloads.items[body_len_index]);
7221 end_index += prong_info.body_len;
7222
7223 switch (strat.tag) {
7224 .break_operand => blk: {
7225 // Switch expressions return `true` for `nodeMayNeedMemoryLocation` thus
7226 // `elide_store_to_block_ptr_instructions` will either be true,
7227 // or all prongs are noreturn.
7228 if (!strat.elide_store_to_block_ptr_instructions)
7229 break :blk;
7230
7231 // There will necessarily be a store_to_block_ptr for
7232 // all prongs, except for prongs that ended with a noreturn instruction.
7233 // Elide all the `store_to_block_ptr` instructions.
7234
7235 // The break instructions need to have their operands coerced if the
7236 // switch's result location is a `ty`. In this case we overwrite the
7237 // `store_to_block_ptr` instruction with an `as` instruction and repurpose
7238 // it as the break operand.
7239 if (prong_info.body_len < 2)
7240 break :blk;
7241
7242 var store_index = end_index - 2;
7243 while (true) : (store_index -= 1) switch (zir_tags[payloads.items[store_index]]) {
7244 .dbg_block_end, .dbg_block_begin, .dbg_stmt, .dbg_var_val, .dbg_var_ptr => {},
7245 else => break,
72187246 };
7219 zir_datas[break_inst].@"break".operand = indexToRef(store_inst);
7220 } else {
7221 payloads.items[body_len_index] -= 1;
7222 astgen.extra.appendSliceAssumeCapacity(payloads.items[start_index .. end_index - 2]);
7223 astgen.extra.appendAssumeCapacity(break_inst);
7224 continue;
7225 }
7226 },
7227 .break_void => {
7228 assert(!strat.elide_store_to_block_ptr_instructions);
7229 const last_inst = payloads.items[end_index - 1];
7230 if (zir_tags[last_inst] == .@"break") {
7231 const inst_data = zir_datas[last_inst].@"break";
7232 const block_inst = astgen.extra.items[inst_data.payload_index];
7233 if (block_inst == switch_block) {
7234 zir_datas[last_inst].@"break".operand = .void_value;
7247 const store_inst = payloads.items[store_index];
7248 if (zir_tags[store_inst] != .store_to_block_ptr or
7249 zir_datas[store_inst].bin.lhs != block_scope.rl_ptr)
7250 break :blk;
7251 const break_inst = payloads.items[end_index - 1];
7252 if (block_scope.rl_ty_inst != .none) {
7253 if (pass == .breaks) {
7254 const break_data = &zir_datas[break_inst].@"break";
7255 const break_src: i32 = @bitCast(astgen.extra.items[
7256 break_data.payload_index +
7257 std.meta.fieldIndex(Zir.Inst.Break, "operand_src_node").?
7258 ]);
7259 if (break_src == Zir.Inst.Break.no_src_node) {
7260 zir_tags[store_inst] = .as;
7261 zir_datas[store_inst].bin = .{
7262 .lhs = block_scope.rl_ty_inst,
7263 .rhs = break_data.operand,
7264 };
7265 } else {
7266 zir_tags[store_inst] = .as_node;
7267 zir_datas[store_inst] = .{ .pl_node = .{
7268 .src_node = break_src,
7269 .payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.As{
7270 .dest_type = block_scope.rl_ty_inst,
7271 .operand = break_data.operand,
7272 }),
7273 } };
7274 }
7275 break_data.operand = indexToRef(store_inst);
7276 }
7277 } else {
7278 if (pass == .body) {
7279 payloads.items[body_len_index] -= 1;
7280 astgen.extra.appendSliceAssumeCapacity(
7281 payloads.items[start_index .. end_index - 2],
7282 );
7283 astgen.extra.appendAssumeCapacity(break_inst);
7284 }
7285 continue;
72357286 }
7236 }
7237 },
7238 }
7287 },
7288 .break_void => if (pass == .breaks) {
7289 assert(!strat.elide_store_to_block_ptr_instructions);
7290 const last_inst = payloads.items[end_index - 1];
7291 if (zir_tags[last_inst] == .@"break") {
7292 const break_data = &zir_datas[last_inst].@"break";
7293 const block_inst = astgen.extra.items[
7294 break_data.payload_index +
7295 std.meta.fieldIndex(Zir.Inst.Break, "block_inst").?
7296 ];
7297 if (block_inst == switch_block) break_data.operand = .void_value;
7298 }
7299 },
7300 }
72397301
7240 astgen.extra.appendSliceAssumeCapacity(payloads.items[start_index..end_index]);
7302 if (pass == .body)
7303 astgen.extra.appendSliceAssumeCapacity(payloads.items[start_index..end_index]);
7304 }
72417305 }
72427306
72437307 const block_ref = indexToRef(switch_block);
......@@ -7403,7 +7467,7 @@ fn parseBitCount(buf: []const u8) std.fmt.ParseIntError!u16 {
74037467 };
74047468
74057469 if (x != 0) x = try std.math.mul(u16, x, 10);
7406 x = try std.math.add(u16, x, @as(u16, digit));
7470 x = try std.math.add(u16, x, digit);
74077471 }
74087472
74097473 return x;
......@@ -7617,7 +7681,7 @@ fn tunnelThroughClosure(
76177681 .src_tok = ns.?.declaring_gz.?.tokenIndexToRelative(token),
76187682 } },
76197683 });
7620 gop.value_ptr.* = @as(Zir.Inst.Index, @intCast(gz.astgen.instructions.len - 1));
7684 gop.value_ptr.* = @intCast(gz.astgen.instructions.len - 1);
76217685 }
76227686
76237687 // Add an instruction to get the value from the closure into
......@@ -7697,7 +7761,7 @@ fn numberLiteral(gz: *GenZir, ri: ResultInfo, node: Ast.Node.Index, source_node:
76977761 const gpa = astgen.gpa;
76987762 var big_int = try std.math.big.int.Managed.init(gpa);
76997763 defer big_int.deinit();
7700 const prefix_offset = @as(u8, 2) * @intFromBool(base != .decimal);
7764 const prefix_offset: usize = if (base == .decimal) 0 else 2;
77017765 big_int.setString(@intFromEnum(base), bytes[prefix_offset..]) catch |err| switch (err) {
77027766 error.InvalidCharacter => unreachable, // caught in `parseNumberLiteral`
77037767 error.InvalidBase => unreachable, // we only pass 16, 8, 2, see above
......@@ -7718,7 +7782,7 @@ fn numberLiteral(gz: *GenZir, ri: ResultInfo, node: Ast.Node.Index, source_node:
77187782 };
77197783 // If the value fits into a f64 without losing any precision, store it that way.
77207784 @setFloatMode(.Strict);
7721 const smaller_float = @as(f64, @floatCast(float_number));
7785 const smaller_float: f64 = @floatCast(float_number);
77227786 const bigger_again: f128 = smaller_float;
77237787 if (bigger_again == float_number) {
77247788 const result = try gz.addFloat(smaller_float);
......@@ -7726,12 +7790,12 @@ fn numberLiteral(gz: *GenZir, ri: ResultInfo, node: Ast.Node.Index, source_node:
77267790 }
77277791 // We need to use 128 bits. Break the float into 4 u32 values so we can
77287792 // put it into the `extra` array.
7729 const int_bits = @as(u128, @bitCast(float_number));
7793 const int_bits: u128 = @bitCast(float_number);
77307794 const result = try gz.addPlNode(.float128, node, Zir.Inst.Float128{
7731 .piece0 = @as(u32, @truncate(int_bits)),
7732 .piece1 = @as(u32, @truncate(int_bits >> 32)),
7733 .piece2 = @as(u32, @truncate(int_bits >> 64)),
7734 .piece3 = @as(u32, @truncate(int_bits >> 96)),
7795 .piece0 = @truncate(int_bits),
7796 .piece1 = @truncate(int_bits >> 32),
7797 .piece2 = @truncate(int_bits >> 64),
7798 .piece3 = @truncate(int_bits >> 96),
77357799 });
77367800 return rvalue(gz, ri, result, source_node);
77377801 },
......@@ -7757,22 +7821,22 @@ fn failWithNumberError(astgen: *AstGen, err: std.zig.number_literal.Error, token
77577821 });
77587822 },
77597823 .digit_after_base => return astgen.failTok(token, "expected a digit after base prefix", .{}),
7760 .upper_case_base => |i| return astgen.failOff(token, @as(u32, @intCast(i)), "base prefix must be lowercase", .{}),
7761 .invalid_float_base => |i| return astgen.failOff(token, @as(u32, @intCast(i)), "invalid base for float literal", .{}),
7762 .repeated_underscore => |i| return astgen.failOff(token, @as(u32, @intCast(i)), "repeated digit separator", .{}),
7763 .invalid_underscore_after_special => |i| return astgen.failOff(token, @as(u32, @intCast(i)), "expected digit before digit separator", .{}),
7764 .invalid_digit => |info| return astgen.failOff(token, @as(u32, @intCast(info.i)), "invalid digit '{c}' for {s} base", .{ bytes[info.i], @tagName(info.base) }),
7765 .invalid_digit_exponent => |i| return astgen.failOff(token, @as(u32, @intCast(i)), "invalid digit '{c}' in exponent", .{bytes[i]}),
7766 .duplicate_exponent => |i| return astgen.failOff(token, @as(u32, @intCast(i)), "duplicate exponent", .{}),
7767 .exponent_after_underscore => |i| return astgen.failOff(token, @as(u32, @intCast(i)), "expected digit before exponent", .{}),
7768 .special_after_underscore => |i| return astgen.failOff(token, @as(u32, @intCast(i)), "expected digit before '{c}'", .{bytes[i]}),
7769 .trailing_special => |i| return astgen.failOff(token, @as(u32, @intCast(i)), "expected digit after '{c}'", .{bytes[i - 1]}),
7770 .trailing_underscore => |i| return astgen.failOff(token, @as(u32, @intCast(i)), "trailing digit separator", .{}),
7824 .upper_case_base => |i| return astgen.failOff(token, @intCast(i), "base prefix must be lowercase", .{}),
7825 .invalid_float_base => |i| return astgen.failOff(token, @intCast(i), "invalid base for float literal", .{}),
7826 .repeated_underscore => |i| return astgen.failOff(token, @intCast(i), "repeated digit separator", .{}),
7827 .invalid_underscore_after_special => |i| return astgen.failOff(token, @intCast(i), "expected digit before digit separator", .{}),
7828 .invalid_digit => |info| return astgen.failOff(token, @intCast(info.i), "invalid digit '{c}' for {s} base", .{ bytes[info.i], @tagName(info.base) }),
7829 .invalid_digit_exponent => |i| return astgen.failOff(token, @intCast(i), "invalid digit '{c}' in exponent", .{bytes[i]}),
7830 .duplicate_exponent => |i| return astgen.failOff(token, @intCast(i), "duplicate exponent", .{}),
7831 .exponent_after_underscore => |i| return astgen.failOff(token, @intCast(i), "expected digit before exponent", .{}),
7832 .special_after_underscore => |i| return astgen.failOff(token, @intCast(i), "expected digit before '{c}'", .{bytes[i]}),
7833 .trailing_special => |i| return astgen.failOff(token, @intCast(i), "expected digit after '{c}'", .{bytes[i - 1]}),
7834 .trailing_underscore => |i| return astgen.failOff(token, @intCast(i), "trailing digit separator", .{}),
77717835 .duplicate_period => unreachable, // Validated by tokenizer
77727836 .invalid_character => unreachable, // Validated by tokenizer
77737837 .invalid_exponent_sign => |i| {
77747838 assert(bytes.len >= 2 and bytes[0] == '0' and bytes[1] == 'x'); // Validated by tokenizer
7775 return astgen.failOff(token, @as(u32, @intCast(i)), "sign '{c}' cannot follow digit '{c}' in hex base", .{ bytes[i], bytes[i - 1] });
7839 return astgen.failOff(token, @intCast(i), "sign '{c}' cannot follow digit '{c}' in hex base", .{ bytes[i], bytes[i - 1] });
77767840 },
77777841 }
77787842}
......@@ -7839,7 +7903,7 @@ fn asmExpr(
78397903 if (output_type_bits != 0) {
78407904 return astgen.failNode(output_node, "inline assembly allows up to one output value", .{});
78417905 }
7842 output_type_bits |= @as(u32, 1) << @as(u5, @intCast(i));
7906 output_type_bits |= @as(u32, 1) << @intCast(i);
78437907 const out_type_node = node_datas[output_node].lhs;
78447908 const out_type_inst = try typeExpr(gz, scope, out_type_node);
78457909 outputs[i] = .{
......@@ -8062,7 +8126,7 @@ fn ptrCast(
80628126 node = node_datas[node].lhs;
80638127 }
80648128
8065 const flags_i = @as(u5, @bitCast(flags));
8129 const flags_i: u5 = @bitCast(flags);
80668130 assert(flags_i != 0);
80678131
80688132 const ptr_only: Zir.Inst.FullPtrCastFlags = .{ .ptr_cast = true };
......@@ -8149,8 +8213,8 @@ fn typeOf(
81498213 const body = typeof_scope.instructionsSlice();
81508214 const body_len = astgen.countBodyLenAfterFixups(body);
81518215 astgen.setExtra(payload_index, Zir.Inst.TypeOfPeer{
8152 .body_len = @as(u32, @intCast(body_len)),
8153 .body_index = @as(u32, @intCast(astgen.extra.items.len)),
8216 .body_len = @intCast(body_len),
8217 .body_index = @intCast(astgen.extra.items.len),
81548218 .src_node = gz.nodeIndexToRelative(node),
81558219 });
81568220 try astgen.extra.ensureUnusedCapacity(gpa, body_len);
......@@ -8509,7 +8573,7 @@ fn builtinCall(
85098573 .node = gz.nodeIndexToRelative(node),
85108574 .operand = operand,
85118575 });
8512 const new_index = @as(Zir.Inst.Index, @intCast(gz.astgen.instructions.len));
8576 const new_index: Zir.Inst.Index = @intCast(gz.astgen.instructions.len);
85138577 gz.astgen.instructions.appendAssumeCapacity(.{
85148578 .tag = .extended,
85158579 .data = .{ .extended = .{
......@@ -9164,7 +9228,7 @@ fn callExpr(
91649228 }
91659229 assert(node != 0);
91669230
9167 const call_index = @as(Zir.Inst.Index, @intCast(astgen.instructions.len));
9231 const call_index: Zir.Inst.Index = @intCast(astgen.instructions.len);
91689232 const call_inst = Zir.indexToRef(call_index);
91699233 try gz.astgen.instructions.append(astgen.gpa, undefined);
91709234 try gz.instructions.append(astgen.gpa, call_index);
......@@ -9188,7 +9252,7 @@ fn callExpr(
91889252 try astgen.scratch.ensureUnusedCapacity(astgen.gpa, countBodyLenAfterFixups(astgen, body));
91899253 appendBodyWithFixupsArrayList(astgen, &astgen.scratch, body);
91909254
9191 astgen.scratch.items[scratch_index] = @as(u32, @intCast(astgen.scratch.items.len - scratch_top));
9255 astgen.scratch.items[scratch_index] = @intCast(astgen.scratch.items.len - scratch_top);
91929256 scratch_index += 1;
91939257 }
91949258
......@@ -9206,8 +9270,8 @@ fn callExpr(
92069270 .callee = callee_obj,
92079271 .flags = .{
92089272 .pop_error_return_trace = !propagate_error_trace,
9209 .packed_modifier = @as(Zir.Inst.Call.Flags.PackedModifier, @intCast(@intFromEnum(modifier))),
9210 .args_len = @as(Zir.Inst.Call.Flags.PackedArgsLen, @intCast(call.ast.params.len)),
9273 .packed_modifier = @intCast(@intFromEnum(modifier)),
9274 .args_len = @intCast(call.ast.params.len),
92119275 },
92129276 });
92139277 if (call.ast.params.len != 0) {
......@@ -9227,8 +9291,8 @@ fn callExpr(
92279291 .field_name_start = callee_field.field_name_start,
92289292 .flags = .{
92299293 .pop_error_return_trace = !propagate_error_trace,
9230 .packed_modifier = @as(Zir.Inst.Call.Flags.PackedModifier, @intCast(@intFromEnum(modifier))),
9231 .args_len = @as(Zir.Inst.Call.Flags.PackedArgsLen, @intCast(call.ast.params.len)),
9294 .packed_modifier = @intCast(@intFromEnum(modifier)),
9295 .args_len = @intCast(call.ast.params.len),
92329296 },
92339297 });
92349298 if (call.ast.params.len != 0) {
......@@ -10703,14 +10767,14 @@ fn appendErrorNodeNotes(
1070310767) Allocator.Error!void {
1070410768 @setCold(true);
1070510769 const string_bytes = &astgen.string_bytes;
10706 const msg = @as(u32, @intCast(string_bytes.items.len));
10770 const msg: u32 = @intCast(string_bytes.items.len);
1070710771 try string_bytes.writer(astgen.gpa).print(format ++ "\x00", args);
1070810772 const notes_index: u32 = if (notes.len != 0) blk: {
1070910773 const notes_start = astgen.extra.items.len;
1071010774 try astgen.extra.ensureTotalCapacity(astgen.gpa, notes_start + 1 + notes.len);
10711 astgen.extra.appendAssumeCapacity(@as(u32, @intCast(notes.len)));
10775 astgen.extra.appendAssumeCapacity(@intCast(notes.len));
1071210776 astgen.extra.appendSliceAssumeCapacity(notes);
10713 break :blk @as(u32, @intCast(notes_start));
10777 break :blk @intCast(notes_start);
1071410778 } else 0;
1071510779 try astgen.compile_errors.append(astgen.gpa, .{
1071610780 .msg = msg,
......@@ -10795,14 +10859,14 @@ fn appendErrorTokNotesOff(
1079510859 @setCold(true);
1079610860 const gpa = astgen.gpa;
1079710861 const string_bytes = &astgen.string_bytes;
10798 const msg = @as(u32, @intCast(string_bytes.items.len));
10862 const msg: u32 = @intCast(string_bytes.items.len);
1079910863 try string_bytes.writer(gpa).print(format ++ "\x00", args);
1080010864 const notes_index: u32 = if (notes.len != 0) blk: {
1080110865 const notes_start = astgen.extra.items.len;
1080210866 try astgen.extra.ensureTotalCapacity(gpa, notes_start + 1 + notes.len);
10803 astgen.extra.appendAssumeCapacity(@as(u32, @intCast(notes.len)));
10867 astgen.extra.appendAssumeCapacity(@intCast(notes.len));
1080410868 astgen.extra.appendSliceAssumeCapacity(notes);
10805 break :blk @as(u32, @intCast(notes_start));
10869 break :blk @intCast(notes_start);
1080610870 } else 0;
1080710871 try astgen.compile_errors.append(gpa, .{
1080810872 .msg = msg,
......@@ -10831,7 +10895,7 @@ fn errNoteTokOff(
1083110895) Allocator.Error!u32 {
1083210896 @setCold(true);
1083310897 const string_bytes = &astgen.string_bytes;
10834 const msg = @as(u32, @intCast(string_bytes.items.len));
10898 const msg: u32 = @intCast(string_bytes.items.len);
1083510899 try string_bytes.writer(astgen.gpa).print(format ++ "\x00", args);
1083610900 return astgen.addExtra(Zir.Inst.CompileErrors.Item{
1083710901 .msg = msg,
......@@ -10850,7 +10914,7 @@ fn errNoteNode(
1085010914) Allocator.Error!u32 {
1085110915 @setCold(true);
1085210916 const string_bytes = &astgen.string_bytes;
10853 const msg = @as(u32, @intCast(string_bytes.items.len));
10917 const msg: u32 = @intCast(string_bytes.items.len);
1085410918 try string_bytes.writer(astgen.gpa).print(format ++ "\x00", args);
1085510919 return astgen.addExtra(Zir.Inst.CompileErrors.Item{
1085610920 .msg = msg,
......@@ -10864,7 +10928,7 @@ fn errNoteNode(
1086410928fn identAsString(astgen: *AstGen, ident_token: Ast.TokenIndex) !u32 {
1086510929 const gpa = astgen.gpa;
1086610930 const string_bytes = &astgen.string_bytes;
10867 const str_index = @as(u32, @intCast(string_bytes.items.len));
10931 const str_index: u32 = @intCast(string_bytes.items.len);
1086810932 try astgen.appendIdentStr(ident_token, string_bytes);
1086910933 const key: []const u8 = string_bytes.items[str_index..];
1087010934 const gop = try astgen.string_table.getOrPutContextAdapted(gpa, key, StringIndexAdapter{
......@@ -10886,7 +10950,7 @@ fn identAsString(astgen: *AstGen, ident_token: Ast.TokenIndex) !u32 {
1088610950/// `end_token` must point at the first token after the last doc coment line.
1088710951/// Returns 0 if no doc comment is present.
1088810952fn docCommentAsString(astgen: *AstGen, end_token: Ast.TokenIndex) !u32 {
10889 if (end_token == 0) return @as(u32, 0);
10953 if (end_token == 0) return 0;
1089010954
1089110955 const token_tags = astgen.tree.tokens.items(.tag);
1089210956
......@@ -10910,7 +10974,7 @@ fn docCommentAsStringFromFirst(
1091010974
1091110975 const gpa = astgen.gpa;
1091210976 const string_bytes = &astgen.string_bytes;
10913 const str_index = @as(u32, @intCast(string_bytes.items.len));
10977 const str_index: u32 = @intCast(string_bytes.items.len);
1091410978 const token_starts = astgen.tree.tokens.items(.start);
1091510979 const token_tags = astgen.tree.tokens.items(.tag);
1091610980
......@@ -10931,8 +10995,8 @@ fn docCommentAsStringFromFirst(
1093110995 }
1093210996 }
1093310997
10934 const key = string_bytes.items[str_index..];
10935 const gop = try astgen.string_table.getOrPutContextAdapted(gpa, @as([]const u8, key), StringIndexAdapter{
10998 const key: []const u8 = string_bytes.items[str_index..];
10999 const gop = try astgen.string_table.getOrPutContextAdapted(gpa, key, StringIndexAdapter{
1093611000 .bytes = string_bytes,
1093711001 }, StringIndexContext{
1093811002 .bytes = string_bytes,
......@@ -10953,11 +11017,11 @@ const IndexSlice = struct { index: u32, len: u32 };
1095311017fn strLitAsString(astgen: *AstGen, str_lit_token: Ast.TokenIndex) !IndexSlice {
1095411018 const gpa = astgen.gpa;
1095511019 const string_bytes = &astgen.string_bytes;
10956 const str_index = @as(u32, @intCast(string_bytes.items.len));
11020 const str_index: u32 = @intCast(string_bytes.items.len);
1095711021 const token_bytes = astgen.tree.tokenSlice(str_lit_token);
1095811022 try astgen.parseStrLit(str_lit_token, string_bytes, token_bytes, 0);
10959 const key = string_bytes.items[str_index..];
10960 const gop = try astgen.string_table.getOrPutContextAdapted(gpa, @as([]const u8, key), StringIndexAdapter{
11023 const key: []const u8 = string_bytes.items[str_index..];
11024 const gop = try astgen.string_table.getOrPutContextAdapted(gpa, key, StringIndexAdapter{
1096111025 .bytes = string_bytes,
1096211026 }, StringIndexContext{
1096311027 .bytes = string_bytes,
......@@ -10966,7 +11030,7 @@ fn strLitAsString(astgen: *AstGen, str_lit_token: Ast.TokenIndex) !IndexSlice {
1096611030 string_bytes.shrinkRetainingCapacity(str_index);
1096711031 return IndexSlice{
1096811032 .index = gop.key_ptr.*,
10969 .len = @as(u32, @intCast(key.len)),
11033 .len = @intCast(key.len),
1097011034 };
1097111035 } else {
1097211036 gop.key_ptr.* = str_index;
......@@ -10976,7 +11040,7 @@ fn strLitAsString(astgen: *AstGen, str_lit_token: Ast.TokenIndex) !IndexSlice {
1097611040 try string_bytes.append(gpa, 0);
1097711041 return IndexSlice{
1097811042 .index = str_index,
10979 .len = @as(u32, @intCast(key.len)),
11043 .len = @intCast(key.len),
1098011044 };
1098111045 }
1098211046}
......@@ -11013,15 +11077,15 @@ fn strLitNodeAsString(astgen: *AstGen, node: Ast.Node.Index) !IndexSlice {
1101311077 const len = string_bytes.items.len - str_index;
1101411078 try string_bytes.append(gpa, 0);
1101511079 return IndexSlice{
11016 .index = @as(u32, @intCast(str_index)),
11017 .len = @as(u32, @intCast(len)),
11080 .index = @intCast(str_index),
11081 .len = @intCast(len),
1101811082 };
1101911083}
1102011084
1102111085fn testNameString(astgen: *AstGen, str_lit_token: Ast.TokenIndex) !u32 {
1102211086 const gpa = astgen.gpa;
1102311087 const string_bytes = &astgen.string_bytes;
11024 const str_index = @as(u32, @intCast(string_bytes.items.len));
11088 const str_index: u32 = @intCast(string_bytes.items.len);
1102511089 const token_bytes = astgen.tree.tokenSlice(str_lit_token);
1102611090 try string_bytes.append(gpa, 0); // Indicates this is a test.
1102711091 try astgen.parseStrLit(str_lit_token, string_bytes, token_bytes, 0);
......@@ -11340,7 +11404,10 @@ const GenZir = struct {
1134011404 parent_gz.instructions.items.len -= src - dst;
1134111405 as_scope.instructions_top = GenZir.unstacked_top;
1134211406 // as_scope now unstacked, can add new instructions to parent_gz
11343 const casted_result = try parent_gz.addBin(.as, dest_type, result);
11407 const casted_result = try parent_gz.addPlNode(.as_node, src_node, Zir.Inst.As{
11408 .dest_type = dest_type,
11409 .operand = result,
11410 });
1134411411 return rvalue(parent_gz, ri, casted_result, src_node);
1134511412 } else {
1134611413 // implicitly move all as_scope instructions to parent_gz
......@@ -11530,7 +11597,7 @@ const GenZir = struct {
1153011597 const astgen = gz.astgen;
1153111598 const gpa = astgen.gpa;
1153211599 const ret_ref = if (args.ret_ref == .void_type) .none else args.ret_ref;
11533 const new_index = @as(Zir.Inst.Index, @intCast(astgen.instructions.len));
11600 const new_index: Zir.Inst.Index = @intCast(astgen.instructions.len);
1153411601
1153511602 try astgen.instructions.ensureUnusedCapacity(gpa, 1);
1153611603
......@@ -11548,8 +11615,8 @@ const GenZir = struct {
1154811615 const block = node_datas[fn_decl].rhs;
1154911616 const rbrace_start = token_starts[tree.lastToken(block)];
1155011617 astgen.advanceSourceCursor(rbrace_start);
11551 const rbrace_line = @as(u32, @intCast(astgen.source_line - gz.decl_line));
11552 const rbrace_column = @as(u32, @intCast(astgen.source_column));
11618 const rbrace_line: u32 = @intCast(astgen.source_line - gz.decl_line);
11619 const rbrace_column: u32 = @intCast(astgen.source_column);
1155311620
1155411621 const columns = args.lbrace_column | (rbrace_column << 16);
1155511622 src_locs_buffer[0] = args.lbrace_line;
......@@ -11626,40 +11693,47 @@ const GenZir = struct {
1162611693 if (align_body.len != 0) {
1162711694 astgen.extra.appendAssumeCapacity(countBodyLenAfterFixups(astgen, align_body));
1162811695 astgen.appendBodyWithFixups(align_body);
11629 const inst_data = zir_datas[align_body[align_body.len - 1]].@"break";
11630 astgen.extra.items[inst_data.payload_index] = new_index;
11696 const break_extra = zir_datas[align_body[align_body.len - 1]].@"break".payload_index;
11697 astgen.extra.items[break_extra + std.meta.fieldIndex(Zir.Inst.Break, "block_inst").?] =
11698 new_index;
1163111699 } else if (args.align_ref != .none) {
1163211700 astgen.extra.appendAssumeCapacity(@intFromEnum(args.align_ref));
1163311701 }
1163411702 if (addrspace_body.len != 0) {
1163511703 astgen.extra.appendAssumeCapacity(countBodyLenAfterFixups(astgen, addrspace_body));
1163611704 astgen.appendBodyWithFixups(addrspace_body);
11637 const inst_data = zir_datas[addrspace_body[addrspace_body.len - 1]].@"break";
11638 astgen.extra.items[inst_data.payload_index] = new_index;
11705 const break_extra =
11706 zir_datas[addrspace_body[addrspace_body.len - 1]].@"break".payload_index;
11707 astgen.extra.items[break_extra + std.meta.fieldIndex(Zir.Inst.Break, "block_inst").?] =
11708 new_index;
1163911709 } else if (args.addrspace_ref != .none) {
1164011710 astgen.extra.appendAssumeCapacity(@intFromEnum(args.addrspace_ref));
1164111711 }
1164211712 if (section_body.len != 0) {
1164311713 astgen.extra.appendAssumeCapacity(countBodyLenAfterFixups(astgen, section_body));
1164411714 astgen.appendBodyWithFixups(section_body);
11645 const inst_data = zir_datas[section_body[section_body.len - 1]].@"break";
11646 astgen.extra.items[inst_data.payload_index] = new_index;
11715 const break_extra =
11716 zir_datas[section_body[section_body.len - 1]].@"break".payload_index;
11717 astgen.extra.items[break_extra + std.meta.fieldIndex(Zir.Inst.Break, "block_inst").?] =
11718 new_index;
1164711719 } else if (args.section_ref != .none) {
1164811720 astgen.extra.appendAssumeCapacity(@intFromEnum(args.section_ref));
1164911721 }
1165011722 if (cc_body.len != 0) {
1165111723 astgen.extra.appendAssumeCapacity(countBodyLenAfterFixups(astgen, cc_body));
1165211724 astgen.appendBodyWithFixups(cc_body);
11653 const inst_data = zir_datas[cc_body[cc_body.len - 1]].@"break";
11654 astgen.extra.items[inst_data.payload_index] = new_index;
11725 const break_extra = zir_datas[cc_body[cc_body.len - 1]].@"break".payload_index;
11726 astgen.extra.items[break_extra + std.meta.fieldIndex(Zir.Inst.Break, "block_inst").?] =
11727 new_index;
1165511728 } else if (args.cc_ref != .none) {
1165611729 astgen.extra.appendAssumeCapacity(@intFromEnum(args.cc_ref));
1165711730 }
1165811731 if (ret_body.len != 0) {
1165911732 astgen.extra.appendAssumeCapacity(countBodyLenAfterFixups(astgen, ret_body));
1166011733 astgen.appendBodyWithFixups(ret_body);
11661 const inst_data = zir_datas[ret_body[ret_body.len - 1]].@"break";
11662 astgen.extra.items[inst_data.payload_index] = new_index;
11734 const break_extra = zir_datas[ret_body[ret_body.len - 1]].@"break".payload_index;
11735 astgen.extra.items[break_extra + std.meta.fieldIndex(Zir.Inst.Break, "block_inst").?] =
11736 new_index;
1166311737 } else if (ret_ref != .none) {
1166411738 astgen.extra.appendAssumeCapacity(@intFromEnum(ret_ref));
1166511739 }
......@@ -11714,8 +11788,9 @@ const GenZir = struct {
1171411788 if (ret_body.len != 0) {
1171511789 astgen.appendBodyWithFixups(ret_body);
1171611790
11717 const inst_data = zir_datas[ret_body[ret_body.len - 1]].@"break";
11718 astgen.extra.items[inst_data.payload_index] = new_index;
11791 const break_extra = zir_datas[ret_body[ret_body.len - 1]].@"break".payload_index;
11792 astgen.extra.items[break_extra + std.meta.fieldIndex(Zir.Inst.Break, "block_inst").?] =
11793 new_index;
1171911794 } else if (ret_ref != .none) {
1172011795 astgen.extra.appendAssumeCapacity(@intFromEnum(ret_ref));
1172111796 }
......@@ -11785,18 +11860,18 @@ const GenZir = struct {
1178511860 astgen.extra.appendAssumeCapacity(@intFromEnum(args.init));
1178611861 }
1178711862
11788 const new_index = @as(Zir.Inst.Index, @intCast(astgen.instructions.len));
11863 const new_index: Zir.Inst.Index = @intCast(astgen.instructions.len);
1178911864 astgen.instructions.appendAssumeCapacity(.{
1179011865 .tag = .extended,
1179111866 .data = .{ .extended = .{
1179211867 .opcode = .variable,
11793 .small = @as(u16, @bitCast(Zir.Inst.ExtendedVar.Small{
11868 .small = @bitCast(Zir.Inst.ExtendedVar.Small{
1179411869 .has_lib_name = args.lib_name != 0,
1179511870 .has_align = args.align_inst != .none,
1179611871 .has_init = args.init != .none,
1179711872 .is_extern = args.is_extern,
1179811873 .is_threadlocal = args.is_threadlocal,
11799 })),
11874 }),
1180011875 .operand = payload_index,
1180111876 } },
1180211877 });
......@@ -11816,7 +11891,7 @@ const GenZir = struct {
1181611891 try gz.instructions.ensureUnusedCapacity(gpa, 1);
1181711892 try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1);
1181811893
11819 const new_index = @as(Zir.Inst.Index, @intCast(gz.astgen.instructions.len));
11894 const new_index: Zir.Inst.Index = @intCast(gz.astgen.instructions.len);
1182011895 gz.astgen.instructions.appendAssumeCapacity(.{
1182111896 .tag = tag,
1182211897 .data = .{ .bool_br = .{
......@@ -11842,12 +11917,12 @@ const GenZir = struct {
1184211917 try astgen.instructions.ensureUnusedCapacity(gpa, 1);
1184311918 try astgen.string_bytes.ensureUnusedCapacity(gpa, @sizeOf(std.math.big.Limb) * limbs.len);
1184411919
11845 const new_index = @as(Zir.Inst.Index, @intCast(astgen.instructions.len));
11920 const new_index: Zir.Inst.Index = @intCast(astgen.instructions.len);
1184611921 astgen.instructions.appendAssumeCapacity(.{
1184711922 .tag = .int_big,
1184811923 .data = .{ .str = .{
11849 .start = @as(u32, @intCast(astgen.string_bytes.items.len)),
11850 .len = @as(u32, @intCast(limbs.len)),
11924 .start = @intCast(astgen.string_bytes.items.len),
11925 .len = @intCast(limbs.len),
1185111926 } },
1185211927 });
1185311928 gz.instructions.appendAssumeCapacity(new_index);
......@@ -11887,7 +11962,7 @@ const GenZir = struct {
1188711962 src_node: Ast.Node.Index,
1188811963 ) !Zir.Inst.Index {
1188911964 assert(operand != .none);
11890 const new_index = @as(Zir.Inst.Index, @intCast(gz.astgen.instructions.len));
11965 const new_index: Zir.Inst.Index = @intCast(gz.astgen.instructions.len);
1189111966 try gz.astgen.instructions.append(gz.astgen.gpa, .{
1189211967 .tag = tag,
1189311968 .data = .{ .un_node = .{
......@@ -11910,7 +11985,7 @@ const GenZir = struct {
1191011985 try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1);
1191111986
1191211987 const payload_index = try gz.astgen.addExtra(extra);
11913 const new_index = @as(Zir.Inst.Index, @intCast(gz.astgen.instructions.len));
11988 const new_index: Zir.Inst.Index = @intCast(gz.astgen.instructions.len);
1191411989 gz.astgen.instructions.appendAssumeCapacity(.{
1191511990 .tag = tag,
1191611991 .data = .{ .pl_node = .{
......@@ -11962,12 +12037,12 @@ const GenZir = struct {
1196212037 const payload_index = gz.astgen.addExtraAssumeCapacity(Zir.Inst.Param{
1196312038 .name = name,
1196412039 .doc_comment = doc_comment_index,
11965 .body_len = @as(u32, @intCast(body_len)),
12040 .body_len = @intCast(body_len),
1196612041 });
1196712042 gz.astgen.appendBodyWithFixups(param_body);
1196812043 param_gz.unstack();
1196912044
11970 const new_index = @as(Zir.Inst.Index, @intCast(gz.astgen.instructions.len));
12045 const new_index: Zir.Inst.Index = @intCast(gz.astgen.instructions.len);
1197112046 gz.astgen.instructions.appendAssumeCapacity(.{
1197212047 .tag = tag,
1197312048 .data = .{ .pl_tok = .{
......@@ -11995,7 +12070,7 @@ const GenZir = struct {
1199512070 try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1);
1199612071
1199712072 const payload_index = try gz.astgen.addExtra(extra);
11998 const new_index = @as(Zir.Inst.Index, @intCast(gz.astgen.instructions.len));
12073 const new_index: Zir.Inst.Index = @intCast(gz.astgen.instructions.len);
1199912074 gz.astgen.instructions.appendAssumeCapacity(.{
1200012075 .tag = .extended,
1200112076 .data = .{ .extended = .{
......@@ -12027,12 +12102,12 @@ const GenZir = struct {
1202712102 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.NodeMultiOp{
1202812103 .src_node = gz.nodeIndexToRelative(node),
1202912104 });
12030 const new_index = @as(Zir.Inst.Index, @intCast(astgen.instructions.len));
12105 const new_index: Zir.Inst.Index = @intCast(astgen.instructions.len);
1203112106 astgen.instructions.appendAssumeCapacity(.{
1203212107 .tag = .extended,
1203312108 .data = .{ .extended = .{
1203412109 .opcode = opcode,
12035 .small = @as(u16, @intCast(operands.len)),
12110 .small = @intCast(operands.len),
1203612111 .operand = payload_index,
1203712112 } },
1203812113 });
......@@ -12052,12 +12127,12 @@ const GenZir = struct {
1205212127
1205312128 try gz.instructions.ensureUnusedCapacity(gpa, 1);
1205412129 try astgen.instructions.ensureUnusedCapacity(gpa, 1);
12055 const new_index = @as(Zir.Inst.Index, @intCast(astgen.instructions.len));
12130 const new_index: Zir.Inst.Index = @intCast(astgen.instructions.len);
1205612131 astgen.instructions.appendAssumeCapacity(.{
1205712132 .tag = .extended,
1205812133 .data = .{ .extended = .{
1205912134 .opcode = opcode,
12060 .small = @as(u16, @intCast(trailing_len)),
12135 .small = @intCast(trailing_len),
1206112136 .operand = payload_index,
1206212137 } },
1206312138 });
......@@ -12090,7 +12165,7 @@ const GenZir = struct {
1209012165 abs_tok_index: Ast.TokenIndex,
1209112166 ) !Zir.Inst.Index {
1209212167 const astgen = gz.astgen;
12093 const new_index = @as(Zir.Inst.Index, @intCast(astgen.instructions.len));
12168 const new_index: Zir.Inst.Index = @intCast(astgen.instructions.len);
1209412169 assert(operand != .none);
1209512170 try astgen.instructions.append(astgen.gpa, .{
1209612171 .tag = tag,
......@@ -12169,21 +12244,8 @@ const GenZir = struct {
1216912244 ) !Zir.Inst.Index {
1217012245 const gpa = gz.astgen.gpa;
1217112246 try gz.instructions.ensureUnusedCapacity(gpa, 1);
12172 try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1);
1217312247
12174 const extra: Zir.Inst.Break = .{
12175 .block_inst = block_inst,
12176 .operand_src_node = Zir.Inst.Break.no_src_node,
12177 };
12178 const payload_index = try gz.astgen.addExtra(extra);
12179 const new_index = @as(Zir.Inst.Index, @intCast(gz.astgen.instructions.len));
12180 gz.astgen.instructions.appendAssumeCapacity(.{
12181 .tag = tag,
12182 .data = .{ .@"break" = .{
12183 .operand = operand,
12184 .payload_index = payload_index,
12185 } },
12186 });
12248 const new_index = try gz.makeBreak(tag, block_inst, operand);
1218712249 gz.instructions.appendAssumeCapacity(new_index);
1218812250 return new_index;
1218912251 }
......@@ -12194,23 +12256,7 @@ const GenZir = struct {
1219412256 block_inst: Zir.Inst.Index,
1219512257 operand: Zir.Inst.Ref,
1219612258 ) !Zir.Inst.Index {
12197 const gpa = gz.astgen.gpa;
12198 try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1);
12199
12200 const extra: Zir.Inst.Break = .{
12201 .block_inst = block_inst,
12202 .operand_src_node = Zir.Inst.Break.no_src_node,
12203 };
12204 const payload_index = try gz.astgen.addExtra(extra);
12205 const new_index = @as(Zir.Inst.Index, @intCast(gz.astgen.instructions.len));
12206 gz.astgen.instructions.appendAssumeCapacity(.{
12207 .tag = tag,
12208 .data = .{ .@"break" = .{
12209 .operand = operand,
12210 .payload_index = payload_index,
12211 } },
12212 });
12213 return new_index;
12259 return gz.makeBreakCommon(tag, block_inst, operand, null);
1221412260 }
1221512261
1221612262 fn addBreakWithSrcNode(
......@@ -12222,21 +12268,8 @@ const GenZir = struct {
1222212268 ) !Zir.Inst.Index {
1222312269 const gpa = gz.astgen.gpa;
1222412270 try gz.instructions.ensureUnusedCapacity(gpa, 1);
12225 try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1);
1222612271
12227 const extra: Zir.Inst.Break = .{
12228 .block_inst = block_inst,
12229 .operand_src_node = gz.nodeIndexToRelative(operand_src_node),
12230 };
12231 const payload_index = try gz.astgen.addExtra(extra);
12232 const new_index = @as(Zir.Inst.Index, @intCast(gz.astgen.instructions.len));
12233 gz.astgen.instructions.appendAssumeCapacity(.{
12234 .tag = tag,
12235 .data = .{ .@"break" = .{
12236 .operand = operand,
12237 .payload_index = payload_index,
12238 } },
12239 });
12272 const new_index = try gz.makeBreakWithSrcNode(tag, block_inst, operand, operand_src_node);
1224012273 gz.instructions.appendAssumeCapacity(new_index);
1224112274 return new_index;
1224212275 }
......@@ -12247,21 +12280,33 @@ const GenZir = struct {
1224712280 block_inst: Zir.Inst.Index,
1224812281 operand: Zir.Inst.Ref,
1224912282 operand_src_node: Ast.Node.Index,
12283 ) !Zir.Inst.Index {
12284 return gz.makeBreakCommon(tag, block_inst, operand, operand_src_node);
12285 }
12286
12287 fn makeBreakCommon(
12288 gz: *GenZir,
12289 tag: Zir.Inst.Tag,
12290 block_inst: Zir.Inst.Index,
12291 operand: Zir.Inst.Ref,
12292 operand_src_node: ?Ast.Node.Index,
1225012293 ) !Zir.Inst.Index {
1225112294 const gpa = gz.astgen.gpa;
1225212295 try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1);
12296 try gz.astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.Break).Struct.fields.len);
1225312297
12254 const extra: Zir.Inst.Break = .{
12255 .block_inst = block_inst,
12256 .operand_src_node = gz.nodeIndexToRelative(operand_src_node),
12257 };
12258 const payload_index = try gz.astgen.addExtra(extra);
12259 const new_index = @as(Zir.Inst.Index, @intCast(gz.astgen.instructions.len));
12298 const new_index: Zir.Inst.Index = @intCast(gz.astgen.instructions.len);
1226012299 gz.astgen.instructions.appendAssumeCapacity(.{
1226112300 .tag = tag,
1226212301 .data = .{ .@"break" = .{
1226312302 .operand = operand,
12264 .payload_index = payload_index,
12303 .payload_index = gz.astgen.addExtraAssumeCapacity(Zir.Inst.Break{
12304 .operand_src_node = if (operand_src_node) |src_node|
12305 gz.nodeIndexToRelative(src_node)
12306 else
12307 Zir.Inst.Break.no_src_node,
12308 .block_inst = block_inst,
12309 }),
1226512310 } },
1226612311 });
1226712312 return new_index;
......@@ -12348,7 +12393,7 @@ const GenZir = struct {
1234812393 .data = .{ .extended = .{
1234912394 .opcode = opcode,
1235012395 .small = undefined,
12351 .operand = @as(u32, @bitCast(gz.nodeIndexToRelative(src_node))),
12396 .operand = @bitCast(gz.nodeIndexToRelative(src_node)),
1235212397 } },
1235312398 });
1235412399 }
......@@ -12372,8 +12417,8 @@ const GenZir = struct {
1237212417 try astgen.extra.ensureUnusedCapacity(
1237312418 gpa,
1237412419 @typeInfo(Zir.Inst.AllocExtended).Struct.fields.len +
12375 @as(usize, @intFromBool(args.type_inst != .none)) +
12376 @as(usize, @intFromBool(args.align_inst != .none)),
12420 @intFromBool(args.type_inst != .none) +
12421 @intFromBool(args.align_inst != .none),
1237712422 );
1237812423 const payload_index = gz.astgen.addExtraAssumeCapacity(Zir.Inst.AllocExtended{
1237912424 .src_node = gz.nodeIndexToRelative(args.node),
......@@ -12391,7 +12436,7 @@ const GenZir = struct {
1239112436 const is_comptime: u4 = @intFromBool(args.is_comptime);
1239212437 const small: u16 = has_type | (has_align << 1) | (is_const << 2) | (is_comptime << 3);
1239312438
12394 const new_index = @as(Zir.Inst.Index, @intCast(astgen.instructions.len));
12439 const new_index: Zir.Inst.Index = @intCast(astgen.instructions.len);
1239512440 astgen.instructions.appendAssumeCapacity(.{
1239612441 .tag = .extended,
1239712442 .data = .{ .extended = .{
......@@ -12450,7 +12495,7 @@ const GenZir = struct {
1245012495 @as(u16, @intCast(args.clobbers.len << 10)) |
1245112496 (@as(u16, @intFromBool(args.is_volatile)) << 15);
1245212497
12453 const new_index = @as(Zir.Inst.Index, @intCast(astgen.instructions.len));
12498 const new_index: Zir.Inst.Index = @intCast(astgen.instructions.len);
1245412499 astgen.instructions.appendAssumeCapacity(.{
1245512500 .tag = .extended,
1245612501 .data = .{ .extended = .{
......@@ -12467,7 +12512,7 @@ const GenZir = struct {
1246712512 /// Does *not* append the block instruction to the scope.
1246812513 /// Leaves the `payload_index` field undefined.
1246912514 fn makeBlockInst(gz: *GenZir, tag: Zir.Inst.Tag, node: Ast.Node.Index) !Zir.Inst.Index {
12470 const new_index = @as(Zir.Inst.Index, @intCast(gz.astgen.instructions.len));
12515 const new_index: Zir.Inst.Index = @intCast(gz.astgen.instructions.len);
1247112516 const gpa = gz.astgen.gpa;
1247212517 try gz.astgen.instructions.append(gpa, .{
1247312518 .tag = tag,
......@@ -12484,7 +12529,7 @@ const GenZir = struct {
1248412529 fn addCondBr(gz: *GenZir, tag: Zir.Inst.Tag, node: Ast.Node.Index) !Zir.Inst.Index {
1248512530 const gpa = gz.astgen.gpa;
1248612531 try gz.instructions.ensureUnusedCapacity(gpa, 1);
12487 const new_index = @as(Zir.Inst.Index, @intCast(gz.astgen.instructions.len));
12532 const new_index: Zir.Inst.Index = @intCast(gz.astgen.instructions.len);
1248812533 try gz.astgen.instructions.append(gpa, .{
1248912534 .tag = tag,
1249012535 .data = .{ .pl_node = .{
......@@ -12511,11 +12556,11 @@ const GenZir = struct {
1251112556 const gpa = astgen.gpa;
1251212557
1251312558 try astgen.extra.ensureUnusedCapacity(gpa, 6);
12514 const payload_index = @as(u32, @intCast(astgen.extra.items.len));
12559 const payload_index: u32 = @intCast(astgen.extra.items.len);
1251512560
1251612561 if (args.src_node != 0) {
1251712562 const node_offset = gz.nodeIndexToRelative(args.src_node);
12518 astgen.extra.appendAssumeCapacity(@as(u32, @bitCast(node_offset)));
12563 astgen.extra.appendAssumeCapacity(@bitCast(node_offset));
1251912564 }
1252012565 if (args.fields_len != 0) {
1252112566 astgen.extra.appendAssumeCapacity(args.fields_len);
......@@ -12533,7 +12578,7 @@ const GenZir = struct {
1253312578 .tag = .extended,
1253412579 .data = .{ .extended = .{
1253512580 .opcode = .struct_decl,
12536 .small = @as(u16, @bitCast(Zir.Inst.StructDecl.Small{
12581 .small = @bitCast(Zir.Inst.StructDecl.Small{
1253712582 .has_src_node = args.src_node != 0,
1253812583 .has_fields_len = args.fields_len != 0,
1253912584 .has_decls_len = args.decls_len != 0,
......@@ -12543,7 +12588,7 @@ const GenZir = struct {
1254312588 .is_tuple = args.is_tuple,
1254412589 .name_strategy = gz.anon_name_strategy,
1254512590 .layout = args.layout,
12546 })),
12591 }),
1254712592 .operand = payload_index,
1254812593 } },
1254912594 });
......@@ -12562,11 +12607,11 @@ const GenZir = struct {
1256212607 const gpa = astgen.gpa;
1256312608
1256412609 try astgen.extra.ensureUnusedCapacity(gpa, 5);
12565 const payload_index = @as(u32, @intCast(astgen.extra.items.len));
12610 const payload_index: u32 = @intCast(astgen.extra.items.len);
1256612611
1256712612 if (args.src_node != 0) {
1256812613 const node_offset = gz.nodeIndexToRelative(args.src_node);
12569 astgen.extra.appendAssumeCapacity(@as(u32, @bitCast(node_offset)));
12614 astgen.extra.appendAssumeCapacity(@bitCast(node_offset));
1257012615 }
1257112616 if (args.tag_type != .none) {
1257212617 astgen.extra.appendAssumeCapacity(@intFromEnum(args.tag_type));
......@@ -12584,7 +12629,7 @@ const GenZir = struct {
1258412629 .tag = .extended,
1258512630 .data = .{ .extended = .{
1258612631 .opcode = .union_decl,
12587 .small = @as(u16, @bitCast(Zir.Inst.UnionDecl.Small{
12632 .small = @bitCast(Zir.Inst.UnionDecl.Small{
1258812633 .has_src_node = args.src_node != 0,
1258912634 .has_tag_type = args.tag_type != .none,
1259012635 .has_body_len = args.body_len != 0,
......@@ -12593,7 +12638,7 @@ const GenZir = struct {
1259312638 .name_strategy = gz.anon_name_strategy,
1259412639 .layout = args.layout,
1259512640 .auto_enum_tag = args.auto_enum_tag,
12596 })),
12641 }),
1259712642 .operand = payload_index,
1259812643 } },
1259912644 });
......@@ -12611,11 +12656,11 @@ const GenZir = struct {
1261112656 const gpa = astgen.gpa;
1261212657
1261312658 try astgen.extra.ensureUnusedCapacity(gpa, 5);
12614 const payload_index = @as(u32, @intCast(astgen.extra.items.len));
12659 const payload_index: u32 = @intCast(astgen.extra.items.len);
1261512660
1261612661 if (args.src_node != 0) {
1261712662 const node_offset = gz.nodeIndexToRelative(args.src_node);
12618 astgen.extra.appendAssumeCapacity(@as(u32, @bitCast(node_offset)));
12663 astgen.extra.appendAssumeCapacity(@bitCast(node_offset));
1261912664 }
1262012665 if (args.tag_type != .none) {
1262112666 astgen.extra.appendAssumeCapacity(@intFromEnum(args.tag_type));
......@@ -12633,7 +12678,7 @@ const GenZir = struct {
1263312678 .tag = .extended,
1263412679 .data = .{ .extended = .{
1263512680 .opcode = .enum_decl,
12636 .small = @as(u16, @bitCast(Zir.Inst.EnumDecl.Small{
12681 .small = @bitCast(Zir.Inst.EnumDecl.Small{
1263712682 .has_src_node = args.src_node != 0,
1263812683 .has_tag_type = args.tag_type != .none,
1263912684 .has_body_len = args.body_len != 0,
......@@ -12641,7 +12686,7 @@ const GenZir = struct {
1264112686 .has_decls_len = args.decls_len != 0,
1264212687 .name_strategy = gz.anon_name_strategy,
1264312688 .nonexhaustive = args.nonexhaustive,
12644 })),
12689 }),
1264512690 .operand = payload_index,
1264612691 } },
1264712692 });
......@@ -12655,11 +12700,11 @@ const GenZir = struct {
1265512700 const gpa = astgen.gpa;
1265612701
1265712702 try astgen.extra.ensureUnusedCapacity(gpa, 2);
12658 const payload_index = @as(u32, @intCast(astgen.extra.items.len));
12703 const payload_index: u32 = @intCast(astgen.extra.items.len);
1265912704
1266012705 if (args.src_node != 0) {
1266112706 const node_offset = gz.nodeIndexToRelative(args.src_node);
12662 astgen.extra.appendAssumeCapacity(@as(u32, @bitCast(node_offset)));
12707 astgen.extra.appendAssumeCapacity(@bitCast(node_offset));
1266312708 }
1266412709 if (args.decls_len != 0) {
1266512710 astgen.extra.appendAssumeCapacity(args.decls_len);
......@@ -12668,11 +12713,11 @@ const GenZir = struct {
1266812713 .tag = .extended,
1266912714 .data = .{ .extended = .{
1267012715 .opcode = .opaque_decl,
12671 .small = @as(u16, @bitCast(Zir.Inst.OpaqueDecl.Small{
12716 .small = @bitCast(Zir.Inst.OpaqueDecl.Small{
1267212717 .has_src_node = args.src_node != 0,
1267312718 .has_decls_len = args.decls_len != 0,
1267412719 .name_strategy = gz.anon_name_strategy,
12675 })),
12720 }),
1267612721 .operand = payload_index,
1267712722 } },
1267812723 });
......@@ -12687,7 +12732,7 @@ const GenZir = struct {
1268712732 try gz.instructions.ensureUnusedCapacity(gpa, 1);
1268812733 try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1);
1268912734
12690 const new_index = @as(Zir.Inst.Index, @intCast(gz.astgen.instructions.len));
12735 const new_index: Zir.Inst.Index = @intCast(gz.astgen.instructions.len);
1269112736 gz.astgen.instructions.appendAssumeCapacity(inst);
1269212737 gz.instructions.appendAssumeCapacity(new_index);
1269312738 return new_index;
......@@ -12698,7 +12743,7 @@ const GenZir = struct {
1269812743 try gz.instructions.ensureUnusedCapacity(gpa, 1);
1269912744 try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1);
1270012745
12701 const new_index = @as(Zir.Inst.Index, @intCast(gz.astgen.instructions.len));
12746 const new_index: Zir.Inst.Index = @intCast(gz.astgen.instructions.len);
1270212747 gz.astgen.instructions.len += 1;
1270312748 gz.instructions.appendAssumeCapacity(new_index);
1270412749 return new_index;
......@@ -12750,7 +12795,7 @@ const GenZir = struct {
1275012795 return;
1275112796 }
1275212797
12753 const new_index = @as(Zir.Inst.Index, @intCast(gz.astgen.instructions.len));
12798 const new_index: Zir.Inst.Index = @intCast(gz.astgen.instructions.len);
1275412799 try gz.astgen.instructions.append(gpa, .{ .tag = .dbg_block_end, .data = undefined });
1275512800 try gz.instructions.append(gpa, new_index);
1275612801 }
......@@ -12759,7 +12804,7 @@ const GenZir = struct {
1275912804/// This can only be for short-lived references; the memory becomes invalidated
1276012805/// when another string is added.
1276112806fn nullTerminatedString(astgen: AstGen, index: usize) [*:0]const u8 {
12762 return @as([*:0]const u8, @ptrCast(astgen.string_bytes.items.ptr)) + index;
12807 return @ptrCast(astgen.string_bytes.items[index..]);
1276312808}
1276412809
1276512810/// Local variables shadowing detection, including function parameters.
......@@ -13038,7 +13083,7 @@ fn isInferred(astgen: *AstGen, ref: Zir.Inst.Ref) bool {
1303813083 .extended => {
1303913084 const zir_data = astgen.instructions.items(.data);
1304013085 if (zir_data[inst].extended.opcode != .alloc) return false;
13041 const small = @as(Zir.Inst.AllocExtended.Small, @bitCast(zir_data[inst].extended.small));
13086 const small: Zir.Inst.AllocExtended.Small = @bitCast(zir_data[inst].extended.small);
1304213087 return !small.has_type;
1304313088 },
1304413089
......@@ -13082,7 +13127,7 @@ fn countBodyLenAfterFixups(astgen: *AstGen, body: []const Zir.Inst.Index) u32 {
1308213127 check_inst = ref_inst;
1308313128 }
1308413129 }
13085 return @as(u32, @intCast(count));
13130 return @intCast(count);
1308613131}
1308713132
1308813133fn emitDbgStmt(gz: *GenZir, lc: LineColumn) !void {
......@@ -13114,7 +13159,7 @@ fn lowerAstErrors(astgen: *AstGen) !void {
1311413159
1311513160 if (token_tags[parse_err.token + @intFromBool(parse_err.token_is_prev)] == .invalid) {
1311613161 const tok = parse_err.token + @intFromBool(parse_err.token_is_prev);
13117 const bad_off = @as(u32, @intCast(tree.tokenSlice(parse_err.token + @intFromBool(parse_err.token_is_prev)).len));
13162 const bad_off: u32 = @intCast(tree.tokenSlice(parse_err.token + @intFromBool(parse_err.token_is_prev)).len);
1311813163 const byte_abs = token_starts[parse_err.token + @intFromBool(parse_err.token_is_prev)] + bad_off;
1311913164 try notes.append(gpa, try astgen.errNoteTokOff(tok, bad_off, "invalid byte: '{'}'", .{
1312013165 std.zig.fmtEscapes(tree.source[byte_abs..][0..1]),
src/Zir.zig+21-21
......@@ -78,12 +78,13 @@ pub fn extraData(code: Zir, comptime T: type, index: usize) ExtraData(T) {
7878 inline for (fields) |field| {
7979 @field(result, field.name) = switch (field.type) {
8080 u32 => code.extra[i],
81 Inst.Ref => @as(Inst.Ref, @enumFromInt(code.extra[i])),
82 i32 => @as(i32, @bitCast(code.extra[i])),
83 Inst.Call.Flags => @as(Inst.Call.Flags, @bitCast(code.extra[i])),
84 Inst.BuiltinCall.Flags => @as(Inst.BuiltinCall.Flags, @bitCast(code.extra[i])),
85 Inst.SwitchBlock.Bits => @as(Inst.SwitchBlock.Bits, @bitCast(code.extra[i])),
86 Inst.FuncFancy.Bits => @as(Inst.FuncFancy.Bits, @bitCast(code.extra[i])),
81 Inst.Ref => @enumFromInt(code.extra[i]),
82 i32,
83 Inst.Call.Flags,
84 Inst.BuiltinCall.Flags,
85 Inst.SwitchBlock.Bits,
86 Inst.FuncFancy.Bits,
87 => @bitCast(code.extra[i]),
8788 else => @compileError("bad field type"),
8889 };
8990 i += 1;
......@@ -115,8 +116,7 @@ pub fn nullTerminatedString2(code: Zir, index: NullTerminatedString) [:0]const u
115116}
116117
117118pub fn refSlice(code: Zir, start: usize, len: usize) []Inst.Ref {
118 const raw_slice = code.extra[start..][0..len];
119 return @as([]Inst.Ref, @ptrCast(raw_slice));
119 return @ptrCast(code.extra[start..][0..len]);
120120}
121121
122122pub fn hasCompileErrors(code: Zir) bool {
......@@ -2347,8 +2347,8 @@ pub const Inst = struct {
23472347 pub const Break = struct {
23482348 pub const no_src_node = std.math.maxInt(i32);
23492349
2350 block_inst: Index,
23512350 operand_src_node: i32,
2351 block_inst: Index,
23522352 };
23532353
23542354 /// Trailing:
......@@ -3266,10 +3266,10 @@ pub const DeclIterator = struct {
32663266 }
32673267 it.decl_i += 1;
32683268
3269 const flags = @as(u4, @truncate(it.cur_bit_bag));
3269 const flags: u4 = @truncate(it.cur_bit_bag);
32703270 it.cur_bit_bag >>= 4;
32713271
3272 const sub_index = @as(u32, @intCast(it.extra_index));
3272 const sub_index: u32 = @intCast(it.extra_index);
32733273 it.extra_index += 5; // src_hash(4) + line(1)
32743274 const name = it.zir.nullTerminatedString(it.zir.extra[it.extra_index]);
32753275 it.extra_index += 3; // name(1) + value(1) + doc_comment(1)
......@@ -3296,7 +3296,7 @@ pub fn declIterator(zir: Zir, decl_inst: u32) DeclIterator {
32963296 const extended = datas[decl_inst].extended;
32973297 switch (extended.opcode) {
32983298 .struct_decl => {
3299 const small = @as(Inst.StructDecl.Small, @bitCast(extended.small));
3299 const small: Inst.StructDecl.Small = @bitCast(extended.small);
33003300 var extra_index: usize = extended.operand;
33013301 extra_index += @intFromBool(small.has_src_node);
33023302 extra_index += @intFromBool(small.has_fields_len);
......@@ -3319,7 +3319,7 @@ pub fn declIterator(zir: Zir, decl_inst: u32) DeclIterator {
33193319 return declIteratorInner(zir, extra_index, decls_len);
33203320 },
33213321 .enum_decl => {
3322 const small = @as(Inst.EnumDecl.Small, @bitCast(extended.small));
3322 const small: Inst.EnumDecl.Small = @bitCast(extended.small);
33233323 var extra_index: usize = extended.operand;
33243324 extra_index += @intFromBool(small.has_src_node);
33253325 extra_index += @intFromBool(small.has_tag_type);
......@@ -3334,7 +3334,7 @@ pub fn declIterator(zir: Zir, decl_inst: u32) DeclIterator {
33343334 return declIteratorInner(zir, extra_index, decls_len);
33353335 },
33363336 .union_decl => {
3337 const small = @as(Inst.UnionDecl.Small, @bitCast(extended.small));
3337 const small: Inst.UnionDecl.Small = @bitCast(extended.small);
33383338 var extra_index: usize = extended.operand;
33393339 extra_index += @intFromBool(small.has_src_node);
33403340 extra_index += @intFromBool(small.has_tag_type);
......@@ -3349,7 +3349,7 @@ pub fn declIterator(zir: Zir, decl_inst: u32) DeclIterator {
33493349 return declIteratorInner(zir, extra_index, decls_len);
33503350 },
33513351 .opaque_decl => {
3352 const small = @as(Inst.OpaqueDecl.Small, @bitCast(extended.small));
3352 const small: Inst.OpaqueDecl.Small = @bitCast(extended.small);
33533353 var extra_index: usize = extended.operand;
33543354 extra_index += @intFromBool(small.has_src_node);
33553355 const decls_len = if (small.has_decls_len) decls_len: {
......@@ -3545,7 +3545,7 @@ fn findDeclsSwitch(
35453545
35463546 const special_prong = extra.data.bits.specialProng();
35473547 if (special_prong != .none) {
3548 const body_len = @as(u31, @truncate(zir.extra[extra_index]));
3548 const body_len: u31 = @truncate(zir.extra[extra_index]);
35493549 extra_index += 1;
35503550 const body = zir.extra[extra_index..][0..body_len];
35513551 extra_index += body.len;
......@@ -3558,7 +3558,7 @@ fn findDeclsSwitch(
35583558 var scalar_i: usize = 0;
35593559 while (scalar_i < scalar_cases_len) : (scalar_i += 1) {
35603560 extra_index += 1;
3561 const body_len = @as(u31, @truncate(zir.extra[extra_index]));
3561 const body_len: u31 = @truncate(zir.extra[extra_index]);
35623562 extra_index += 1;
35633563 const body = zir.extra[extra_index..][0..body_len];
35643564 extra_index += body_len;
......@@ -3573,7 +3573,7 @@ fn findDeclsSwitch(
35733573 extra_index += 1;
35743574 const ranges_len = zir.extra[extra_index];
35753575 extra_index += 1;
3576 const body_len = @as(u31, @truncate(zir.extra[extra_index]));
3576 const body_len: u31 = @truncate(zir.extra[extra_index]);
35773577 extra_index += 1;
35783578 const items = zir.refSlice(extra_index, items_len);
35793579 extra_index += items_len;
......@@ -3655,7 +3655,7 @@ pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo {
36553655 ret_ty_ref = .void_type;
36563656 },
36573657 1 => {
3658 ret_ty_ref = @as(Inst.Ref, @enumFromInt(zir.extra[extra_index]));
3658 ret_ty_ref = @enumFromInt(zir.extra[extra_index]);
36593659 extra_index += 1;
36603660 },
36613661 else => {
......@@ -3709,7 +3709,7 @@ pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo {
37093709 ret_ty_body = zir.extra[extra_index..][0..body_len];
37103710 extra_index += ret_ty_body.len;
37113711 } else if (extra.data.bits.has_ret_ty_ref) {
3712 ret_ty_ref = @as(Inst.Ref, @enumFromInt(zir.extra[extra_index]));
3712 ret_ty_ref = @enumFromInt(zir.extra[extra_index]);
37133713 extra_index += 1;
37143714 }
37153715
......@@ -3753,7 +3753,7 @@ pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo {
37533753pub const ref_start_index: u32 = InternPool.static_len;
37543754
37553755pub fn indexToRef(inst: Inst.Index) Inst.Ref {
3756 return @as(Inst.Ref, @enumFromInt(ref_start_index + inst));
3756 return @enumFromInt(ref_start_index + inst);
37573757}
37583758
37593759pub fn refToIndex(inst: Inst.Ref) ?Inst.Index {
test/cases/compile_errors/invalid_coercion_in_aggregate_literal.zig created+22
......@@ -0,0 +1,22 @@
1export fn invalidArrayElem() u8 {
2 const array_literal = [1]u8{@as(u8, 256)};
3 return array_literal[0];
4}
5
6export fn invalidTupleElem() u8 {
7 const tuple_literal = struct { u8 }{@as(u8, 256)};
8 return tuple_literal[0];
9}
10
11export fn invalidStructField() u8 {
12 const struct_literal = struct { field: u8 }{ .field = @as(u8, 256) };
13 return struct_literal.field;
14}
15
16// error
17// backend=stage2
18// target=native
19//
20// :2:41: error: type 'u8' cannot represent integer value '256'
21// :7:49: error: type 'u8' cannot represent integer value '256'
22// :12:67: error: type 'u8' cannot represent integer value '256'
test/cases/compile_errors/invalid_coercion_in_labeled_break.zig created+12
......@@ -0,0 +1,12 @@
1export fn invalidBreak() u8 {
2 const result: u8 = label: {
3 break :label 256;
4 };
5 return result;
6}
7
8// error
9// backend=stage2
10// target=native
11//
12// :3:22: error: type 'u8' cannot represent integer value '256'
test/cases/compile_errors/invalid_if_expr_result_location_coercion.zig created+32
......@@ -0,0 +1,32 @@
1export fn invalidRuntimeThen(cond: bool) u0 {
2 const invalid: u16 = 256;
3 const result: u8 = if (cond) invalid else 0;
4 return result;
5}
6
7export fn invalidComptimeThen() u0 {
8 const invalid: u16 = 256;
9 const result: u8 = if (true) invalid else 0;
10 return result;
11}
12
13export fn invalidRuntimeElse(cond: bool) u0 {
14 const invalid: u16 = 256;
15 const result: u8 = if (cond) 0 else invalid;
16 return result;
17}
18
19export fn invalidComptimeElse() u0 {
20 const invalid: u16 = 256;
21 const result: u8 = if (false) 0 else invalid;
22 return result;
23}
24
25// error
26// backend=stage2
27// target=native
28//
29// :3:34: error: type 'u8' cannot represent integer value '256'
30// :9:34: error: type 'u8' cannot represent integer value '256'
31// :15:41: error: type 'u8' cannot represent integer value '256'
32// :21:42: error: type 'u8' cannot represent integer value '256'
test/cases/compile_errors/invalid_switch_expr_result_location_coercion.zig created+46
......@@ -0,0 +1,46 @@
1const Enum = enum(u8) { first, second, _ };
2
3export fn invalidFirstProng(enum_value: Enum) u8 {
4 const result: u8 = switch (enum_value) {
5 .first => 256,
6 .second => 0,
7 else => 0,
8 };
9 return result;
10}
11
12export fn invalidSecondProng(enum_value: Enum) u8 {
13 const result: u8 = switch (enum_value) {
14 .first => 0,
15 .second => 256,
16 _ => 0,
17 };
18 return result;
19}
20
21export fn invalidElseProng(enum_value: Enum) u8 {
22 const result: u8 = switch (enum_value) {
23 .first => 0,
24 .second => 0,
25 else => 256,
26 };
27 return result;
28}
29
30export fn invalidNonExhaustiveProng(enum_value: Enum) u8 {
31 const result: u8 = switch (enum_value) {
32 .first => 0,
33 .second => 0,
34 _ => 256,
35 };
36 return result;
37}
38
39// error
40// backend=stage2
41// target=native
42//
43// :5:19: error: type 'u8' cannot represent integer value '256'
44// :15:20: error: type 'u8' cannot represent integer value '256'
45// :25:17: error: type 'u8' cannot represent integer value '256'
46// :34:14: error: type 'u8' cannot represent integer value '256'