authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-02-16 19:09:57+00:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-02-16 19:09:57+00:00
log88d4b5cb18bdc68099162f1db8103d614025659c
tree4a60f9dd5ceb0a67d9fc43a4e12f6850f314837d
parent51e96a823ce21a6b027bfdf55d94c3872a103703
parentb2f28a104deb96eead604aa4a1a5a703689e2b61
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #18909 from mlugg/random-enhancements

A loose collection of random enhancements: * Eliminate `Sema.src` field * Optimize size of ZIR * Slightly improve some compile errors

7 files changed, 534 insertions(+), 505 deletions(-)

src/AstGen.zig+218-179
......@@ -44,6 +44,9 @@ compile_errors: ArrayListUnmanaged(Zir.Inst.CompileErrors.Item) = .{},
4444/// The topmost block of the current function.
4545fn_block: ?*GenZir = null,
4646fn_var_args: bool = false,
47/// The return type of the current function. This may be a trivial `Ref`, or
48/// otherwise it refers to a `ret_type` instruction.
49fn_ret_ty: Zir.Inst.Ref = .none,
4750/// Maps string table indexes to the first `@import` ZIR instruction
4851/// that uses this string as the operand.
4952imports: std.AutoArrayHashMapUnmanaged(Zir.NullTerminatedString, Ast.TokenIndex) = .{},
......@@ -357,16 +360,11 @@ const ResultInfo = struct {
357360 };
358361};
359362
360/// TODO: modify Sema to remove in favour of `coerced_align_ri`
361const align_ri: ResultInfo = .{ .rl = .{ .ty = .u29_type } };
362363const coerced_align_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .u29_type } };
363/// TODO: modify Sema to remove in favour of `coerced_addrspace_ri`
364const addrspace_ri: ResultInfo = .{ .rl = .{ .ty = .address_space_type } };
365364const coerced_addrspace_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .address_space_type } };
366365const coerced_linksection_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .slice_const_u8_type } };
367const bool_ri: ResultInfo = .{ .rl = .{ .ty = .bool_type } };
368const type_ri: ResultInfo = .{ .rl = .{ .ty = .type_type } };
369366const coerced_type_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .type_type } };
367const coerced_bool_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .bool_type } };
370368
371369fn typeExpr(gz: *GenZir, scope: *Scope, type_node: Ast.Node.Index) InnerError!Zir.Inst.Ref {
372370 return comptimeExpr(gz, scope, coerced_type_ri, type_node);
......@@ -783,7 +781,7 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
783781 .bool_and => return boolBinOp(gz, scope, ri, node, .bool_br_and),
784782 .bool_or => return boolBinOp(gz, scope, ri, node, .bool_br_or),
785783
786 .bool_not => return simpleUnOp(gz, scope, ri, node, bool_ri, node_datas[node].lhs, .bool_not),
784 .bool_not => return simpleUnOp(gz, scope, ri, node, coerced_bool_ri, node_datas[node].lhs, .bool_not),
787785 .bit_not => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, node_datas[node].lhs, .bit_not),
788786
789787 .negation => return negation(gz, scope, ri, node),
......@@ -1369,7 +1367,7 @@ fn fnProtoExpr(
13691367 };
13701368
13711369 const align_ref: Zir.Inst.Ref = if (fn_proto.ast.align_expr == 0) .none else inst: {
1372 break :inst try expr(&block_scope, scope, align_ri, fn_proto.ast.align_expr);
1370 break :inst try expr(&block_scope, scope, coerced_align_ri, fn_proto.ast.align_expr);
13731371 };
13741372
13751373 if (fn_proto.ast.addrspace_expr != 0) {
......@@ -1384,7 +1382,7 @@ fn fnProtoExpr(
13841382 try expr(
13851383 &block_scope,
13861384 scope,
1387 .{ .rl = .{ .ty = .calling_convention_type } },
1385 .{ .rl = .{ .coerced_ty = .calling_convention_type } },
13881386 fn_proto.ast.callconv_expr,
13891387 )
13901388 else
......@@ -2119,7 +2117,7 @@ fn restoreErrRetIndex(
21192117 else => .none, // always restore/pop
21202118 },
21212119 };
2122 _ = try gz.addRestoreErrRetIndex(bt, .{ .if_non_error = op });
2120 _ = try gz.addRestoreErrRetIndex(bt, .{ .if_non_error = op }, node);
21232121}
21242122
21252123fn breakExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref {
......@@ -2176,7 +2174,7 @@ fn breakExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index) Inn
21762174
21772175 // As our last action before the break, "pop" the error trace if needed
21782176 if (!block_gz.is_comptime)
2179 _ = try parent_gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always);
2177 _ = try parent_gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always, node);
21802178
21812179 _ = try parent_gz.addBreak(break_tag, block_inst, .void_value);
21822180 return Zir.Inst.Ref.unreachable_value;
......@@ -2268,7 +2266,7 @@ fn continueExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index)
22682266
22692267 // As our last action before the continue, "pop" the error trace if needed
22702268 if (!gen_zir.is_comptime)
2271 _ = try parent_gz.addRestoreErrRetIndex(.{ .block = continue_block }, .always);
2269 _ = try parent_gz.addRestoreErrRetIndex(.{ .block = continue_block }, .always, node);
22722270
22732271 _ = try parent_gz.addBreak(break_tag, continue_block, .void_value);
22742272 return Zir.Inst.Ref.unreachable_value;
......@@ -2328,7 +2326,7 @@ fn blockExpr(
23282326
23292327 if (!block_scope.endsWithNoReturn()) {
23302328 // As our last action before the break, "pop" the error trace if needed
2331 _ = try gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always);
2329 _ = try gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always, block_node);
23322330 _ = try block_scope.addBreak(.@"break", block_inst, .void_value);
23332331 }
23342332
......@@ -2423,7 +2421,7 @@ fn labeledBlockExpr(
24232421 try blockExprStmts(&block_scope, &block_scope.base, statements);
24242422 if (!block_scope.endsWithNoReturn()) {
24252423 // As our last action before the return, "pop" the error trace if needed
2426 _ = try gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always);
2424 _ = try gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always, block_node);
24272425 _ = try block_scope.addBreak(.@"break", block_inst, .void_value);
24282426 }
24292427
......@@ -2815,7 +2813,6 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
28152813 .export_value,
28162814 .set_eval_branch_quota,
28172815 .atomic_store,
2818 .store,
28192816 .store_node,
28202817 .store_to_inferred_ptr,
28212818 .resolve_inferred_alloc,
......@@ -2826,7 +2823,8 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
28262823 .validate_deref,
28272824 .validate_destructure,
28282825 .save_err_ret_index,
2829 .restore_err_ret_index,
2826 .restore_err_ret_index_unconditional,
2827 .restore_err_ret_index_fn_entry,
28302828 .validate_struct_init_ty,
28312829 .validate_struct_init_result_ty,
28322830 .validate_ptr_struct_init,
......@@ -3133,7 +3131,7 @@ fn varDecl(
31333131 }
31343132
31353133 const align_inst: Zir.Inst.Ref = if (var_decl.ast.align_node != 0)
3136 try expr(gz, scope, align_ri, var_decl.ast.align_node)
3134 try expr(gz, scope, coerced_align_ri, var_decl.ast.align_node)
31373135 else
31383136 .none;
31393137
......@@ -3329,31 +3327,11 @@ fn emitDbgNode(gz: *GenZir, node: Ast.Node.Index) !void {
33293327 // If the current block will be evaluated only during semantic analysis
33303328 // then no dbg_stmt ZIR instruction is needed.
33313329 if (gz.is_comptime) return;
3332
33333330 const astgen = gz.astgen;
33343331 astgen.advanceSourceCursorToNode(node);
33353332 const line = astgen.source_line - gz.decl_line;
33363333 const column = astgen.source_column;
3337
3338 if (gz.instructions.items.len > 0) {
3339 const last = gz.instructions.items[gz.instructions.items.len - 1];
3340 const zir_tags = astgen.instructions.items(.tag);
3341 if (zir_tags[@intFromEnum(last)] == .dbg_stmt) {
3342 const zir_datas = astgen.instructions.items(.data);
3343 zir_datas[@intFromEnum(last)].dbg_stmt = .{
3344 .line = line,
3345 .column = column,
3346 };
3347 return;
3348 }
3349 }
3350
3351 _ = try gz.add(.{ .tag = .dbg_stmt, .data = .{
3352 .dbg_stmt = .{
3353 .line = line,
3354 .column = column,
3355 },
3356 } });
3334 try emitDbgStmt(gz, .{ line, column });
33573335}
33583336
33593337fn assign(gz: *GenZir, scope: *Scope, infix_node: Ast.Node.Index) InnerError!void {
......@@ -3522,7 +3500,7 @@ fn assignDestructureMaybeDecls(
35223500 const this_lhs_comptime = is_comptime or (is_const and rhs_is_comptime);
35233501
35243502 const align_inst: Zir.Inst.Ref = if (full.ast.align_node != 0)
3525 try expr(gz, scope, align_ri, full.ast.align_node)
3503 try expr(gz, scope, coerced_align_ri, full.ast.align_node)
35263504 else
35273505 .none;
35283506
......@@ -3709,7 +3687,10 @@ fn assignOp(
37093687 .lhs = lhs,
37103688 .rhs = rhs,
37113689 });
3712 _ = try gz.addBin(.store, lhs_ptr, result);
3690 _ = try gz.addPlNode(.store_node, infix_node, Zir.Inst.Bin{
3691 .lhs = lhs_ptr,
3692 .rhs = result,
3693 });
37133694}
37143695
37153696fn assignShift(
......@@ -3732,7 +3713,10 @@ fn assignShift(
37323713 .lhs = lhs,
37333714 .rhs = rhs,
37343715 });
3735 _ = try gz.addBin(.store, lhs_ptr, result);
3716 _ = try gz.addPlNode(.store_node, infix_node, Zir.Inst.Bin{
3717 .lhs = lhs_ptr,
3718 .rhs = result,
3719 });
37363720}
37373721
37383722fn assignShiftSat(gz: *GenZir, scope: *Scope, infix_node: Ast.Node.Index) InnerError!void {
......@@ -3750,7 +3734,10 @@ fn assignShiftSat(gz: *GenZir, scope: *Scope, infix_node: Ast.Node.Index) InnerE
37503734 .lhs = lhs,
37513735 .rhs = rhs,
37523736 });
3753 _ = try gz.addBin(.store, lhs_ptr, result);
3737 _ = try gz.addPlNode(.store_node, infix_node, Zir.Inst.Bin{
3738 .lhs = lhs_ptr,
3739 .rhs = result,
3740 });
37543741}
37553742
37563743fn ptrType(
......@@ -3791,7 +3778,7 @@ fn ptrType(
37913778 gz.astgen.source_line = source_line;
37923779 gz.astgen.source_column = source_column;
37933780
3794 addrspace_ref = try expr(gz, scope, addrspace_ri, ptr_info.ast.addrspace_node);
3781 addrspace_ref = try expr(gz, scope, coerced_addrspace_ri, ptr_info.ast.addrspace_node);
37953782 trailing_count += 1;
37963783 }
37973784 if (ptr_info.ast.align_node != 0) {
......@@ -4184,7 +4171,7 @@ fn fnDecl(
41844171 var addrspace_gz = decl_gz.makeSubBlock(params_scope);
41854172 defer addrspace_gz.unstack();
41864173 const addrspace_ref: Zir.Inst.Ref = if (fn_proto.ast.addrspace_expr == 0) .none else inst: {
4187 const inst = try expr(&decl_gz, params_scope, addrspace_ri, fn_proto.ast.addrspace_expr);
4174 const inst = try expr(&decl_gz, params_scope, coerced_addrspace_ri, fn_proto.ast.addrspace_expr);
41884175 if (addrspace_gz.instructionsSlice().len == 0) {
41894176 // In this case we will send a len=0 body which can be encoded more efficiently.
41904177 break :inst inst;
......@@ -4284,8 +4271,19 @@ fn fnDecl(
42844271 fn_gz.instructions_top = ret_gz.instructions.items.len;
42854272
42864273 const prev_fn_block = astgen.fn_block;
4274 const prev_fn_ret_ty = astgen.fn_ret_ty;
42874275 astgen.fn_block = &fn_gz;
4288 defer astgen.fn_block = prev_fn_block;
4276 astgen.fn_ret_ty = if (is_inferred_error or ret_ref.toIndex() != null) r: {
4277 // We're essentially guaranteed to need the return type at some point,
4278 // since the return type is likely not `void` or `noreturn` so there
4279 // will probably be an explicit return requiring RLS. Fetch this
4280 // return type now so the rest of the function can use it.
4281 break :r try fn_gz.addNode(.ret_type, decl_node);
4282 } else ret_ref;
4283 defer {
4284 astgen.fn_block = prev_fn_block;
4285 astgen.fn_ret_ty = prev_fn_ret_ty;
4286 }
42894287
42904288 const prev_var_args = astgen.fn_var_args;
42914289 astgen.fn_var_args = is_var_args;
......@@ -4300,7 +4298,7 @@ fn fnDecl(
43004298
43014299 if (!fn_gz.endsWithNoReturn()) {
43024300 // As our last action before the return, "pop" the error trace if needed
4303 _ = try gz.addRestoreErrRetIndex(.ret, .always);
4301 _ = try gz.addRestoreErrRetIndex(.ret, .always, decl_node);
43044302
43054303 // Add implicit return at end of function.
43064304 _ = try fn_gz.addUnTok(.ret_implicit, .void_value, tree.lastToken(body_node));
......@@ -4428,7 +4426,7 @@ fn globalVarDecl(
44284426 try expr(
44294427 &block_scope,
44304428 &block_scope.base,
4431 .{ .rl = .{ .ty = .type_type } },
4429 coerced_type_ri,
44324430 var_decl.ast.type_node,
44334431 )
44344432 else
......@@ -4732,8 +4730,13 @@ fn testDecl(
47324730 defer fn_block.unstack();
47334731
47344732 const prev_fn_block = astgen.fn_block;
4733 const prev_fn_ret_ty = astgen.fn_ret_ty;
47354734 astgen.fn_block = &fn_block;
4736 defer astgen.fn_block = prev_fn_block;
4735 astgen.fn_ret_ty = .anyerror_void_error_union_type;
4736 defer {
4737 astgen.fn_block = prev_fn_block;
4738 astgen.fn_ret_ty = prev_fn_ret_ty;
4739 }
47374740
47384741 astgen.advanceSourceCursorToNode(body_node);
47394742 const lbrace_line = astgen.source_line - decl_block.decl_line;
......@@ -4743,7 +4746,7 @@ fn testDecl(
47434746 if (fn_block.isEmpty() or !fn_block.refIsNoReturn(block_result)) {
47444747
47454748 // As our last action before the return, "pop" the error trace if needed
4746 _ = try gz.addRestoreErrRetIndex(.ret, .always);
4749 _ = try gz.addRestoreErrRetIndex(.ret, .always, node);
47474750
47484751 // Add implicit return at end of function.
47494752 _ = try fn_block.addUnTok(.ret_implicit, .void_value, tree.lastToken(body_node));
......@@ -5246,7 +5249,7 @@ fn unionDeclInner(
52465249 return astgen.failNode(member_node, "union field missing type", .{});
52475250 }
52485251 if (have_align) {
5249 const align_inst = try expr(&block_scope, &block_scope.base, .{ .rl = .{ .ty = .u32_type } }, member.ast.align_expr);
5252 const align_inst = try expr(&block_scope, &block_scope.base, coerced_align_ri, member.ast.align_expr);
52505253 wip_members.appendToField(@intFromEnum(align_inst));
52515254 any_aligned_fields = true;
52525255 }
......@@ -5514,7 +5517,7 @@ fn containerDecl(
55145517 namespace.base.tag = .enum_namespace;
55155518
55165519 const arg_inst: Zir.Inst.Ref = if (container_decl.ast.arg != 0)
5517 try comptimeExpr(&block_scope, &namespace.base, .{ .rl = .{ .ty = .type_type } }, container_decl.ast.arg)
5520 try comptimeExpr(&block_scope, &namespace.base, coerced_type_ri, container_decl.ast.arg)
55185521 else
55195522 .none;
55205523
......@@ -6071,7 +6074,7 @@ fn arrayAccess(
60716074
60726075 const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
60736076
6074 const rhs = try expr(gz, scope, .{ .rl = .{ .ty = .usize_type } }, node_datas[node].rhs);
6077 const rhs = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[node].rhs);
60756078 try emitDbgStmt(gz, cursor);
60766079
60776080 return gz.addPlNode(.elem_ptr_node, node, Zir.Inst.Bin{ .lhs = lhs, .rhs = rhs });
......@@ -6081,7 +6084,7 @@ fn arrayAccess(
60816084
60826085 const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
60836086
6084 const rhs = try expr(gz, scope, .{ .rl = .{ .ty = .usize_type } }, node_datas[node].rhs);
6087 const rhs = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[node].rhs);
60856088 try emitDbgStmt(gz, cursor);
60866089
60876090 return rvalue(gz, ri, try gz.addPlNode(.elem_val_node, node, Zir.Inst.Bin{ .lhs = lhs, .rhs = rhs }), node);
......@@ -6149,16 +6152,16 @@ fn boolBinOp(
61496152 const tree = astgen.tree;
61506153 const node_datas = tree.nodes.items(.data);
61516154
6152 const lhs = try expr(gz, scope, bool_ri, node_datas[node].lhs);
6153 const bool_br = try gz.addBoolBr(zir_tag, lhs);
6155 const lhs = try expr(gz, scope, coerced_bool_ri, node_datas[node].lhs);
6156 const bool_br = (try gz.addPlNodePayloadIndex(zir_tag, node, undefined)).toIndex().?;
61546157
61556158 var rhs_scope = gz.makeSubBlock(scope);
61566159 defer rhs_scope.unstack();
6157 const rhs = try expr(&rhs_scope, &rhs_scope.base, bool_ri, node_datas[node].rhs);
6160 const rhs = try expr(&rhs_scope, &rhs_scope.base, coerced_bool_ri, node_datas[node].rhs);
61586161 if (!gz.refIsNoReturn(rhs)) {
61596162 _ = try rhs_scope.addBreakWithSrcNode(.break_inline, bool_br, rhs, node_datas[node].rhs);
61606163 }
6161 try rhs_scope.setBoolBrBody(bool_br);
6164 try rhs_scope.setBoolBrBody(bool_br, lhs);
61626165
61636166 const block_ref = bool_br.toRef();
61646167 return rvalue(gz, ri, block_ref, node);
......@@ -6222,7 +6225,7 @@ fn ifExpr(
62226225 .bool_bit = try block_scope.addUnNode(tag, optional, if_full.ast.cond_expr),
62236226 };
62246227 } else {
6225 const cond = try expr(&block_scope, &block_scope.base, bool_ri, if_full.ast.cond_expr);
6228 const cond = try expr(&block_scope, &block_scope.base, coerced_bool_ri, if_full.ast.cond_expr);
62266229 break :c .{
62276230 .inst = cond,
62286231 .bool_bit = cond,
......@@ -6468,7 +6471,7 @@ fn whileExpr(
64686471 .bool_bit = try cond_scope.addUnNode(tag, optional, while_full.ast.cond_expr),
64696472 };
64706473 } else {
6471 const cond = try expr(&cond_scope, &cond_scope.base, bool_ri, while_full.ast.cond_expr);
6474 const cond = try expr(&cond_scope, &cond_scope.base, coerced_bool_ri, while_full.ast.cond_expr);
64726475 break :c .{
64736476 .inst = cond,
64746477 .bool_bit = cond,
......@@ -6726,7 +6729,10 @@ fn forExpr(
67266729 const alloc_tag: Zir.Inst.Tag = if (is_inline) .alloc_comptime_mut else .alloc;
67276730 const index_ptr = try parent_gz.addUnNode(alloc_tag, .usize_type, node);
67286731 // initialize to zero
6729 _ = try parent_gz.addBin(.store, index_ptr, .zero_usize);
6732 _ = try parent_gz.addPlNode(.store_node, node, Zir.Inst.Bin{
6733 .lhs = index_ptr,
6734 .rhs = .zero_usize,
6735 });
67306736 break :blk index_ptr;
67316737 };
67326738
......@@ -6956,7 +6962,10 @@ fn forExpr(
69566962 .lhs = index,
69576963 .rhs = .one_usize,
69586964 });
6959 _ = try loop_scope.addBin(.store, index_ptr, index_plus_one);
6965 _ = try loop_scope.addPlNode(.store_node, node, Zir.Inst.Bin{
6966 .lhs = index_ptr,
6967 .rhs = index_plus_one,
6968 });
69606969 const repeat_tag: Zir.Inst.Tag = if (is_inline) .repeat_inline else .repeat;
69616970 _ = try loop_scope.addNode(repeat_tag, node);
69626971
......@@ -7124,7 +7133,7 @@ fn switchExprErrUnion(
71247133 block_scope.setBreakResultInfo(block_ri);
71257134
71267135 // Sema expects a dbg_stmt immediately before switch_block_err_union
7127 try emitDbgStmt(parent_gz, operand_lc);
7136 try emitDbgStmtForceCurrentIndex(parent_gz, operand_lc);
71287137 // This gets added to the parent block later, after the item expressions.
71297138 const switch_block = try parent_gz.makeBlockInst(.switch_block_err_union, switch_node);
71307139
......@@ -7704,7 +7713,7 @@ fn switchExpr(
77047713 block_scope.setBreakResultInfo(block_ri);
77057714
77067715 // Sema expects a dbg_stmt immediately before switch_block(_ref)
7707 try emitDbgStmt(parent_gz, operand_lc);
7716 try emitDbgStmtForceCurrentIndex(parent_gz, operand_lc);
77087717 // This gets added to the parent block later, after the item expressions.
77097718 const switch_tag: Zir.Inst.Tag = if (any_payload_is_ref) .switch_block_ref else .switch_block;
77107719 const switch_block = try parent_gz.makeBlockInst(switch_tag, switch_node);
......@@ -8009,7 +8018,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
80098018 try genDefers(gz, defer_outer, scope, .normal_only);
80108019
80118020 // As our last action before the return, "pop" the error trace if needed
8012 _ = try gz.addRestoreErrRetIndex(.ret, .always);
8021 _ = try gz.addRestoreErrRetIndex(.ret, .always, node);
80138022
80148023 _ = try gz.addUnNode(.ret_node, .void_value, node);
80158024 return Zir.Inst.Ref.unreachable_value;
......@@ -8038,7 +8047,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
80388047 .rl = .{ .ptr = .{ .inst = try gz.addNode(.ret_ptr, node) } },
80398048 .ctx = .@"return",
80408049 } else .{
8041 .rl = .{ .ty = try gz.addNode(.ret_type, node) },
8050 .rl = .{ .coerced_ty = astgen.fn_ret_ty },
80428051 .ctx = .@"return",
80438052 };
80448053 const prev_anon_name_strategy = gz.anon_name_strategy;
......@@ -8052,7 +8061,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
80528061 try genDefers(gz, defer_outer, scope, .normal_only);
80538062
80548063 // As our last action before the return, "pop" the error trace if needed
8055 _ = try gz.addRestoreErrRetIndex(.ret, .always);
8064 _ = try gz.addRestoreErrRetIndex(.ret, .always, node);
80568065
80578066 try emitDbgStmt(gz, ret_lc);
80588067 try gz.addRet(ri, operand, node);
......@@ -8075,7 +8084,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
80758084
80768085 // As our last action before the return, "pop" the error trace if needed
80778086 const result = if (ri.rl == .ptr) try gz.addUnNode(.load, ri.rl.ptr.inst, node) else operand;
8078 _ = try gz.addRestoreErrRetIndex(.ret, .{ .if_non_error = result });
8087 _ = try gz.addRestoreErrRetIndex(.ret, .{ .if_non_error = result }, node);
80798088
80808089 try gz.addRet(ri, operand, node);
80818090 return Zir.Inst.Ref.unreachable_value;
......@@ -8092,7 +8101,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
80928101 try genDefers(&then_scope, defer_outer, scope, .normal_only);
80938102
80948103 // As our last action before the return, "pop" the error trace if needed
8095 _ = try then_scope.addRestoreErrRetIndex(.ret, .always);
8104 _ = try then_scope.addRestoreErrRetIndex(.ret, .always, node);
80968105
80978106 try emitDbgStmt(&then_scope, ret_lc);
80988107 try then_scope.addRet(ri, operand, node);
......@@ -8674,7 +8683,7 @@ fn unionInit(
86748683 params: []const Ast.Node.Index,
86758684) InnerError!Zir.Inst.Ref {
86768685 const union_type = try typeExpr(gz, scope, params[0]);
8677 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = .slice_const_u8_type } }, params[1]);
8686 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1]);
86788687 const field_type = try gz.addPlNode(.field_type_ref, node, Zir.Inst.FieldTypeRef{
86798688 .container_type = union_type,
86808689 .field_name = field_name,
......@@ -8987,12 +8996,12 @@ fn builtinCall(
89878996 if (ri.rl == .ref or ri.rl == .ref_coerced_ty) {
89888997 return gz.addPlNode(.field_ptr_named, node, Zir.Inst.FieldNamed{
89898998 .lhs = try expr(gz, scope, .{ .rl = .ref }, params[0]),
8990 .field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = .slice_const_u8_type } }, params[1]),
8999 .field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1]),
89919000 });
89929001 }
89939002 const result = try gz.addPlNode(.field_val_named, node, Zir.Inst.FieldNamed{
89949003 .lhs = try expr(gz, scope, .{ .rl = .none }, params[0]),
8995 .field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = .slice_const_u8_type } }, params[1]),
9004 .field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1]),
89969005 });
89979006 return rvalue(gz, ri, result, node);
89989007 },
......@@ -9119,7 +9128,7 @@ fn builtinCall(
91199128 return rvalue(gz, ri, .void_value, node);
91209129 },
91219130 .set_align_stack => {
9122 const order = try expr(gz, scope, align_ri, params[0]);
9131 const order = try expr(gz, scope, coerced_align_ri, params[0]);
91239132 _ = try gz.addExtendedPayload(.set_align_stack, Zir.Inst.UnNode{
91249133 .node = gz.nodeIndexToRelative(node),
91259134 .operand = order,
......@@ -9161,32 +9170,32 @@ fn builtinCall(
91619170 .bit_size_of => return simpleUnOpType(gz, scope, ri, node, params[0], .bit_size_of),
91629171 .align_of => return simpleUnOpType(gz, scope, ri, node, params[0], .align_of),
91639172
9164 .int_from_ptr => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .int_from_ptr),
9165 .compile_error => return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .ty = .slice_const_u8_type } }, params[0], .compile_error),
9166 .set_eval_branch_quota => return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .set_eval_branch_quota),
9167 .int_from_enum => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .int_from_enum),
9168 .int_from_bool => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .int_from_bool),
9169 .embed_file => return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .ty = .slice_const_u8_type } }, params[0], .embed_file),
9170 .error_name => return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .ty = .anyerror_type } }, params[0], .error_name),
9171 .set_runtime_safety => return simpleUnOp(gz, scope, ri, node, bool_ri, params[0], .set_runtime_safety),
9172 .sqrt => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .sqrt),
9173 .sin => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .sin),
9174 .cos => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .cos),
9175 .tan => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .tan),
9176 .exp => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .exp),
9177 .exp2 => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .exp2),
9178 .log => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .log),
9179 .log2 => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .log2),
9180 .log10 => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .log10),
9181 .abs => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .abs),
9182 .floor => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .floor),
9183 .ceil => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .ceil),
9184 .trunc => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .trunc),
9185 .round => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .round),
9186 .tag_name => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .tag_name),
9187 .type_name => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .type_name),
9188 .Frame => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .frame_type),
9189 .frame_size => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .frame_size),
9173 .int_from_ptr => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .int_from_ptr),
9174 .compile_error => return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[0], .compile_error),
9175 .set_eval_branch_quota => return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .set_eval_branch_quota),
9176 .int_from_enum => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .int_from_enum),
9177 .int_from_bool => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .int_from_bool),
9178 .embed_file => return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[0], .embed_file),
9179 .error_name => return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .coerced_ty = .anyerror_type } }, params[0], .error_name),
9180 .set_runtime_safety => return simpleUnOp(gz, scope, ri, node, coerced_bool_ri, params[0], .set_runtime_safety),
9181 .sqrt => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .sqrt),
9182 .sin => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .sin),
9183 .cos => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .cos),
9184 .tan => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .tan),
9185 .exp => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .exp),
9186 .exp2 => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .exp2),
9187 .log => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .log),
9188 .log2 => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .log2),
9189 .log10 => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .log10),
9190 .abs => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .abs),
9191 .floor => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .floor),
9192 .ceil => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .ceil),
9193 .trunc => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .trunc),
9194 .round => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .round),
9195 .tag_name => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .tag_name),
9196 .type_name => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .type_name),
9197 .Frame => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .frame_type),
9198 .frame_size => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, params[0], .frame_size),
91909199
91919200 .int_from_float => return typeCast(gz, scope, ri, node, params[0], .int_from_float, builtin_name),
91929201 .float_from_int => return typeCast(gz, scope, ri, node, params[0], .float_from_int, builtin_name),
......@@ -9224,7 +9233,7 @@ fn builtinCall(
92249233 },
92259234 .panic => {
92269235 try emitDbgNode(gz, node);
9227 return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .ty = .slice_const_u8_type } }, params[0], .panic);
9236 return simpleUnOp(gz, scope, ri, node, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[0], .panic);
92289237 },
92299238 .trap => {
92309239 try emitDbgNode(gz, node);
......@@ -9313,7 +9322,7 @@ fn builtinCall(
93139322 },
93149323 .c_define => {
93159324 if (!gz.c_import) return gz.astgen.failNode(node, "C define valid only inside C import block", .{});
9316 const name = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = .slice_const_u8_type } }, params[0]);
9325 const name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[0]);
93179326 const value = try comptimeExpr(gz, scope, .{ .rl = .none }, params[1]);
93189327 const result = try gz.addExtendedPayload(.c_define, Zir.Inst.BinNode{
93199328 .node = gz.nodeIndexToRelative(node),
......@@ -9334,7 +9343,7 @@ fn builtinCall(
93349343 return rvalue(gz, ri, result, node);
93359344 },
93369345 .reduce => {
9337 const op = try expr(gz, scope, .{ .rl = .{ .ty = .reduce_op_type } }, params[0]);
9346 const op = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .reduce_op_type } }, params[0]);
93389347 const scalar = try expr(gz, scope, .{ .rl = .none }, params[1]);
93399348 const result = try gz.addPlNode(.reduce, node, Zir.Inst.Bin{
93409349 .lhs = op,
......@@ -9410,7 +9419,7 @@ fn builtinCall(
94109419 },
94119420 .field_parent_ptr => {
94129421 const parent_type = try typeExpr(gz, scope, params[0]);
9413 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = .slice_const_u8_type } }, params[1]);
9422 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1]);
94149423 const result = try gz.addPlNode(.field_parent_ptr, node, Zir.Inst.FieldParentPtr{
94159424 .parent_type = parent_type,
94169425 .field_name = field_name,
......@@ -9547,7 +9556,7 @@ fn hasDeclOrField(
95479556 tag: Zir.Inst.Tag,
95489557) InnerError!Zir.Inst.Ref {
95499558 const container_type = try typeExpr(gz, scope, lhs_node);
9550 const name = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = .slice_const_u8_type } }, rhs_node);
9559 const name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, rhs_node);
95519560 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
95529561 .lhs = container_type,
95539562 .rhs = name,
......@@ -9697,7 +9706,7 @@ fn simpleCBuiltin(
96979706) InnerError!Zir.Inst.Ref {
96989707 const name: []const u8 = if (tag == .c_undef) "C undef" else "C include";
96999708 if (!gz.c_import) return gz.astgen.failNode(node, "{s} valid only inside C import block", .{name});
9700 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = .slice_const_u8_type } }, operand_node);
9709 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, operand_node);
97019710 _ = try gz.addExtendedPayload(tag, Zir.Inst.UnNode{
97029711 .node = gz.nodeIndexToRelative(node),
97039712 .operand = operand,
......@@ -9715,7 +9724,7 @@ fn offsetOf(
97159724 tag: Zir.Inst.Tag,
97169725) InnerError!Zir.Inst.Ref {
97179726 const type_inst = try typeExpr(gz, scope, lhs_node);
9718 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = .slice_const_u8_type } }, rhs_node);
9727 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, rhs_node);
97199728 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
97209729 .lhs = type_inst,
97219730 .rhs = field_name,
......@@ -9828,13 +9837,8 @@ fn callExpr(
98289837 astgen.advanceSourceCursor(astgen.tree.tokens.items(.start)[call.ast.lparen]);
98299838 const line = astgen.source_line - gz.decl_line;
98309839 const column = astgen.source_column;
9831
9832 _ = try gz.add(.{ .tag = .dbg_stmt, .data = .{
9833 .dbg_stmt = .{
9834 .line = line,
9835 .column = column,
9836 },
9837 } });
9840 // Sema expects a dbg_stmt immediately before call,
9841 try emitDbgStmtForceCurrentIndex(gz, .{ line, column });
98389842 }
98399843
98409844 switch (callee) {
......@@ -10985,7 +10989,10 @@ fn rvalueInner(
1098510989 return .void_value;
1098610990 },
1098710991 .inferred_ptr => |alloc| {
10988 _ = try gz.addBin(.store_to_inferred_ptr, alloc, result);
10992 _ = try gz.addPlNode(.store_to_inferred_ptr, src_node, Zir.Inst.Bin{
10993 .lhs = alloc,
10994 .rhs = result,
10995 });
1098910996 return .void_value;
1099010997 },
1099110998 .destructure => |destructure| {
......@@ -11012,7 +11019,10 @@ fn rvalueInner(
1101211019 });
1101311020 },
1101411021 .inferred_ptr => |ptr_inst| {
11015 _ = try gz.addBin(.store_to_inferred_ptr, ptr_inst, elem_val);
11022 _ = try gz.addPlNode(.store_to_inferred_ptr, src_node, Zir.Inst.Bin{
11023 .lhs = ptr_inst,
11024 .rhs = elem_val,
11025 });
1101611026 },
1101711027 .discard => unreachable,
1101811028 }
......@@ -11834,19 +11844,20 @@ const GenZir = struct {
1183411844 }
1183511845
1183611846 /// Assumes nothing stacked on `gz`. Unstacks `gz`.
11837 fn setBoolBrBody(gz: *GenZir, inst: Zir.Inst.Index) !void {
11847 fn setBoolBrBody(gz: *GenZir, bool_br: Zir.Inst.Index, bool_br_lhs: Zir.Inst.Ref) !void {
1183811848 const astgen = gz.astgen;
1183911849 const gpa = astgen.gpa;
1184011850 const body = gz.instructionsSlice();
1184111851 const body_len = astgen.countBodyLenAfterFixups(body);
1184211852 try astgen.extra.ensureUnusedCapacity(
1184311853 gpa,
11844 @typeInfo(Zir.Inst.Block).Struct.fields.len + body_len,
11854 @typeInfo(Zir.Inst.BoolBr).Struct.fields.len + body_len,
1184511855 );
1184611856 const zir_datas = astgen.instructions.items(.data);
11847 zir_datas[@intFromEnum(inst)].bool_br.payload_index = astgen.addExtraAssumeCapacity(
11848 Zir.Inst.Block{ .body_len = body_len },
11849 );
11857 zir_datas[@intFromEnum(bool_br)].pl_node.payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.BoolBr{
11858 .lhs = bool_br_lhs,
11859 .body_len = body_len,
11860 });
1185011861 astgen.appendBodyWithFixups(body);
1185111862 gz.unstack();
1185211863 }
......@@ -12231,30 +12242,6 @@ const GenZir = struct {
1223112242 return new_index.toRef();
1223212243 }
1223312244
12234 /// Note that this returns a `Zir.Inst.Index` not a ref.
12235 /// Leaves the `payload_index` field undefined.
12236 fn addBoolBr(
12237 gz: *GenZir,
12238 tag: Zir.Inst.Tag,
12239 lhs: Zir.Inst.Ref,
12240 ) !Zir.Inst.Index {
12241 assert(lhs != .none);
12242 const gpa = gz.astgen.gpa;
12243 try gz.instructions.ensureUnusedCapacity(gpa, 1);
12244 try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1);
12245
12246 const new_index: Zir.Inst.Index = @enumFromInt(gz.astgen.instructions.len);
12247 gz.astgen.instructions.appendAssumeCapacity(.{
12248 .tag = tag,
12249 .data = .{ .bool_br = .{
12250 .lhs = lhs,
12251 .payload_index = undefined,
12252 } },
12253 });
12254 gz.instructions.appendAssumeCapacity(new_index);
12255 return new_index;
12256 }
12257
1225812245 fn addInt(gz: *GenZir, integer: u64) !Zir.Inst.Ref {
1225912246 return gz.add(.{
1226012247 .tag = .int,
......@@ -12575,17 +12562,37 @@ const GenZir = struct {
1257512562 always: void,
1257612563 if_non_error: Zir.Inst.Ref,
1257712564 },
12565 src_node: Ast.Node.Index,
1257812566 ) !Zir.Inst.Index {
12579 return gz.addAsIndex(.{
12580 .tag = .restore_err_ret_index,
12581 .data = .{ .restore_err_ret_index = .{
12582 .block = switch (bt) {
12583 .ret => .none,
12584 .block => |b| b.toRef(),
12585 },
12586 .operand = if (cond == .if_non_error) cond.if_non_error else .none,
12587 } },
12588 });
12567 switch (cond) {
12568 .always => return gz.addAsIndex(.{
12569 .tag = .restore_err_ret_index_unconditional,
12570 .data = .{ .un_node = .{
12571 .operand = switch (bt) {
12572 .ret => .none,
12573 .block => |b| b.toRef(),
12574 },
12575 .src_node = gz.nodeIndexToRelative(src_node),
12576 } },
12577 }),
12578 .if_non_error => |operand| switch (bt) {
12579 .ret => return gz.addAsIndex(.{
12580 .tag = .restore_err_ret_index_fn_entry,
12581 .data = .{ .un_node = .{
12582 .operand = operand,
12583 .src_node = gz.nodeIndexToRelative(src_node),
12584 } },
12585 }),
12586 .block => |block| return (try gz.addExtendedPayload(
12587 .restore_err_ret_index,
12588 Zir.Inst.RestoreErrRetIndex{
12589 .src_node = gz.nodeIndexToRelative(src_node),
12590 .block = block.toRef(),
12591 .operand = operand,
12592 },
12593 )).toIndex().?,
12594 },
12595 }
1258912596 }
1259012597
1259112598 fn addBreak(
......@@ -12911,20 +12918,20 @@ const GenZir = struct {
1291112918 const astgen = gz.astgen;
1291212919 const gpa = astgen.gpa;
1291312920
12921 // Node 0 is valid for the root `struct_decl` of a file!
12922 assert(args.src_node != 0 or gz.parent.tag == .top);
12923
1291412924 const fields_hash_arr: [4]u32 = @bitCast(args.fields_hash);
1291512925
12916 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.StructDecl).Struct.fields.len + 6);
12926 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.StructDecl).Struct.fields.len + 4);
1291712927 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.StructDecl{
1291812928 .fields_hash_0 = fields_hash_arr[0],
1291912929 .fields_hash_1 = fields_hash_arr[1],
1292012930 .fields_hash_2 = fields_hash_arr[2],
1292112931 .fields_hash_3 = fields_hash_arr[3],
12932 .src_node = gz.nodeIndexToRelative(args.src_node),
1292212933 });
1292312934
12924 if (args.src_node != 0) {
12925 const node_offset = gz.nodeIndexToRelative(args.src_node);
12926 astgen.extra.appendAssumeCapacity(@bitCast(node_offset));
12927 }
1292812935 if (args.fields_len != 0) {
1292912936 astgen.extra.appendAssumeCapacity(args.fields_len);
1293012937 }
......@@ -12942,7 +12949,6 @@ const GenZir = struct {
1294212949 .data = .{ .extended = .{
1294312950 .opcode = .struct_decl,
1294412951 .small = @bitCast(Zir.Inst.StructDecl.Small{
12945 .has_src_node = args.src_node != 0,
1294612952 .has_fields_len = args.fields_len != 0,
1294712953 .has_decls_len = args.decls_len != 0,
1294812954 .has_backing_int = args.backing_int_ref != .none,
......@@ -12974,20 +12980,19 @@ const GenZir = struct {
1297412980 const astgen = gz.astgen;
1297512981 const gpa = astgen.gpa;
1297612982
12983 assert(args.src_node != 0);
12984
1297712985 const fields_hash_arr: [4]u32 = @bitCast(args.fields_hash);
1297812986
12979 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.UnionDecl).Struct.fields.len + 5);
12987 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.UnionDecl).Struct.fields.len + 4);
1298012988 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.UnionDecl{
1298112989 .fields_hash_0 = fields_hash_arr[0],
1298212990 .fields_hash_1 = fields_hash_arr[1],
1298312991 .fields_hash_2 = fields_hash_arr[2],
1298412992 .fields_hash_3 = fields_hash_arr[3],
12993 .src_node = gz.nodeIndexToRelative(args.src_node),
1298512994 });
1298612995
12987 if (args.src_node != 0) {
12988 const node_offset = gz.nodeIndexToRelative(args.src_node);
12989 astgen.extra.appendAssumeCapacity(@bitCast(node_offset));
12990 }
1299112996 if (args.tag_type != .none) {
1299212997 astgen.extra.appendAssumeCapacity(@intFromEnum(args.tag_type));
1299312998 }
......@@ -13005,7 +13010,6 @@ const GenZir = struct {
1300513010 .data = .{ .extended = .{
1300613011 .opcode = .union_decl,
1300713012 .small = @bitCast(Zir.Inst.UnionDecl.Small{
13008 .has_src_node = args.src_node != 0,
1300913013 .has_tag_type = args.tag_type != .none,
1301013014 .has_body_len = args.body_len != 0,
1301113015 .has_fields_len = args.fields_len != 0,
......@@ -13032,20 +13036,19 @@ const GenZir = struct {
1303213036 const astgen = gz.astgen;
1303313037 const gpa = astgen.gpa;
1303413038
13039 assert(args.src_node != 0);
13040
1303513041 const fields_hash_arr: [4]u32 = @bitCast(args.fields_hash);
1303613042
13037 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.EnumDecl).Struct.fields.len + 5);
13043 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.EnumDecl).Struct.fields.len + 4);
1303813044 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.EnumDecl{
1303913045 .fields_hash_0 = fields_hash_arr[0],
1304013046 .fields_hash_1 = fields_hash_arr[1],
1304113047 .fields_hash_2 = fields_hash_arr[2],
1304213048 .fields_hash_3 = fields_hash_arr[3],
13049 .src_node = gz.nodeIndexToRelative(args.src_node),
1304313050 });
1304413051
13045 if (args.src_node != 0) {
13046 const node_offset = gz.nodeIndexToRelative(args.src_node);
13047 astgen.extra.appendAssumeCapacity(@bitCast(node_offset));
13048 }
1304913052 if (args.tag_type != .none) {
1305013053 astgen.extra.appendAssumeCapacity(@intFromEnum(args.tag_type));
1305113054 }
......@@ -13063,7 +13066,6 @@ const GenZir = struct {
1306313066 .data = .{ .extended = .{
1306413067 .opcode = .enum_decl,
1306513068 .small = @bitCast(Zir.Inst.EnumDecl.Small{
13066 .has_src_node = args.src_node != 0,
1306713069 .has_tag_type = args.tag_type != .none,
1306813070 .has_body_len = args.body_len != 0,
1306913071 .has_fields_len = args.fields_len != 0,
......@@ -13083,13 +13085,13 @@ const GenZir = struct {
1308313085 const astgen = gz.astgen;
1308413086 const gpa = astgen.gpa;
1308513087
13086 try astgen.extra.ensureUnusedCapacity(gpa, 2);
13087 const payload_index: u32 = @intCast(astgen.extra.items.len);
13088 assert(args.src_node != 0);
13089
13090 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.OpaqueDecl).Struct.fields.len + 1);
13091 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.OpaqueDecl{
13092 .src_node = gz.nodeIndexToRelative(args.src_node),
13093 });
1308813094
13089 if (args.src_node != 0) {
13090 const node_offset = gz.nodeIndexToRelative(args.src_node);
13091 astgen.extra.appendAssumeCapacity(@bitCast(node_offset));
13092 }
1309313095 if (args.decls_len != 0) {
1309413096 astgen.extra.appendAssumeCapacity(args.decls_len);
1309513097 }
......@@ -13098,7 +13100,6 @@ const GenZir = struct {
1309813100 .data = .{ .extended = .{
1309913101 .opcode = .opaque_decl,
1310013102 .small = @bitCast(Zir.Inst.OpaqueDecl.Small{
13101 .has_src_node = args.src_node != 0,
1310213103 .has_decls_len = args.decls_len != 0,
1310313104 .name_strategy = gz.anon_name_strategy,
1310413105 }),
......@@ -13136,7 +13137,7 @@ const GenZir = struct {
1313613137 fn addRet(gz: *GenZir, ri: ResultInfo, operand: Zir.Inst.Ref, node: Ast.Node.Index) !void {
1313713138 switch (ri.rl) {
1313813139 .ptr => |ptr_res| _ = try gz.addUnNode(.ret_load, ptr_res.inst, node),
13139 .ty => _ = try gz.addUnNode(.ret_node, operand, node),
13140 .coerced_ty => _ = try gz.addUnNode(.ret_node, operand, node),
1314013141 else => unreachable,
1314113142 }
1314213143 }
......@@ -13517,6 +13518,44 @@ fn countBodyLenAfterFixups(astgen: *AstGen, body: []const Zir.Inst.Index) u32 {
1351713518
1351813519fn emitDbgStmt(gz: *GenZir, lc: LineColumn) !void {
1351913520 if (gz.is_comptime) return;
13521 if (gz.instructions.items.len > 0) {
13522 const astgen = gz.astgen;
13523 const last = gz.instructions.items[gz.instructions.items.len - 1];
13524 if (astgen.instructions.items(.tag)[@intFromEnum(last)] == .dbg_stmt) {
13525 astgen.instructions.items(.data)[@intFromEnum(last)].dbg_stmt = .{
13526 .line = lc[0],
13527 .column = lc[1],
13528 };
13529 return;
13530 }
13531 }
13532
13533 _ = try gz.add(.{ .tag = .dbg_stmt, .data = .{
13534 .dbg_stmt = .{
13535 .line = lc[0],
13536 .column = lc[1],
13537 },
13538 } });
13539}
13540
13541/// In some cases, Sema expects us to generate a `dbg_stmt` at the instruction
13542/// *index* directly preceding the next instruction (e.g. if a call is %10, it
13543/// expects a dbg_stmt at %9). TODO: this logic may allow redundant dbg_stmt
13544/// instructions; fix up Sema so we don't need it!
13545fn emitDbgStmtForceCurrentIndex(gz: *GenZir, lc: LineColumn) !void {
13546 const astgen = gz.astgen;
13547 if (gz.instructions.items.len > 0 and
13548 @intFromEnum(gz.instructions.items[gz.instructions.items.len - 1]) == astgen.instructions.len - 1)
13549 {
13550 const last = astgen.instructions.len - 1;
13551 if (astgen.instructions.items(.tag)[last] == .dbg_stmt) {
13552 astgen.instructions.items(.data)[last].dbg_stmt = .{
13553 .line = lc[0],
13554 .column = lc[1],
13555 };
13556 return;
13557 }
13558 }
1352013559
1352113560 _ = try gz.add(.{ .tag = .dbg_stmt, .data = .{
1352213561 .dbg_stmt = .{
src/Autodoc.zig+15-48
......@@ -1799,7 +1799,8 @@ fn walkInstruction(
17991799 };
18001800 },
18011801 .bool_br_and, .bool_br_or => {
1802 const bool_br = data[@intFromEnum(inst)].bool_br;
1802 const pl_node = data[@intFromEnum(inst)].pl_node;
1803 const extra = file.zir.extraData(Zir.Inst.BoolBr, pl_node.payload_index);
18031804
18041805 const bin_index = self.exprs.items.len;
18051806 try self.exprs.append(self.arena, .{ .binOp = .{ .lhs = 0, .rhs = 0 } });
......@@ -1808,14 +1809,13 @@ fn walkInstruction(
18081809 file,
18091810 parent_scope,
18101811 parent_src,
1811 bool_br.lhs,
1812 extra.data.lhs,
18121813 false,
18131814 call_ctx,
18141815 );
18151816 const lhs_index = self.exprs.items.len;
18161817 try self.exprs.append(self.arena, lhs.expr);
18171818
1818 const extra = file.zir.extraData(Zir.Inst.Block, bool_br.payload_index);
18191819 const rhs = try self.walkInstruction(
18201820 file,
18211821 parent_scope,
......@@ -3395,19 +3395,10 @@ fn walkInstruction(
33953395 .enclosing_type = type_slot_index,
33963396 };
33973397
3398 const small = @as(Zir.Inst.OpaqueDecl.Small, @bitCast(extended.small));
3399 var extra_index: usize = extended.operand;
3400
3401 const src_node: ?i32 = if (small.has_src_node) blk: {
3402 const src_node = @as(i32, @bitCast(file.zir.extra[extra_index]));
3403 extra_index += 1;
3404 break :blk src_node;
3405 } else null;
3398 const extra = file.zir.extraData(Zir.Inst.OpaqueDecl, extended.operand);
3399 var extra_index: usize = extra.end;
34063400
3407 const src_info = if (src_node) |sn|
3408 try self.srcLocInfo(file, sn, parent_src)
3409 else
3410 parent_src;
3401 const src_info = try self.srcLocInfo(file, extra.data.src_node, parent_src);
34113402
34123403 var decl_indexes: std.ArrayListUnmanaged(usize) = .{};
34133404 var priv_decl_indexes: std.ArrayListUnmanaged(usize) = .{};
......@@ -3498,18 +3489,10 @@ fn walkInstruction(
34983489 };
34993490
35003491 const small = @as(Zir.Inst.UnionDecl.Small, @bitCast(extended.small));
3501 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.UnionDecl).Struct.fields.len;
3502
3503 const src_node: ?i32 = if (small.has_src_node) blk: {
3504 const src_node = @as(i32, @bitCast(file.zir.extra[extra_index]));
3505 extra_index += 1;
3506 break :blk src_node;
3507 } else null;
3492 const extra = file.zir.extraData(Zir.Inst.UnionDecl, extended.operand);
3493 var extra_index: usize = extra.end;
35083494
3509 const src_info = if (src_node) |sn|
3510 try self.srcLocInfo(file, sn, parent_src)
3511 else
3512 parent_src;
3495 const src_info = try self.srcLocInfo(file, extra.data.src_node, parent_src);
35133496
35143497 // We delay analysis because union tags can refer to
35153498 // decls defined inside the union itself.
......@@ -3628,18 +3611,10 @@ fn walkInstruction(
36283611 };
36293612
36303613 const small = @as(Zir.Inst.EnumDecl.Small, @bitCast(extended.small));
3631 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.EnumDecl).Struct.fields.len;
3632
3633 const src_node: ?i32 = if (small.has_src_node) blk: {
3634 const src_node = @as(i32, @bitCast(file.zir.extra[extra_index]));
3635 extra_index += 1;
3636 break :blk src_node;
3637 } else null;
3614 const extra = file.zir.extraData(Zir.Inst.EnumDecl, extended.operand);
3615 var extra_index: usize = extra.end;
36383616
3639 const src_info = if (src_node) |sn|
3640 try self.srcLocInfo(file, sn, parent_src)
3641 else
3642 parent_src;
3617 const src_info = try self.srcLocInfo(file, extra.data.src_node, parent_src);
36433618
36443619 const tag_type: ?DocData.Expr = if (small.has_tag_type) blk: {
36453620 const tag_type = file.zir.extra[extra_index];
......@@ -3779,18 +3754,10 @@ fn walkInstruction(
37793754 };
37803755
37813756 const small = @as(Zir.Inst.StructDecl.Small, @bitCast(extended.small));
3782 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len;
3783
3784 const src_node: ?i32 = if (small.has_src_node) blk: {
3785 const src_node = @as(i32, @bitCast(file.zir.extra[extra_index]));
3786 extra_index += 1;
3787 break :blk src_node;
3788 } else null;
3757 const extra = file.zir.extraData(Zir.Inst.StructDecl, extended.operand);
3758 var extra_index: usize = extra.end;
37893759
3790 const src_info = if (src_node) |sn|
3791 try self.srcLocInfo(file, sn, parent_src)
3792 else
3793 parent_src;
3760 const src_info = try self.srcLocInfo(file, extra.data.src_node, parent_src);
37943761
37953762 const fields_len = if (small.has_fields_len) blk: {
37963763 const fields_len = file.zir.extra[extra_index];
src/Module.zig+15
......@@ -1867,6 +1867,16 @@ pub const SrcLoc = struct {
18671867 else => return nodeToSpan(tree, node),
18681868 }
18691869 },
1870 .node_offset_return_operand => |node_off| {
1871 const tree = try src_loc.file_scope.getTree(gpa);
1872 const node = src_loc.declRelativeToNodeIndex(node_off);
1873 const node_tags = tree.nodes.items(.tag);
1874 const node_datas = tree.nodes.items(.data);
1875 if (node_tags[node] == .@"return" and node_datas[node].lhs != 0) {
1876 return nodeToSpan(tree, node_datas[node].lhs);
1877 }
1878 return nodeToSpan(tree, node);
1879 },
18701880 }
18711881 }
18721882
......@@ -2221,6 +2231,10 @@ pub const LazySrcLoc = union(enum) {
22212231 /// The source location points to the RHS of an assignment.
22222232 /// The Decl is determined contextually.
22232233 node_offset_store_operand: i32,
2234 /// The source location points to the operand of a `return` statement, or
2235 /// the `return` itself if there is no explicit operand.
2236 /// The Decl is determined contextually.
2237 node_offset_return_operand: i32,
22242238 /// The source location points to a for loop input.
22252239 /// The Decl is determined contextually.
22262240 for_input: struct {
......@@ -2347,6 +2361,7 @@ pub const LazySrcLoc = union(enum) {
23472361 .node_offset_init_ty,
23482362 .node_offset_store_ptr,
23492363 .node_offset_store_operand,
2364 .node_offset_return_operand,
23502365 .for_input,
23512366 .for_capture_from_input,
23522367 .array_cat_lhs,
src/Sema.zig+146-141
......@@ -50,11 +50,6 @@ branch_count: u32 = 0,
5050/// Populated when returning `error.ComptimeBreak`. Used to communicate the
5151/// break instruction up the stack to find the corresponding Block.
5252comptime_break_inst: Zir.Inst.Index = undefined,
53/// This field is updated when a new source location becomes active, so that
54/// instructions which do not have explicitly mapped source locations still have
55/// access to the source location set by the previous instruction which did
56/// contain a mapped source location.
57src: LazySrcLoc = .{ .token_offset = 0 },
5853decl_val_table: std.AutoHashMapUnmanaged(InternPool.DeclIndex, Air.Inst.Ref) = .{},
5954/// When doing a generic function instantiation, this array collects a value
6055/// for each parameter of the generic owner. `none` for non-comptime parameters.
......@@ -1006,10 +1001,10 @@ fn analyzeBodyInner(
10061001 const air_inst: Air.Inst.Ref = switch (tags[@intFromEnum(inst)]) {
10071002 // zig fmt: off
10081003 .alloc => try sema.zirAlloc(block, inst),
1009 .alloc_inferred => try sema.zirAllocInferred(block, inst, true),
1010 .alloc_inferred_mut => try sema.zirAllocInferred(block, inst, false),
1011 .alloc_inferred_comptime => try sema.zirAllocInferredComptime(inst, true),
1012 .alloc_inferred_comptime_mut => try sema.zirAllocInferredComptime(inst, false),
1004 .alloc_inferred => try sema.zirAllocInferred(block, true),
1005 .alloc_inferred_mut => try sema.zirAllocInferred(block, false),
1006 .alloc_inferred_comptime => try sema.zirAllocInferredComptime(true),
1007 .alloc_inferred_comptime_mut => try sema.zirAllocInferredComptime(false),
10131008 .alloc_mut => try sema.zirAllocMut(block, inst),
10141009 .alloc_comptime_mut => try sema.zirAllocComptime(block, inst),
10151010 .make_ptr_const => try sema.zirMakePtrConst(block, inst),
......@@ -1308,6 +1303,11 @@ fn analyzeBodyInner(
13081303 i += 1;
13091304 continue;
13101305 },
1306 .restore_err_ret_index => {
1307 try sema.zirRestoreErrRetIndex(block, extended);
1308 i += 1;
1309 continue;
1310 },
13111311 .value_placeholder => unreachable, // never appears in a body
13121312 };
13131313 },
......@@ -1369,11 +1369,6 @@ fn analyzeBodyInner(
13691369 i += 1;
13701370 continue;
13711371 },
1372 .store => {
1373 try sema.zirStore(block, inst);
1374 i += 1;
1375 continue;
1376 },
13771372 .store_node => {
13781373 try sema.zirStoreNode(block, inst);
13791374 i += 1;
......@@ -1518,8 +1513,15 @@ fn analyzeBodyInner(
15181513 i += 1;
15191514 continue;
15201515 },
1521 .restore_err_ret_index => {
1522 try sema.zirRestoreErrRetIndex(block, inst);
1516 .restore_err_ret_index_unconditional => {
1517 const un_node = datas[@intFromEnum(inst)].un_node;
1518 try sema.restoreErrRetIndex(block, un_node.src(), un_node.operand, .none);
1519 i += 1;
1520 continue;
1521 },
1522 .restore_err_ret_index_fn_entry => {
1523 const un_node = datas[@intFromEnum(inst)].un_node;
1524 try sema.restoreErrRetIndex(block, un_node.src(), .none, un_node.operand);
15231525 i += 1;
15241526 continue;
15251527 },
......@@ -2723,7 +2725,6 @@ pub fn getStructType(
27232725 const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);
27242726
27252727 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len;
2726 extra_index += @intFromBool(small.has_src_node);
27272728 const fields_len = if (small.has_fields_len) blk: {
27282729 const fields_len = sema.code.extra[extra_index];
27292730 extra_index += 1;
......@@ -2776,10 +2777,7 @@ fn zirStructDecl(
27762777 const mod = sema.mod;
27772778 const ip = &mod.intern_pool;
27782779 const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);
2779 const src: LazySrcLoc = if (small.has_src_node) blk: {
2780 const node_offset: i32 = @bitCast(sema.code.extra[extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len]);
2781 break :blk LazySrcLoc.nodeOffset(node_offset);
2782 } else sema.src;
2780 const src = sema.code.extraData(Zir.Inst.StructDecl, extended.operand).data.src();
27832781
27842782 // Because these three things each reference each other, `undefined`
27852783 // placeholders are used before being set after the struct type gains an
......@@ -2939,13 +2937,10 @@ fn zirEnumDecl(
29392937 const mod = sema.mod;
29402938 const gpa = sema.gpa;
29412939 const small: Zir.Inst.EnumDecl.Small = @bitCast(extended.small);
2942 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.EnumDecl).Struct.fields.len;
2940 const extra = sema.code.extraData(Zir.Inst.EnumDecl, extended.operand);
2941 var extra_index: usize = extra.end;
29432942
2944 const src: LazySrcLoc = if (small.has_src_node) blk: {
2945 const node_offset: i32 = @bitCast(sema.code.extra[extra_index]);
2946 extra_index += 1;
2947 break :blk LazySrcLoc.nodeOffset(node_offset);
2948 } else sema.src;
2943 const src = extra.data.src();
29492944 const tag_ty_src: LazySrcLoc = .{ .node_offset_container_tag = src.node_offset.x };
29502945
29512946 const tag_type_ref = if (small.has_tag_type) blk: {
......@@ -3212,13 +3207,10 @@ fn zirUnionDecl(
32123207 const mod = sema.mod;
32133208 const gpa = sema.gpa;
32143209 const small: Zir.Inst.UnionDecl.Small = @bitCast(extended.small);
3215 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.UnionDecl).Struct.fields.len;
3210 const extra = sema.code.extraData(Zir.Inst.UnionDecl, extended.operand);
3211 var extra_index: usize = extra.end;
32163212
3217 const src: LazySrcLoc = if (small.has_src_node) blk: {
3218 const node_offset: i32 = @bitCast(sema.code.extra[extra_index]);
3219 extra_index += 1;
3220 break :blk LazySrcLoc.nodeOffset(node_offset);
3221 } else sema.src;
3213 const src = extra.data.src();
32223214
32233215 extra_index += @intFromBool(small.has_tag_type);
32243216 extra_index += @intFromBool(small.has_body_len);
......@@ -3321,13 +3313,10 @@ fn zirOpaqueDecl(
33213313
33223314 const mod = sema.mod;
33233315 const small: Zir.Inst.OpaqueDecl.Small = @bitCast(extended.small);
3324 var extra_index: usize = extended.operand;
3316 const extra = sema.code.extraData(Zir.Inst.OpaqueDecl, extended.operand);
3317 var extra_index: usize = extra.end;
33253318
3326 const src: LazySrcLoc = if (small.has_src_node) blk: {
3327 const node_offset: i32 = @bitCast(sema.code.extra[extra_index]);
3328 extra_index += 1;
3329 break :blk LazySrcLoc.nodeOffset(node_offset);
3330 } else sema.src;
3319 const src = extra.data.src();
33313320
33323321 const decls_len = if (small.has_decls_len) blk: {
33333322 const decls_len = sema.code.extra[extra_index];
......@@ -3977,13 +3966,9 @@ fn makePtrConst(sema: *Sema, block: *Block, alloc: Air.Inst.Ref) CompileError!Ai
39773966
39783967fn zirAllocInferredComptime(
39793968 sema: *Sema,
3980 inst: Zir.Inst.Index,
39813969 is_const: bool,
39823970) CompileError!Air.Inst.Ref {
39833971 const gpa = sema.gpa;
3984 const src_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].node;
3985 const src = LazySrcLoc.nodeOffset(src_node);
3986 sema.src = src;
39873972
39883973 try sema.air_instructions.append(gpa, .{
39893974 .tag = .inferred_alloc_comptime,
......@@ -4042,16 +4027,12 @@ fn zirAllocMut(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
40424027fn zirAllocInferred(
40434028 sema: *Sema,
40444029 block: *Block,
4045 inst: Zir.Inst.Index,
40464030 is_const: bool,
40474031) CompileError!Air.Inst.Ref {
40484032 const tracy = trace(@src());
40494033 defer tracy.end();
40504034
40514035 const gpa = sema.gpa;
4052 const src_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].node;
4053 const src = LazySrcLoc.nodeOffset(src_node);
4054 sema.src = src;
40554036
40564037 if (block.is_comptime) {
40574038 try sema.air_instructions.append(gpa, .{
......@@ -5428,10 +5409,11 @@ fn zirStoreToInferredPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compi
54285409 const tracy = trace(@src());
54295410 defer tracy.end();
54305411
5431 const src: LazySrcLoc = sema.src;
5432 const bin_inst = sema.code.instructions.items(.data)[@intFromEnum(inst)].bin;
5433 const ptr = try sema.resolveInst(bin_inst.lhs);
5434 const operand = try sema.resolveInst(bin_inst.rhs);
5412 const pl_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
5413 const src = pl_node.src();
5414 const bin = sema.code.extraData(Zir.Inst.Bin, pl_node.payload_index).data;
5415 const ptr = try sema.resolveInst(bin.lhs);
5416 const operand = try sema.resolveInst(bin.rhs);
54355417 const ptr_inst = ptr.toIndex().?;
54365418 const air_datas = sema.air_instructions.items(.data);
54375419
......@@ -5496,16 +5478,6 @@ fn zirSetEvalBranchQuota(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compi
54965478 sema.branch_quota = @max(sema.branch_quota, quota);
54975479}
54985480
5499fn zirStore(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
5500 const tracy = trace(@src());
5501 defer tracy.end();
5502
5503 const bin_inst = sema.code.instructions.items(.data)[@intFromEnum(inst)].bin;
5504 const ptr = try sema.resolveInst(bin_inst.lhs);
5505 const value = try sema.resolveInst(bin_inst.rhs);
5506 return sema.storePtr(block, sema.src, ptr, value);
5507}
5508
55095481fn zirStoreNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
55105482 const tracy = trace(@src());
55115483 defer tracy.end();
......@@ -5699,17 +5671,20 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.I
56995671 const src = inst_data.src();
57005672 const msg_inst = try sema.resolveInst(inst_data.operand);
57015673
5674 // `panicWithMsg` would perform this coercion for us, but we can get a better
5675 // source location if we do it here.
5676 const coerced_msg = try sema.coerce(block, Type.slice_const_u8, msg_inst, .{ .node_offset_builtin_call_arg0 = inst_data.src_node });
5677
57025678 if (block.is_comptime) {
57035679 return sema.fail(block, src, "encountered @panic at comptime", .{});
57045680 }
5705 try sema.panicWithMsg(block, src, msg_inst, .@"@panic");
5681 try sema.panicWithMsg(block, src, coerced_msg, .@"@panic");
57065682 return always_noreturn;
57075683}
57085684
57095685fn zirTrap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
57105686 const src_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].node;
57115687 const src = LazySrcLoc.nodeOffset(src_node);
5712 sema.src = src;
57135688 if (block.is_comptime)
57145689 return sema.fail(block, src, "encountered @trap at comptime", .{});
57155690 _ = try block.addNoOp(.trap);
......@@ -6384,10 +6359,6 @@ fn zirBreak(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index) CompileError
63846359}
63856360
63866361fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
6387 // We do not set sema.src here because dbg_stmt instructions are only emitted for
6388 // ZIR code that possibly will need to generate runtime code. So error messages
6389 // and other source locations must not rely on sema.src being set from dbg_stmt
6390 // instructions.
63916362 if (block.is_comptime or block.ownerModule().strip) return;
63926363
63936364 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;
......@@ -6632,7 +6603,6 @@ fn funcDeclSrc(sema: *Sema, func_inst: Air.Inst.Ref) !?*Decl {
66326603pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref {
66336604 const mod = sema.mod;
66346605 const gpa = sema.gpa;
6635 const src = sema.src;
66366606
66376607 if (block.is_comptime or block.is_typeof) {
66386608 const index_val = try mod.intValue_u64(Type.usize, sema.comptime_err_ret_trace.items.len);
......@@ -6650,9 +6620,10 @@ pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref
66506620 else => |e| return e,
66516621 };
66526622 const field_name = try mod.intern_pool.getOrPutString(gpa, "index");
6653 const field_index = sema.structFieldIndex(block, stack_trace_ty, field_name, src) catch |err| switch (err) {
6654 error.NeededSourceLocation, error.GenericPoison, error.ComptimeReturn, error.ComptimeBreak => unreachable,
6655 else => |e| return e,
6623 const field_index = sema.structFieldIndex(block, stack_trace_ty, field_name, .unneeded) catch |err| switch (err) {
6624 error.AnalysisFail, error.NeededSourceLocation => @panic("std.builtin.StackTrace is corrupt"),
6625 error.GenericPoison, error.ComptimeReturn, error.ComptimeBreak => unreachable,
6626 error.OutOfMemory => |e| return e,
66566627 };
66576628
66586629 return try block.addInst(.{
......@@ -9900,7 +9871,6 @@ fn zirAsNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
99009871 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
99019872 const src = inst_data.src();
99029873 const extra = sema.code.extraData(Zir.Inst.As, inst_data.payload_index).data;
9903 sema.src = src;
99049874 return sema.analyzeAs(block, src, extra.dest_type, extra.operand, false);
99059875}
99069876
......@@ -10508,7 +10478,8 @@ fn zirElemValNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1050810478 const elem_index_src: LazySrcLoc = .{ .node_offset_array_access_index = inst_data.src_node };
1050910479 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
1051010480 const array = try sema.resolveInst(extra.lhs);
10511 const elem_index = try sema.resolveInst(extra.rhs);
10481 const uncoerced_elem_index = try sema.resolveInst(extra.rhs);
10482 const elem_index = try sema.coerce(block, Type.usize, uncoerced_elem_index, elem_index_src);
1051210483 return sema.elemVal(block, src, array, elem_index, elem_index_src, true);
1051310484}
1051410485
......@@ -10560,7 +10531,8 @@ fn zirElemPtrNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1056010531 const elem_index_src: LazySrcLoc = .{ .node_offset_array_access_index = inst_data.src_node };
1056110532 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
1056210533 const array_ptr = try sema.resolveInst(extra.lhs);
10563 const elem_index = try sema.resolveInst(extra.rhs);
10534 const uncoerced_elem_index = try sema.resolveInst(extra.rhs);
10535 const elem_index = try sema.coerce(block, Type.usize, uncoerced_elem_index, elem_index_src);
1056410536 return sema.elemPtr(block, src, array_ptr, elem_index, elem_index_src, false, true);
1056510537}
1056610538
......@@ -10855,7 +10827,7 @@ const SwitchProngAnalysis = struct {
1085510827 .address_space = operand_ptr_ty.ptrAddressSpace(mod),
1085610828 },
1085710829 });
10858 if (try sema.resolveDefinedValue(block, sema.src, spa.operand_ptr)) |union_ptr| {
10830 if (try sema.resolveDefinedValue(block, operand_src, spa.operand_ptr)) |union_ptr| {
1085910831 return Air.internedToRef((try mod.intern(.{ .ptr = .{
1086010832 .ty = ptr_field_ty.toIntern(),
1086110833 .addr = .{ .field = .{
......@@ -10866,7 +10838,7 @@ const SwitchProngAnalysis = struct {
1086610838 }
1086710839 return block.addStructFieldPtr(spa.operand_ptr, field_index, ptr_field_ty);
1086810840 } else {
10869 if (try sema.resolveDefinedValue(block, sema.src, spa.operand)) |union_val| {
10841 if (try sema.resolveDefinedValue(block, operand_src, spa.operand)) |union_val| {
1087010842 const tag_and_val = ip.indexToKey(union_val.toIntern()).un;
1087110843 return Air.internedToRef(tag_and_val.val);
1087210844 }
......@@ -13191,6 +13163,7 @@ fn validateErrSetSwitch(
1319113163 // else => |e| return e,
1319213164 // even if all the possible errors were already handled.
1319313165 const tags = sema.code.instructions.items(.tag);
13166 const datas = sema.code.instructions.items(.data);
1319413167 for (else_case.body) |else_inst| switch (tags[@intFromEnum(else_inst)]) {
1319513168 .dbg_block_begin,
1319613169 .dbg_block_end,
......@@ -13205,11 +13178,16 @@ fn validateErrSetSwitch(
1320513178 .err_union_code,
1320613179 .ret_err_value_code,
1320713180 .save_err_ret_index,
13208 .restore_err_ret_index,
13181 .restore_err_ret_index_unconditional,
13182 .restore_err_ret_index_fn_entry,
1320913183 .is_non_err,
1321013184 .ret_is_non_err,
1321113185 .condbr,
1321213186 => {},
13187 .extended => switch (datas[@intFromEnum(else_inst)].extended.opcode) {
13188 .restore_err_ret_index => {},
13189 else => break,
13190 },
1321313191 else => break,
1321413192 } else break :else_validation;
1321513193
......@@ -13707,7 +13685,6 @@ fn zirShl(
1370713685 const mod = sema.mod;
1370813686 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1370913687 const src = inst_data.src();
13710 sema.src = src;
1371113688 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1371213689 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1371313690 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -13878,7 +13855,6 @@ fn zirShr(
1387813855 const mod = sema.mod;
1387913856 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1388013857 const src = inst_data.src();
13881 sema.src = src;
1388213858 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1388313859 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1388413860 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -14014,7 +13990,6 @@ fn zirBitwise(
1401413990 const mod = sema.mod;
1401513991 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1401613992 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
14017 sema.src = src;
1401813993 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1401913994 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1402013995 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -14795,21 +14770,20 @@ fn zirArithmetic(
1479514770 defer tracy.end();
1479614771
1479714772 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
14798 sema.src = .{ .node_offset_bin_op = inst_data.src_node };
14773 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
1479914774 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1480014775 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1480114776 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
1480214777 const lhs = try sema.resolveInst(extra.lhs);
1480314778 const rhs = try sema.resolveInst(extra.rhs);
1480414779
14805 return sema.analyzeArithmetic(block, zir_tag, lhs, rhs, sema.src, lhs_src, rhs_src, safety);
14780 return sema.analyzeArithmetic(block, zir_tag, lhs, rhs, src, lhs_src, rhs_src, safety);
1480614781}
1480714782
1480814783fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1480914784 const mod = sema.mod;
1481014785 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1481114786 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
14812 sema.src = src;
1481314787 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1481414788 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1481514789 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -14975,7 +14949,6 @@ fn zirDivExact(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1497514949 const mod = sema.mod;
1497614950 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1497714951 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
14978 sema.src = src;
1497914952 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1498014953 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1498114954 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -15141,7 +15114,6 @@ fn zirDivFloor(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1514115114 const mod = sema.mod;
1514215115 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1514315116 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
15144 sema.src = src;
1514515117 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1514615118 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1514715119 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -15252,7 +15224,6 @@ fn zirDivTrunc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1525215224 const mod = sema.mod;
1525315225 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1525415226 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
15255 sema.src = src;
1525615227 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1525715228 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1525815229 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -15494,7 +15465,6 @@ fn zirModRem(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
1549415465 const mod = sema.mod;
1549515466 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1549615467 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
15497 sema.src = src;
1549815468 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1549915469 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1550015470 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -15679,7 +15649,6 @@ fn zirMod(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
1567915649 const mod = sema.mod;
1568015650 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1568115651 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
15682 sema.src = src;
1568315652 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1568415653 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1568515654 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -15775,7 +15744,6 @@ fn zirRem(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
1577515744 const mod = sema.mod;
1577615745 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1577715746 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
15778 sema.src = src;
1577915747 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
1578015748 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
1578115749 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -18741,13 +18709,18 @@ fn zirBoolBr(
1874118709 defer tracy.end();
1874218710
1874318711 const mod = sema.mod;
18712 const gpa = sema.gpa;
18713
1874418714 const datas = sema.code.instructions.items(.data);
18745 const inst_data = datas[@intFromEnum(inst)].bool_br;
18746 const lhs = try sema.resolveInst(inst_data.lhs);
18747 const lhs_src = sema.src;
18748 const extra = sema.code.extraData(Zir.Inst.Block, inst_data.payload_index);
18715 const inst_data = datas[@intFromEnum(inst)].pl_node;
18716 const extra = sema.code.extraData(Zir.Inst.BoolBr, inst_data.payload_index);
18717
18718 const uncoerced_lhs = try sema.resolveInst(extra.data.lhs);
1874918719 const body = sema.code.bodySlice(extra.end, extra.data.body_len);
18750 const gpa = sema.gpa;
18720 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
18721 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
18722
18723 const lhs = try sema.coerce(parent_block, Type.bool, uncoerced_lhs, lhs_src);
1875118724
1875218725 if (try sema.resolveDefinedValue(parent_block, lhs_src, lhs)) |lhs_val| {
1875318726 if (is_bool_or and lhs_val.toBool()) {
......@@ -18758,7 +18731,11 @@ fn zirBoolBr(
1875818731 // comptime-known left-hand side. No need for a block here; the result
1875918732 // is simply the rhs expression. Here we rely on there only being 1
1876018733 // break instruction (`break_inline`).
18761 return sema.resolveBody(parent_block, body, inst);
18734 const rhs_result = try sema.resolveBody(parent_block, body, inst);
18735 if (sema.typeOf(rhs_result).isNoReturn(mod)) {
18736 return rhs_result;
18737 }
18738 return sema.coerce(parent_block, Type.bool, rhs_result, rhs_src);
1876218739 }
1876318740
1876418741 const block_inst: Air.Inst.Index = @enumFromInt(sema.air_instructions.len);
......@@ -18789,13 +18766,16 @@ fn zirBoolBr(
1878918766 _ = try lhs_block.addBr(block_inst, lhs_result);
1879018767
1879118768 const rhs_result = try sema.resolveBody(rhs_block, body, inst);
18792 if (!sema.typeOf(rhs_result).isNoReturn(mod)) {
18793 _ = try rhs_block.addBr(block_inst, rhs_result);
18794 }
18769 const rhs_noret = sema.typeOf(rhs_result).isNoReturn(mod);
18770 const coerced_rhs_result = if (!rhs_noret) rhs: {
18771 const coerced_result = try sema.coerce(rhs_block, Type.bool, rhs_result, rhs_src);
18772 _ = try rhs_block.addBr(block_inst, coerced_result);
18773 break :rhs coerced_result;
18774 } else rhs_result;
1879518775
1879618776 const result = sema.finishCondBr(parent_block, &child_block, &then_block, &else_block, lhs, block_inst);
18797 if (!sema.typeOf(rhs_result).isNoReturn(mod)) {
18798 if (try sema.resolveDefinedValue(rhs_block, sema.src, rhs_result)) |rhs_val| {
18777 if (!rhs_noret) {
18778 if (try sema.resolveDefinedValue(rhs_block, rhs_src, coerced_rhs_result)) |rhs_val| {
1879918779 if (is_bool_or and rhs_val.toBool()) {
1880018780 return .bool_true;
1880118781 } else if (!is_bool_or and !rhs_val.toBool()) {
......@@ -19206,7 +19186,7 @@ fn zirRetErrValue(
1920619186 .ty = error_set_type.toIntern(),
1920719187 .name = err_name,
1920819188 } })));
19209 return sema.analyzeRet(block, result_inst, src);
19189 return sema.analyzeRet(block, result_inst, src, src);
1921019190}
1921119191
1921219192fn zirRetImplicit(
......@@ -19256,7 +19236,7 @@ fn zirRetImplicit(
1925619236 return sema.failWithOwnedErrorMsg(block, msg);
1925719237 }
1925819238
19259 return sema.analyzeRet(block, operand, r_brace_src);
19239 return sema.analyzeRet(block, operand, r_brace_src, r_brace_src);
1926019240}
1926119241
1926219242fn zirRetNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
......@@ -19267,7 +19247,7 @@ fn zirRetNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir
1926719247 const operand = try sema.resolveInst(inst_data.operand);
1926819248 const src = inst_data.src();
1926919249
19270 return sema.analyzeRet(block, operand, src);
19250 return sema.analyzeRet(block, operand, src, .{ .node_offset_return_operand = inst_data.src_node });
1927119251}
1927219252
1927319253fn zirRetLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir.Inst.Index {
......@@ -19280,7 +19260,7 @@ fn zirRetLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Zir
1928019260
1928119261 if (block.is_comptime or block.inlining != null or sema.func_is_naked) {
1928219262 const operand = try sema.analyzeLoad(block, src, ret_ptr, src);
19283 return sema.analyzeRet(block, operand, src);
19263 return sema.analyzeRet(block, operand, src, .{ .node_offset_return_operand = inst_data.src_node });
1928419264 }
1928519265
1928619266 if (sema.wantErrorReturnTracing(sema.fn_ret_ty)) {
......@@ -19375,17 +19355,21 @@ fn zirSaveErrRetIndex(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
1937519355 block.error_return_trace_index = try sema.analyzeSaveErrRetIndex(block);
1937619356}
1937719357
19378fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index) CompileError!void {
19379 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].restore_err_ret_index;
19380 const src = sema.src; // TODO
19381
19382 const mod = sema.mod;
19383 const ip = &mod.intern_pool;
19358fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void {
19359 const extra = sema.code.extraData(Zir.Inst.RestoreErrRetIndex, extended.operand).data;
19360 return sema.restoreErrRetIndex(start_block, extra.src(), extra.block, extra.operand);
19361}
1938419362
19363/// If `operand` is non-error (or is `none`), restores the error return trace to
19364/// its state at the point `block` was reached (or, if `block` is `none`, the
19365/// point this function began execution).
19366fn restoreErrRetIndex(sema: *Sema, start_block: *Block, src: LazySrcLoc, target_block: Zir.Inst.Ref, operand_zir: Zir.Inst.Ref) CompileError!void {
1938519367 const tracy = trace(@src());
1938619368 defer tracy.end();
1938719369
19388 const saved_index = if (inst_data.block.toIndexAllowNone()) |zir_block| b: {
19370 const mod = sema.mod;
19371
19372 const saved_index = if (target_block.toIndexAllowNone()) |zir_block| b: {
1938919373 var block = start_block;
1939019374 while (true) {
1939119375 if (block.label) |label| {
......@@ -19409,7 +19393,7 @@ fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index)
1940919393 return; // No need to restore
1941019394 };
1941119395
19412 const operand = try sema.resolveInstAllowNone(inst_data.operand);
19396 const operand = try sema.resolveInstAllowNone(operand_zir);
1941319397
1941419398 if (start_block.is_comptime or start_block.is_typeof) {
1941519399 const is_non_error = if (operand != .none) blk: {
......@@ -19427,7 +19411,7 @@ fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index)
1942719411 return;
1942819412 }
1942919413
19430 if (!ip.funcAnalysis(sema.owner_func_index).calls_or_awaits_errorable_fn) return;
19414 if (!mod.intern_pool.funcAnalysis(sema.owner_func_index).calls_or_awaits_errorable_fn) return;
1943119415 if (!start_block.ownerModule().error_tracing) return;
1943219416
1943319417 assert(saved_index != .none); // The .error_return_trace_index field was dropped somewhere
......@@ -19470,6 +19454,7 @@ fn analyzeRet(
1947019454 block: *Block,
1947119455 uncasted_operand: Air.Inst.Ref,
1947219456 src: LazySrcLoc,
19457 operand_src: LazySrcLoc,
1947319458) CompileError!Zir.Inst.Index {
1947419459 // Special case for returning an error to an inferred error set; we need to
1947519460 // add the error tag to the inferred error set of the in-scope function, so
......@@ -19478,14 +19463,14 @@ fn analyzeRet(
1947819463 if (sema.fn_ret_ty_ies != null and sema.fn_ret_ty.zigTypeTag(mod) == .ErrorUnion) {
1947919464 try sema.addToInferredErrorSet(uncasted_operand);
1948019465 }
19481 const operand = sema.coerceExtra(block, sema.fn_ret_ty, uncasted_operand, src, .{ .is_ret = true }) catch |err| switch (err) {
19466 const operand = sema.coerceExtra(block, sema.fn_ret_ty, uncasted_operand, operand_src, .{ .is_ret = true }) catch |err| switch (err) {
1948219467 error.NotCoercible => unreachable,
1948319468 else => |e| return e,
1948419469 };
1948519470
1948619471 if (block.inlining) |inlining| {
1948719472 if (block.is_comptime) {
19488 const ret_val = try sema.resolveConstValue(block, src, operand, .{
19473 const ret_val = try sema.resolveConstValue(block, operand_src, operand, .{
1948919474 .needed_comptime_reason = "value being returned at comptime must be comptime-known",
1949019475 });
1949119476 inlining.comptime_result = operand;
......@@ -19520,7 +19505,7 @@ fn analyzeRet(
1952019505 if (sema.wantErrorReturnTracing(sema.fn_ret_ty)) {
1952119506 // Avoid adding a frame to the error return trace in case the value is comptime-known
1952219507 // to be not an error.
19523 const is_non_err = try sema.analyzeIsNonErr(block, src, operand);
19508 const is_non_err = try sema.analyzeIsNonErr(block, operand_src, operand);
1952419509 return sema.retWithErrTracing(block, src, is_non_err, air_tag, operand);
1952519510 }
1952619511
......@@ -20768,8 +20753,9 @@ fn zirIntFromBool(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
2076820753
2076920754fn zirErrorName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
2077020755 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
20771 const operand = try sema.resolveInst(inst_data.operand);
2077220756 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
20757 const uncoerced_operand = try sema.resolveInst(inst_data.operand);
20758 const operand = try sema.coerce(block, Type.anyerror, uncoerced_operand, operand_src);
2077320759
2077420760 if (try sema.resolveDefinedValue(block, operand_src, operand)) |val| {
2077520761 const err_name = sema.mod.intern_pool.indexToKey(val.toIntern()).err.name;
......@@ -23161,7 +23147,6 @@ fn zirOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
2316123147fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u64 {
2316223148 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
2316323149 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
23164 sema.src = src;
2316523150 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
2316623151 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
2316723152 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
......@@ -25394,15 +25379,21 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2539425379 } else if (extra.data.bits.has_align_ref) blk: {
2539525380 const align_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);
2539625381 extra_index += 1;
25397 const align_tv = sema.resolveInstConst(block, align_src, align_ref, .{
25382 const uncoerced_align = sema.resolveInst(align_ref) catch |err| switch (err) {
25383 error.GenericPoison => break :blk null,
25384 else => |e| return e,
25385 };
25386 const coerced_align = sema.coerce(block, Type.u29, uncoerced_align, align_src) catch |err| switch (err) {
25387 error.GenericPoison => break :blk null,
25388 else => |e| return e,
25389 };
25390 const align_val = sema.resolveConstDefinedValue(block, align_src, coerced_align, .{
2539825391 .needed_comptime_reason = "alignment must be comptime-known",
2539925392 }) catch |err| switch (err) {
25400 error.GenericPoison => {
25401 break :blk null;
25402 },
25393 error.GenericPoison => break :blk null,
2540325394 else => |e| return e,
2540425395 };
25405 const alignment = try sema.validateAlignAllowZero(block, align_src, try align_tv.val.toUnsignedIntAdvanced(sema));
25396 const alignment = try sema.validateAlignAllowZero(block, align_src, try align_val.toUnsignedIntAdvanced(sema));
2540625397 const default = target_util.defaultFunctionAlignment(target);
2540725398 break :blk if (alignment == default) .none else alignment;
2540825399 } else .none;
......@@ -25413,7 +25404,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2541325404 const body = sema.code.bodySlice(extra_index, body_len);
2541425405 extra_index += body.len;
2541525406
25416 const addrspace_ty = try sema.getBuiltinType("AddressSpace");
25407 const addrspace_ty = Type.fromInterned(.address_space_type);
2541725408 const val = try sema.resolveGenericBody(block, addrspace_src, body, inst, addrspace_ty, .{
2541825409 .needed_comptime_reason = "addrspace must be comptime-known",
2541925410 });
......@@ -25424,15 +25415,22 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2542425415 } else if (extra.data.bits.has_addrspace_ref) blk: {
2542525416 const addrspace_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);
2542625417 extra_index += 1;
25427 const addrspace_tv = sema.resolveInstConst(block, addrspace_src, addrspace_ref, .{
25418 const addrspace_ty = Type.fromInterned(.address_space_type);
25419 const uncoerced_addrspace = sema.resolveInst(addrspace_ref) catch |err| switch (err) {
25420 error.GenericPoison => break :blk null,
25421 else => |e| return e,
25422 };
25423 const coerced_addrspace = sema.coerce(block, addrspace_ty, uncoerced_addrspace, addrspace_src) catch |err| switch (err) {
25424 error.GenericPoison => break :blk null,
25425 else => |e| return e,
25426 };
25427 const addrspace_val = sema.resolveConstDefinedValue(block, addrspace_src, coerced_addrspace, .{
2542825428 .needed_comptime_reason = "addrspace must be comptime-known",
2542925429 }) catch |err| switch (err) {
25430 error.GenericPoison => {
25431 break :blk null;
25432 },
25430 error.GenericPoison => break :blk null,
2543325431 else => |e| return e,
2543425432 };
25435 break :blk mod.toEnum(std.builtin.AddressSpace, addrspace_tv.val);
25433 break :blk mod.toEnum(std.builtin.AddressSpace, addrspace_val);
2543625434 } else target_util.defaultAddressSpace(target, .function);
2543725435
2543825436 const section: Section = if (extra.data.bits.has_section_body) blk: {
......@@ -25480,15 +25478,22 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2548025478 } else if (extra.data.bits.has_cc_ref) blk: {
2548125479 const cc_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);
2548225480 extra_index += 1;
25483 const cc_tv = sema.resolveInstConst(block, cc_src, cc_ref, .{
25481 const cc_ty = Type.fromInterned(.calling_convention_type);
25482 const uncoerced_cc = sema.resolveInst(cc_ref) catch |err| switch (err) {
25483 error.GenericPoison => break :blk null,
25484 else => |e| return e,
25485 };
25486 const coerced_cc = sema.coerce(block, cc_ty, uncoerced_cc, cc_src) catch |err| switch (err) {
25487 error.GenericPoison => break :blk null,
25488 else => |e| return e,
25489 };
25490 const cc_val = sema.resolveConstDefinedValue(block, cc_src, coerced_cc, .{
2548425491 .needed_comptime_reason = "calling convention must be comptime-known",
2548525492 }) catch |err| switch (err) {
25486 error.GenericPoison => {
25487 break :blk null;
25488 },
25493 error.GenericPoison => break :blk null,
2548925494 else => |e| return e,
2549025495 };
25491 break :blk mod.toEnum(std.builtin.CallingConvention, cc_tv.val);
25496 break :blk mod.toEnum(std.builtin.CallingConvention, cc_val);
2549225497 } else if (sema.owner_decl.is_exported and has_body)
2549325498 .C
2549425499 else
......@@ -26416,12 +26421,16 @@ fn preparePanicId(sema: *Sema, block: *Block, panic_id: Module.PanicId) !InternP
2641626421 try sema.prepareSimplePanic(block);
2641726422
2641826423 const panic_messages_ty = try sema.getBuiltinType("panic_messages");
26419 const msg_decl_index = (try sema.namespaceLookup(
26424 const msg_decl_index = (sema.namespaceLookup(
2642026425 block,
26421 sema.src,
26426 .unneeded,
2642226427 panic_messages_ty.getNamespaceIndex(mod).unwrap().?,
2642326428 try mod.intern_pool.getOrPutString(gpa, @tagName(panic_id)),
26424 )).?;
26429 ) catch |err| switch (err) {
26430 error.AnalysisFail, error.NeededSourceLocation => @panic("std.builtin.panic_messages is corrupt"),
26431 error.GenericPoison, error.ComptimeReturn, error.ComptimeBreak => unreachable,
26432 error.OutOfMemory => |e| return e,
26433 }).?;
2642526434 try sema.ensureDeclAnalyzed(msg_decl_index);
2642626435 mod.panic_messages[@intFromEnum(panic_id)] = msg_decl_index.toOptional();
2642726436 return msg_decl_index;
......@@ -35645,7 +35654,6 @@ fn semaBackingIntType(mod: *Module, struct_type: InternPool.Key.StructType) Comp
3564535654
3564635655 if (small.has_backing_int) {
3564735656 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len;
35648 extra_index += @intFromBool(small.has_src_node);
3564935657 extra_index += @intFromBool(small.has_fields_len);
3565035658 extra_index += @intFromBool(small.has_decls_len);
3565135659
......@@ -36357,8 +36365,6 @@ fn structZirInfo(zir: Zir, zir_index: Zir.Inst.Index) struct {
3635736365 const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);
3635836366 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len;
3635936367
36360 extra_index += @intFromBool(small.has_src_node);
36361
3636236368 const fields_len = if (small.has_fields_len) blk: {
3636336369 const fields_len = zir.extra[extra_index];
3636436370 extra_index += 1;
......@@ -36826,7 +36832,6 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un
3682636832 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.UnionDecl).Struct.fields.len;
3682736833
3682836834 const src = LazySrcLoc.nodeOffset(0);
36829 extra_index += @intFromBool(small.has_src_node);
3683036835
3683136836 const tag_type_ref: Zir.Inst.Ref = if (small.has_tag_type) blk: {
3683236837 const ty_ref: Zir.Inst.Ref = @enumFromInt(zir.extra[extra_index]);
src/Zir.zig+108-77
......@@ -303,11 +303,11 @@ pub const Inst = struct {
303303 bool_not,
304304 /// Short-circuiting boolean `and`. `lhs` is a boolean `Ref` and the other operand
305305 /// is a block, which is evaluated if `lhs` is `true`.
306 /// Uses the `bool_br` union field.
306 /// Uses the `pl_node` union field. Payload is `BoolBr`.
307307 bool_br_and,
308308 /// Short-circuiting boolean `or`. `lhs` is a boolean `Ref` and the other operand
309309 /// is a block, which is evaluated if `lhs` is `false`.
310 /// Uses the `bool_br` union field.
310 /// Uses the `pl_node` union field. Payload is `BoolBr`.
311311 bool_br_or,
312312 /// Return a value from a block.
313313 /// Uses the `break` union field.
......@@ -592,16 +592,12 @@ pub const Inst = struct {
592592 /// Returns a pointer to the subslice.
593593 /// Uses the `pl_node` field. AST node is the slice syntax. Payload is `SliceLength`.
594594 slice_length,
595 /// Write a value to a pointer. For loading, see `load`.
596 /// Source location is assumed to be same as previous instruction.
597 /// Uses the `bin` union field.
598 store,
599595 /// Same as `store` except provides a source location.
600596 /// Uses the `pl_node` union field. Payload is `Bin`.
601597 store_node,
602 /// Same as `store` but the type of the value being stored will be used to infer
603 /// the pointer type.
604 /// Uses the `bin` union field.
598 /// Same as `store_node` but the type of the value being stored will be
599 /// used to infer the pointer type of an `alloc_inferred`.
600 /// Uses the `pl_node` union field. Payload is `Bin`.
605601 store_to_inferred_ptr,
606602 /// String Literal. Makes an anonymous Decl and then takes a pointer to it.
607603 /// Uses the `str` union field.
......@@ -1036,10 +1032,18 @@ pub const Inst = struct {
10361032 /// block, if the operand is .none or of an error/error-union type.
10371033 /// Uses the `save_err_ret_index` field.
10381034 save_err_ret_index,
1039 /// Sets error return trace to zero if no operand is given,
1040 /// otherwise sets the value to the given amount.
1041 /// Uses the `restore_err_ret_index` union field.
1042 restore_err_ret_index,
1035 /// Specialized form of `Extended.restore_err_ret_index`.
1036 /// Unconditionally restores the error return index to its last saved state
1037 /// in the block referred to by `operand`. If `operand` is `none`, restores
1038 /// to the point of function entry.
1039 /// Uses the `un_node` field.
1040 restore_err_ret_index_unconditional,
1041 /// Specialized form of `Extended.restore_err_ret_index`.
1042 /// Restores the error return index to its state at the entry of
1043 /// the current function conditional on `operand` being a non-error.
1044 /// If `operand` is `none`, restores unconditionally.
1045 /// Uses the `un_node` field.
1046 restore_err_ret_index_fn_entry,
10431047
10441048 /// The ZIR instruction tag is one of the `Extended` ones.
10451049 /// Uses the `extended` union field.
......@@ -1145,7 +1149,6 @@ pub const Inst = struct {
11451149 .shl,
11461150 .shl_sat,
11471151 .shr,
1148 .store,
11491152 .store_node,
11501153 .store_to_inferred_ptr,
11511154 .str,
......@@ -1265,7 +1268,6 @@ pub const Inst = struct {
12651268 .@"defer",
12661269 .defer_err_code,
12671270 .save_err_ret_index,
1268 .restore_err_ret_index,
12691271 .for_len,
12701272 .opt_eu_base_ptr_init,
12711273 .coerce_ptr_elem_ty,
......@@ -1290,6 +1292,8 @@ pub const Inst = struct {
12901292 .array_init_elem_type,
12911293 .array_init_elem_ptr,
12921294 .validate_ref_ty,
1295 .restore_err_ret_index_unconditional,
1296 .restore_err_ret_index_fn_entry,
12931297 => false,
12941298
12951299 .@"break",
......@@ -1338,7 +1342,6 @@ pub const Inst = struct {
13381342 .ensure_err_union_payload_void,
13391343 .set_eval_branch_quota,
13401344 .atomic_store,
1341 .store,
13421345 .store_node,
13431346 .store_to_inferred_ptr,
13441347 .resolve_inferred_alloc,
......@@ -1352,8 +1355,9 @@ pub const Inst = struct {
13521355 .check_comptime_control_flow,
13531356 .@"defer",
13541357 .defer_err_code,
1355 .restore_err_ret_index,
13561358 .save_err_ret_index,
1359 .restore_err_ret_index_unconditional,
1360 .restore_err_ret_index_fn_entry,
13571361 .validate_struct_init_ty,
13581362 .validate_struct_init_result_ty,
13591363 .validate_ptr_struct_init,
......@@ -1635,8 +1639,8 @@ pub const Inst = struct {
16351639 .declaration = .pl_node,
16361640 .suspend_block = .pl_node,
16371641 .bool_not = .un_node,
1638 .bool_br_and = .bool_br,
1639 .bool_br_or = .bool_br,
1642 .bool_br_and = .pl_node,
1643 .bool_br_or = .pl_node,
16401644 .@"break" = .@"break",
16411645 .break_inline = .@"break",
16421646 .check_comptime_control_flow = .un_node,
......@@ -1713,9 +1717,8 @@ pub const Inst = struct {
17131717 .slice_end = .pl_node,
17141718 .slice_sentinel = .pl_node,
17151719 .slice_length = .pl_node,
1716 .store = .bin,
17171720 .store_node = .pl_node,
1718 .store_to_inferred_ptr = .bin,
1721 .store_to_inferred_ptr = .pl_node,
17191722 .str = .str,
17201723 .negate = .un_node,
17211724 .negate_wrap = .un_node,
......@@ -1845,7 +1848,8 @@ pub const Inst = struct {
18451848 .defer_err_code = .defer_err_code,
18461849
18471850 .save_err_ret_index = .save_err_ret_index,
1848 .restore_err_ret_index = .restore_err_ret_index,
1851 .restore_err_ret_index_unconditional = .un_node,
1852 .restore_err_ret_index_fn_entry = .un_node,
18491853
18501854 .struct_init_empty = .un_node,
18511855 .struct_init_empty_result = .un_node,
......@@ -2075,6 +2079,13 @@ pub const Inst = struct {
20752079 /// Implements the `@inComptime` builtin.
20762080 /// `operand` is `src_node: i32`.
20772081 in_comptime,
2082 /// Restores the error return index to its last saved state in a given
2083 /// block. If the block is `.none`, restores to the state from the point
2084 /// of function entry. If the operand is not `.none`, the restore is
2085 /// conditional on the operand value not being an error.
2086 /// `operand` is payload index to `RestoreErrRetIndex`.
2087 /// `small` is undefined.
2088 restore_err_ret_index,
20782089 /// Used as a placeholder instruction which is just a dummy index for Sema to replace
20792090 /// with a specific value. For instance, this is used for the capture of an `errdefer`.
20802091 /// This should never appear in a body.
......@@ -2345,11 +2356,6 @@ pub const Inst = struct {
23452356 return LazySrcLoc.nodeOffset(self.src_node);
23462357 }
23472358 },
2348 bool_br: struct {
2349 lhs: Ref,
2350 /// Points to a `Block`.
2351 payload_index: u32,
2352 },
23532359 @"unreachable": struct {
23542360 /// Offset from Decl AST node index.
23552361 /// `Tag` determines which kind of AST node this points to.
......@@ -2396,10 +2402,6 @@ pub const Inst = struct {
23962402 save_err_ret_index: struct {
23972403 operand: Ref, // If error type (or .none), save new trace index
23982404 },
2399 restore_err_ret_index: struct {
2400 block: Ref, // If restored, the index is from this block's entrypoint
2401 operand: Ref, // If non-error (or .none), then restore the index
2402 },
24032405 elem_val_imm: struct {
24042406 /// The indexable value being accessed.
24052407 operand: Ref,
......@@ -2435,7 +2437,6 @@ pub const Inst = struct {
24352437 float,
24362438 ptr_type,
24372439 int_type,
2438 bool_br,
24392440 @"unreachable",
24402441 @"break",
24412442 dbg_stmt,
......@@ -2444,7 +2445,6 @@ pub const Inst = struct {
24442445 @"defer",
24452446 defer_err_code,
24462447 save_err_ret_index,
2447 restore_err_ret_index,
24482448 elem_val_imm,
24492449 };
24502450 };
......@@ -2630,6 +2630,13 @@ pub const Inst = struct {
26302630 body_len: u32,
26312631 };
26322632
2633 /// Trailing:
2634 /// * inst: Index // for each `body_len`
2635 pub const BoolBr = struct {
2636 lhs: Ref,
2637 body_len: u32,
2638 };
2639
26332640 /// Trailing:
26342641 /// 0. doc_comment: u32 // if `has_doc_comment`; null-terminated string index
26352642 /// 1. align_body_len: u32 // if `has_align_linksection_addrspace`; 0 means no `align`
......@@ -3015,20 +3022,19 @@ pub const Inst = struct {
30153022 };
30163023
30173024 /// Trailing:
3018 /// 0. src_node: i32, // if has_src_node
3019 /// 1. fields_len: u32, // if has_fields_len
3020 /// 2. decls_len: u32, // if has_decls_len
3021 /// 3. backing_int_body_len: u32, // if has_backing_int
3022 /// 4. backing_int_ref: Ref, // if has_backing_int and backing_int_body_len is 0
3023 /// 5. backing_int_body_inst: Inst, // if has_backing_int and backing_int_body_len is > 0
3024 /// 6. decl: Index, // for every decls_len; points to a `declaration` instruction
3025 /// 7. flags: u32 // for every 8 fields
3025 /// 0. fields_len: u32, // if has_fields_len
3026 /// 1. decls_len: u32, // if has_decls_len
3027 /// 2. backing_int_body_len: u32, // if has_backing_int
3028 /// 3. backing_int_ref: Ref, // if has_backing_int and backing_int_body_len is 0
3029 /// 4. backing_int_body_inst: Inst, // if has_backing_int and backing_int_body_len is > 0
3030 /// 5. decl: Index, // for every decls_len; points to a `declaration` instruction
3031 /// 6. flags: u32 // for every 8 fields
30263032 /// - sets of 4 bits:
30273033 /// 0b000X: whether corresponding field has an align expression
30283034 /// 0b00X0: whether corresponding field has a default expression
30293035 /// 0b0X00: whether corresponding field is comptime
30303036 /// 0bX000: whether corresponding field has a type expression
3031 /// 8. fields: { // for every fields_len
3037 /// 7. fields: { // for every fields_len
30323038 /// field_name: u32, // if !is_tuple
30333039 /// doc_comment: NullTerminatedString, // .empty if no doc comment
30343040 /// field_type: Ref, // if corresponding bit is not set. none means anytype.
......@@ -3036,7 +3042,7 @@ pub const Inst = struct {
30363042 /// align_body_len: u32, // if corresponding bit is set
30373043 /// init_body_len: u32, // if corresponding bit is set
30383044 /// }
3039 /// 10. bodies: { // for every fields_len
3045 /// 8. bodies: { // for every fields_len
30403046 /// field_type_body_inst: Inst, // for each field_type_body_len
30413047 /// align_body_inst: Inst, // for each align_body_len
30423048 /// init_body_inst: Inst, // for each init_body_len
......@@ -3048,8 +3054,13 @@ pub const Inst = struct {
30483054 fields_hash_1: u32,
30493055 fields_hash_2: u32,
30503056 fields_hash_3: u32,
3057 src_node: i32,
3058
3059 pub fn src(self: StructDecl) LazySrcLoc {
3060 return LazySrcLoc.nodeOffset(self.src_node);
3061 }
3062
30513063 pub const Small = packed struct {
3052 has_src_node: bool,
30533064 has_fields_len: bool,
30543065 has_decls_len: bool,
30553066 has_backing_int: bool,
......@@ -3061,7 +3072,7 @@ pub const Inst = struct {
30613072 any_default_inits: bool,
30623073 any_comptime_fields: bool,
30633074 any_aligned_fields: bool,
3064 _: u2 = undefined,
3075 _: u3 = undefined,
30653076 };
30663077 };
30673078
......@@ -3095,16 +3106,15 @@ pub const Inst = struct {
30953106 };
30963107
30973108 /// Trailing:
3098 /// 0. src_node: i32, // if has_src_node
3099 /// 1. tag_type: Ref, // if has_tag_type
3100 /// 2. body_len: u32, // if has_body_len
3101 /// 3. fields_len: u32, // if has_fields_len
3102 /// 4. decls_len: u32, // if has_decls_len
3103 /// 5. decl: Index, // for every decls_len; points to a `declaration` instruction
3104 /// 6. inst: Index // for every body_len
3105 /// 7. has_bits: u32 // for every 32 fields
3109 /// 0. tag_type: Ref, // if has_tag_type
3110 /// 1. body_len: u32, // if has_body_len
3111 /// 2. fields_len: u32, // if has_fields_len
3112 /// 3. decls_len: u32, // if has_decls_len
3113 /// 4. decl: Index, // for every decls_len; points to a `declaration` instruction
3114 /// 5. inst: Index // for every body_len
3115 /// 6. has_bits: u32 // for every 32 fields
31063116 /// - the bit is whether corresponding field has an value expression
3107 /// 8. fields: { // for every fields_len
3117 /// 7. fields: { // for every fields_len
31083118 /// field_name: u32,
31093119 /// doc_comment: u32, // .empty if no doc_comment
31103120 /// value: Ref, // if corresponding bit is set
......@@ -3116,33 +3126,37 @@ pub const Inst = struct {
31163126 fields_hash_1: u32,
31173127 fields_hash_2: u32,
31183128 fields_hash_3: u32,
3129 src_node: i32,
3130
3131 pub fn src(self: EnumDecl) LazySrcLoc {
3132 return LazySrcLoc.nodeOffset(self.src_node);
3133 }
3134
31193135 pub const Small = packed struct {
3120 has_src_node: bool,
31213136 has_tag_type: bool,
31223137 has_body_len: bool,
31233138 has_fields_len: bool,
31243139 has_decls_len: bool,
31253140 name_strategy: NameStrategy,
31263141 nonexhaustive: bool,
3127 _: u8 = undefined,
3142 _: u9 = undefined,
31283143 };
31293144 };
31303145
31313146 /// Trailing:
3132 /// 0. src_node: i32, // if has_src_node
3133 /// 1. tag_type: Ref, // if has_tag_type
3134 /// 2. body_len: u32, // if has_body_len
3135 /// 3. fields_len: u32, // if has_fields_len
3136 /// 4. decls_len: u32, // if has_decls_len
3137 /// 5. decl: Index, // for every decls_len; points to a `declaration` instruction
3138 /// 6. inst: Index // for every body_len
3139 /// 7. has_bits: u32 // for every 8 fields
3147 /// 0. tag_type: Ref, // if has_tag_type
3148 /// 1. body_len: u32, // if has_body_len
3149 /// 2. fields_len: u32, // if has_fields_len
3150 /// 3. decls_len: u32, // if has_decls_len
3151 /// 4. decl: Index, // for every decls_len; points to a `declaration` instruction
3152 /// 5. inst: Index // for every body_len
3153 /// 6. has_bits: u32 // for every 8 fields
31403154 /// - sets of 4 bits:
31413155 /// 0b000X: whether corresponding field has a type expression
31423156 /// 0b00X0: whether corresponding field has a align expression
31433157 /// 0b0X00: whether corresponding field has a tag value expression
31443158 /// 0bX000: unused
3145 /// 8. fields: { // for every fields_len
3159 /// 7. fields: { // for every fields_len
31463160 /// field_name: NullTerminatedString, // null terminated string index
31473161 /// doc_comment: NullTerminatedString, // .empty if no doc comment
31483162 /// field_type: Ref, // if corresponding bit is set
......@@ -3157,8 +3171,13 @@ pub const Inst = struct {
31573171 fields_hash_1: u32,
31583172 fields_hash_2: u32,
31593173 fields_hash_3: u32,
3174 src_node: i32,
3175
3176 pub fn src(self: UnionDecl) LazySrcLoc {
3177 return LazySrcLoc.nodeOffset(self.src_node);
3178 }
3179
31603180 pub const Small = packed struct {
3161 has_src_node: bool,
31623181 has_tag_type: bool,
31633182 has_body_len: bool,
31643183 has_fields_len: bool,
......@@ -3173,20 +3192,24 @@ pub const Inst = struct {
31733192 /// true | false | union(T) { }
31743193 auto_enum_tag: bool,
31753194 any_aligned_fields: bool,
3176 _: u5 = undefined,
3195 _: u6 = undefined,
31773196 };
31783197 };
31793198
31803199 /// Trailing:
3181 /// 0. src_node: i32, // if has_src_node
3182 /// 1. decls_len: u32, // if has_decls_len
3183 /// 2. decl: Index, // for every decls_len; points to a `declaration` instruction
3200 /// 0. decls_len: u32, // if has_decls_len
3201 /// 1. decl: Index, // for every decls_len; points to a `declaration` instruction
31843202 pub const OpaqueDecl = struct {
3203 src_node: i32,
3204
3205 pub fn src(self: OpaqueDecl) LazySrcLoc {
3206 return LazySrcLoc.nodeOffset(self.src_node);
3207 }
3208
31853209 pub const Small = packed struct {
3186 has_src_node: bool,
31873210 has_decls_len: bool,
31883211 name_strategy: NameStrategy,
3189 _: u12 = undefined,
3212 _: u13 = undefined,
31903213 };
31913214 };
31923215
......@@ -3439,6 +3462,18 @@ pub const Inst = struct {
34393462 /// The RHS of the array multiplication.
34403463 rhs: Ref,
34413464 };
3465
3466 pub const RestoreErrRetIndex = struct {
3467 src_node: i32,
3468 /// If `.none`, restore the trace to its state upon function entry.
3469 block: Ref,
3470 /// If `.none`, restore unconditionally.
3471 operand: Ref,
3472
3473 pub fn src(self: RestoreErrRetIndex) LazySrcLoc {
3474 return LazySrcLoc.nodeOffset(self.src_node);
3475 }
3476 };
34423477};
34433478
34443479pub const SpecialProng = enum { none, @"else", under };
......@@ -3476,7 +3511,6 @@ pub fn declIterator(zir: Zir, decl_inst: Zir.Inst.Index) DeclIterator {
34763511 .struct_decl => {
34773512 const small: Inst.StructDecl.Small = @bitCast(extended.small);
34783513 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.StructDecl).Struct.fields.len);
3479 extra_index += @intFromBool(small.has_src_node);
34803514 extra_index += @intFromBool(small.has_fields_len);
34813515 const decls_len = if (small.has_decls_len) decls_len: {
34823516 const decls_len = zir.extra[extra_index];
......@@ -3503,7 +3537,6 @@ pub fn declIterator(zir: Zir, decl_inst: Zir.Inst.Index) DeclIterator {
35033537 .enum_decl => {
35043538 const small: Inst.EnumDecl.Small = @bitCast(extended.small);
35053539 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.EnumDecl).Struct.fields.len);
3506 extra_index += @intFromBool(small.has_src_node);
35073540 extra_index += @intFromBool(small.has_tag_type);
35083541 extra_index += @intFromBool(small.has_body_len);
35093542 extra_index += @intFromBool(small.has_fields_len);
......@@ -3522,7 +3555,6 @@ pub fn declIterator(zir: Zir, decl_inst: Zir.Inst.Index) DeclIterator {
35223555 .union_decl => {
35233556 const small: Inst.UnionDecl.Small = @bitCast(extended.small);
35243557 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.UnionDecl).Struct.fields.len);
3525 extra_index += @intFromBool(small.has_src_node);
35263558 extra_index += @intFromBool(small.has_tag_type);
35273559 extra_index += @intFromBool(small.has_body_len);
35283560 extra_index += @intFromBool(small.has_fields_len);
......@@ -3540,8 +3572,7 @@ pub fn declIterator(zir: Zir, decl_inst: Zir.Inst.Index) DeclIterator {
35403572 },
35413573 .opaque_decl => {
35423574 const small: Inst.OpaqueDecl.Small = @bitCast(extended.small);
3543 var extra_index: u32 = extended.operand;
3544 extra_index += @intFromBool(small.has_src_node);
3575 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.OpaqueDecl).Struct.fields.len);
35453576 const decls_len = if (small.has_decls_len) decls_len: {
35463577 const decls_len = zir.extra[extra_index];
35473578 extra_index += 1;
src/print_zir.zig+30-58
......@@ -199,10 +199,6 @@ const Writer = struct {
199199 const tag = tags[@intFromEnum(inst)];
200200 try stream.print("= {s}(", .{@tagName(tags[@intFromEnum(inst)])});
201201 switch (tag) {
202 .store,
203 .store_to_inferred_ptr,
204 => try self.writeBin(stream, inst),
205
206202 .alloc,
207203 .alloc_mut,
208204 .alloc_comptime_mut,
......@@ -280,6 +276,8 @@ const Writer = struct {
280276 .validate_deref,
281277 .check_comptime_control_flow,
282278 .opt_eu_base_ptr_init,
279 .restore_err_ret_index_unconditional,
280 .restore_err_ret_index_fn_entry,
283281 => try self.writeUnNode(stream, inst),
284282
285283 .ref,
......@@ -303,7 +301,6 @@ const Writer = struct {
303301 .int_type => try self.writeIntType(stream, inst),
304302
305303 .save_err_ret_index => try self.writeSaveErrRetIndex(stream, inst),
306 .restore_err_ret_index => try self.writeRestoreErrRetIndex(stream, inst),
307304
308305 .@"break",
309306 .break_inline,
......@@ -392,6 +389,7 @@ const Writer = struct {
392389 .shr_exact,
393390 .xor,
394391 .store_node,
392 .store_to_inferred_ptr,
395393 .error_union_type,
396394 .merge_error_sets,
397395 .bit_and,
......@@ -615,6 +613,8 @@ const Writer = struct {
615613 .cmpxchg => try self.writeCmpxchg(stream, extended),
616614 .ptr_cast_full => try self.writePtrCastFull(stream, extended),
617615 .ptr_cast_no_dest => try self.writePtrCastNoDest(stream, extended),
616
617 .restore_err_ret_index => try self.writeRestoreErrRetIndex(stream, extended),
618618 }
619619 }
620620
......@@ -624,14 +624,6 @@ const Writer = struct {
624624 try self.writeSrc(stream, src);
625625 }
626626
627 fn writeBin(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
628 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].bin;
629 try self.writeInstRef(stream, inst_data.lhs);
630 try stream.writeAll(", ");
631 try self.writeInstRef(stream, inst_data.rhs);
632 try stream.writeByte(')');
633 }
634
635627 fn writeArrayInitElemType(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
636628 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].bin;
637629 try self.writeInstRef(stream, inst_data.lhs);
......@@ -1413,12 +1405,6 @@ const Writer = struct {
14131405
14141406 var extra_index: usize = extra.end;
14151407
1416 const src_node: ?i32 = if (small.has_src_node) blk: {
1417 const src_node = @as(i32, @bitCast(self.code.extra[extra_index]));
1418 extra_index += 1;
1419 break :blk src_node;
1420 } else null;
1421
14221408 const fields_len = if (small.has_fields_len) blk: {
14231409 const fields_len = self.code.extra[extra_index];
14241410 extra_index += 1;
......@@ -1461,7 +1447,7 @@ const Writer = struct {
14611447 try stream.writeAll("{}, ");
14621448 } else {
14631449 const prev_parent_decl_node = self.parent_decl_node;
1464 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);
1450 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
14651451 defer self.parent_decl_node = prev_parent_decl_node;
14661452
14671453 try stream.writeAll("{\n");
......@@ -1542,7 +1528,7 @@ const Writer = struct {
15421528 }
15431529
15441530 const prev_parent_decl_node = self.parent_decl_node;
1545 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);
1531 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
15461532 try stream.writeAll("{\n");
15471533 self.indent += 2;
15481534
......@@ -1595,7 +1581,7 @@ const Writer = struct {
15951581 try stream.writeByteNTimes(' ', self.indent);
15961582 try stream.writeAll("})");
15971583 }
1598 try self.writeSrcNode(stream, src_node);
1584 try self.writeSrcNode(stream, extra.data.src_node);
15991585 }
16001586
16011587 fn writeUnionDecl(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
......@@ -1613,12 +1599,6 @@ const Writer = struct {
16131599
16141600 var extra_index: usize = extra.end;
16151601
1616 const src_node: ?i32 = if (small.has_src_node) blk: {
1617 const src_node = @as(i32, @bitCast(self.code.extra[extra_index]));
1618 extra_index += 1;
1619 break :blk src_node;
1620 } else null;
1621
16221602 const tag_type_ref = if (small.has_tag_type) blk: {
16231603 const tag_type_ref = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));
16241604 extra_index += 1;
......@@ -1652,7 +1632,7 @@ const Writer = struct {
16521632 try stream.writeAll("{}");
16531633 } else {
16541634 const prev_parent_decl_node = self.parent_decl_node;
1655 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);
1635 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
16561636 defer self.parent_decl_node = prev_parent_decl_node;
16571637
16581638 try stream.writeAll("{\n");
......@@ -1671,7 +1651,7 @@ const Writer = struct {
16711651
16721652 if (fields_len == 0) {
16731653 try stream.writeAll("})");
1674 try self.writeSrcNode(stream, src_node);
1654 try self.writeSrcNode(stream, extra.data.src_node);
16751655 return;
16761656 }
16771657 try stream.writeAll(", ");
......@@ -1680,7 +1660,7 @@ const Writer = struct {
16801660 extra_index += body.len;
16811661
16821662 const prev_parent_decl_node = self.parent_decl_node;
1683 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);
1663 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
16841664 try self.writeBracedDecl(stream, body);
16851665 try stream.writeAll(", {\n");
16861666
......@@ -1748,7 +1728,7 @@ const Writer = struct {
17481728 self.indent -= 2;
17491729 try stream.writeByteNTimes(' ', self.indent);
17501730 try stream.writeAll("})");
1751 try self.writeSrcNode(stream, src_node);
1731 try self.writeSrcNode(stream, extra.data.src_node);
17521732 }
17531733
17541734 fn writeEnumDecl(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
......@@ -1766,12 +1746,6 @@ const Writer = struct {
17661746
17671747 var extra_index: usize = extra.end;
17681748
1769 const src_node: ?i32 = if (small.has_src_node) blk: {
1770 const src_node = @as(i32, @bitCast(self.code.extra[extra_index]));
1771 extra_index += 1;
1772 break :blk src_node;
1773 } else null;
1774
17751749 const tag_type_ref = if (small.has_tag_type) blk: {
17761750 const tag_type_ref = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));
17771751 extra_index += 1;
......@@ -1803,7 +1777,7 @@ const Writer = struct {
18031777 try stream.writeAll("{}, ");
18041778 } else {
18051779 const prev_parent_decl_node = self.parent_decl_node;
1806 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);
1780 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
18071781 defer self.parent_decl_node = prev_parent_decl_node;
18081782
18091783 try stream.writeAll("{\n");
......@@ -1824,7 +1798,7 @@ const Writer = struct {
18241798 extra_index += body.len;
18251799
18261800 const prev_parent_decl_node = self.parent_decl_node;
1827 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);
1801 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
18281802 try self.writeBracedDecl(stream, body);
18291803 if (fields_len == 0) {
18301804 try stream.writeAll(", {})");
......@@ -1872,7 +1846,7 @@ const Writer = struct {
18721846 try stream.writeByteNTimes(' ', self.indent);
18731847 try stream.writeAll("})");
18741848 }
1875 try self.writeSrcNode(stream, src_node);
1849 try self.writeSrcNode(stream, extra.data.src_node);
18761850 }
18771851
18781852 fn writeOpaqueDecl(
......@@ -1881,13 +1855,8 @@ const Writer = struct {
18811855 extended: Zir.Inst.Extended.InstData,
18821856 ) !void {
18831857 const small = @as(Zir.Inst.OpaqueDecl.Small, @bitCast(extended.small));
1884 var extra_index: usize = extended.operand;
1885
1886 const src_node: ?i32 = if (small.has_src_node) blk: {
1887 const src_node = @as(i32, @bitCast(self.code.extra[extra_index]));
1888 extra_index += 1;
1889 break :blk src_node;
1890 } else null;
1858 const extra = self.code.extraData(Zir.Inst.OpaqueDecl, extended.operand);
1859 var extra_index: usize = extra.end;
18911860
18921861 const decls_len = if (small.has_decls_len) blk: {
18931862 const decls_len = self.code.extra[extra_index];
......@@ -1901,7 +1870,7 @@ const Writer = struct {
19011870 try stream.writeAll("{})");
19021871 } else {
19031872 const prev_parent_decl_node = self.parent_decl_node;
1904 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);
1873 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
19051874 defer self.parent_decl_node = prev_parent_decl_node;
19061875
19071876 try stream.writeAll("{\n");
......@@ -1911,7 +1880,7 @@ const Writer = struct {
19111880 try stream.writeByteNTimes(' ', self.indent);
19121881 try stream.writeAll("})");
19131882 }
1914 try self.writeSrcNode(stream, src_node);
1883 try self.writeSrcNode(stream, extra.data.src_node);
19151884 }
19161885
19171886 fn writeErrorSetDecl(
......@@ -2505,12 +2474,14 @@ const Writer = struct {
25052474 }
25062475
25072476 fn writeBoolBr(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
2508 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].bool_br;
2509 const extra = self.code.extraData(Zir.Inst.Block, inst_data.payload_index);
2477 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
2478 const extra = self.code.extraData(Zir.Inst.BoolBr, inst_data.payload_index);
25102479 const body = self.code.bodySlice(extra.end, extra.data.body_len);
2511 try self.writeInstRef(stream, inst_data.lhs);
2480 try self.writeInstRef(stream, extra.data.lhs);
25122481 try stream.writeAll(", ");
25132482 try self.writeBracedBody(stream, body);
2483 try stream.writeAll(") ");
2484 try self.writeSrc(stream, inst_data.src());
25142485 }
25152486
25162487 fn writeIntType(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
......@@ -2531,13 +2502,14 @@ const Writer = struct {
25312502 try stream.writeAll(")");
25322503 }
25332504
2534 fn writeRestoreErrRetIndex(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
2535 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].restore_err_ret_index;
2505 fn writeRestoreErrRetIndex(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
2506 const extra = self.code.extraData(Zir.Inst.RestoreErrRetIndex, extended.operand).data;
25362507
2537 try self.writeInstRef(stream, inst_data.block);
2538 try self.writeInstRef(stream, inst_data.operand);
2508 try self.writeInstRef(stream, extra.block);
2509 try self.writeInstRef(stream, extra.operand);
25392510
2540 try stream.writeAll(")");
2511 try stream.writeAll(") ");
2512 try self.writeSrc(stream, extra.src());
25412513 }
25422514
25432515 fn writeBreak(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
test/cases/compile_errors/non_constant_expression_in_array_size.zig+2-2
......@@ -14,6 +14,6 @@ export fn entry() usize {
1414// backend=stage2
1515// target=native
1616//
17// :6:5: error: unable to resolve comptime value
18// :6:5: note: value being returned at comptime must be comptime-known
17// :6:12: error: unable to resolve comptime value
18// :6:12: note: value being returned at comptime must be comptime-known
1919// :2:12: note: called from here