authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-12-29 16:24:07+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-12-31 09:55:03+00:00
log6026a5f217398b202b92a4ecc2e129691bbb3a69
treeee077302bdb0c28d620eb6233d23cd02360a5684
parent6d67658965bc298a697dc756a4e06bda144427de
signaturelock-open Commit is signed but in an unrecognized format.

compiler: ensure result of `block_comptime` is comptime-known

To avoid this PR regressing error messages, most of the work here has gone towards improving error notes for why code was comptime-evaluated. ZIR `block_comptime` now stores a "comptime reason", the enum for which is also used by Sema. There are two types in Sema: * `ComptimeReason` represents the reason we started evaluating something at comptime. * `BlockComptimeReason` represents the reason a given block is evaluated at comptime; it's either a `ComptimeReason` with an attached source location, or it's because we're in a function which was called at comptime (and that function's `Block` should be consulted for the "parent" reason). Every `Block` stores a `?BlockComptimeReason`. The old `is_comptime` field is replaced with a trivial `isComptime()` method which returns whether that reason is non-`null`. Lastly, the handling for `block_comptime` has been simplified. It was previously going through an unnecessary runtime-handling path; now, it is a trivial sub block exited through a `break_inline` instruction. Resolves: #22296

7 files changed, 821 insertions(+), 658 deletions(-)

lib/std/zig.zig+159
......@@ -718,6 +718,165 @@ pub const EnvVar = enum {
718718 }
719719};
720720
721pub const SimpleComptimeReason = enum(u32) {
722 // Evaluating at comptime because a builtin operand must be comptime-known.
723 // These messages all mention a specific builtin.
724 operand_Type,
725 operand_setEvalBranchQuota,
726 operand_setFloatMode,
727 operand_branchHint,
728 operand_setRuntimeSafety,
729 operand_embedFile,
730 operand_cImport,
731 operand_cDefine_macro_name,
732 operand_cDefine_macro_value,
733 operand_cInclude_file_name,
734 operand_cUndef_macro_name,
735 operand_shuffle_mask,
736 operand_atomicRmw_operation,
737 operand_reduce_operation,
738
739 // Evaluating at comptime because an operand must be comptime-known.
740 // These messages do not mention a specific builtin (and may not be about a builtin at all).
741 export_target,
742 export_options,
743 extern_options,
744 prefetch_options,
745 call_modifier,
746 compile_error_string,
747 inline_assembly_code,
748 atomic_order,
749 array_mul_factor,
750 slice_cat_operand,
751 comptime_call_target,
752 wasm_memory_index,
753 work_group_dim_index,
754
755 // Evaluating at comptime because types must be comptime-known.
756 // Reasons other than `.type` are just more specific messages.
757 type,
758 array_sentinel,
759 pointer_sentinel,
760 slice_sentinel,
761 array_length,
762 vector_length,
763 error_set_contents,
764 struct_fields,
765 enum_fields,
766 union_fields,
767 function_ret_ty,
768 function_parameters,
769
770 // Evaluating at comptime because decl/field name must be comptime-known.
771 decl_name,
772 field_name,
773 struct_field_name,
774 enum_field_name,
775 union_field_name,
776 tuple_field_name,
777 tuple_field_index,
778
779 // Evaluating at comptime because it is an attribute of a global declaration.
780 container_var_init,
781 @"callconv",
782 @"align",
783 @"addrspace",
784 @"linksection",
785
786 // Miscellaneous reasons.
787 comptime_keyword,
788 comptime_call_modifier,
789 switch_item,
790 tuple_field_default_value,
791 struct_field_default_value,
792 enum_field_tag_value,
793 slice_single_item_ptr_bounds,
794 comptime_param_arg,
795 stored_to_comptime_field,
796 stored_to_comptime_var,
797 casted_to_comptime_enum,
798 casted_to_comptime_int,
799 casted_to_comptime_float,
800 panic_handler,
801
802 pub fn message(r: SimpleComptimeReason) []const u8 {
803 return switch (r) {
804 // zig fmt: off
805 .operand_Type => "operand to '@Type' must be comptime-known",
806 .operand_setEvalBranchQuota => "operand to '@setEvalBranchQuota' must be comptime-known",
807 .operand_setFloatMode => "operand to '@setFloatMode' must be comptime-known",
808 .operand_branchHint => "operand to '@branchHint' must be comptime-known",
809 .operand_setRuntimeSafety => "operand to '@setRuntimeSafety' must be comptime-known",
810 .operand_embedFile => "operand to '@embedFile' must be comptime-known",
811 .operand_cImport => "operand to '@cImport' is evaluated at comptime",
812 .operand_cDefine_macro_name => "'@cDefine' macro name must be comptime-known",
813 .operand_cDefine_macro_value => "'@cDefine' macro value must be comptime-known",
814 .operand_cInclude_file_name => "'@cInclude' file name must be comptime-known",
815 .operand_cUndef_macro_name => "'@cUndef' macro name must be comptime-known",
816 .operand_shuffle_mask => "'@shuffle' mask must be comptime-known",
817 .operand_atomicRmw_operation => "'@atomicRmw' operation must be comptime-known",
818 .operand_reduce_operation => "'@reduce' operation must be comptime-known",
819
820 .export_target => "export target must be comptime-known",
821 .export_options => "export options must be comptime-known",
822 .extern_options => "extern options must be comptime-known",
823 .prefetch_options => "prefetch options must be comptime-known",
824 .call_modifier => "call modifier must be comptime-known",
825 .compile_error_string => "compile error string must be comptime-known",
826 .inline_assembly_code => "inline assembly code must be comptime-known",
827 .atomic_order => "atomic order must be comptime-known",
828 .array_mul_factor => "array multiplication factor must be comptime-known",
829 .slice_cat_operand => "slice being concatenated must be comptime-known",
830 .comptime_call_target => "function being called at comptime must be comptime-known",
831 .wasm_memory_index => "wasm memory index must be comptime-known",
832 .work_group_dim_index => "work group dimension index must be comptime-known",
833
834 .type => "types must be comptime-known",
835 .array_sentinel => "array sentinel value must be comptime-known",
836 .pointer_sentinel => "pointer sentinel value must be comptime-known",
837 .slice_sentinel => "slice sentinel value must be comptime-known",
838 .array_length => "array length must be comptime-known",
839 .vector_length => "vector length must be comptime-known",
840 .error_set_contents => "error set contents must be comptime-known",
841 .struct_fields => "struct fields must be comptime-known",
842 .enum_fields => "enum fields must be comptime-known",
843 .union_fields => "union fields must be comptime-known",
844 .function_ret_ty => "function return type must be comptime-known",
845 .function_parameters => "function parameters must be comptime-known",
846
847 .decl_name => "declaration name must be comptime-known",
848 .field_name => "field name must be comptime-known",
849 .struct_field_name => "struct field name must be comptime-known",
850 .enum_field_name => "enum field name must be comptime-known",
851 .union_field_name => "union field name must be comptime-known",
852 .tuple_field_name => "tuple field name must be comptime-known",
853 .tuple_field_index => "tuple field index must be comptime-known",
854
855 .container_var_init => "initializer of container-level variable must be comptime-known",
856 .@"callconv" => "calling convention must be comptime-known",
857 .@"align" => "alignment must be comptime-known",
858 .@"addrspace" => "address space must be comptime-known",
859 .@"linksection" => "linksection must be comptime-known",
860
861 .comptime_keyword => "'comptime' keyword forces comptime evaluation",
862 .comptime_call_modifier => "'.compile_time' call modifier forces comptime evaluation",
863 .switch_item => "switch prong values must be comptime-known",
864 .tuple_field_default_value => "tuple field default value must be comptime-known",
865 .struct_field_default_value => "struct field default value must be comptime-known",
866 .enum_field_tag_value => "enum field tag value must be comptime-known",
867 .slice_single_item_ptr_bounds => "slice of single-item pointer must have comptime-known bounds",
868 .comptime_param_arg => "argument to comptime parameter must be comptime-known",
869 .stored_to_comptime_field => "value stored to a comptime field must be comptime-known",
870 .stored_to_comptime_var => "value stored to a comptime variable must be comptime-known",
871 .casted_to_comptime_enum => "value casted to enum with 'comptime_int' tag type must be comptime-known",
872 .casted_to_comptime_int => "value casted to 'comptime_int' must be comptime-known",
873 .casted_to_comptime_float => "value casted to 'comptime_float' must be comptime-known",
874 .panic_handler => "panic handler must be comptime-known",
875 // zig fmt: on
876 };
877 }
878};
879
721880test {
722881 _ = Ast;
723882 _ = AstRlAnnotate;
lib/std/zig/AstGen.zig+146-65
......@@ -97,6 +97,7 @@ fn setExtra(astgen: *AstGen, index: usize, extra: anytype) void {
9797 Zir.Inst.Ref,
9898 Zir.Inst.Index,
9999 Zir.Inst.Declaration.Name,
100 std.zig.SimpleComptimeReason,
100101 Zir.NullTerminatedString,
101102 => @intFromEnum(@field(extra, field.name)),
102103
......@@ -379,7 +380,7 @@ const coerced_type_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .type_type } };
379380const coerced_bool_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .bool_type } };
380381
381382fn typeExpr(gz: *GenZir, scope: *Scope, type_node: Ast.Node.Index) InnerError!Zir.Inst.Ref {
382 return comptimeExpr(gz, scope, coerced_type_ri, type_node);
383 return comptimeExpr(gz, scope, coerced_type_ri, type_node, .type);
383384}
384385
385386fn reachableTypeExpr(
......@@ -388,7 +389,7 @@ fn reachableTypeExpr(
388389 type_node: Ast.Node.Index,
389390 reachable_node: Ast.Node.Index,
390391) InnerError!Zir.Inst.Ref {
391 return reachableExprComptime(gz, scope, coerced_type_ri, type_node, reachable_node, true);
392 return reachableExprComptime(gz, scope, coerced_type_ri, type_node, reachable_node, .type);
392393}
393394
394395/// Same as `expr` but fails with a compile error if the result type is `noreturn`.
......@@ -399,7 +400,7 @@ fn reachableExpr(
399400 node: Ast.Node.Index,
400401 reachable_node: Ast.Node.Index,
401402) InnerError!Zir.Inst.Ref {
402 return reachableExprComptime(gz, scope, ri, node, reachable_node, false);
403 return reachableExprComptime(gz, scope, ri, node, reachable_node, null);
403404}
404405
405406fn reachableExprComptime(
......@@ -408,10 +409,11 @@ fn reachableExprComptime(
408409 ri: ResultInfo,
409410 node: Ast.Node.Index,
410411 reachable_node: Ast.Node.Index,
411 force_comptime: bool,
412 /// If `null`, the expression is not evaluated in a comptime context.
413 comptime_reason: ?std.zig.SimpleComptimeReason,
412414) InnerError!Zir.Inst.Ref {
413 const result_inst = if (force_comptime)
414 try comptimeExpr(gz, scope, ri, node)
415 const result_inst = if (comptime_reason) |r|
416 try comptimeExpr(gz, scope, ri, node, r)
415417 else
416418 try expr(gz, scope, ri, node);
417419
......@@ -782,7 +784,7 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
782784 const result = try gz.addPlNode(.array_mul, node, Zir.Inst.ArrayMul{
783785 .res_ty = if (try ri.rl.resultType(gz, node)) |t| t else .none,
784786 .lhs = try expr(gz, scope, .{ .rl = .none }, node_datas[node].lhs),
785 .rhs = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[node].rhs),
787 .rhs = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, node_datas[node].rhs, .array_mul_factor),
786788 });
787789 return rvalue(gz, ri, result, node);
788790 },
......@@ -1453,7 +1455,7 @@ fn arrayInitExpr(
14531455 });
14541456 break :inst .{ array_type_inst, elem_type };
14551457 } else {
1456 const sentinel = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = elem_type } }, array_type.ast.sentinel);
1458 const sentinel = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = elem_type } }, array_type.ast.sentinel, .array_sentinel);
14571459 const array_type_inst = try gz.addPlNode(
14581460 .array_type_sentinel,
14591461 array_init.ast.type_expr,
......@@ -1721,7 +1723,7 @@ fn structInitExpr(
17211723 .rhs = elem_type,
17221724 });
17231725 } else blk: {
1724 const sentinel = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = elem_type } }, array_type.ast.sentinel);
1726 const sentinel = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = elem_type } }, array_type.ast.sentinel, .array_sentinel);
17251727 break :blk try gz.addPlNode(
17261728 .array_type_sentinel,
17271729 struct_init.ast.type_expr,
......@@ -1966,6 +1968,20 @@ fn comptimeExpr(
19661968 scope: *Scope,
19671969 ri: ResultInfo,
19681970 node: Ast.Node.Index,
1971 reason: std.zig.SimpleComptimeReason,
1972) InnerError!Zir.Inst.Ref {
1973 return comptimeExpr2(gz, scope, ri, node, node, reason);
1974}
1975
1976/// Like `comptimeExpr`, but draws a distinction between `node`, the expression to evaluate at comptime,
1977/// and `src_node`, the node to attach to the `block_comptime`.
1978fn comptimeExpr2(
1979 gz: *GenZir,
1980 scope: *Scope,
1981 ri: ResultInfo,
1982 node: Ast.Node.Index,
1983 src_node: Ast.Node.Index,
1984 reason: std.zig.SimpleComptimeReason,
19691985) InnerError!Zir.Inst.Ref {
19701986 if (gz.is_comptime) {
19711987 // No need to change anything!
......@@ -2049,23 +2065,23 @@ fn comptimeExpr(
20492065 block_scope.is_comptime = true;
20502066 defer block_scope.unstack();
20512067
2052 const block_inst = try gz.makeBlockInst(.block_comptime, node);
2068 const block_inst = try gz.makeBlockInst(.block_comptime, src_node);
20532069 // Replace result location and copy back later - see above.
20542070 const ty_only_ri: ResultInfo = .{
20552071 .ctx = ri.ctx,
2056 .rl = if (try ri.rl.resultType(gz, node)) |res_ty|
2072 .rl = if (try ri.rl.resultType(gz, src_node)) |res_ty|
20572073 .{ .coerced_ty = res_ty }
20582074 else
20592075 .none,
20602076 };
20612077 const block_result = try fullBodyExpr(&block_scope, scope, ty_only_ri, node, .normal);
20622078 if (!gz.refIsNoReturn(block_result)) {
2063 _ = try block_scope.addBreak(.@"break", block_inst, block_result);
2079 _ = try block_scope.addBreak(.break_inline, block_inst, block_result);
20642080 }
2065 try block_scope.setBlockBody(block_inst);
2081 try block_scope.setBlockComptimeBody(block_inst, reason);
20662082 try gz.instructions.append(gz.astgen.gpa, block_inst);
20672083
2068 return rvalue(gz, ri, block_inst.toRef(), node);
2084 return rvalue(gz, ri, block_inst.toRef(), src_node);
20692085}
20702086
20712087/// This one is for an actual `comptime` syntax, and will emit a compile error if
......@@ -2084,7 +2100,7 @@ fn comptimeExprAst(
20842100 const tree = astgen.tree;
20852101 const node_datas = tree.nodes.items(.data);
20862102 const body_node = node_datas[node].lhs;
2087 return comptimeExpr(gz, scope, ri, body_node);
2103 return comptimeExpr2(gz, scope, ri, body_node, node, .comptime_keyword);
20882104}
20892105
20902106/// Restore the error return trace index. Performs the restore only if the result is a non-error or
......@@ -2494,10 +2510,10 @@ fn labeledBlockExpr(
24942510
24952511 // Reserve the Block ZIR instruction index so that we can put it into the GenZir struct
24962512 // so that break statements can reference it.
2497 const block_tag: Zir.Inst.Tag = if (force_comptime) .block_comptime else .block;
2498 const block_inst = try gz.makeBlockInst(block_tag, block_node);
2513 const block_inst = try gz.makeBlockInst(if (force_comptime) .block_comptime else .block, block_node);
24992514 try gz.instructions.append(astgen.gpa, block_inst);
25002515 var block_scope = gz.makeSubBlock(parent_scope);
2516 block_scope.is_inline = force_comptime;
25012517 block_scope.label = GenZir.Label{
25022518 .token = label_token,
25032519 .block_inst = block_inst,
......@@ -2511,14 +2527,20 @@ fn labeledBlockExpr(
25112527 // As our last action before the return, "pop" the error trace if needed
25122528 _ = try gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always, block_node);
25132529 const result = try rvalue(gz, block_scope.break_result_info, .void_value, block_node);
2514 _ = try block_scope.addBreak(.@"break", block_inst, result);
2530 const break_tag: Zir.Inst.Tag = if (force_comptime) .break_inline else .@"break";
2531 _ = try block_scope.addBreak(break_tag, block_inst, result);
25152532 }
25162533
25172534 if (!block_scope.label.?.used) {
25182535 try astgen.appendErrorTok(label_token, "unused block label", .{});
25192536 }
25202537
2521 try block_scope.setBlockBody(block_inst);
2538 if (force_comptime) {
2539 try block_scope.setBlockComptimeBody(block_inst, .comptime_keyword);
2540 } else {
2541 try block_scope.setBlockBody(block_inst);
2542 }
2543
25222544 if (need_result_rvalue) {
25232545 return rvalue(gz, ri, block_inst.toRef(), block_node);
25242546 } else {
......@@ -3255,7 +3277,7 @@ fn varDecl(
32553277 } else .{ .rl = .none, .ctx = .const_init };
32563278 const prev_anon_name_strategy = gz.anon_name_strategy;
32573279 gz.anon_name_strategy = .dbg_var;
3258 const init_inst = try reachableExprComptime(gz, scope, result_info, var_decl.ast.init_node, node, force_comptime);
3280 const init_inst = try reachableExprComptime(gz, scope, result_info, var_decl.ast.init_node, node, if (force_comptime) .comptime_keyword else null);
32593281 gz.anon_name_strategy = prev_anon_name_strategy;
32603282
32613283 try gz.addDbgVar(.dbg_var_val, ident_name, init_inst);
......@@ -3321,7 +3343,7 @@ fn varDecl(
33213343 const prev_anon_name_strategy = gz.anon_name_strategy;
33223344 gz.anon_name_strategy = .dbg_var;
33233345 defer gz.anon_name_strategy = prev_anon_name_strategy;
3324 const init_inst = try reachableExprComptime(gz, scope, init_result_info, var_decl.ast.init_node, node, force_comptime);
3346 const init_inst = try reachableExprComptime(gz, scope, init_result_info, var_decl.ast.init_node, node, if (force_comptime) .comptime_keyword else null);
33253347
33263348 // The const init expression may have modified the error return trace, so signal
33273349 // to Sema that it should save the new index for restoring later.
......@@ -3393,7 +3415,14 @@ fn varDecl(
33933415 };
33943416 const prev_anon_name_strategy = gz.anon_name_strategy;
33953417 gz.anon_name_strategy = .dbg_var;
3396 _ = try reachableExprComptime(gz, scope, result_info, var_decl.ast.init_node, node, is_comptime);
3418 _ = try reachableExprComptime(
3419 gz,
3420 scope,
3421 result_info,
3422 var_decl.ast.init_node,
3423 node,
3424 if (var_decl.comptime_token != null) .comptime_keyword else null,
3425 );
33973426 gz.anon_name_strategy = prev_anon_name_strategy;
33983427 const final_ptr: Zir.Inst.Ref = if (resolve_inferred) ptr: {
33993428 break :ptr try gz.addUnNode(.resolve_inferred_alloc, alloc, node);
......@@ -3501,8 +3530,8 @@ fn assignDestructure(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerErro
35013530
35023531 if (full.comptime_token) |_| {
35033532 const comptime_block_inst = try gz.makeBlockInst(.block_comptime, node);
3504 _ = try inner_gz.addBreak(.@"break", comptime_block_inst, .void_value);
3505 try inner_gz.setBlockBody(comptime_block_inst);
3533 _ = try inner_gz.addBreak(.break_inline, comptime_block_inst, .void_value);
3534 try inner_gz.setBlockComptimeBody(comptime_block_inst, .comptime_keyword);
35063535 try gz.instructions.append(gz.astgen.gpa, comptime_block_inst);
35073536 }
35083537}
......@@ -3673,8 +3702,8 @@ fn assignDestructureMaybeDecls(
36733702 // Finish the block_comptime. Inferred alloc resolution etc will occur
36743703 // in the parent block.
36753704 const comptime_block_inst = try gz.makeBlockInst(.block_comptime, node);
3676 _ = try inner_gz.addBreak(.@"break", comptime_block_inst, .void_value);
3677 try inner_gz.setBlockBody(comptime_block_inst);
3705 _ = try inner_gz.addBreak(.break_inline, comptime_block_inst, .void_value);
3706 try inner_gz.setBlockComptimeBody(comptime_block_inst, .comptime_keyword);
36783707 try gz.instructions.append(gz.astgen.gpa, comptime_block_inst);
36793708 }
36803709
......@@ -3867,7 +3896,16 @@ fn ptrType(
38673896 gz.astgen.source_line = source_line;
38683897 gz.astgen.source_column = source_column;
38693898
3870 sentinel_ref = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = elem_type } }, ptr_info.ast.sentinel);
3899 sentinel_ref = try comptimeExpr(
3900 gz,
3901 scope,
3902 .{ .rl = .{ .ty = elem_type } },
3903 ptr_info.ast.sentinel,
3904 switch (ptr_info.size) {
3905 .Slice => .slice_sentinel,
3906 else => .pointer_sentinel,
3907 },
3908 );
38713909 trailing_count += 1;
38723910 }
38733911 if (ptr_info.ast.addrspace_node != 0) {
......@@ -3953,7 +3991,7 @@ fn arrayType(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) !
39533991 {
39543992 return astgen.failNode(len_node, "unable to infer array size", .{});
39553993 }
3956 const len = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, len_node);
3994 const len = try reachableExprComptime(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, len_node, node, .type);
39573995 const elem_type = try typeExpr(gz, scope, node_datas[node].rhs);
39583996
39593997 const result = try gz.addPlNode(.array_type, node, Zir.Inst.Bin{
......@@ -3977,9 +4015,9 @@ fn arrayTypeSentinel(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.
39774015 {
39784016 return astgen.failNode(len_node, "unable to infer array size", .{});
39794017 }
3980 const len = try reachableExpr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, len_node, node);
4018 const len = try reachableExprComptime(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, len_node, node, .array_length);
39814019 const elem_type = try typeExpr(gz, scope, extra.elem_type);
3982 const sentinel = try reachableExprComptime(gz, scope, .{ .rl = .{ .coerced_ty = elem_type } }, extra.sentinel, node, true);
4020 const sentinel = try reachableExprComptime(gz, scope, .{ .rl = .{ .coerced_ty = elem_type } }, extra.sentinel, node, .array_sentinel);
39834021
39844022 const result = try gz.addPlNode(.array_type_sentinel, node, Zir.Inst.ArrayTypeSentinel{
39854023 .len = len,
......@@ -5321,7 +5359,7 @@ fn tupleDecl(
53215359 astgen.scratch.appendAssumeCapacity(@intFromEnum(field_type_ref));
53225360
53235361 if (field.ast.value_expr != 0) {
5324 const field_init_ref = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = field_type_ref } }, field.ast.value_expr);
5362 const field_init_ref = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = field_type_ref } }, field.ast.value_expr, .tuple_field_default_value);
53255363 astgen.scratch.appendAssumeCapacity(@intFromEnum(field_init_ref));
53265364 } else {
53275365 astgen.scratch.appendAssumeCapacity(@intFromEnum(Zir.Inst.Ref.none));
......@@ -5693,7 +5731,7 @@ fn containerDecl(
56935731 namespace.base.tag = .namespace;
56945732
56955733 const arg_inst: Zir.Inst.Ref = if (container_decl.ast.arg != 0)
5696 try comptimeExpr(&block_scope, &namespace.base, coerced_type_ri, container_decl.ast.arg)
5734 try comptimeExpr(&block_scope, &namespace.base, coerced_type_ri, container_decl.ast.arg, .type)
56975735 else
56985736 .none;
56995737
......@@ -7573,7 +7611,7 @@ fn switchExprErrUnion(
75737611 if (node_tags[item_node] == .switch_range) continue;
75747612 items_len += 1;
75757613
7576 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node);
7614 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node, .switch_item);
75777615 try payloads.append(gpa, @intFromEnum(item_inst));
75787616 }
75797617
......@@ -7583,8 +7621,8 @@ fn switchExprErrUnion(
75837621 if (node_tags[range] != .switch_range) continue;
75847622 ranges_len += 1;
75857623
7586 const first = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].lhs);
7587 const last = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].rhs);
7624 const first = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].lhs, .switch_item);
7625 const last = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].rhs, .switch_item);
75887626 try payloads.appendSlice(gpa, &[_]u32{
75897627 @intFromEnum(first), @intFromEnum(last),
75907628 });
......@@ -7602,7 +7640,7 @@ fn switchExprErrUnion(
76027640 scalar_case_index += 1;
76037641 try payloads.resize(gpa, header_index + 2); // item, body_len
76047642 const item_node = case.ast.values[0];
7605 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node);
7643 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node, .switch_item);
76067644 payloads.items[header_index] = @intFromEnum(item_inst);
76077645 break :blk header_index + 1;
76087646 };
......@@ -8046,7 +8084,7 @@ fn switchExpr(
80468084 if (node_tags[item_node] == .switch_range) continue;
80478085 items_len += 1;
80488086
8049 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node);
8087 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node, .switch_item);
80508088 try payloads.append(gpa, @intFromEnum(item_inst));
80518089 }
80528090
......@@ -8056,8 +8094,8 @@ fn switchExpr(
80568094 if (node_tags[range] != .switch_range) continue;
80578095 ranges_len += 1;
80588096
8059 const first = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].lhs);
8060 const last = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].rhs);
8097 const first = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].lhs, .switch_item);
8098 const last = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].rhs, .switch_item);
80618099 try payloads.appendSlice(gpa, &[_]u32{
80628100 @intFromEnum(first), @intFromEnum(last),
80638101 });
......@@ -8075,7 +8113,7 @@ fn switchExpr(
80758113 scalar_case_index += 1;
80768114 try payloads.resize(gpa, header_index + 2); // item, body_len
80778115 const item_node = case.ast.values[0];
8078 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node);
8116 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node, .switch_item);
80798117 payloads.items[header_index] = @intFromEnum(item_inst);
80808118 break :blk header_index + 1;
80818119 };
......@@ -8836,7 +8874,7 @@ fn asmExpr(
88368874 },
88378875 else => .{
88388876 .tag = .asm_expr,
8839 .tmpl = @enumFromInt(@intFromEnum(try comptimeExpr(gz, scope, .{ .rl = .none }, full.ast.template))),
8877 .tmpl = @enumFromInt(@intFromEnum(try comptimeExpr(gz, scope, .{ .rl = .none }, full.ast.template, .inline_assembly_code))),
88408878 },
88418879 };
88428880
......@@ -8973,7 +9011,7 @@ fn unionInit(
89739011 params: []const Ast.Node.Index,
89749012) InnerError!Zir.Inst.Ref {
89759013 const union_type = try typeExpr(gz, scope, params[0]);
8976 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1]);
9014 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1], .union_field_name);
89779015 const field_type = try gz.addPlNode(.field_type_ref, node, Zir.Inst.FieldTypeRef{
89789016 .container_type = union_type,
89799017 .field_name = field_name,
......@@ -9078,7 +9116,7 @@ fn ptrCast(
90789116 const flags_int: FlagsInt = @bitCast(flags);
90799117 const cursor = maybeAdvanceSourceCursorToMainToken(gz, root_node);
90809118 const parent_ptr_type = try ri.rl.resultTypeForCast(gz, root_node, "@alignCast");
9081 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, node_datas[node].lhs);
9119 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, node_datas[node].lhs, .field_name);
90829120 const field_ptr = try expr(gz, scope, .{ .rl = .none }, node_datas[node].rhs);
90839121 try emitDbgStmt(gz, cursor);
90849122 const result = try gz.addExtendedPayloadSmall(.field_parent_ptr, flags_int, Zir.Inst.FieldParentPtr{
......@@ -9279,7 +9317,7 @@ fn builtinCall(
92799317 return astgen.failNode(node, "'@branchHint' must appear as the first statement in a function or conditional branch", .{});
92809318 }
92819319 const hint_ty = try gz.addBuiltinValue(node, .branch_hint);
9282 const hint_val = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = hint_ty } }, params[0]);
9320 const hint_val = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = hint_ty } }, params[0], .operand_branchHint);
92839321 _ = try gz.addExtendedPayload(.branch_hint, Zir.Inst.UnNode{
92849322 .node = gz.nodeIndexToRelative(node),
92859323 .operand = hint_val,
......@@ -9326,18 +9364,18 @@ fn builtinCall(
93269364 if (ri.rl == .ref or ri.rl == .ref_coerced_ty) {
93279365 return gz.addPlNode(.field_ptr_named, node, Zir.Inst.FieldNamed{
93289366 .lhs = try expr(gz, scope, .{ .rl = .ref }, params[0]),
9329 .field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1]),
9367 .field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1], .field_name),
93309368 });
93319369 }
93329370 const result = try gz.addPlNode(.field_val_named, node, Zir.Inst.FieldNamed{
93339371 .lhs = try expr(gz, scope, .{ .rl = .none }, params[0]),
9334 .field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1]),
9372 .field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1], .field_name),
93359373 });
93369374 return rvalue(gz, ri, result, node);
93379375 },
93389376 .FieldType => {
93399377 const ty_inst = try typeExpr(gz, scope, params[0]);
9340 const name_inst = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1]);
9378 const name_inst = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1], .field_name);
93419379 const result = try gz.addPlNode(.field_type_ref, node, Zir.Inst.FieldTypeRef{
93429380 .container_type = ty_inst,
93439381 .field_name = name_inst,
......@@ -9358,7 +9396,7 @@ fn builtinCall(
93589396 .@"export" => {
93599397 const exported = try expr(gz, scope, .{ .rl = .none }, params[0]);
93609398 const export_options_ty = try gz.addBuiltinValue(node, .export_options);
9361 const options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = export_options_ty } }, params[1]);
9399 const options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = export_options_ty } }, params[1], .export_options);
93629400 _ = try gz.addPlNode(.@"export", node, Zir.Inst.Export{
93639401 .exported = exported,
93649402 .options = options,
......@@ -9368,7 +9406,7 @@ fn builtinCall(
93689406 .@"extern" => {
93699407 const type_inst = try typeExpr(gz, scope, params[0]);
93709408 const extern_options_ty = try gz.addBuiltinValue(node, .extern_options);
9371 const options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = extern_options_ty } }, params[1]);
9409 const options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = extern_options_ty } }, params[1], .extern_options);
93729410 const result = try gz.addExtendedPayload(.builtin_extern, Zir.Inst.BinNode{
93739411 .node = gz.nodeIndexToRelative(node),
93749412 .lhs = type_inst,
......@@ -9560,7 +9598,7 @@ fn builtinCall(
95609598 // zig fmt: on
95619599
95629600 .wasm_memory_size => {
9563 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);
9601 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .wasm_memory_index);
95649602 const result = try gz.addExtendedPayload(.wasm_memory_size, Zir.Inst.UnNode{
95659603 .node = gz.nodeIndexToRelative(node),
95669604 .operand = operand,
......@@ -9568,7 +9606,7 @@ fn builtinCall(
95689606 return rvalue(gz, ri, result, node);
95699607 },
95709608 .wasm_memory_grow => {
9571 const index_arg = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);
9609 const index_arg = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .wasm_memory_index);
95729610 const delta_arg = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, params[1]);
95739611 const result = try gz.addExtendedPayload(.wasm_memory_grow, Zir.Inst.BinNode{
95749612 .node = gz.nodeIndexToRelative(node),
......@@ -9579,8 +9617,8 @@ fn builtinCall(
95799617 },
95809618 .c_define => {
95819619 if (!gz.c_import) return gz.astgen.failNode(node, "C define valid only inside C import block", .{});
9582 const name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[0]);
9583 const value = try comptimeExpr(gz, scope, .{ .rl = .none }, params[1]);
9620 const name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[0], .operand_cDefine_macro_name);
9621 const value = try comptimeExpr(gz, scope, .{ .rl = .none }, params[1], .operand_cDefine_macro_value);
95849622 const result = try gz.addExtendedPayload(.c_define, Zir.Inst.BinNode{
95859623 .node = gz.nodeIndexToRelative(node),
95869624 .lhs = name,
......@@ -9666,7 +9704,7 @@ fn builtinCall(
96669704 },
96679705 .call => {
96689706 const call_modifier_ty = try gz.addBuiltinValue(node, .call_modifier);
9669 const modifier = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = call_modifier_ty } }, params[0]);
9707 const modifier = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = call_modifier_ty } }, params[0], .call_modifier);
96709708 const callee = try expr(gz, scope, .{ .rl = .none }, params[1]);
96719709 const args = try expr(gz, scope, .{ .rl = .none }, params[2]);
96729710 const result = try gz.addPlNode(.builtin_call, node, Zir.Inst.BuiltinCall{
......@@ -9682,7 +9720,7 @@ fn builtinCall(
96829720 },
96839721 .field_parent_ptr => {
96849722 const parent_ptr_type = try ri.rl.resultTypeForCast(gz, node, builtin_name);
9685 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[0]);
9723 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[0], .field_name);
96869724 const result = try gz.addExtendedPayloadSmall(.field_parent_ptr, 0, Zir.Inst.FieldParentPtr{
96879725 .src_node = gz.nodeIndexToRelative(node),
96889726 .parent_ptr_type = parent_ptr_type,
......@@ -9713,7 +9751,7 @@ fn builtinCall(
97139751 .elem_type = try typeExpr(gz, scope, params[0]),
97149752 .a = try expr(gz, scope, .{ .rl = .none }, params[1]),
97159753 .b = try expr(gz, scope, .{ .rl = .none }, params[2]),
9716 .mask = try comptimeExpr(gz, scope, .{ .rl = .none }, params[3]),
9754 .mask = try comptimeExpr(gz, scope, .{ .rl = .none }, params[3], .operand_shuffle_mask),
97179755 });
97189756 return rvalue(gz, ri, result, node);
97199757 },
......@@ -9739,7 +9777,7 @@ fn builtinCall(
97399777 },
97409778 .Vector => {
97419779 const result = try gz.addPlNode(.vector_type, node, Zir.Inst.Bin{
9742 .lhs = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]),
9780 .lhs = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .type),
97439781 .rhs = try typeExpr(gz, scope, params[1]),
97449782 });
97459783 return rvalue(gz, ri, result, node);
......@@ -9747,7 +9785,7 @@ fn builtinCall(
97479785 .prefetch => {
97489786 const prefetch_options_ty = try gz.addBuiltinValue(node, .prefetch_options);
97499787 const ptr = try expr(gz, scope, .{ .rl = .none }, params[0]);
9750 const options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = prefetch_options_ty } }, params[1]);
9788 const options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = prefetch_options_ty } }, params[1], .prefetch_options);
97519789 _ = try gz.addExtendedPayload(.prefetch, Zir.Inst.BinNode{
97529790 .node = gz.nodeIndexToRelative(node),
97539791 .lhs = ptr,
......@@ -9785,7 +9823,7 @@ fn builtinCall(
97859823 },
97869824
97879825 .work_item_id => {
9788 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);
9826 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .work_group_dim_index);
97899827 const result = try gz.addExtendedPayload(.work_item_id, Zir.Inst.UnNode{
97909828 .node = gz.nodeIndexToRelative(node),
97919829 .operand = operand,
......@@ -9793,7 +9831,7 @@ fn builtinCall(
97939831 return rvalue(gz, ri, result, node);
97949832 },
97959833 .work_group_size => {
9796 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);
9834 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .work_group_dim_index);
97979835 const result = try gz.addExtendedPayload(.work_group_size, Zir.Inst.UnNode{
97989836 .node = gz.nodeIndexToRelative(node),
97999837 .operand = operand,
......@@ -9801,7 +9839,7 @@ fn builtinCall(
98019839 return rvalue(gz, ri, result, node);
98029840 },
98039841 .work_group_id => {
9804 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);
9842 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .work_group_dim_index);
98059843 const result = try gz.addExtendedPayload(.work_group_id, Zir.Inst.UnNode{
98069844 .node = gz.nodeIndexToRelative(node),
98079845 .operand = operand,
......@@ -9821,7 +9859,13 @@ fn hasDeclOrField(
98219859 tag: Zir.Inst.Tag,
98229860) InnerError!Zir.Inst.Ref {
98239861 const container_type = try typeExpr(gz, scope, lhs_node);
9824 const name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, rhs_node);
9862 const name = try comptimeExpr(
9863 gz,
9864 scope,
9865 .{ .rl = .{ .coerced_ty = .slice_const_u8_type } },
9866 rhs_node,
9867 if (tag == .has_decl) .decl_name else .field_name,
9868 );
98259869 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
98269870 .lhs = container_type,
98279871 .rhs = name,
......@@ -9874,7 +9918,7 @@ fn simpleUnOp(
98749918) InnerError!Zir.Inst.Ref {
98759919 const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
98769920 const operand = if (tag == .compile_error)
9877 try comptimeExpr(gz, scope, operand_ri, operand_node)
9921 try comptimeExpr(gz, scope, operand_ri, operand_node, .compile_error_string)
98789922 else
98799923 try expr(gz, scope, operand_ri, operand_node);
98809924 switch (tag) {
......@@ -9972,7 +10016,13 @@ fn simpleCBuiltin(
997210016) InnerError!Zir.Inst.Ref {
997310017 const name: []const u8 = if (tag == .c_undef) "C undef" else "C include";
997410018 if (!gz.c_import) return gz.astgen.failNode(node, "{s} valid only inside C import block", .{name});
9975 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, operand_node);
10019 const operand = try comptimeExpr(
10020 gz,
10021 scope,
10022 .{ .rl = .{ .coerced_ty = .slice_const_u8_type } },
10023 operand_node,
10024 if (tag == .c_undef) .operand_cUndef_macro_name else .operand_cInclude_file_name,
10025 );
997610026 _ = try gz.addExtendedPayload(tag, Zir.Inst.UnNode{
997710027 .node = gz.nodeIndexToRelative(node),
997810028 .operand = operand,
......@@ -9990,7 +10040,7 @@ fn offsetOf(
999010040 tag: Zir.Inst.Tag,
999110041) InnerError!Zir.Inst.Ref {
999210042 const type_inst = try typeExpr(gz, scope, lhs_node);
9993 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, rhs_node);
10043 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, rhs_node, .field_name);
999410044 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
999510045 .lhs = type_inst,
999610046 .rhs = field_name,
......@@ -11996,11 +12046,16 @@ const GenZir = struct {
1199612046 }
1199712047
1199812048 /// Assumes nothing stacked on `gz`. Unstacks `gz`.
12049 /// Asserts `inst` is not a `block_comptime`.
1199912050 fn setBlockBody(gz: *GenZir, inst: Zir.Inst.Index) !void {
1200012051 const astgen = gz.astgen;
1200112052 const gpa = astgen.gpa;
1200212053 const body = gz.instructionsSlice();
1200312054 const body_len = astgen.countBodyLenAfterFixups(body);
12055
12056 const zir_tags = astgen.instructions.items(.tag);
12057 assert(zir_tags[@intFromEnum(inst)] != .block_comptime); // use `setComptimeBlockBody` instead
12058
1200412059 try astgen.extra.ensureUnusedCapacity(
1200512060 gpa,
1200612061 @typeInfo(Zir.Inst.Block).@"struct".fields.len + body_len,
......@@ -12013,6 +12068,32 @@ const GenZir = struct {
1201312068 gz.unstack();
1201412069 }
1201512070
12071 /// Assumes nothing stacked on `gz`. Unstacks `gz`.
12072 /// Asserts `inst` is a `block_comptime`.
12073 fn setBlockComptimeBody(gz: *GenZir, inst: Zir.Inst.Index, comptime_reason: std.zig.SimpleComptimeReason) !void {
12074 const astgen = gz.astgen;
12075 const gpa = astgen.gpa;
12076 const body = gz.instructionsSlice();
12077 const body_len = astgen.countBodyLenAfterFixups(body);
12078
12079 const zir_tags = astgen.instructions.items(.tag);
12080 assert(zir_tags[@intFromEnum(inst)] == .block_comptime); // use `setBlockBody` instead
12081
12082 try astgen.extra.ensureUnusedCapacity(
12083 gpa,
12084 @typeInfo(Zir.Inst.BlockComptime).@"struct".fields.len + body_len,
12085 );
12086 const zir_datas = astgen.instructions.items(.data);
12087 zir_datas[@intFromEnum(inst)].pl_node.payload_index = astgen.addExtraAssumeCapacity(
12088 Zir.Inst.BlockComptime{
12089 .reason = comptime_reason,
12090 .body_len = body_len,
12091 },
12092 );
12093 astgen.appendBodyWithFixups(body);
12094 gz.unstack();
12095 }
12096
1201612097 /// Assumes nothing stacked on `gz`. Unstacks `gz`.
1201712098 fn setTryBody(gz: *GenZir, inst: Zir.Inst.Index, operand: Zir.Inst.Ref) !void {
1201812099 const astgen = gz.astgen;
lib/std/zig/Zir.zig+16-2
......@@ -78,6 +78,7 @@ pub fn extraData(code: Zir, comptime T: type, index: usize) ExtraData(T) {
7878 Inst.Ref,
7979 Inst.Index,
8080 Inst.Declaration.Name,
81 std.zig.SimpleComptimeReason,
8182 NullTerminatedString,
8283 => @enumFromInt(code.extra[i]),
8384
......@@ -291,7 +292,8 @@ pub const Inst = struct {
291292 /// Uses the `pl_node` union field. Payload is `Block`.
292293 block,
293294 /// Like `block`, but forces full evaluation of its contents at compile-time.
294 /// Uses the `pl_node` union field. Payload is `Block`.
295 /// Exited with `break_inline`.
296 /// Uses the `pl_node` union field. Payload is `BlockComptime`.
295297 block_comptime,
296298 /// A list of instructions which are analyzed in the parent context, without
297299 /// generating a runtime block. Must terminate with an "inline" variant of
......@@ -2547,6 +2549,13 @@ pub const Inst = struct {
25472549 body_len: u32,
25482550 };
25492551
2552 /// Trailing:
2553 /// * inst: Index // for each `body_len`
2554 pub const BlockComptime = struct {
2555 reason: std.zig.SimpleComptimeReason,
2556 body_len: u32,
2557 };
2558
25502559 /// Trailing:
25512560 /// * inst: Index // for each `body_len`
25522561 pub const BoolBr = struct {
......@@ -4517,7 +4526,6 @@ fn findTrackableInner(
45174526 // Block instructions, recurse over the bodies.
45184527
45194528 .block,
4520 .block_comptime,
45214529 .block_inline,
45224530 .c_import,
45234531 .typeof_builtin,
......@@ -4528,6 +4536,12 @@ fn findTrackableInner(
45284536 const body = zir.bodySlice(extra.end, extra.data.body_len);
45294537 return zir.findTrackableBody(gpa, contents, defers, body);
45304538 },
4539 .block_comptime => {
4540 const inst_data = datas[@intFromEnum(inst)].pl_node;
4541 const extra = zir.extraData(Inst.BlockComptime, inst_data.payload_index);
4542 const body = zir.bodySlice(extra.end, extra.data.body_len);
4543 return zir.findTrackableBody(gpa, contents, defers, body);
4544 },
45314545 .condbr, .condbr_inline => {
45324546 const inst_data = datas[@intFromEnum(inst)].pl_node;
45334547 const extra = zir.extraData(Inst.CondBr, inst_data.payload_index);
src/Compilation.zig+5-1
......@@ -3435,6 +3435,7 @@ pub fn addModuleErrorMsg(
34353435 var notes: std.ArrayHashMapUnmanaged(ErrorBundle.ErrorMessage, void, ErrorNoteHashContext, true) = .empty;
34363436 defer notes.deinit(gpa);
34373437
3438 var last_note_loc: ?std.zig.Loc = null;
34383439 for (module_err_msg.notes) |module_note| {
34393440 const note_src_loc = module_note.src_loc.upgrade(zcu);
34403441 const source = try note_src_loc.file_scope.getSource(gpa);
......@@ -3443,6 +3444,9 @@ pub fn addModuleErrorMsg(
34433444 const note_file_path = try note_src_loc.file_scope.fullPath(gpa);
34443445 defer gpa.free(note_file_path);
34453446
3447 const omit_source_line = loc.eql(err_loc) or (last_note_loc != null and loc.eql(last_note_loc.?));
3448 last_note_loc = loc;
3449
34463450 const gop = try notes.getOrPutContext(gpa, .{
34473451 .msg = try eb.addString(module_note.msg),
34483452 .src_loc = try eb.addSourceLocation(.{
......@@ -3452,7 +3456,7 @@ pub fn addModuleErrorMsg(
34523456 .span_end = span.end,
34533457 .line = @intCast(loc.line),
34543458 .column = @intCast(loc.column),
3455 .source_line = if (err_loc.eql(loc)) 0 else try eb.addString(loc.source_line),
3459 .source_line = if (omit_source_line) 0 else try eb.addString(loc.source_line),
34563460 }),
34573461 }, .{ .eb = eb });
34583462 if (gop.found_existing) {
src/Sema.zig+463-580
......@@ -377,9 +377,7 @@ pub const Block = struct {
377377 runtime_index: RuntimeIndex = .zero,
378378 inline_block: Zir.Inst.OptionalIndex = .none,
379379
380 comptime_reason: ?*const ComptimeReason = null,
381 // TODO is_comptime and comptime_reason should probably be merged together.
382 is_comptime: bool,
380 comptime_reason: ?BlockComptimeReason = null,
383381 is_typeof: bool = false,
384382
385383 /// Keep track of the active error return trace index around blocks so that we can correctly
......@@ -419,6 +417,10 @@ pub const Block = struct {
419417 };
420418 }
421419
420 fn isComptime(block: Block) bool {
421 return block.comptime_reason != null;
422 }
423
422424 fn builtinCallArgSrc(block: *Block, builtin_call_node: i32, arg_index: u32) LazySrcLoc {
423425 return block.src(.{ .node_offset_builtin_call_arg = .{
424426 .builtin_call_node = builtin_call_node,
......@@ -434,44 +436,6 @@ pub const Block = struct {
434436 return block.src(.{ .token_offset = tok_offset });
435437 }
436438
437 const ComptimeReason = union(enum) {
438 c_import: struct {
439 src: LazySrcLoc,
440 },
441 comptime_ret_ty: struct {
442 func: Air.Inst.Ref,
443 func_src: LazySrcLoc,
444 return_ty: Type,
445 },
446
447 fn explain(cr: ComptimeReason, sema: *Sema, msg: ?*Zcu.ErrorMsg) !void {
448 const parent = msg orelse return;
449 const pt = sema.pt;
450 const prefix = "expression is evaluated at comptime because ";
451 switch (cr) {
452 .c_import => |ci| {
453 try sema.errNote(ci.src, parent, prefix ++ "it is inside a @cImport", .{});
454 },
455 .comptime_ret_ty => |rt| {
456 const ret_ty_src: LazySrcLoc = if (try sema.funcDeclSrcInst(rt.func)) |fn_decl_inst| .{
457 .base_node_inst = fn_decl_inst,
458 .offset = .{ .node_offset_fn_type_ret_ty = 0 },
459 } else rt.func_src;
460 if (rt.return_ty.isGenericPoison()) {
461 return sema.errNote(ret_ty_src, parent, prefix ++ "the generic function was instantiated with a comptime-only return type", .{});
462 }
463 try sema.errNote(
464 ret_ty_src,
465 parent,
466 prefix ++ "the function returns a comptime-only type '{}'",
467 .{rt.return_ty.fmt(pt)},
468 );
469 try sema.explainWhyTypeIsComptime(parent, ret_ty_src, rt.return_ty);
470 },
471 }
472 }
473 };
474
475439 const Param = struct {
476440 /// `none` means `anytype`.
477441 ty: InternPool.Index,
......@@ -539,7 +503,6 @@ pub const Block = struct {
539503 .instructions = .{},
540504 .label = null,
541505 .inlining = parent.inlining,
542 .is_comptime = parent.is_comptime,
543506 .comptime_reason = parent.comptime_reason,
544507 .is_typeof = parent.is_typeof,
545508 .runtime_cond = parent.runtime_cond,
......@@ -860,6 +823,77 @@ pub const Block = struct {
860823 .inst = inst,
861824 });
862825 }
826
827 /// Returns the `*Block` that should be passed to `Sema.failWithOwnedErrorMsg`, because all inline
828 /// calls below it have already been reported with "called at comptime from here" notes.
829 fn explainWhyBlockIsComptime(start_block: *Block, err_msg: *Zcu.ErrorMsg) !*Block {
830 const sema = start_block.sema;
831 var block = start_block;
832 while (true) {
833 switch (block.comptime_reason.?) {
834 .inlining_parent => {
835 const inlining = block.inlining.?;
836 try sema.errNote(inlining.call_src, err_msg, "called at comptime from here", .{});
837 block = inlining.call_block;
838 },
839 .reason => |r| {
840 try r.r.explain(sema, r.src, err_msg);
841 return block;
842 },
843 }
844 }
845 }
846};
847
848const ComptimeReason = union(enum) {
849 /// Evaluating at comptime for a reason in the `std.zig.SimpleComptimeReason` enum.
850 simple: std.zig.SimpleComptimeReason,
851
852 /// Evaluating at comptime because of a comptime-only type.
853 /// The format string looks like "foo '{}' bar", where "{}" is the comptime-only type.
854 /// We will then explain why this type is comptime-only.
855 comptime_only: struct {
856 ty: Type,
857 msg: enum {
858 union_init,
859 struct_init,
860 tuple_init,
861 param_ty_arg,
862 ret_ty_call,
863 ret_ty_generic_call,
864 },
865 },
866
867 fn explain(reason: ComptimeReason, sema: *Sema, src: LazySrcLoc, err_msg: *Zcu.ErrorMsg) !void {
868 switch (reason) {
869 .simple => |simple| {
870 try sema.errNote(src, err_msg, "{s}", .{simple.message()});
871 },
872 .comptime_only => |co| {
873 const pre, const post = switch (co.msg) {
874 .union_init => .{ "initializer of comptime-only union", "must be comptime-known" },
875 .struct_init => .{ "initializer of comptime-only struct", "must be comptime-known" },
876 .tuple_init => .{ "initializer of comptime-only tuple", "must be comptime-known" },
877 .param_ty_arg => .{ "argument to parameter with comptime-only type", "must be comptime-known" },
878 .ret_ty_call => .{ "function with comptime-only return type", "is evaluated at comptime" },
879 .ret_ty_generic_call => .{ "generic function instantiated with comptime-only return type", "is evaluated at comptime" },
880 };
881 try sema.errNote(src, err_msg, "{s} '{}' {s}", .{ pre, co.ty.fmt(sema.pt), post });
882 try sema.explainWhyTypeIsComptime(err_msg, src, co.ty);
883 },
884 }
885 }
886};
887
888const BlockComptimeReason = union(enum) {
889 /// This block inherits being comptime-only from the `inlining` call site.
890 inlining_parent,
891
892 /// This block is comptime for the given reason at the given source location.
893 reason: struct {
894 src: LazySrcLoc,
895 r: ComptimeReason,
896 },
863897};
864898
865899const LabeledBlock = struct {
......@@ -885,12 +919,6 @@ const InferredAlloc = struct {
885919 prongs: std.ArrayListUnmanaged(Air.Inst.Index) = .empty,
886920};
887921
888const NeededComptimeReason = struct {
889 needed_comptime_reason: []const u8,
890 value_comptime_reason: ?[]const u8 = null,
891 block_comptime_reason: ?*const Block.ComptimeReason = null,
892};
893
894922pub fn deinit(sema: *Sema) void {
895923 const gpa = sema.gpa;
896924 sema.air_instructions.deinit(gpa);
......@@ -954,7 +982,7 @@ pub fn analyzeFnBody(
954982/// we are evaluating at comptime, semantically analyze the body and return the result from it.
955983/// Returns `null` if control flow did not break from this block, but instead terminated with some
956984/// other runtime noreturn instruction. Compile-time breaks to blocks further up the stack still
957/// return `error.ComptimeBreak`. If `block.is_comptime`, this function will never return `null`.
985/// return `error.ComptimeBreak`. If `block.isComptime()`, this function will never return `null`.
958986fn analyzeInlineBody(
959987 sema: *Sema,
960988 block: *Block,
......@@ -1003,7 +1031,7 @@ pub fn resolveInlineBody(
10031031/// If this function returns normally, the merges of `block` were populated with all possible
10041032/// (runtime) results of this block. Peer type resolution should be performed on the result,
10051033/// and relevant runtime instructions written to perform necessary coercions and breaks. See
1006/// `resolveAnalyzedBlock`. This form of return is impossible if `block.is_comptime == true`.
1034/// `resolveAnalyzedBlock`. This form of return is impossible if `block.isComptime()`.
10071035///
10081036/// Alternatively, this function may return `error.ComptimeBreak`. This indicates that comptime
10091037/// control flow is happening, and we are breaking at comptime from a block indicated by the
......@@ -1340,7 +1368,7 @@ fn analyzeBodyInner(
13401368 continue;
13411369 },
13421370 .breakpoint => {
1343 if (!block.is_comptime) {
1371 if (!block.isComptime()) {
13441372 _ = try block.addNoOp(.breakpoint);
13451373 }
13461374 i += 1;
......@@ -1515,7 +1543,7 @@ fn analyzeBodyInner(
15151543 continue;
15161544 },
15171545 .check_comptime_control_flow => {
1518 if (!block.is_comptime) {
1546 if (!block.isComptime()) {
15191547 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
15201548 const src = block.nodeOffset(inst_data.src_node);
15211549 const inline_block = inst_data.operand.toIndex().?;
......@@ -1562,7 +1590,7 @@ fn analyzeBodyInner(
15621590
15631591 // Special case instructions to handle comptime control flow.
15641592 .@"break" => {
1565 if (block.is_comptime) {
1593 if (block.isComptime()) {
15661594 sema.comptime_break_inst = inst;
15671595 return error.ComptimeBreak;
15681596 } else {
......@@ -1575,7 +1603,7 @@ fn analyzeBodyInner(
15751603 return error.ComptimeBreak;
15761604 },
15771605 .repeat => {
1578 if (block.is_comptime) {
1606 if (block.isComptime()) {
15791607 // Send comptime control flow back to the beginning of this block.
15801608 const src = block.nodeOffset(datas[@intFromEnum(inst)].node);
15811609 try sema.emitBackwardBranch(block, src);
......@@ -1597,7 +1625,7 @@ fn analyzeBodyInner(
15971625 i = 0;
15981626 continue;
15991627 },
1600 .switch_continue => if (block.is_comptime) {
1628 .switch_continue => if (block.isComptime()) {
16011629 sema.comptime_break_inst = inst;
16021630 return error.ComptimeBreak;
16031631 } else {
......@@ -1605,17 +1633,40 @@ fn analyzeBodyInner(
16051633 break;
16061634 },
16071635
1608 .loop => if (block.is_comptime) {
1636 .loop => if (block.isComptime()) {
16091637 continue :inst .block_inline;
16101638 } else try sema.zirLoop(block, inst),
16111639
1612 .block => if (block.is_comptime) {
1640 .block => if (block.isComptime()) {
16131641 continue :inst .block_inline;
1614 } else try sema.zirBlock(block, inst, false),
1642 } else try sema.zirBlock(block, inst),
16151643
1616 .block_comptime => if (block.is_comptime) {
1617 continue :inst .block_inline;
1618 } else try sema.zirBlock(block, inst, true),
1644 .block_comptime => {
1645 const pl_node = datas[@intFromEnum(inst)].pl_node;
1646 const src = block.nodeOffset(pl_node.src_node);
1647 const extra = sema.code.extraData(Zir.Inst.BlockComptime, pl_node.payload_index);
1648 const block_body = sema.code.bodySlice(extra.end, extra.data.body_len);
1649
1650 if (block.isComptime()) {
1651 // No need for a sub-block; just resolve the other body directly!
1652 break :inst try sema.resolveInlineBody(block, block_body, inst);
1653 }
1654
1655 var child_block = block.makeSubBlock();
1656 defer child_block.instructions.deinit(sema.gpa);
1657 child_block.comptime_reason = .{ .reason = .{
1658 .src = src,
1659 .r = .{ .simple = extra.data.reason },
1660 } };
1661
1662 const result = try sema.resolveInlineBody(&child_block, block_body, inst);
1663
1664 if (!try sema.isComptimeKnown(result)) {
1665 return sema.failWithNeededComptime(&child_block, src, null);
1666 }
1667
1668 break :inst result;
1669 },
16191670
16201671 .block_inline => blk: {
16211672 // Directly analyze the block body without introducing a new block.
......@@ -1725,7 +1776,7 @@ fn analyzeBodyInner(
17251776 return error.ComptimeBreak;
17261777 }
17271778 },
1728 .condbr => if (block.is_comptime) {
1779 .condbr => if (block.isComptime()) {
17291780 continue :inst .condbr_inline;
17301781 } else {
17311782 try sema.zirCondbr(block, inst);
......@@ -1742,10 +1793,7 @@ fn analyzeBodyInner(
17421793 );
17431794 const uncasted_cond = try sema.resolveInst(extra.data.condition);
17441795 const cond = try sema.coerce(block, Type.bool, uncasted_cond, cond_src);
1745 const cond_val = try sema.resolveConstDefinedValue(block, cond_src, cond, .{
1746 .needed_comptime_reason = "condition in comptime branch must be comptime-known",
1747 .block_comptime_reason = block.comptime_reason,
1748 });
1796 const cond_val = try sema.resolveConstDefinedValue(block, cond_src, cond, null);
17491797 const inline_body = if (cond_val.toBool()) then_body else else_body;
17501798
17511799 try sema.maybeErrorUnwrapCondbr(block, inline_body, extra.data.condition, cond_src);
......@@ -1756,7 +1804,7 @@ fn analyzeBodyInner(
17561804 break :inst result;
17571805 },
17581806 .@"try" => blk: {
1759 if (!block.is_comptime) break :blk try sema.zirTry(block, inst);
1807 if (!block.isComptime()) break :blk try sema.zirTry(block, inst);
17601808 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
17611809 const src = block.nodeOffset(inst_data.src_node);
17621810 const operand_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node });
......@@ -1771,10 +1819,7 @@ fn analyzeBodyInner(
17711819 }
17721820 const is_non_err = try sema.analyzeIsNonErrComptimeOnly(block, operand_src, err_union);
17731821 assert(is_non_err != .none);
1774 const is_non_err_val = try sema.resolveConstDefinedValue(block, operand_src, is_non_err, .{
1775 .needed_comptime_reason = "try operand inside comptime block must be comptime-known",
1776 .block_comptime_reason = block.comptime_reason,
1777 });
1822 const is_non_err_val = try sema.resolveConstDefinedValue(block, operand_src, is_non_err, null);
17781823 if (is_non_err_val.toBool()) {
17791824 break :blk try sema.analyzeErrUnionPayload(block, src, err_union_ty, err_union, operand_src, false);
17801825 }
......@@ -1782,7 +1827,7 @@ fn analyzeBodyInner(
17821827 break :blk result;
17831828 },
17841829 .try_ptr => blk: {
1785 if (!block.is_comptime) break :blk try sema.zirTryPtr(block, inst);
1830 if (!block.isComptime()) break :blk try sema.zirTryPtr(block, inst);
17861831 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
17871832 const src = block.nodeOffset(inst_data.src_node);
17881833 const operand_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node });
......@@ -1792,10 +1837,7 @@ fn analyzeBodyInner(
17921837 const err_union = try sema.analyzeLoad(block, src, operand, operand_src);
17931838 const is_non_err = try sema.analyzeIsNonErrComptimeOnly(block, operand_src, err_union);
17941839 assert(is_non_err != .none);
1795 const is_non_err_val = try sema.resolveConstDefinedValue(block, operand_src, is_non_err, .{
1796 .needed_comptime_reason = "try operand inside comptime block must be comptime-known",
1797 .block_comptime_reason = block.comptime_reason,
1798 });
1840 const is_non_err_val = try sema.resolveConstDefinedValue(block, operand_src, is_non_err, null);
17991841 if (is_non_err_val.toBool()) {
18001842 break :blk try sema.analyzeErrUnionPayloadPtr(block, src, operand, false, false);
18011843 }
......@@ -1873,7 +1915,7 @@ fn resolveConstBool(
18731915 block: *Block,
18741916 src: LazySrcLoc,
18751917 zir_ref: Zir.Inst.Ref,
1876 reason: NeededComptimeReason,
1918 reason: ComptimeReason,
18771919) !bool {
18781920 const air_inst = try sema.resolveInst(zir_ref);
18791921 const wanted_type = Type.bool;
......@@ -1887,7 +1929,7 @@ fn resolveConstString(
18871929 block: *Block,
18881930 src: LazySrcLoc,
18891931 zir_ref: Zir.Inst.Ref,
1890 reason: NeededComptimeReason,
1932 reason: ComptimeReason,
18911933) ![]u8 {
18921934 const air_inst = try sema.resolveInst(zir_ref);
18931935 return sema.toConstString(block, src, air_inst, reason);
......@@ -1898,7 +1940,7 @@ pub fn toConstString(
18981940 block: *Block,
18991941 src: LazySrcLoc,
19001942 air_inst: Air.Inst.Ref,
1901 reason: NeededComptimeReason,
1943 reason: ComptimeReason,
19021944) ![]u8 {
19031945 const pt = sema.pt;
19041946 const coerced_inst = try sema.coerce(block, Type.slice_const_u8, air_inst, src);
......@@ -1912,7 +1954,7 @@ pub fn resolveConstStringIntern(
19121954 block: *Block,
19131955 src: LazySrcLoc,
19141956 zir_ref: Zir.Inst.Ref,
1915 reason: NeededComptimeReason,
1957 reason: ComptimeReason,
19161958) !InternPool.NullTerminatedString {
19171959 const air_inst = try sema.resolveInst(zir_ref);
19181960 const wanted_type = Type.slice_const_u8;
......@@ -2063,9 +2105,7 @@ fn analyzeAsType(
20632105) !Type {
20642106 const wanted_type = Type.type;
20652107 const coerced_inst = try sema.coerce(block, wanted_type, air_inst, src);
2066 const val = try sema.resolveConstDefinedValue(block, src, coerced_inst, .{
2067 .needed_comptime_reason = "types must be comptime-known",
2068 });
2108 const val = try sema.resolveConstDefinedValue(block, src, coerced_inst, .{ .simple = .type });
20692109 return val.toType();
20702110}
20712111
......@@ -2077,7 +2117,7 @@ pub fn setupErrorReturnTrace(sema: *Sema, block: *Block, last_arg_index: usize)
20772117 const ip = &zcu.intern_pool;
20782118 if (!comp.config.any_error_tracing) return;
20792119
2080 assert(!block.is_comptime);
2120 assert(!block.isComptime());
20812121 var err_trace_block = block.makeSubBlock();
20822122 defer err_trace_block.instructions.deinit(gpa);
20832123
......@@ -2148,7 +2188,7 @@ fn resolveConstValue(
21482188 block: *Block,
21492189 src: LazySrcLoc,
21502190 inst: Air.Inst.Ref,
2151 reason: NeededComptimeReason,
2191 reason: ?ComptimeReason,
21522192) CompileError!Value {
21532193 return try sema.resolveValue(inst) orelse {
21542194 return sema.failWithNeededComptime(block, src, reason);
......@@ -2177,7 +2217,7 @@ fn resolveConstDefinedValue(
21772217 block: *Block,
21782218 src: LazySrcLoc,
21792219 air_ref: Air.Inst.Ref,
2180 reason: NeededComptimeReason,
2220 reason: ?ComptimeReason,
21812221) CompileError!Value {
21822222 const val = try sema.resolveConstValue(block, src, air_ref, reason);
21832223 if (val.isUndef(sema.pt.zcu)) return sema.failWithUseOfUndef(block, src);
......@@ -2217,15 +2257,16 @@ pub fn resolveFinalDeclValue(
22172257 const val: Value = .fromInterned(ip_index);
22182258 break :rt_ptr val.isPtrRuntimeValue(zcu);
22192259 };
2220 const value_comptime_reason: ?[]const u8 = if (is_runtime_ptr)
2221 "thread local and dll imported variables have runtime-known addresses"
2222 else
2223 null;
22242260
2225 return sema.failWithNeededComptime(block, src, .{
2226 .needed_comptime_reason = "global variable initializer must be comptime-known",
2227 .value_comptime_reason = value_comptime_reason,
2228 });
2261 switch (sema.failWithNeededComptime(block, src, .{ .simple = .container_var_init })) {
2262 error.AnalysisFail => |e| {
2263 if (sema.err != null and is_runtime_ptr) {
2264 try sema.errNote(src, sema.err.?, "threadlocal and dll imported variables have runtime-known addresses", .{});
2265 }
2266 return e;
2267 },
2268 else => |e| return e,
2269 }
22292270 };
22302271
22312272 if (val.canMutateComptimeVarState(zcu)) {
......@@ -2235,21 +2276,19 @@ pub fn resolveFinalDeclValue(
22352276 return val;
22362277}
22372278
2238fn failWithNeededComptime(sema: *Sema, block: *Block, src: LazySrcLoc, reason: NeededComptimeReason) CompileError {
2239 const msg = msg: {
2279fn failWithNeededComptime(sema: *Sema, block: *Block, src: LazySrcLoc, reason: ?ComptimeReason) CompileError {
2280 const msg, const fail_block = msg: {
22402281 const msg = try sema.errMsg(src, "unable to resolve comptime value", .{});
22412282 errdefer msg.destroy(sema.gpa);
2242 try sema.errNote(src, msg, "{s}", .{reason.needed_comptime_reason});
2243 if (reason.value_comptime_reason) |value_comptime_reason| {
2244 try sema.errNote(src, msg, "{s}", .{value_comptime_reason});
2245 }
2246
2247 if (reason.block_comptime_reason) |block_comptime_reason| {
2248 try block_comptime_reason.explain(sema, msg);
2249 }
2250 break :msg msg;
2283 const fail_block = if (reason) |r| b: {
2284 try r.explain(sema, src, msg);
2285 break :b block;
2286 } else b: {
2287 break :b try block.explainWhyBlockIsComptime(msg);
2288 };
2289 break :msg .{ msg, fail_block };
22512290 };
2252 return sema.failWithOwnedErrorMsg(block, msg);
2291 return sema.failWithOwnedErrorMsg(fail_block, msg);
22532292}
22542293
22552294fn failWithUseOfUndef(sema: *Sema, block: *Block, src: LazySrcLoc) CompileError {
......@@ -2578,9 +2617,7 @@ pub fn analyzeAsAlign(
25782617 src: LazySrcLoc,
25792618 air_ref: Air.Inst.Ref,
25802619) !Alignment {
2581 const alignment_big = try sema.analyzeAsInt(block, src, air_ref, align_ty, .{
2582 .needed_comptime_reason = "alignment must be comptime-known",
2583 });
2620 const alignment_big = try sema.analyzeAsInt(block, src, air_ref, align_ty, .{ .simple = .@"align" });
25842621 return sema.validateAlign(block, src, alignment_big);
25852622}
25862623
......@@ -2615,7 +2652,7 @@ fn resolveInt(
26152652 src: LazySrcLoc,
26162653 zir_ref: Zir.Inst.Ref,
26172654 dest_ty: Type,
2618 reason: NeededComptimeReason,
2655 reason: ComptimeReason,
26192656) !u64 {
26202657 const air_ref = try sema.resolveInst(zir_ref);
26212658 return sema.analyzeAsInt(block, src, air_ref, dest_ty, reason);
......@@ -2627,7 +2664,7 @@ fn analyzeAsInt(
26272664 src: LazySrcLoc,
26282665 air_ref: Air.Inst.Ref,
26292666 dest_ty: Type,
2630 reason: NeededComptimeReason,
2667 reason: ComptimeReason,
26312668) !u64 {
26322669 const coerced = try sema.coerce(block, dest_ty, air_ref, src);
26332670 const val = try sema.resolveConstDefinedValue(block, src, coerced, reason);
......@@ -2687,9 +2724,7 @@ fn zirTupleDecl(
26872724 if (zir_field_init != .none) {
26882725 const uncoerced_field_init = try sema.resolveInst(zir_field_init);
26892726 const coerced_field_init = try sema.coerce(block, field_type, uncoerced_field_init, init_src);
2690 const field_init_val = try sema.resolveConstDefinedValue(block, init_src, coerced_field_init, .{
2691 .needed_comptime_reason = "tuple field default value must be comptime-known",
2692 });
2727 const field_init_val = try sema.resolveConstDefinedValue(block, init_src, coerced_field_init, .{ .simple = .tuple_field_default_value });
26932728 if (field_init_val.canMutateComptimeVarState(zcu)) {
26942729 return sema.fail(block, init_src, "field default value contains reference to comptime-mutable memory", .{});
26952730 }
......@@ -3414,7 +3449,7 @@ fn zirRetPtr(sema: *Sema, block: *Block) CompileError!Air.Inst.Ref {
34143449
34153450 const pt = sema.pt;
34163451
3417 if (block.is_comptime or try sema.fn_ret_ty.comptimeOnlySema(pt)) {
3452 if (block.isComptime() or try sema.fn_ret_ty.comptimeOnlySema(pt)) {
34183453 try sema.fn_ret_ty.resolveFields(pt);
34193454 return sema.analyzeComptimeAlloc(block, sema.fn_ret_ty, .none);
34203455 }
......@@ -3608,7 +3643,7 @@ fn zirAllocExtended(
36083643 break :blk try sema.resolveAlign(block, align_src, align_ref);
36093644 } else .none;
36103645
3611 if (block.is_comptime or small.is_comptime) {
3646 if (block.isComptime() or small.is_comptime) {
36123647 if (small.has_type) {
36133648 return sema.analyzeComptimeAlloc(block, var_ty, alignment);
36143649 } else {
......@@ -4075,7 +4110,7 @@ fn zirAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
40754110 const ty_src = block.src(.{ .node_offset_var_decl_ty = inst_data.src_node });
40764111
40774112 const var_ty = try sema.resolveType(block, ty_src, inst_data.operand);
4078 if (block.is_comptime or try var_ty.comptimeOnlySema(pt)) {
4113 if (block.isComptime() or try var_ty.comptimeOnlySema(pt)) {
40794114 return sema.analyzeComptimeAlloc(block, var_ty, .none);
40804115 }
40814116 if (sema.func_is_naked and try var_ty.hasRuntimeBitsSema(pt)) {
......@@ -4103,7 +4138,7 @@ fn zirAllocMut(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
41034138 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
41044139 const ty_src = block.src(.{ .node_offset_var_decl_ty = inst_data.src_node });
41054140 const var_ty = try sema.resolveType(block, ty_src, inst_data.operand);
4106 if (block.is_comptime) {
4141 if (block.isComptime()) {
41074142 return sema.analyzeComptimeAlloc(block, var_ty, .none);
41084143 }
41094144 if (sema.func_is_naked and try var_ty.hasRuntimeBitsSema(pt)) {
......@@ -4129,7 +4164,7 @@ fn zirAllocInferred(
41294164
41304165 const gpa = sema.gpa;
41314166
4132 if (block.is_comptime) {
4167 if (block.isComptime()) {
41334168 try sema.air_instructions.append(gpa, .{
41344169 .tag = .inferred_alloc_comptime,
41354170 .data = .{ .inferred_alloc_comptime = .{
......@@ -4778,7 +4813,7 @@ fn validateUnionInit(
47784813 return sema.failWithOwnedErrorMsg(block, msg);
47794814 }
47804815
4781 if (block.is_comptime and
4816 if (block.isComptime() and
47824817 (try sema.resolveDefinedValue(block, init_src, union_ptr)) != null)
47834818 {
47844819 // In this case, comptime machinery already did everything. No work to do here.
......@@ -4897,9 +4932,11 @@ fn validateUnionInit(
48974932 try sema.storePtr2(block, init_src, union_ptr, init_src, union_init, init_src, .store);
48984933 return;
48994934 } else if (try union_ty.comptimeOnlySema(pt)) {
4900 return sema.failWithNeededComptime(block, block.nodeOffset(field_ptr_data.src_node), .{
4901 .needed_comptime_reason = "initializer of comptime only union must be comptime-known",
4902 });
4935 const src = block.nodeOffset(field_ptr_data.src_node);
4936 return sema.failWithNeededComptime(block, src, .{ .comptime_only = .{
4937 .ty = union_ty,
4938 .msg = .union_init,
4939 } });
49034940 }
49044941 if (init_ref) |v| try sema.validateRuntimeValue(block, block.nodeOffset(field_ptr_data.src_node), v);
49054942
......@@ -4953,7 +4990,7 @@ fn validateStructInit(
49534990 errdefer if (root_msg) |msg| msg.destroy(sema.gpa);
49544991
49554992 const struct_ptr = try sema.resolveInst(struct_ptr_zir_ref);
4956 if (block.is_comptime and
4993 if (block.isComptime() and
49574994 (try sema.resolveDefinedValue(block, init_src, struct_ptr)) != null)
49584995 {
49594996 try struct_ty.resolveLayout(pt);
......@@ -5081,9 +5118,11 @@ fn validateStructInit(
50815118 field_values[i] = val.toIntern();
50825119 } else if (require_comptime) {
50835120 const field_ptr_data = sema.code.instructions.items(.data)[@intFromEnum(field_ptr)].pl_node;
5084 return sema.failWithNeededComptime(block, block.nodeOffset(field_ptr_data.src_node), .{
5085 .needed_comptime_reason = "initializer of comptime only struct must be comptime-known",
5086 });
5121 const src = block.nodeOffset(field_ptr_data.src_node);
5122 return sema.failWithNeededComptime(block, src, .{ .comptime_only = .{
5123 .ty = struct_ty,
5124 .msg = .struct_init,
5125 } });
50875126 } else {
50885127 struct_is_comptime = false;
50895128 }
......@@ -5253,7 +5292,7 @@ fn zirValidatePtrArrayInit(
52535292 else => unreachable,
52545293 };
52555294
5256 if (block.is_comptime and
5295 if (block.isComptime() and
52575296 (try sema.resolveDefinedValue(block, init_src, array_ptr)) != null)
52585297 {
52595298 // In this case the comptime machinery will have evaluated the store instructions
......@@ -5629,9 +5668,7 @@ fn storeToInferredAllocComptime(
56295668 // There will be only one store_to_inferred_ptr because we are running at comptime.
56305669 // The alloc will turn into a Decl or a ComptimeAlloc.
56315670 const operand_val = try sema.resolveValue(operand) orelse {
5632 return sema.failWithNeededComptime(block, src, .{
5633 .needed_comptime_reason = "value being stored to a comptime variable must be comptime-known",
5634 });
5671 return sema.failWithNeededComptime(block, src, .{ .simple = .stored_to_comptime_var });
56355672 };
56365673 const alloc_ty = try pt.ptrTypeSema(.{
56375674 .child = operand_ty.toIntern(),
......@@ -5663,9 +5700,7 @@ fn storeToInferredAllocComptime(
56635700fn zirSetEvalBranchQuota(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
56645701 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
56655702 const src = block.nodeOffset(inst_data.src_node);
5666 const quota: u32 = @intCast(try sema.resolveInt(block, src, inst_data.operand, Type.u32, .{
5667 .needed_comptime_reason = "eval branch quota must be comptime-known",
5668 }));
5703 const quota: u32 = @intCast(try sema.resolveInt(block, src, inst_data.operand, .u32, .{ .simple = .operand_setEvalBranchQuota }));
56695704 sema.branch_quota = @max(sema.branch_quota, quota);
56705705 sema.allow_memoize = false;
56715706}
......@@ -5794,9 +5829,7 @@ fn zirCompileError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
57945829 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
57955830 const src = block.nodeOffset(inst_data.src_node);
57965831 const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0);
5797 const msg = try sema.resolveConstString(block, operand_src, inst_data.operand, .{
5798 .needed_comptime_reason = "compile error string must be comptime-known",
5799 });
5832 const msg = try sema.resolveConstString(block, operand_src, inst_data.operand, .{ .simple = .compile_error_string });
58005833 return sema.fail(block, src, "{s}", .{msg});
58015834}
58025835
......@@ -5848,7 +5881,7 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
58485881 // source location if we do it here.
58495882 const coerced_msg = try sema.coerce(block, Type.slice_const_u8, msg_inst, block.builtinCallArgSrc(inst_data.src_node, 0));
58505883
5851 if (block.is_comptime) {
5884 if (block.isComptime()) {
58525885 return sema.fail(block, src, "encountered @panic at comptime", .{});
58535886 }
58545887
......@@ -5864,7 +5897,7 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
58645897fn zirTrap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
58655898 const src_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].node;
58665899 const src = block.nodeOffset(src_node);
5867 if (block.is_comptime)
5900 if (block.isComptime())
58685901 return sema.fail(block, src, "encountered @trap at comptime", .{});
58695902 _ = try block.addNoOp(.trap);
58705903}
......@@ -5974,15 +6007,16 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
59746007 var c_import_buf = std.ArrayList(u8).init(gpa);
59756008 defer c_import_buf.deinit();
59766009
5977 const comptime_reason: Block.ComptimeReason = .{ .c_import = .{ .src = src } };
59786010 var child_block: Block = .{
59796011 .parent = parent_block,
59806012 .sema = sema,
59816013 .namespace = parent_block.namespace,
59826014 .instructions = .{},
59836015 .inlining = parent_block.inlining,
5984 .is_comptime = true,
5985 .comptime_reason = &comptime_reason,
6016 .comptime_reason = .{ .reason = .{
6017 .src = src,
6018 .r = .{ .simple = .operand_cImport },
6019 } },
59866020 .c_import_buf = &c_import_buf,
59876021 .runtime_cond = parent_block.runtime_cond,
59886022 .runtime_loop = parent_block.runtime_loop,
......@@ -6073,7 +6107,7 @@ fn zirSuspendBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) Comp
60736107 return sema.failWithUseOfAsync(parent_block, src);
60746108}
60756109
6076fn zirBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index, force_comptime: bool) CompileError!Air.Inst.Ref {
6110fn zirBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
60776111 const tracy = trace(@src());
60786112 defer tracy.end();
60796113
......@@ -6109,7 +6143,6 @@ fn zirBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index, force_compt
61096143 .instructions = .{},
61106144 .label = &label,
61116145 .inlining = parent_block.inlining,
6112 .is_comptime = parent_block.is_comptime or force_comptime,
61136146 .comptime_reason = parent_block.comptime_reason,
61146147 .is_typeof = parent_block.is_typeof,
61156148 .want_safety = parent_block.want_safety,
......@@ -6143,7 +6176,7 @@ fn resolveBlockBody(
61436176 body_inst: Zir.Inst.Index,
61446177 merges: *Block.Merges,
61456178) CompileError!Air.Inst.Ref {
6146 if (child_block.is_comptime) {
6179 if (child_block.isComptime()) {
61476180 return sema.resolveInlineBody(child_block, body, body_inst);
61486181 } else {
61496182 assert(sema.air_instructions.items(.tag)[@intFromEnum(merges.block_inst)] == .block);
......@@ -6303,7 +6336,7 @@ fn resolveAnalyzedBlock(
63036336 }
63046337 }
63056338 // It is impossible to have the number of results be > 1 in a comptime scope.
6306 assert(!child_block.is_comptime); // Should already got a compile error in the condbr condition.
6339 assert(!child_block.isComptime()); // Should already got a compile error in the condbr condition.
63076340
63086341 // Note that we'll always create an AIR block here, so `need_debug_scope` is irrelevant.
63096342
......@@ -6425,9 +6458,7 @@ fn zirExport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
64256458 const options_src = block.builtinCallArgSrc(inst_data.src_node, 1);
64266459
64276460 const ptr = try sema.resolveInst(extra.exported);
6428 const ptr_val = try sema.resolveConstDefinedValue(block, ptr_src, ptr, .{
6429 .needed_comptime_reason = "export target must be comptime-known",
6430 });
6461 const ptr_val = try sema.resolveConstDefinedValue(block, ptr_src, ptr, .{ .simple = .export_target });
64316462 const ptr_ty = ptr_val.typeOf(zcu);
64326463
64336464 const options = try sema.resolveExportOptions(block, options_src, extra.options);
......@@ -6553,17 +6584,13 @@ fn zirDisableInstrumentation(sema: *Sema) CompileError!void {
65536584fn zirSetFloatMode(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void {
65546585 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
65556586 const src = block.builtinCallArgSrc(extra.node, 0);
6556 block.float_mode = try sema.resolveBuiltinEnum(block, src, extra.operand, "FloatMode", .{
6557 .needed_comptime_reason = "operand to @setFloatMode must be comptime-known",
6558 });
6587 block.float_mode = try sema.resolveBuiltinEnum(block, src, extra.operand, "FloatMode", .{ .simple = .operand_setFloatMode });
65596588}
65606589
65616590fn zirSetRuntimeSafety(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
65626591 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
65636592 const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0);
6564 block.want_safety = try sema.resolveConstBool(block, operand_src, inst_data.operand, .{
6565 .needed_comptime_reason = "operand to @setRuntimeSafety must be comptime-known",
6566 });
6593 block.want_safety = try sema.resolveConstBool(block, operand_src, inst_data.operand, .{ .simple = .operand_setRuntimeSafety });
65676594}
65686595
65696596fn zirBreak(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index) CompileError!void {
......@@ -6650,7 +6677,7 @@ fn zirSwitchContinue(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index) Com
66506677}
66516678
66526679fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
6653 if (block.is_comptime or block.ownerModule().strip) return;
6680 if (block.isComptime() or block.ownerModule().strip) return;
66546681
66556682 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;
66566683
......@@ -6676,7 +6703,7 @@ fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi
66766703}
66776704
66786705fn zirDbgEmptyStmt(_: *Sema, block: *Block, _: Zir.Inst.Index) CompileError!void {
6679 if (block.is_comptime or block.ownerModule().strip) return;
6706 if (block.isComptime() or block.ownerModule().strip) return;
66806707 _ = try block.addNoOp(.dbg_empty_stmt);
66816708}
66826709
......@@ -6699,7 +6726,7 @@ fn addDbgVar(
66996726 air_tag: Air.Inst.Tag,
67006727 name: []const u8,
67016728) CompileError!void {
6702 if (block.is_comptime or block.ownerModule().strip) return;
6729 if (block.isComptime() or block.ownerModule().strip) return;
67036730
67046731 const pt = sema.pt;
67056732 const zcu = pt.zcu;
......@@ -6931,7 +6958,7 @@ pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref
69316958 const zcu = pt.zcu;
69326959 const gpa = sema.gpa;
69336960
6934 if (block.is_comptime or block.is_typeof) {
6961 if (block.isComptime() or block.is_typeof) {
69356962 const index_val = try pt.intValue_u64(Type.usize, sema.comptime_err_ret_trace.items.len);
69366963 return Air.internedToRef(index_val.toIntern());
69376964 }
......@@ -7134,7 +7161,7 @@ fn zirCall(
71347161 }
71357162
71367163 if (block.ownerModule().error_tracing and
7137 !block.is_comptime and !block.is_typeof and (input_is_error or pop_error_return_trace))
7164 !block.isComptime() and !block.is_typeof and (input_is_error or pop_error_return_trace))
71387165 {
71397166 const return_ty = sema.typeOf(call_inst);
71407167 if (modifier != .always_tail and return_ty.isNoReturn(zcu))
......@@ -7404,12 +7431,14 @@ const CallArgsInfo = union(enum) {
74047431 };
74057432
74067433 // Generate args to comptime params in comptime block
7407 const parent_comptime = block.is_comptime;
7408 defer block.is_comptime = parent_comptime;
7434 const parent_comptime = block.comptime_reason;
7435 defer block.comptime_reason = parent_comptime;
74097436 // Note that we are indexing into parameters, not arguments, so use `arg_index` instead of `real_arg_idx`
74107437 if (arg_index < @min(param_count, 32) and func_ty_info.paramIsComptime(@intCast(arg_index))) {
7411 block.is_comptime = true;
7412 // TODO set comptime_reason
7438 block.comptime_reason = .{ .reason = .{
7439 .src = cai.argSrc(block, arg_index),
7440 .r = .{ .simple = .comptime_param_arg },
7441 } };
74137442 }
74147443 // Give the arg its result type
74157444 const provide_param_ty = if (maybe_param_ty) |t| t else Type.generic_poison;
......@@ -7611,23 +7640,37 @@ fn analyzeCall(
76117640
76127641 const gpa = sema.gpa;
76137642
7643 const func_ret_ty_src: LazySrcLoc = if (try sema.funcDeclSrcInst(func)) |fn_decl_inst| .{
7644 .base_node_inst = fn_decl_inst,
7645 .offset = .{ .node_offset_fn_type_ret_ty = 0 },
7646 } else func_src;
7647
7648 // If this is not `null`, the call is comptime.
7649 var comptime_call_reason: ?BlockComptimeReason = cr: {
7650 if (block.comptime_reason) |r| break :cr r;
7651 if (modifier == .compile_time) break :cr .{ .reason = .{
7652 .src = call_src,
7653 .r = .{ .simple = .comptime_call_modifier },
7654 } };
7655 break :cr null;
7656 };
7657
76147658 const is_generic_call = func_ty_info.is_generic;
7615 var is_comptime_call = block.is_comptime or modifier == .compile_time;
7616 var is_inline_call = is_comptime_call or modifier == .always_inline or func_ty_info.cc == .@"inline";
7617 var comptime_reason: ?*const Block.ComptimeReason = null;
7618 if (!is_inline_call and !is_comptime_call) {
7659 var is_inline_call = comptime_call_reason != null or modifier == .always_inline or func_ty_info.cc == .@"inline";
7660 if (!is_inline_call) {
76197661 if (try Type.fromInterned(func_ty_info.return_type).comptimeOnlySema(pt)) {
7620 is_comptime_call = true;
76217662 is_inline_call = true;
7622 comptime_reason = &.{ .comptime_ret_ty = .{
7623 .func = func,
7624 .func_src = func_src,
7625 .return_ty = Type.fromInterned(func_ty_info.return_type),
7663 comptime_call_reason = .{ .reason = .{
7664 .src = func_ret_ty_src,
7665 .r = .{ .comptime_only = .{
7666 .ty = .fromInterned(func_ty_info.return_type),
7667 .msg = .ret_ty_call,
7668 } },
76267669 } };
76277670 }
76287671 }
76297672
7630 if (sema.func_is_naked and !is_inline_call and !is_comptime_call) {
7673 if (sema.func_is_naked and !is_inline_call) {
76317674 const msg = msg: {
76327675 const msg = try sema.errMsg(call_src, "runtime {s} not allowed in naked function", .{@tagName(operation)});
76337676 errdefer msg.destroy(sema.gpa);
......@@ -7642,6 +7685,7 @@ fn analyzeCall(
76427685 }
76437686
76447687 if (!is_inline_call and is_generic_call) {
7688 var comptime_ret_ty: Type = undefined;
76457689 if (sema.instantiateGenericCall(
76467690 block,
76477691 func,
......@@ -7651,6 +7695,7 @@ fn analyzeCall(
76517695 args_info,
76527696 call_tag,
76537697 call_dbg_node,
7698 &comptime_ret_ty,
76547699 )) |some| {
76557700 return some;
76567701 } else |err| switch (err) {
......@@ -7659,26 +7704,34 @@ fn analyzeCall(
76597704 },
76607705 error.ComptimeReturn => {
76617706 is_inline_call = true;
7662 is_comptime_call = true;
7663 comptime_reason = &.{ .comptime_ret_ty = .{
7664 .func = func,
7665 .func_src = func_src,
7666 .return_ty = Type.fromInterned(func_ty_info.return_type),
7707 comptime_call_reason = .{ .reason = .{
7708 .src = func_ret_ty_src,
7709 .r = .{
7710 .comptime_only = .{
7711 .ty = comptime_ret_ty,
7712 .msg = .ret_ty_generic_call,
7713 },
7714 },
76677715 } };
76687716 },
76697717 else => |e| return e,
76707718 }
76717719 }
76727720
7721 const is_comptime_call = comptime_call_reason != null;
7722 // `comptime_call_reason` shouldn't be mutated again
7723 defer assert(is_comptime_call == (comptime_call_reason != null));
7724
76737725 if (is_comptime_call and modifier == .never_inline) {
76747726 return sema.fail(block, call_src, "unable to perform 'never_inline' call at compile-time", .{});
76757727 }
76767728
76777729 const result: Air.Inst.Ref = if (is_inline_call) res: {
7678 const func_val = try sema.resolveConstDefinedValue(block, func_src, func, .{
7679 .needed_comptime_reason = "function being called at comptime must be comptime-known",
7680 .block_comptime_reason = comptime_reason,
7681 });
7730 const old_comptime_reason = block.comptime_reason;
7731 block.comptime_reason = comptime_call_reason;
7732 defer block.comptime_reason = old_comptime_reason;
7733
7734 const func_val = try sema.resolveConstDefinedValue(block, func_src, func, .{ .simple = .comptime_call_target });
76827735 const module_fn_index = switch (zcu.intern_pool.indexToKey(func_val.toIntern())) {
76837736 .@"extern" => return sema.fail(block, call_src, "{s} call of extern function", .{
76847737 @as([]const u8, if (is_comptime_call) "comptime" else "inline"),
......@@ -7767,8 +7820,7 @@ fn analyzeCall(
77677820 .label = null,
77687821 .inlining = &inlining,
77697822 .is_typeof = block.is_typeof,
7770 .is_comptime = is_comptime_call,
7771 .comptime_reason = comptime_reason,
7823 .comptime_reason = if (is_comptime_call) .inlining_parent else null,
77727824 .error_return_trace_index = block.error_return_trace_index,
77737825 .runtime_cond = block.runtime_cond,
77747826 .runtime_loop = block.runtime_loop,
......@@ -7857,11 +7909,16 @@ fn analyzeCall(
78577909 // on parameters, we must now do the same for the return type as we just did with
78587910 // each of the parameters, resolving the return type and providing it to the child
78597911 // `Sema` so that it can be used for the `ret_ptr` instruction.
7860 const ret_ty_inst = if (fn_info.ret_ty_body.len != 0)
7861 try sema.resolveInlineBody(&child_block, fn_info.ret_ty_body, module_fn.zir_body_inst.resolve(ip) orelse return error.AnalysisFail)
7862 else
7863 try sema.resolveInst(fn_info.ret_ty_ref);
78647912 const ret_ty_src: LazySrcLoc = .{ .base_node_inst = module_fn.zir_body_inst, .offset = .{ .node_offset_fn_type_ret_ty = 0 } };
7913 const ret_ty_inst = if (fn_info.ret_ty_body.len != 0) r: {
7914 const old_child_comptime_reason = child_block.comptime_reason;
7915 defer child_block.comptime_reason = old_child_comptime_reason;
7916 child_block.comptime_reason = .{ .reason = .{
7917 .src = ret_ty_src,
7918 .r = .{ .simple = .function_ret_ty },
7919 } };
7920 break :r try sema.resolveInlineBody(&child_block, fn_info.ret_ty_body, module_fn.zir_body_inst.resolve(ip) orelse return error.AnalysisFail);
7921 } else try sema.resolveInst(fn_info.ret_ty_ref);
78657922 sema.fn_ret_ty = try sema.analyzeAsType(&child_block, ret_ty_src, ret_ty_inst);
78667923 if (module_fn.analysisUnordered(ip).inferred_error_set) {
78677924 // Create a fresh inferred error set type for inline/comptime calls.
......@@ -8136,23 +8193,18 @@ fn analyzeInlineCallArg(
81368193 return casted_arg;
81378194 }
81388195 const arg_src = args_info.argSrc(arg_block, arg_i.*);
8139 if (try Type.fromInterned(param_ty).comptimeOnlySema(ics.callee().pt)) {
8140 _ = try ics.caller().resolveConstValue(arg_block, arg_src, casted_arg, .{
8141 .needed_comptime_reason = "argument to parameter with comptime-only type must be comptime-known",
8142 .block_comptime_reason = param_block.comptime_reason,
8143 });
8144 } else if (!is_comptime_call and zir_tags[@intFromEnum(inst)] == .param_comptime) {
8145 _ = try ics.caller().resolveConstValue(arg_block, arg_src, casted_arg, .{
8146 .needed_comptime_reason = "parameter is comptime",
8147 });
8196 if (zir_tags[@intFromEnum(inst)] == .param_comptime) {
8197 _ = try ics.caller().resolveConstValue(arg_block, arg_src, casted_arg, .{ .simple = .comptime_param_arg });
8198 } else if (!is_comptime_call and try Type.fromInterned(param_ty).comptimeOnlySema(ics.callee().pt)) {
8199 _ = try ics.caller().resolveConstValue(arg_block, arg_src, casted_arg, .{ .comptime_only = .{
8200 .ty = .fromInterned(param_ty),
8201 .msg = .param_ty_arg,
8202 } });
81488203 }
81498204
81508205 if (is_comptime_call) {
81518206 ics.callee().inst_map.putAssumeCapacityNoClobber(inst, casted_arg);
8152 const arg_val = try ics.caller().resolveConstValue(arg_block, arg_src, casted_arg, .{
8153 .needed_comptime_reason = "argument to function being called at comptime must be comptime-known",
8154 .block_comptime_reason = param_block.comptime_reason,
8155 });
8207 const arg_val = try ics.caller().resolveConstValue(arg_block, arg_src, casted_arg, null);
81568208 switch (arg_val.toIntern()) {
81578209 .generic_poison, .generic_poison_type => {
81588210 // This function is currently evaluated as part of an as-of-yet unresolvable
......@@ -8188,10 +8240,7 @@ fn analyzeInlineCallArg(
81888240
81898241 if (is_comptime_call) {
81908242 ics.callee().inst_map.putAssumeCapacityNoClobber(inst, uncasted_arg);
8191 const arg_val = try ics.caller().resolveConstValue(arg_block, arg_src, uncasted_arg, .{
8192 .needed_comptime_reason = "argument to function being called at comptime must be comptime-known",
8193 .block_comptime_reason = param_block.comptime_reason,
8194 });
8243 const arg_val = try ics.caller().resolveConstValue(arg_block, arg_src, uncasted_arg, null);
81958244 switch (arg_val.toIntern()) {
81968245 .generic_poison, .generic_poison_type => {
81978246 // This function is currently evaluated as part of an as-of-yet unresolvable
......@@ -8208,9 +8257,7 @@ fn analyzeInlineCallArg(
82088257 memoized_arg_values[arg_i.*] = resolved_arg_val.toIntern();
82098258 } else {
82108259 if (zir_tags[@intFromEnum(inst)] == .param_anytype_comptime) {
8211 _ = try ics.caller().resolveConstValue(arg_block, arg_src, uncasted_arg, .{
8212 .needed_comptime_reason = "parameter is comptime",
8213 });
8260 _ = try ics.caller().resolveConstValue(arg_block, arg_src, uncasted_arg, .{ .simple = .comptime_param_arg });
82148261 }
82158262 ics.callee().inst_map.putAssumeCapacityNoClobber(inst, uncasted_arg);
82168263 }
......@@ -8237,15 +8284,18 @@ fn instantiateGenericCall(
82378284 args_info: CallArgsInfo,
82388285 call_tag: Air.Inst.Tag,
82398286 call_dbg_node: ?Zir.Inst.Index,
8287 /// Populated when `error.ComptimeReturn` is returned.
8288 comptime_ret_ty: *Type,
82408289) CompileError!Air.Inst.Ref {
82418290 const pt = sema.pt;
82428291 const zcu = pt.zcu;
82438292 const gpa = sema.gpa;
82448293 const ip = &zcu.intern_pool;
82458294
8246 const func_val = try sema.resolveConstDefinedValue(block, func_src, func, .{
8247 .needed_comptime_reason = "generic function being called must be comptime-known",
8248 });
8295 // Generic function pointers are comptime-only types, so `func` is definitely comptime-known.
8296 const func_val = (sema.resolveValue(func) catch unreachable).?;
8297 if (func_val.isUndef(zcu)) return sema.failWithUseOfUndef(block, func_src);
8298
82498299 const generic_owner = switch (zcu.intern_pool.indexToKey(func_val.toIntern())) {
82508300 .func => func_val.toIntern(),
82518301 .ptr => |ptr| ip.getNav(ptr.base_addr.nav).status.fully_resolved.val,
......@@ -8310,7 +8360,7 @@ fn instantiateGenericCall(
83108360 .namespace = fn_nav.analysis.?.namespace,
83118361 .instructions = .{},
83128362 .inlining = null,
8313 .is_comptime = true,
8363 .comptime_reason = undefined, // set as needed
83148364 .src_base_inst = fn_nav.analysis.?.zir_index,
83158365 .type_name_ctx = fn_nav.fqn,
83168366 };
......@@ -8354,12 +8404,13 @@ fn instantiateGenericCall(
83548404 child_sema.generic_call_src = prev_generic_call_src;
83558405 }
83568406
8407 const param_ty_src = child_block.tokenOffset(param_data.src_tok);
8408 child_block.comptime_reason = .{ .reason = .{
8409 .src = param_ty_src,
8410 .r = .{ .simple = .type },
8411 } };
83578412 const param_ty_inst = try child_sema.resolveInlineBody(&child_block, param_ty_body, param_inst);
8358 break :param_ty try child_sema.analyzeAsType(
8359 &child_block,
8360 child_block.tokenOffset(param_data.src_tok),
8361 param_ty_inst,
8362 );
8413 break :param_ty try child_sema.analyzeAsType(&child_block, param_ty_src, param_ty_inst);
83638414 },
83648415 else => unreachable,
83658416 }
......@@ -8452,6 +8503,10 @@ fn instantiateGenericCall(
84528503
84538504 // We've already handled parameters, so don't resolve the whole body. Instead, just
84548505 // do the instructions after the params (i.e. the func itself).
8506 child_block.comptime_reason = .{ .reason = .{
8507 .src = call_src,
8508 .r = .{ .simple = .type },
8509 } };
84558510 const new_func_inst = try child_sema.resolveInlineBody(&child_block, fn_info.param_body[args_info.count()..], fn_info.param_body_inst);
84568511 const callee_index = (child_sema.resolveConstDefinedValue(&child_block, LazySrcLoc.unneeded, new_func_inst, undefined) catch unreachable).toIntern();
84578512
......@@ -8465,6 +8520,7 @@ fn instantiateGenericCall(
84658520 // If the call evaluated to a return type that requires comptime, never mind
84668521 // our generic instantiation. Instead we need to perform a comptime call.
84678522 if (try Type.fromInterned(func_ty_info.return_type).comptimeOnlySema(pt)) {
8523 comptime_ret_ty.* = .fromInterned(func_ty_info.return_type);
84688524 return error.ComptimeReturn;
84698525 }
84708526 // Similarly, if the call evaluated to a generic type we need to instead
......@@ -8622,9 +8678,7 @@ fn zirVectorType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
86228678 const len_src = block.builtinCallArgSrc(inst_data.src_node, 0);
86238679 const elem_type_src = block.builtinCallArgSrc(inst_data.src_node, 1);
86248680 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
8625 const len: u32 = @intCast(try sema.resolveInt(block, len_src, extra.lhs, Type.u32, .{
8626 .needed_comptime_reason = "vector length must be comptime-known",
8627 }));
8681 const len: u32 = @intCast(try sema.resolveInt(block, len_src, extra.lhs, Type.u32, .{ .simple = .vector_length }));
86288682 const elem_type = try sema.resolveType(block, elem_type_src, extra.rhs);
86298683 try sema.checkVectorElemType(block, elem_type_src, elem_type);
86308684 const vector_type = try sema.pt.vectorType(.{
......@@ -8642,9 +8696,7 @@ fn zirArrayType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
86428696 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
86438697 const len_src = block.src(.{ .node_offset_array_type_len = inst_data.src_node });
86448698 const elem_src = block.src(.{ .node_offset_array_type_elem = inst_data.src_node });
8645 const len = try sema.resolveInt(block, len_src, extra.lhs, Type.usize, .{
8646 .needed_comptime_reason = "array length must be comptime-known",
8647 });
8699 const len = try sema.resolveInt(block, len_src, extra.lhs, Type.usize, .{ .simple = .array_length });
86488700 const elem_type = try sema.resolveType(block, elem_src, extra.rhs);
86498701 try sema.validateArrayElemType(block, elem_type, elem_src);
86508702 const array_ty = try sema.pt.arrayType(.{
......@@ -8664,16 +8716,12 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compil
86648716 const len_src = block.src(.{ .node_offset_array_type_len = inst_data.src_node });
86658717 const sentinel_src = block.src(.{ .node_offset_array_type_sentinel = inst_data.src_node });
86668718 const elem_src = block.src(.{ .node_offset_array_type_elem = inst_data.src_node });
8667 const len = try sema.resolveInt(block, len_src, extra.len, Type.usize, .{
8668 .needed_comptime_reason = "array length must be comptime-known",
8669 });
8719 const len = try sema.resolveInt(block, len_src, extra.len, Type.usize, .{ .simple = .array_length });
86708720 const elem_type = try sema.resolveType(block, elem_src, extra.elem_type);
86718721 try sema.validateArrayElemType(block, elem_type, elem_src);
86728722 const uncasted_sentinel = try sema.resolveInst(extra.sentinel);
86738723 const sentinel = try sema.coerce(block, elem_type, uncasted_sentinel, sentinel_src);
8674 const sentinel_val = try sema.resolveConstDefinedValue(block, sentinel_src, sentinel, .{
8675 .needed_comptime_reason = "array sentinel value must be comptime-known",
8676 });
8724 const sentinel_val = try sema.resolveConstDefinedValue(block, sentinel_src, sentinel, .{ .simple = .array_sentinel });
86778725 const array_ty = try sema.pt.arrayType(.{
86788726 .len = len,
86798727 .sentinel = sentinel_val.toIntern(),
......@@ -9071,9 +9119,7 @@ fn zirEnumFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
90719119 }
90729120
90739121 if (dest_ty.intTagType(zcu).zigTypeTag(zcu) == .comptime_int) {
9074 return sema.failWithNeededComptime(block, operand_src, .{
9075 .needed_comptime_reason = "value being casted to enum with 'comptime_int' tag type must be comptime-known",
9076 });
9122 return sema.failWithNeededComptime(block, operand_src, .{ .simple = .casted_to_comptime_enum });
90779123 }
90789124
90799125 if (try sema.typeHasOnePossibleValue(dest_ty)) |opv| {
......@@ -9487,9 +9533,7 @@ fn zirFunc(
94879533 const ret_ty_body = sema.code.bodySlice(extra_index, extra.data.ret_body_len);
94889534 extra_index += ret_ty_body.len;
94899535
9490 const ret_ty_val = try sema.resolveGenericBody(block, ret_ty_src, ret_ty_body, inst, Type.type, .{
9491 .needed_comptime_reason = "return type must be comptime-known",
9492 });
9536 const ret_ty_val = try sema.resolveGenericBody(block, ret_ty_src, ret_ty_body, inst, Type.type, .{ .simple = .function_ret_ty });
94939537 break :blk ret_ty_val.toType();
94949538 },
94959539 };
......@@ -9556,7 +9600,7 @@ fn resolveGenericBody(
95569600 body: []const Zir.Inst.Index,
95579601 func_inst: Zir.Inst.Index,
95589602 dest_ty: Type,
9559 reason: NeededComptimeReason,
9603 reason: ComptimeReason,
95609604) !Value {
95619605 assert(body.len != 0);
95629606
......@@ -9894,7 +9938,7 @@ fn funcCommon(
98949938 };
98959939 return sema.failWithOwnedErrorMsg(block, msg);
98969940 }
9897 if (is_source_decl and requires_comptime and !param_is_comptime and has_body and !block.is_comptime) {
9941 if (is_source_decl and requires_comptime and !param_is_comptime and has_body and !block.isComptime()) {
98989942 const msg = msg: {
98999943 const msg = try sema.errMsg(param_src, "parameter of type '{}' must be declared comptime", .{
99009944 param_ty.fmt(pt),
......@@ -10132,7 +10176,7 @@ fn finishFunc(
1013210176
1013310177 // If the return type is comptime-only but not dependent on parameters then
1013410178 // all parameter types also need to be comptime.
10135 if (is_source_decl and opt_func_index != .none and ret_ty_requires_comptime and !block.is_comptime) comptime_check: {
10179 if (is_source_decl and opt_func_index != .none and ret_ty_requires_comptime and !block.isComptime()) comptime_check: {
1013610180 for (block.params.items(.is_comptime)) |is_comptime| {
1013710181 if (!is_comptime) break;
1013810182 } else break :comptime_check;
......@@ -10547,9 +10591,7 @@ fn zirFieldValNamed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
1054710591 const field_name_src = block.builtinCallArgSrc(inst_data.src_node, 1);
1054810592 const extra = sema.code.extraData(Zir.Inst.FieldNamed, inst_data.payload_index).data;
1054910593 const object = try sema.resolveInst(extra.lhs);
10550 const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, .{
10551 .needed_comptime_reason = "field name must be comptime-known",
10552 });
10594 const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, .{ .simple = .field_name });
1055310595 return sema.fieldVal(block, src, object, field_name, field_name_src);
1055410596}
1055510597
......@@ -10562,9 +10604,7 @@ fn zirFieldPtrNamed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
1056210604 const field_name_src = block.builtinCallArgSrc(inst_data.src_node, 1);
1056310605 const extra = sema.code.extraData(Zir.Inst.FieldNamed, inst_data.payload_index).data;
1056410606 const object_ptr = try sema.resolveInst(extra.lhs);
10565 const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, .{
10566 .needed_comptime_reason = "field name must be comptime-known",
10567 });
10607 const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, .{ .simple = .field_name });
1056810608 return sema.fieldPtr(block, src, object_ptr, field_name, field_name_src, false);
1056910609}
1057010610
......@@ -11923,7 +11963,6 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1192311963 .instructions = .{},
1192411964 .label = &label,
1192511965 .inlining = block.inlining,
11926 .is_comptime = block.is_comptime,
1192711966 .comptime_reason = block.comptime_reason,
1192811967 .is_typeof = block.is_typeof,
1192911968 .c_import_buf = block.c_import_buf,
......@@ -12027,11 +12066,8 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1202712066 };
1202812067 }
1202912068
12030 if (child_block.is_comptime) {
12031 _ = try sema.resolveConstDefinedValue(&child_block, main_operand_src, raw_operand_val, .{
12032 .needed_comptime_reason = "condition in comptime switch must be comptime-known",
12033 .block_comptime_reason = child_block.comptime_reason,
12034 });
12069 if (child_block.isComptime()) {
12070 _ = try sema.resolveConstDefinedValue(&child_block, main_operand_src, raw_operand_val, null);
1203512071 unreachable;
1203612072 }
1203712073
......@@ -12148,7 +12184,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1214812184
1214912185 const operand_ty = sema.typeOf(val);
1215012186
12151 if (extra.data.bits.has_continue and !block.is_comptime) {
12187 if (extra.data.bits.has_continue and !block.isComptime()) {
1215212188 // Even if the operand is comptime-known, this `switch` is runtime.
1215312189 if (try operand_ty.comptimeOnlySema(pt)) {
1215412190 return sema.failWithOwnedErrorMsg(block, msg: {
......@@ -12707,7 +12743,6 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1270712743 .instructions = .{},
1270812744 .label = &label,
1270912745 .inlining = block.inlining,
12710 .is_comptime = block.is_comptime,
1271112746 .comptime_reason = block.comptime_reason,
1271212747 .is_typeof = block.is_typeof,
1271312748 .c_import_buf = block.c_import_buf,
......@@ -12790,11 +12825,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1279012825 },
1279112826 }
1279212827
12793 if (child_block.is_comptime) {
12794 _ = try sema.resolveConstDefinedValue(&child_block, operand_src, operand.simple.cond, .{
12795 .needed_comptime_reason = "condition in comptime switch must be comptime-known",
12796 .block_comptime_reason = child_block.comptime_reason,
12797 });
12828 if (child_block.isComptime()) {
12829 _ = try sema.resolveConstDefinedValue(&child_block, operand_src, operand.simple.cond, null);
1279812830 unreachable;
1279912831 }
1280012832
......@@ -13582,10 +13614,7 @@ fn resolveSwitchComptimeLoop(
1358213614
1358313615 const cond_ref = try sema.switchCond(child_block, src, val);
1358413616
13585 cond_val = try sema.resolveConstDefinedValue(child_block, src, cond_ref, .{
13586 .needed_comptime_reason = "condition in comptime switch must be comptime-known",
13587 .block_comptime_reason = child_block.comptime_reason,
13588 });
13617 cond_val = try sema.resolveConstDefinedValue(child_block, src, cond_ref, null);
1358913618 spa.operand = .{ .simple = .{
1359013619 .by_val = val,
1359113620 .by_ref = ref,
......@@ -13825,9 +13854,7 @@ fn resolveSwitchItemVal(
1382513854
1382613855 const item = try sema.coerce(block, coerce_ty, uncoerced_item, item_src);
1382713856
13828 const maybe_lazy = try sema.resolveConstDefinedValue(block, item_src, item, .{
13829 .needed_comptime_reason = "switch prong values must be comptime-known",
13830 });
13857 const maybe_lazy = try sema.resolveConstDefinedValue(block, item_src, item, .{ .simple = .switch_item });
1383113858
1383213859 const val = try sema.resolveLazyValue(maybe_lazy);
1383313860 const new_item = if (val.toIntern() != maybe_lazy.toIntern()) blk: {
......@@ -14295,9 +14322,7 @@ fn zirHasField(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1429514322 const ty_src = block.builtinCallArgSrc(inst_data.src_node, 0);
1429614323 const name_src = block.builtinCallArgSrc(inst_data.src_node, 1);
1429714324 const ty = try sema.resolveType(block, ty_src, extra.lhs);
14298 const field_name = try sema.resolveConstStringIntern(block, name_src, extra.rhs, .{
14299 .needed_comptime_reason = "field name must be comptime-known",
14300 });
14325 const field_name = try sema.resolveConstStringIntern(block, name_src, extra.rhs, .{ .simple = .field_name });
1430114326 try ty.resolveFields(pt);
1430214327 const ip = &zcu.intern_pool;
1430314328
......@@ -14344,9 +14369,7 @@ fn zirHasDecl(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1434414369 const lhs_src = block.builtinCallArgSrc(inst_data.src_node, 0);
1434514370 const rhs_src = block.builtinCallArgSrc(inst_data.src_node, 1);
1434614371 const container_type = try sema.resolveType(block, lhs_src, extra.lhs);
14347 const decl_name = try sema.resolveConstStringIntern(block, rhs_src, extra.rhs, .{
14348 .needed_comptime_reason = "decl name must be comptime-known",
14349 });
14372 const decl_name = try sema.resolveConstStringIntern(block, rhs_src, extra.rhs, .{ .simple = .decl_name });
1435014373
1435114374 try sema.checkNamespaceType(block, lhs_src, container_type);
1435214375
......@@ -14399,9 +14422,7 @@ fn zirEmbedFile(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1439914422 const pt = sema.pt;
1440014423 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
1440114424 const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0);
14402 const name = try sema.resolveConstString(block, operand_src, inst_data.operand, .{
14403 .needed_comptime_reason = "file path name must be comptime-known",
14404 });
14425 const name = try sema.resolveConstString(block, operand_src, inst_data.operand, .{ .simple = .operand_embedFile });
1440514426
1440614427 if (name.len == 0) {
1440714428 return sema.fail(block, operand_src, "file path name cannot be empty", .{});
......@@ -14985,7 +15006,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1498515006
1498615007 const resolved_elem_ty = t: {
1498715008 var trash_block = block.makeSubBlock();
14988 trash_block.is_comptime = false;
15009 trash_block.comptime_reason = null;
1498915010 defer trash_block.instructions.deinit(sema.gpa);
1499015011
1499115012 const instructions = [_]Air.Inst.Ref{
......@@ -15268,9 +15289,7 @@ fn getArrayCatInfo(sema: *Sema, block: *Block, src: LazySrcLoc, operand: Air.Ins
1526815289 const ptr_info = operand_ty.ptrInfo(zcu);
1526915290 switch (ptr_info.flags.size) {
1527015291 .Slice => {
15271 const val = try sema.resolveConstDefinedValue(block, src, operand, .{
15272 .needed_comptime_reason = "slice value being concatenated must be comptime-known",
15273 });
15292 const val = try sema.resolveConstDefinedValue(block, src, operand, .{ .simple = .slice_cat_operand });
1527415293 return Type.ArrayInfo{
1527515294 .elem_type = Type.fromInterned(ptr_info.child),
1527615295 .sentinel = switch (ptr_info.sentinel) {
......@@ -15431,9 +15450,7 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1543115450
1543215451 if (lhs_ty.isTuple(zcu)) {
1543315452 // In `**` rhs must be comptime-known, but lhs can be runtime-known
15434 const factor = try sema.resolveInt(block, rhs_src, extra.rhs, Type.usize, .{
15435 .needed_comptime_reason = "array multiplication factor must be comptime-known",
15436 });
15453 const factor = try sema.resolveInt(block, rhs_src, extra.rhs, Type.usize, .{ .simple = .array_mul_factor });
1543715454 const factor_casted = try sema.usizeCast(block, rhs_src, factor);
1543815455 return sema.analyzeTupleMul(block, inst_data.src_node, lhs, factor_casted);
1543915456 }
......@@ -15455,9 +15472,7 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1545515472 };
1545615473
1545715474 // In `**` rhs must be comptime-known, but lhs can be runtime-known
15458 const factor = try sema.resolveInt(block, rhs_src, extra.rhs, Type.usize, .{
15459 .needed_comptime_reason = "array multiplication factor must be comptime-known",
15460 });
15475 const factor = try sema.resolveInt(block, rhs_src, extra.rhs, Type.usize, .{ .simple = .array_mul_factor });
1546115476
1546215477 const result_len_u64 = std.math.mul(u64, lhs_info.len, factor) catch
1546315478 return sema.fail(block, rhs_src, "operation results in overflow", .{});
......@@ -17635,12 +17650,9 @@ fn zirAsm(
1763517650 const is_global_assembly = sema.func_index == .none;
1763617651 const zir_tags = sema.code.instructions.items(.tag);
1763717652
17638 const asm_source: []const u8 = if (tmpl_is_expr) blk: {
17653 const asm_source: []const u8 = if (tmpl_is_expr) s: {
1763917654 const tmpl: Zir.Inst.Ref = @enumFromInt(@intFromEnum(extra.data.asm_source));
17640 const s: []const u8 = try sema.resolveConstString(block, src, tmpl, .{
17641 .needed_comptime_reason = "assembly code must be comptime-known",
17642 });
17643 break :blk s;
17655 break :s try sema.resolveConstString(block, src, tmpl, .{ .simple = .inline_assembly_code });
1764417656 } else sema.code.nullTerminatedString(extra.data.asm_source);
1764517657
1764617658 if (is_global_assembly) {
......@@ -18203,7 +18215,7 @@ fn zirClosureGet(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
1820318215 return sema.failWithOwnedErrorMsg(block, msg);
1820418216 }
1820518217
18206 if (!block.is_typeof and !block.is_comptime and sema.func_index != .none) {
18218 if (!block.is_typeof and !block.isComptime() and sema.func_index != .none) {
1820718219 const msg = msg: {
1820818220 const name = name: {
1820918221 const file, const src_base_node = Zcu.LazySrcLoc.resolveBaseNode(block.src_base_inst, zcu).?;
......@@ -18244,7 +18256,7 @@ fn zirRetAddr(
1824418256 extended: Zir.Inst.Extended.InstData,
1824518257) CompileError!Air.Inst.Ref {
1824618258 _ = extended;
18247 if (block.is_comptime) {
18259 if (block.isComptime()) {
1824818260 // TODO: we could give a meaningful lazy value here. #14938
1824918261 return sema.pt.intRef(Type.usize, 0);
1825018262 } else {
......@@ -19342,7 +19354,7 @@ fn zirTypeofBuiltin(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
1934219354 .namespace = block.namespace,
1934319355 .instructions = .{},
1934419356 .inlining = block.inlining,
19345 .is_comptime = false,
19357 .comptime_reason = null,
1934619358 .is_typeof = true,
1934719359 .want_safety = false,
1934819360 .error_return_trace_index = block.error_return_trace_index,
......@@ -19422,7 +19434,7 @@ fn zirTypeofPeer(
1942219434 .namespace = block.namespace,
1942319435 .instructions = .{},
1942419436 .inlining = block.inlining,
19425 .is_comptime = false,
19437 .comptime_reason = null,
1942619438 .is_typeof = true,
1942719439 .runtime_cond = block.runtime_cond,
1942819440 .runtime_loop = block.runtime_loop,
......@@ -19980,7 +19992,7 @@ fn ensurePostHoc(sema: *Sema, block: *Block, dest_block: Zir.Inst.Index) !*Label
1998019992 .instructions = .{},
1998119993 .label = &labeled_block.label,
1998219994 .inlining = block.inlining,
19983 .is_comptime = block.is_comptime,
19995 .comptime_reason = block.comptime_reason,
1998419996 .src_base_inst = block.src_base_inst,
1998519997 .type_name_ctx = block.type_name_ctx,
1998619998 },
......@@ -20013,7 +20025,7 @@ fn zirUnreachable(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
2001320025 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].@"unreachable";
2001420026 const src = block.nodeOffset(inst_data.src_node);
2001520027
20016 if (block.is_comptime) {
20028 if (block.isComptime()) {
2001720029 return sema.fail(block, src, "reached unreachable code", .{});
2001820030 }
2001920031 // TODO Add compile error for @optimizeFor occurring too late in a scope.
......@@ -20066,7 +20078,7 @@ fn zirRetImplicit(
2006620078 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_tok;
2006720079 const r_brace_src = block.tokenOffset(inst_data.src_tok);
2006820080 if (block.inlining == null and sema.func_is_naked) {
20069 assert(!block.is_comptime);
20081 assert(!block.isComptime());
2007020082 if (block.wantSafety()) {
2007120083 // Calling a safety function from a naked function would not be legal.
2007220084 _ = try block.addNoOp(.trap);
......@@ -20123,7 +20135,7 @@ fn zirRetLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi
2012320135 const src = block.nodeOffset(inst_data.src_node);
2012420136 const ret_ptr = try sema.resolveInst(inst_data.operand);
2012520137
20126 if (block.is_comptime or block.inlining != null or sema.func_is_naked) {
20138 if (block.isComptime() or block.inlining != null or sema.func_is_naked) {
2012720139 const operand = try sema.analyzeLoad(block, src, ret_ptr, src);
2012820140 return sema.analyzeRet(block, operand, src, block.src(.{ .node_offset_return_operand = inst_data.src_node }));
2012920141 }
......@@ -20215,7 +20227,7 @@ fn zirSaveErrRetIndex(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
2021520227 if (!block.ownerModule().error_tracing) return;
2021620228
2021720229 // This is only relevant at runtime.
20218 if (block.is_comptime or block.is_typeof) return;
20230 if (block.isComptime() or block.is_typeof) return;
2021920231
2022020232 const save_index = inst_data.operand == .none or b: {
2022120233 const operand = try sema.resolveInst(inst_data.operand);
......@@ -20268,7 +20280,7 @@ fn restoreErrRetIndex(sema: *Sema, start_block: *Block, src: LazySrcLoc, target_
2026820280
2026920281 const operand = try sema.resolveInstAllowNone(operand_zir);
2027020282
20271 if (start_block.is_comptime or start_block.is_typeof) {
20283 if (start_block.isComptime() or start_block.is_typeof) {
2027220284 const is_non_error = if (operand != .none) blk: {
2027320285 const is_non_error_inst = try sema.analyzeIsNonErr(start_block, src, operand);
2027420286 const cond_val = try sema.resolveDefinedValue(start_block, src, is_non_error_inst);
......@@ -20345,10 +20357,8 @@ fn analyzeRet(
2034520357 };
2034620358
2034720359 if (block.inlining) |inlining| {
20348 if (block.is_comptime) {
20349 const ret_val = try sema.resolveConstValue(block, operand_src, operand, .{
20350 .needed_comptime_reason = "value being returned at comptime must be comptime-known",
20351 });
20360 if (block.isComptime()) {
20361 const ret_val = try sema.resolveConstValue(block, operand_src, operand, null);
2035220362 inlining.comptime_result = operand;
2035320363
2035420364 if (sema.fn_ret_ty.isError(zcu) and ret_val.getErrorName(zcu) != .none) {
......@@ -20362,7 +20372,7 @@ fn analyzeRet(
2036220372 try inlining.merges.br_list.append(sema.gpa, br_inst.toIndex().?);
2036320373 try inlining.merges.src_locs.append(sema.gpa, operand_src);
2036420374 return;
20365 } else if (block.is_comptime) {
20375 } else if (block.isComptime()) {
2036620376 return sema.fail(block, src, "function called at runtime cannot return value at comptime", .{});
2036720377 } else if (sema.func_is_naked) {
2036820378 const msg = msg: {
......@@ -20436,9 +20446,7 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
2043620446 const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]);
2043720447 extra_i += 1;
2043820448 const coerced = try sema.coerce(block, elem_ty, try sema.resolveInst(ref), sentinel_src);
20439 const val = try sema.resolveConstDefinedValue(block, sentinel_src, coerced, .{
20440 .needed_comptime_reason = "pointer sentinel value must be comptime-known",
20441 });
20449 const val = try sema.resolveConstDefinedValue(block, sentinel_src, coerced, .{ .simple = .pointer_sentinel });
2044220450 try checkSentinelType(sema, block, sentinel_src, elem_ty);
2044320451 break :blk val.toIntern();
2044420452 } else .none;
......@@ -20447,9 +20455,7 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
2044720455 const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]);
2044820456 extra_i += 1;
2044920457 const coerced = try sema.coerce(block, Type.u32, try sema.resolveInst(ref), align_src);
20450 const val = try sema.resolveConstDefinedValue(block, align_src, coerced, .{
20451 .needed_comptime_reason = "pointer alignment must be comptime-known",
20452 });
20458 const val = try sema.resolveConstDefinedValue(block, align_src, coerced, .{ .simple = .@"align" });
2045320459 // Check if this happens to be the lazy alignment of our element type, in
2045420460 // which case we can make this 0 without resolving it.
2045520461 switch (zcu.intern_pool.indexToKey(val.toIntern())) {
......@@ -20472,18 +20478,14 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
2047220478 const bit_offset: u16 = if (inst_data.flags.has_bit_range) blk: {
2047320479 const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]);
2047420480 extra_i += 1;
20475 const bit_offset = try sema.resolveInt(block, bitoffset_src, ref, Type.u16, .{
20476 .needed_comptime_reason = "pointer bit-offset must be comptime-known",
20477 });
20481 const bit_offset = try sema.resolveInt(block, bitoffset_src, ref, Type.u16, .{ .simple = .type });
2047820482 break :blk @intCast(bit_offset);
2047920483 } else 0;
2048020484
2048120485 const host_size: u16 = if (inst_data.flags.has_bit_range) blk: {
2048220486 const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]);
2048320487 extra_i += 1;
20484 const host_size = try sema.resolveInt(block, hostsize_src, ref, Type.u16, .{
20485 .needed_comptime_reason = "pointer host size must be comptime-known",
20486 });
20488 const host_size = try sema.resolveInt(block, hostsize_src, ref, Type.u16, .{ .simple = .type });
2048720489 break :blk @intCast(host_size);
2048820490 } else 0;
2048920491
......@@ -20671,9 +20673,7 @@ fn zirUnionInit(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2067120673 if (union_ty.zigTypeTag(pt.zcu) != .@"union") {
2067220674 return sema.fail(block, ty_src, "expected union type, found '{}'", .{union_ty.fmt(pt)});
2067320675 }
20674 const field_name = try sema.resolveConstStringIntern(block, field_src, extra.field_name, .{
20675 .needed_comptime_reason = "name of field being initialized must be comptime-known",
20676 });
20676 const field_name = try sema.resolveConstStringIntern(block, field_src, extra.field_name, .{ .simple = .union_field_name });
2067720677 const init = try sema.resolveInst(extra.init);
2067820678 return sema.unionInit(block, init, init_src, union_ty, ty_src, field_name, field_src);
2067920679}
......@@ -20800,9 +20800,7 @@ fn zirStructInit(
2080020800 try resolved_ty.resolveStructFieldInits(pt);
2080120801 if (try resolved_ty.structFieldValueComptime(pt, field_index)) |default_value| {
2080220802 const init_val = (try sema.resolveValue(field_inits[field_index])) orelse {
20803 return sema.failWithNeededComptime(block, field_src, .{
20804 .needed_comptime_reason = "value stored in comptime field must be comptime-known",
20805 });
20803 return sema.failWithNeededComptime(block, field_src, .{ .simple = .stored_to_comptime_field });
2080620804 };
2080720805
2080820806 if (!init_val.eql(default_value, resolved_ty.fieldType(field_index, zcu), zcu)) {
......@@ -20862,9 +20860,10 @@ fn zirStructInit(
2086220860 }
2086320861
2086420862 if (try resolved_ty.comptimeOnlySema(pt)) {
20865 return sema.failWithNeededComptime(block, field_src, .{
20866 .needed_comptime_reason = "initializer of comptime only union must be comptime-known",
20867 });
20863 return sema.failWithNeededComptime(block, field_src, .{ .comptime_only = .{
20864 .ty = resolved_ty,
20865 .msg = .union_init,
20866 } });
2086820867 }
2086920868
2087020869 try sema.validateRuntimeValue(block, field_src, init_inst);
......@@ -21003,9 +21002,10 @@ fn finishStructInit(
2100321002 return sema.failWithNeededComptime(block, block.src(.{ .init_elem = .{
2100421003 .init_node_offset = init_src.offset.node_offset.x,
2100521004 .elem_index = @intCast(runtime_index),
21006 } }), .{
21007 .needed_comptime_reason = "initializer of comptime only struct must be comptime-known",
21008 });
21005 } }), .{ .comptime_only = .{
21006 .ty = struct_ty,
21007 .msg = .struct_init,
21008 } });
2100921009 }
2101021010
2101121011 for (field_inits) |field_init| {
......@@ -21315,11 +21315,7 @@ fn zirArrayInit(
2131521315 if (array_ty.structFieldIsComptime(i, zcu))
2131621316 try array_ty.resolveStructFieldInits(pt);
2131721317 if (try array_ty.structFieldValueComptime(pt, i)) |field_val| {
21318 const init_val = try sema.resolveValue(dest.*) orelse {
21319 return sema.failWithNeededComptime(block, elem_src, .{
21320 .needed_comptime_reason = "value stored in comptime field must be comptime-known",
21321 });
21322 };
21318 const init_val = try sema.resolveConstValue(block, elem_src, dest.*, .{ .simple = .stored_to_comptime_field });
2132321319 if (!field_val.eql(init_val, elem_ty, zcu)) {
2132421320 return sema.failWithInvalidComptimeFieldStore(block, elem_src, array_ty, i);
2132521321 }
......@@ -21508,9 +21504,7 @@ fn zirFieldTypeRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
2150821504 const ty_src = block.builtinCallArgSrc(inst_data.src_node, 0);
2150921505 const field_src = block.builtinCallArgSrc(inst_data.src_node, 1);
2151021506 const aggregate_ty = try sema.resolveType(block, ty_src, extra.container_type);
21511 const field_name = try sema.resolveConstStringIntern(block, field_src, extra.field_name, .{
21512 .needed_comptime_reason = "field name must be comptime-known",
21513 });
21507 const field_name = try sema.resolveConstStringIntern(block, field_src, extra.field_name, .{ .simple = .field_name });
2151421508 return sema.fieldType(block, aggregate_ty, field_name, field_src, ty_src);
2151521509}
2151621510
......@@ -21890,9 +21884,7 @@ fn zirReify(
2189021884 const type_info_ty = try sema.getBuiltinType("Type");
2189121885 const uncasted_operand = try sema.resolveInst(extra.operand);
2189221886 const type_info = try sema.coerce(block, type_info_ty, uncasted_operand, operand_src);
21893 const val = try sema.resolveConstDefinedValue(block, operand_src, type_info, .{
21894 .needed_comptime_reason = "operand to @Type must be comptime-known",
21895 });
21887 const val = try sema.resolveConstDefinedValue(block, operand_src, type_info, .{ .simple = .operand_Type });
2189621888 const union_val = ip.indexToKey(val.toIntern()).un;
2189721889 if (try sema.anyUndef(block, operand_src, Value.fromInterned(union_val.val))) {
2189821890 return sema.failWithUseOfUndef(block, operand_src);
......@@ -22136,9 +22128,7 @@ fn zirReify(
2213622128 const payload_val = Value.fromInterned(union_val.val).optionalValue(zcu) orelse
2213722129 return Air.internedToRef(Type.anyerror.toIntern());
2213822130
22139 const names_val = try sema.derefSliceAsArray(block, src, payload_val, .{
22140 .needed_comptime_reason = "error set contents must be comptime-known",
22141 });
22131 const names_val = try sema.derefSliceAsArray(block, src, payload_val, .{ .simple = .error_set_contents });
2214222132
2214322133 const len = try sema.usizeCast(block, src, names_val.typeOf(zcu).arrayLen(zcu));
2214422134 var names: InferredErrorSet.NameMap = .{};
......@@ -22151,9 +22141,7 @@ fn zirReify(
2215122141 try ip.getOrPutString(gpa, pt.tid, "name", .no_embedded_nulls),
2215222142 ).?);
2215322143
22154 const name = try sema.sliceToIpString(block, src, name_val, .{
22155 .needed_comptime_reason = "error set contents must be comptime-known",
22156 });
22144 const name = try sema.sliceToIpString(block, src, name_val, .{ .simple = .error_set_contents });
2215722145 _ = try pt.getErrorValue(name);
2215822146 const gop = names.getOrPutAssumeCapacity(name);
2215922147 if (gop.found_existing) {
......@@ -22200,9 +22188,7 @@ fn zirReify(
2220022188 return sema.fail(block, src, "non-packed struct does not support backing integer type", .{});
2220122189 }
2220222190
22203 const fields_arr = try sema.derefSliceAsArray(block, operand_src, fields_val, .{
22204 .needed_comptime_reason = "struct fields must be comptime-known",
22205 });
22191 const fields_arr = try sema.derefSliceAsArray(block, operand_src, fields_val, .{ .simple = .struct_fields });
2220622192
2220722193 if (is_tuple_val.toBool()) {
2220822194 switch (layout) {
......@@ -22238,9 +22224,7 @@ fn zirReify(
2223822224 return sema.fail(block, src, "reified enums must have no decls", .{});
2223922225 }
2224022226
22241 const fields_arr = try sema.derefSliceAsArray(block, operand_src, fields_val, .{
22242 .needed_comptime_reason = "enum fields must be comptime-known",
22243 });
22227 const fields_arr = try sema.derefSliceAsArray(block, operand_src, fields_val, .{ .simple = .enum_fields });
2224422228
2224522229 return sema.reifyEnum(block, inst, src, tag_type_val.toType(), is_exhaustive_val.toBool(), fields_arr, name_strategy);
2224622230 },
......@@ -22311,9 +22295,7 @@ fn zirReify(
2231122295 }
2231222296 const layout = zcu.toEnum(std.builtin.Type.ContainerLayout, layout_val);
2231322297
22314 const fields_arr = try sema.derefSliceAsArray(block, operand_src, fields_val, .{
22315 .needed_comptime_reason = "union fields must be comptime-known",
22316 });
22298 const fields_arr = try sema.derefSliceAsArray(block, operand_src, fields_val, .{ .simple = .union_fields });
2231722299
2231822300 return sema.reifyUnion(block, inst, src, layout, tag_type_val, fields_arr, name_strategy);
2231922301 },
......@@ -22354,9 +22336,7 @@ fn zirReify(
2235422336 const return_type = return_type_val.optionalValue(zcu) orelse
2235522337 return sema.fail(block, src, "Type.Fn.return_type must be non-null for @Type", .{});
2235622338
22357 const params_val = try sema.derefSliceAsArray(block, operand_src, params_slice_val, .{
22358 .needed_comptime_reason = "function parameters must be comptime-known",
22359 });
22339 const params_val = try sema.derefSliceAsArray(block, operand_src, params_slice_val, .{ .simple = .function_parameters });
2236022340
2236122341 const args_len = try sema.usizeCast(block, src, params_val.typeOf(zcu).arrayLen(zcu));
2236222342 const param_types = try sema.arena.alloc(InternPool.Index, args_len);
......@@ -22444,9 +22424,7 @@ fn reifyEnum(
2244422424 const field_name_val = try field_info.fieldValue(pt, 0);
2244522425 const field_value_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 1));
2244622426
22447 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{
22448 .needed_comptime_reason = "enum field name must be comptime-known",
22449 });
22427 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{ .simple = .enum_field_name });
2245022428
2245122429 std.hash.autoHash(&hasher, .{
2245222430 field_name,
......@@ -22591,9 +22569,7 @@ fn reifyUnion(
2259122569 const field_type_val = try field_info.fieldValue(pt, 1);
2259222570 const field_align_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 2));
2259322571
22594 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{
22595 .needed_comptime_reason = "union field name must be comptime-known",
22596 });
22572 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{ .simple = .union_field_name });
2259722573
2259822574 std.hash.autoHash(&hasher, .{
2259922575 field_name,
......@@ -22835,9 +22811,7 @@ fn reifyTuple(
2283522811 const field_is_comptime_val = try field_info.fieldValue(pt, 3);
2283622812 const field_alignment_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 4));
2283722813
22838 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{
22839 .needed_comptime_reason = "tuple field name must be comptime-known",
22840 });
22814 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{ .simple = .tuple_field_name });
2284122815 const field_type = field_type_val.toType();
2284222816 const field_default_value: InternPool.Index = if (field_default_value_val.optionalValue(zcu)) |ptr_val| d: {
2284322817 const ptr_ty = try pt.singleConstPtrType(field_type_val.toType());
......@@ -22845,7 +22819,7 @@ fn reifyTuple(
2284522819 const val = try sema.pointerDeref(block, src, ptr_val, ptr_ty) orelse return sema.failWithNeededComptime(
2284622820 block,
2284722821 src,
22848 .{ .needed_comptime_reason = "tuple field default value must be comptime-known" },
22822 .{ .simple = .tuple_field_default_value },
2284922823 );
2285022824 // Resolve the value so that lazy values do not create distinct types.
2285122825 break :d (try sema.resolveLazyValue(val)).toIntern();
......@@ -22951,9 +22925,7 @@ fn reifyStruct(
2295122925 const field_is_comptime_val = try field_info.fieldValue(pt, 3);
2295222926 const field_alignment_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 4));
2295322927
22954 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{
22955 .needed_comptime_reason = "struct field name must be comptime-known",
22956 });
22928 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{ .simple = .struct_field_name });
2295722929 const field_is_comptime = field_is_comptime_val.toBool();
2295822930 const field_default_value: InternPool.Index = if (field_default_value_val.optionalValue(zcu)) |ptr_val| d: {
2295922931 const ptr_ty = try pt.singleConstPtrType(field_type_val.toType());
......@@ -22961,7 +22933,7 @@ fn reifyStruct(
2296122933 const val = try sema.pointerDeref(block, src, ptr_val, ptr_ty) orelse return sema.failWithNeededComptime(
2296222934 block,
2296322935 src,
22964 .{ .needed_comptime_reason = "struct field default value must be comptime-known" },
22936 .{ .simple = .struct_field_default_value },
2296522937 );
2296622938 // Resolve the value so that lazy values do not create distinct types.
2296722939 break :d (try sema.resolveLazyValue(val)).toIntern();
......@@ -23285,9 +23257,7 @@ fn zirIntFromFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
2328523257 const result_val = try sema.intFromFloat(block, operand_src, operand_val, operand_ty, dest_ty, .truncate);
2328623258 return Air.internedToRef(result_val.toIntern());
2328723259 } else if (dest_scalar_ty.zigTypeTag(zcu) == .comptime_int) {
23288 return sema.failWithNeededComptime(block, operand_src, .{
23289 .needed_comptime_reason = "value being casted to 'comptime_int' must be comptime-known",
23290 });
23260 return sema.failWithNeededComptime(block, operand_src, .{ .simple = .casted_to_comptime_int });
2329123261 }
2329223262
2329323263 try sema.requireRuntimeBlock(block, src, operand_src);
......@@ -23368,9 +23338,7 @@ fn zirFloatFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
2336823338 const result_val = try operand_val.floatFromIntAdvanced(sema.arena, operand_ty, dest_ty, pt, .sema);
2336923339 return Air.internedToRef(result_val.toIntern());
2337023340 } else if (dest_scalar_ty.zigTypeTag(zcu) == .comptime_float) {
23371 return sema.failWithNeededComptime(block, operand_src, .{
23372 .needed_comptime_reason = "value being casted to 'comptime_float' must be comptime-known",
23373 });
23341 return sema.failWithNeededComptime(block, operand_src, .{ .simple = .casted_to_comptime_float });
2337423342 }
2337523343
2337623344 try sema.requireRuntimeBlock(block, src, operand_src);
......@@ -24394,9 +24362,7 @@ fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u6
2439424362 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
2439524363
2439624364 const ty = try sema.resolveType(block, lhs_src, extra.lhs);
24397 const field_name = try sema.resolveConstStringIntern(block, rhs_src, extra.rhs, .{
24398 .needed_comptime_reason = "name of field must be comptime-known",
24399 });
24365 const field_name = try sema.resolveConstStringIntern(block, rhs_src, extra.rhs, .{ .simple = .field_name });
2440024366
2440124367 const pt = sema.pt;
2440224368 const zcu = pt.zcu;
......@@ -24850,31 +24816,21 @@ fn resolveExportOptions(
2485024816 const visibility_src = block.src(.{ .init_field_visibility = src.offset.node_offset_builtin_call_arg.builtin_call_node });
2485124817
2485224818 const name_operand = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "name", .no_embedded_nulls), name_src);
24853 const name = try sema.toConstString(block, name_src, name_operand, .{
24854 .needed_comptime_reason = "name of exported value must be comptime-known",
24855 });
24819 const name = try sema.toConstString(block, name_src, name_operand, .{ .simple = .export_options });
2485624820
2485724821 const linkage_operand = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "linkage", .no_embedded_nulls), linkage_src);
24858 const linkage_val = try sema.resolveConstDefinedValue(block, linkage_src, linkage_operand, .{
24859 .needed_comptime_reason = "linkage of exported value must be comptime-known",
24860 });
24822 const linkage_val = try sema.resolveConstDefinedValue(block, linkage_src, linkage_operand, .{ .simple = .export_options });
2486124823 const linkage = zcu.toEnum(std.builtin.GlobalLinkage, linkage_val);
2486224824
2486324825 const section_operand = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "section", .no_embedded_nulls), section_src);
24864 const section_opt_val = try sema.resolveConstDefinedValue(block, section_src, section_operand, .{
24865 .needed_comptime_reason = "linksection of exported value must be comptime-known",
24866 });
24826 const section_opt_val = try sema.resolveConstDefinedValue(block, section_src, section_operand, .{ .simple = .export_options });
2486724827 const section = if (section_opt_val.optionalValue(zcu)) |section_val|
24868 try sema.toConstString(block, section_src, Air.internedToRef(section_val.toIntern()), .{
24869 .needed_comptime_reason = "linksection of exported value must be comptime-known",
24870 })
24828 try sema.toConstString(block, section_src, Air.internedToRef(section_val.toIntern()), .{ .simple = .export_options })
2487124829 else
2487224830 null;
2487324831
2487424832 const visibility_operand = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "visibility", .no_embedded_nulls), visibility_src);
24875 const visibility_val = try sema.resolveConstDefinedValue(block, visibility_src, visibility_operand, .{
24876 .needed_comptime_reason = "visibility of exported value must be comptime-known",
24877 });
24833 const visibility_val = try sema.resolveConstDefinedValue(block, visibility_src, visibility_operand, .{ .simple = .export_options });
2487824834 const visibility = zcu.toEnum(std.builtin.SymbolVisibility, visibility_val);
2487924835
2488024836 if (name.len < 1) {
......@@ -24901,7 +24857,7 @@ fn resolveBuiltinEnum(
2490124857 src: LazySrcLoc,
2490224858 zir_ref: Zir.Inst.Ref,
2490324859 comptime name: []const u8,
24904 reason: NeededComptimeReason,
24860 reason: ComptimeReason,
2490524861) CompileError!@field(std.builtin, name) {
2490624862 const pt = sema.pt;
2490724863 const ty = try sema.getBuiltinType(name);
......@@ -24916,7 +24872,7 @@ fn resolveAtomicOrder(
2491624872 block: *Block,
2491724873 src: LazySrcLoc,
2491824874 zir_ref: Zir.Inst.Ref,
24919 reason: NeededComptimeReason,
24875 reason: ComptimeReason,
2492024876) CompileError!std.builtin.AtomicOrder {
2492124877 return sema.resolveBuiltinEnum(block, src, zir_ref, "AtomicOrder", reason);
2492224878}
......@@ -24927,9 +24883,7 @@ fn resolveAtomicRmwOp(
2492724883 src: LazySrcLoc,
2492824884 zir_ref: Zir.Inst.Ref,
2492924885) CompileError!std.builtin.AtomicRmwOp {
24930 return sema.resolveBuiltinEnum(block, src, zir_ref, "AtomicRmwOp", .{
24931 .needed_comptime_reason = "@atomicRmW operation must be comptime-known",
24932 });
24886 return sema.resolveBuiltinEnum(block, src, zir_ref, "AtomicRmwOp", .{ .simple = .operand_atomicRmw_operation });
2493324887}
2493424888
2493524889fn zirCmpxchg(
......@@ -24967,12 +24921,8 @@ fn zirCmpxchg(
2496724921 const uncasted_ptr = try sema.resolveInst(extra.ptr);
2496824922 const ptr = try sema.checkAtomicPtrOperand(block, elem_ty, elem_ty_src, uncasted_ptr, ptr_src, false);
2496924923 const new_value = try sema.coerce(block, elem_ty, try sema.resolveInst(extra.new_value), new_value_src);
24970 const success_order = try sema.resolveAtomicOrder(block, success_order_src, extra.success_order, .{
24971 .needed_comptime_reason = "atomic order of cmpxchg success must be comptime-known",
24972 });
24973 const failure_order = try sema.resolveAtomicOrder(block, failure_order_src, extra.failure_order, .{
24974 .needed_comptime_reason = "atomic order of cmpxchg failure must be comptime-known",
24975 });
24924 const success_order = try sema.resolveAtomicOrder(block, success_order_src, extra.success_order, .{ .simple = .atomic_order });
24925 const failure_order = try sema.resolveAtomicOrder(block, failure_order_src, extra.failure_order, .{ .simple = .atomic_order });
2497624926
2497724927 if (@intFromEnum(success_order) < @intFromEnum(std.builtin.AtomicOrder.monotonic)) {
2497824928 return sema.fail(block, success_order_src, "success atomic ordering must be monotonic or stricter", .{});
......@@ -25113,9 +25063,7 @@ fn zirReduce(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
2511325063 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
2511425064 const op_src = block.builtinCallArgSrc(inst_data.src_node, 0);
2511525065 const operand_src = block.builtinCallArgSrc(inst_data.src_node, 1);
25116 const operation = try sema.resolveBuiltinEnum(block, op_src, extra.lhs, "ReduceOp", .{
25117 .needed_comptime_reason = "@reduce operation must be comptime-known",
25118 });
25066 const operation = try sema.resolveBuiltinEnum(block, op_src, extra.lhs, "ReduceOp", .{ .simple = .operand_reduce_operation });
2511925067 const operand = try sema.resolveInst(extra.rhs);
2512025068 const operand_ty = sema.typeOf(operand);
2512125069 const pt = sema.pt;
......@@ -25204,9 +25152,7 @@ fn zirShuffle(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
2520425152 .child = .i32_type,
2520525153 });
2520625154 mask = try sema.coerce(block, mask_ty, mask, mask_src);
25207 const mask_val = try sema.resolveConstValue(block, mask_src, mask, .{
25208 .needed_comptime_reason = "shuffle mask must be comptime-known",
25209 });
25155 const mask_val = try sema.resolveConstValue(block, mask_src, mask, .{ .simple = .operand_shuffle_mask });
2521025156 return sema.analyzeShuffle(block, inst_data.src_node, elem_ty, a, b, mask_val, @intCast(mask_len));
2521125157}
2521225158
......@@ -25474,9 +25420,7 @@ fn zirAtomicLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
2547425420 const elem_ty = try sema.resolveType(block, elem_ty_src, extra.elem_type);
2547525421 const uncasted_ptr = try sema.resolveInst(extra.ptr);
2547625422 const ptr = try sema.checkAtomicPtrOperand(block, elem_ty, elem_ty_src, uncasted_ptr, ptr_src, true);
25477 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, .{
25478 .needed_comptime_reason = "atomic order of @atomicLoad must be comptime-known",
25479 });
25423 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, .{ .simple = .atomic_order });
2548025424
2548125425 switch (order) {
2548225426 .release, .acq_rel => {
......@@ -25542,9 +25486,7 @@ fn zirAtomicRmw(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2554225486 },
2554325487 else => {},
2554425488 }
25545 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, .{
25546 .needed_comptime_reason = "atomic order of @atomicRmW must be comptime-known",
25547 });
25489 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, .{ .simple = .atomic_order });
2554825490
2554925491 if (order == .unordered) {
2555025492 return sema.fail(block, order_src, "@atomicRmw atomic ordering must not be unordered", .{});
......@@ -25611,9 +25553,7 @@ fn zirAtomicStore(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
2561125553 const elem_ty = sema.typeOf(operand);
2561225554 const uncasted_ptr = try sema.resolveInst(extra.ptr);
2561325555 const ptr = try sema.checkAtomicPtrOperand(block, elem_ty, elem_ty_src, uncasted_ptr, ptr_src, false);
25614 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, .{
25615 .needed_comptime_reason = "atomic order of @atomicStore must be comptime-known",
25616 });
25556 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, .{ .simple = .atomic_order });
2561725557
2561825558 const air_tag: Air.Inst.Tag = switch (order) {
2561925559 .acquire, .acq_rel => {
......@@ -25716,14 +25656,12 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
2571625656 const modifier_ty = try sema.getBuiltinType("CallModifier");
2571725657 const air_ref = try sema.resolveInst(extra.modifier);
2571825658 const modifier_ref = try sema.coerce(block, modifier_ty, air_ref, modifier_src);
25719 const modifier_val = try sema.resolveConstDefinedValue(block, modifier_src, modifier_ref, .{
25720 .needed_comptime_reason = "call modifier must be comptime-known",
25721 });
25659 const modifier_val = try sema.resolveConstDefinedValue(block, modifier_src, modifier_ref, .{ .simple = .call_modifier });
2572225660 var modifier = zcu.toEnum(std.builtin.CallModifier, modifier_val);
2572325661 switch (modifier) {
2572425662 // These can be upgraded to comptime or nosuspend calls.
2572525663 .auto, .never_tail, .no_async => {
25726 if (block.is_comptime) {
25664 if (block.isComptime()) {
2572725665 if (modifier == .never_tail) {
2572825666 return sema.fail(block, modifier_src, "unable to perform 'never_tail' call at compile-time", .{});
2572925667 }
......@@ -25738,12 +25676,12 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
2573825676 return sema.fail(block, func_src, "modifier '{s}' requires a comptime-known function", .{@tagName(modifier)});
2573925677 };
2574025678
25741 if (block.is_comptime) {
25679 if (block.isComptime()) {
2574225680 modifier = .compile_time;
2574325681 }
2574425682 },
2574525683 .always_tail => {
25746 if (block.is_comptime) {
25684 if (block.isComptime()) {
2574725685 modifier = .compile_time;
2574825686 }
2574925687 },
......@@ -25751,12 +25689,12 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
2575125689 if (extra.flags.is_nosuspend) {
2575225690 return sema.fail(block, modifier_src, "modifier 'async_kw' cannot be used inside nosuspend block", .{});
2575325691 }
25754 if (block.is_comptime) {
25692 if (block.isComptime()) {
2575525693 return sema.fail(block, modifier_src, "modifier 'async_kw' cannot be used in combination with comptime function call", .{});
2575625694 }
2575725695 },
2575825696 .never_inline => {
25759 if (block.is_comptime) {
25697 if (block.isComptime()) {
2576025698 return sema.fail(block, modifier_src, "unable to perform 'never_inline' call at compile-time", .{});
2576125699 }
2576225700 },
......@@ -25820,9 +25758,7 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Ins
2582025758 }
2582125759 try parent_ty.resolveLayout(pt);
2582225760
25823 const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, .{
25824 .needed_comptime_reason = "field name must be comptime-known",
25825 });
25761 const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, .{ .simple = .field_name });
2582625762 const field_index = switch (parent_ty.zigTypeTag(zcu)) {
2582725763 .@"struct" => blk: {
2582825764 if (parent_ty.isTuple(zcu)) {
......@@ -26680,9 +26616,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2668026616 extra_index += body.len;
2668126617
2668226618 const cc_ty = try sema.getBuiltinType("CallingConvention");
26683 const val = try sema.resolveGenericBody(block, cc_src, body, inst, cc_ty, .{
26684 .needed_comptime_reason = "calling convention must be comptime-known",
26685 });
26619 const val = try sema.resolveGenericBody(block, cc_src, body, inst, cc_ty, .{ .simple = .@"callconv" });
2668626620 break :blk try sema.analyzeValueAsCallconv(block, cc_src, val);
2668726621 } else if (extra.data.bits.has_cc_ref) blk: {
2668826622 const cc_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);
......@@ -26690,9 +26624,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2669026624 const cc_ty = try sema.getBuiltinType("CallingConvention");
2669126625 const uncoerced_cc = try sema.resolveInst(cc_ref);
2669226626 const coerced_cc = try sema.coerce(block, cc_ty, uncoerced_cc, cc_src);
26693 const cc_val = try sema.resolveConstDefinedValue(block, cc_src, coerced_cc, .{
26694 .needed_comptime_reason = "calling convention must be comptime-known",
26695 });
26627 const cc_val = try sema.resolveConstDefinedValue(block, cc_src, coerced_cc, .{ .simple = .@"callconv" });
2669626628 break :blk try sema.analyzeValueAsCallconv(block, cc_src, cc_val);
2669726629 } else cc: {
2669826630 if (has_body) {
......@@ -26730,9 +26662,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2673026662 const body = sema.code.bodySlice(extra_index, body_len);
2673126663 extra_index += body.len;
2673226664
26733 const val = try sema.resolveGenericBody(block, ret_src, body, inst, Type.type, .{
26734 .needed_comptime_reason = "return type must be comptime-known",
26735 });
26665 const val = try sema.resolveGenericBody(block, ret_src, body, inst, Type.type, .{ .simple = .function_ret_ty });
2673626666 const ty = val.toType();
2673726667 break :blk ty;
2673826668 } else if (extra.data.bits.has_ret_ty_ref) blk: {
......@@ -26742,9 +26672,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2674226672 error.GenericPoison => break :blk Type.generic_poison,
2674326673 else => |e| return e,
2674426674 };
26745 const ret_ty_val = sema.resolveConstDefinedValue(block, ret_src, ret_ty_air_ref, .{
26746 .needed_comptime_reason = "return type must be comptime-known",
26747 }) catch |err| switch (err) {
26675 const ret_ty_val = sema.resolveConstDefinedValue(block, ret_src, ret_ty_air_ref, .{ .simple = .function_ret_ty }) catch |err| switch (err) {
2674826676 error.GenericPoison => break :blk Type.generic_poison,
2674926677 else => |e| return e,
2675026678 };
......@@ -26790,9 +26718,7 @@ fn zirCUndef(
2679026718 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
2679126719 const src = block.builtinCallArgSrc(extra.node, 0);
2679226720
26793 const name = try sema.resolveConstString(block, src, extra.operand, .{
26794 .needed_comptime_reason = "name of macro being undefined must be comptime-known",
26795 });
26721 const name = try sema.resolveConstString(block, src, extra.operand, .{ .simple = .operand_cUndef_macro_name });
2679626722 try block.c_import_buf.?.writer().print("#undef {s}\n", .{name});
2679726723 return .void_value;
2679826724}
......@@ -26805,9 +26731,7 @@ fn zirCInclude(
2680526731 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
2680626732 const src = block.builtinCallArgSrc(extra.node, 0);
2680726733
26808 const name = try sema.resolveConstString(block, src, extra.operand, .{
26809 .needed_comptime_reason = "path being included must be comptime-known",
26810 });
26734 const name = try sema.resolveConstString(block, src, extra.operand, .{ .simple = .operand_cInclude_file_name });
2681126735 try block.c_import_buf.?.writer().print("#include <{s}>\n", .{name});
2681226736 return .void_value;
2681326737}
......@@ -26823,14 +26747,10 @@ fn zirCDefine(
2682326747 const name_src = block.builtinCallArgSrc(extra.node, 0);
2682426748 const val_src = block.builtinCallArgSrc(extra.node, 1);
2682526749
26826 const name = try sema.resolveConstString(block, name_src, extra.lhs, .{
26827 .needed_comptime_reason = "name of macro being undefined must be comptime-known",
26828 });
26750 const name = try sema.resolveConstString(block, name_src, extra.lhs, .{ .simple = .operand_cDefine_macro_name });
2682926751 const rhs = try sema.resolveInst(extra.rhs);
2683026752 if (sema.typeOf(rhs).zigTypeTag(zcu) != .void) {
26831 const value = try sema.resolveConstString(block, val_src, extra.rhs, .{
26832 .needed_comptime_reason = "value of macro being undefined must be comptime-known",
26833 });
26753 const value = try sema.resolveConstString(block, val_src, extra.rhs, .{ .simple = .operand_cDefine_macro_value });
2683426754 try block.c_import_buf.?.writer().print("#define {s} {s}\n", .{ name, value });
2683526755 } else {
2683626756 try block.c_import_buf.?.writer().print("#define {s}\n", .{name});
......@@ -26851,9 +26771,7 @@ fn zirWasmMemorySize(
2685126771 return sema.fail(block, builtin_src, "builtin @wasmMemorySize is available when targeting WebAssembly; targeted CPU architecture is {s}", .{@tagName(target.cpu.arch)});
2685226772 }
2685326773
26854 const index: u32 = @intCast(try sema.resolveInt(block, index_src, extra.operand, Type.u32, .{
26855 .needed_comptime_reason = "wasm memory size index must be comptime-known",
26856 }));
26774 const index: u32 = @intCast(try sema.resolveInt(block, index_src, extra.operand, Type.u32, .{ .simple = .wasm_memory_index }));
2685726775 try sema.requireRuntimeBlock(block, builtin_src, null);
2685826776 return block.addInst(.{
2685926777 .tag = .wasm_memory_size,
......@@ -26878,9 +26796,7 @@ fn zirWasmMemoryGrow(
2687826796 return sema.fail(block, builtin_src, "builtin @wasmMemoryGrow is available when targeting WebAssembly; targeted CPU architecture is {s}", .{@tagName(target.cpu.arch)});
2687926797 }
2688026798
26881 const index: u32 = @intCast(try sema.resolveInt(block, index_src, extra.lhs, Type.u32, .{
26882 .needed_comptime_reason = "wasm memory size index must be comptime-known",
26883 }));
26799 const index: u32 = @intCast(try sema.resolveInt(block, index_src, extra.lhs, Type.u32, .{ .simple = .wasm_memory_index }));
2688426800 const delta = try sema.coerce(block, Type.usize, try sema.resolveInst(extra.rhs), delta_src);
2688526801
2688626802 try sema.requireRuntimeBlock(block, builtin_src, null);
......@@ -26911,19 +26827,13 @@ fn resolvePrefetchOptions(
2691126827 const cache_src = block.src(.{ .init_field_cache = src.offset.node_offset_builtin_call_arg.builtin_call_node });
2691226828
2691326829 const rw = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "rw", .no_embedded_nulls), rw_src);
26914 const rw_val = try sema.resolveConstDefinedValue(block, rw_src, rw, .{
26915 .needed_comptime_reason = "prefetch read/write must be comptime-known",
26916 });
26830 const rw_val = try sema.resolveConstDefinedValue(block, rw_src, rw, .{ .simple = .prefetch_options });
2691726831
2691826832 const locality = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "locality", .no_embedded_nulls), locality_src);
26919 const locality_val = try sema.resolveConstDefinedValue(block, locality_src, locality, .{
26920 .needed_comptime_reason = "prefetch locality must be comptime-known",
26921 });
26833 const locality_val = try sema.resolveConstDefinedValue(block, locality_src, locality, .{ .simple = .prefetch_options });
2692226834
2692326835 const cache = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "cache", .no_embedded_nulls), cache_src);
26924 const cache_val = try sema.resolveConstDefinedValue(block, cache_src, cache, .{
26925 .needed_comptime_reason = "prefetch cache must be comptime-known",
26926 });
26836 const cache_val = try sema.resolveConstDefinedValue(block, cache_src, cache, .{ .simple = .prefetch_options });
2692726837
2692826838 return std.builtin.PrefetchOptions{
2692926839 .rw = zcu.toEnum(std.builtin.PrefetchOptions.Rw, rw_val),
......@@ -26945,7 +26855,7 @@ fn zirPrefetch(
2694526855
2694626856 const options = try sema.resolvePrefetchOptions(block, opts_src, extra.rhs);
2694726857
26948 if (!block.is_comptime) {
26858 if (!block.isComptime()) {
2694926859 _ = try block.addInst(.{
2695026860 .tag = .prefetch,
2695126861 .data = .{ .prefetch = .{
......@@ -26987,30 +26897,20 @@ fn resolveExternOptions(
2698726897 const dll_import_src = block.src(.{ .init_field_dll_import = src.offset.node_offset_builtin_call_arg.builtin_call_node });
2698826898
2698926899 const name_ref = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "name", .no_embedded_nulls), name_src);
26990 const name = try sema.toConstString(block, name_src, name_ref, .{
26991 .needed_comptime_reason = "name of the extern symbol must be comptime-known",
26992 });
26900 const name = try sema.toConstString(block, name_src, name_ref, .{ .simple = .extern_options });
2699326901
2699426902 const library_name_inst = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "library_name", .no_embedded_nulls), library_src);
26995 const library_name_val = try sema.resolveConstDefinedValue(block, library_src, library_name_inst, .{
26996 .needed_comptime_reason = "library in which extern symbol is must be comptime-known",
26997 });
26903 const library_name_val = try sema.resolveConstDefinedValue(block, library_src, library_name_inst, .{ .simple = .extern_options });
2699826904
2699926905 const linkage_ref = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "linkage", .no_embedded_nulls), linkage_src);
27000 const linkage_val = try sema.resolveConstDefinedValue(block, linkage_src, linkage_ref, .{
27001 .needed_comptime_reason = "linkage of the extern symbol must be comptime-known",
27002 });
26906 const linkage_val = try sema.resolveConstDefinedValue(block, linkage_src, linkage_ref, .{ .simple = .extern_options });
2700326907 const linkage = zcu.toEnum(std.builtin.GlobalLinkage, linkage_val);
2700426908
2700526909 const is_thread_local = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "is_thread_local", .no_embedded_nulls), thread_local_src);
27006 const is_thread_local_val = try sema.resolveConstDefinedValue(block, thread_local_src, is_thread_local, .{
27007 .needed_comptime_reason = "threadlocality of the extern symbol must be comptime-known",
27008 });
26910 const is_thread_local_val = try sema.resolveConstDefinedValue(block, thread_local_src, is_thread_local, .{ .simple = .extern_options });
2700926911
2701026912 const library_name = if (library_name_val.optionalValue(zcu)) |library_name_payload| library_name: {
27011 const library_name = try sema.toConstString(block, library_src, Air.internedToRef(library_name_payload.toIntern()), .{
27012 .needed_comptime_reason = "library in which extern symbol is must be comptime-known",
27013 });
26913 const library_name = try sema.toConstString(block, library_src, Air.internedToRef(library_name_payload.toIntern()), .{ .simple = .extern_options });
2701426914 if (library_name.len == 0) {
2701526915 return sema.fail(block, library_src, "library name cannot be empty", .{});
2701626916 }
......@@ -27019,9 +26919,7 @@ fn resolveExternOptions(
2701926919 } else null;
2702026920
2702126921 const is_dll_import_ref = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "is_dll_import", .no_embedded_nulls), dll_import_src);
27022 const is_dll_import_val = try sema.resolveConstDefinedValue(block, dll_import_src, is_dll_import_ref, .{
27023 .needed_comptime_reason = "it must be comptime-known if the symbol is imported from a dll",
27024 });
26922 const is_dll_import_val = try sema.resolveConstDefinedValue(block, dll_import_src, is_dll_import_ref, .{ .simple = .extern_options });
2702526923
2702626924 if (name.len == 0) {
2702726925 return sema.fail(block, name_src, "extern symbol name cannot be empty", .{});
......@@ -27134,9 +27032,7 @@ fn zirWorkItem(
2713427032 },
2713527033 }
2713627034
27137 const dimension: u32 = @intCast(try sema.resolveInt(block, dimension_src, extra.operand, Type.u32, .{
27138 .needed_comptime_reason = "dimension must be comptime-known",
27139 }));
27035 const dimension: u32 = @intCast(try sema.resolveInt(block, dimension_src, extra.operand, Type.u32, .{ .simple = .work_group_dim_index }));
2714027036 try sema.requireRuntimeBlock(block, builtin_src, null);
2714127037
2714227038 return block.addInst(.{
......@@ -27158,7 +27054,7 @@ fn zirInComptime(
2715827054 block: *Block,
2715927055) CompileError!Air.Inst.Ref {
2716027056 _ = sema;
27161 return if (block.is_comptime) .bool_true else .bool_false;
27057 return if (block.isComptime()) .bool_true else .bool_false;
2716227058}
2716327059
2716427060fn zirBuiltinValue(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
......@@ -27249,9 +27145,7 @@ fn zirBranchHint(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
2724927145
2725027146 const hint_ty = try sema.getBuiltinType("BranchHint");
2725127147 const coerced_hint = try sema.coerce(block, hint_ty, uncoerced_hint, operand_src);
27252 const hint_val = try sema.resolveConstDefinedValue(block, operand_src, coerced_hint, .{
27253 .needed_comptime_reason = "operand to '@branchHint' must be comptime-known",
27254 });
27148 const hint_val = try sema.resolveConstDefinedValue(block, operand_src, coerced_hint, .{ .simple = .operand_branchHint });
2725527149
2725627150 // We only apply the first hint in a branch.
2725727151 // This allows user-provided hints to override implicit cold hints.
......@@ -27261,20 +27155,20 @@ fn zirBranchHint(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
2726127155}
2726227156
2726327157fn requireRuntimeBlock(sema: *Sema, block: *Block, src: LazySrcLoc, runtime_src: ?LazySrcLoc) !void {
27264 if (block.is_comptime) {
27265 const msg = msg: {
27158 if (block.isComptime()) {
27159 const msg, const fail_block = msg: {
2726627160 const msg = try sema.errMsg(src, "unable to evaluate comptime expression", .{});
2726727161 errdefer msg.destroy(sema.gpa);
2726827162
2726927163 if (runtime_src) |some| {
2727027164 try sema.errNote(some, msg, "operation is runtime due to this operand", .{});
2727127165 }
27272 if (block.comptime_reason) |some| {
27273 try some.explain(sema, msg);
27274 }
27275 break :msg msg;
27166
27167 const fail_block = try block.explainWhyBlockIsComptime(msg);
27168
27169 break :msg .{ msg, fail_block };
2727627170 };
27277 return sema.failWithOwnedErrorMsg(block, msg);
27171 return sema.failWithOwnedErrorMsg(fail_block, msg);
2727827172 }
2727927173}
2728027174
......@@ -27759,7 +27653,7 @@ fn addSafetyCheck(
2775927653 panic_id: Zcu.PanicId,
2776027654) !void {
2776127655 const gpa = sema.gpa;
27762 assert(!parent_block.is_comptime);
27656 assert(!parent_block.isComptime());
2776327657
2776427658 var fail_block: Block = .{
2776527659 .parent = parent_block,
......@@ -27767,7 +27661,7 @@ fn addSafetyCheck(
2776727661 .namespace = parent_block.namespace,
2776827662 .instructions = .{},
2776927663 .inlining = parent_block.inlining,
27770 .is_comptime = false,
27664 .comptime_reason = null,
2777127665 .src_base_inst = parent_block.src_base_inst,
2777227666 .type_name_ctx = parent_block.type_name_ctx,
2777327667 };
......@@ -27874,7 +27768,7 @@ fn addSafetyCheckUnwrapError(
2787427768 unwrap_err_tag: Air.Inst.Tag,
2787527769 is_non_err_tag: Air.Inst.Tag,
2787627770) !void {
27877 assert(!parent_block.is_comptime);
27771 assert(!parent_block.isComptime());
2787827772 const ok = try parent_block.addUnOp(is_non_err_tag, operand);
2787927773 const gpa = sema.gpa;
2788027774
......@@ -27884,7 +27778,7 @@ fn addSafetyCheckUnwrapError(
2788427778 .namespace = parent_block.namespace,
2788527779 .instructions = .{},
2788627780 .inlining = parent_block.inlining,
27887 .is_comptime = false,
27781 .comptime_reason = null,
2788827782 .src_base_inst = parent_block.src_base_inst,
2788927783 .type_name_ctx = parent_block.type_name_ctx,
2789027784 };
......@@ -27918,7 +27812,7 @@ fn addSafetyCheckIndexOob(
2791827812 len: Air.Inst.Ref,
2791927813 cmp_op: Air.Inst.Tag,
2792027814) !void {
27921 assert(!parent_block.is_comptime);
27815 assert(!parent_block.isComptime());
2792227816 const ok = try parent_block.addBinOp(cmp_op, index, len);
2792327817 return addSafetyCheckCall(sema, parent_block, src, ok, "outOfBounds", &.{ index, len });
2792427818}
......@@ -27930,7 +27824,7 @@ fn addSafetyCheckInactiveUnionField(
2793027824 active_tag: Air.Inst.Ref,
2793127825 wanted_tag: Air.Inst.Ref,
2793227826) !void {
27933 assert(!parent_block.is_comptime);
27827 assert(!parent_block.isComptime());
2793427828 const ok = try parent_block.addBinOp(.cmp_eq, active_tag, wanted_tag);
2793527829 return addSafetyCheckCall(sema, parent_block, src, ok, "inactiveUnionField", &.{ active_tag, wanted_tag });
2793627830}
......@@ -27944,7 +27838,7 @@ fn addSafetyCheckSentinelMismatch(
2794427838 ptr: Air.Inst.Ref,
2794527839 sentinel_index: Air.Inst.Ref,
2794627840) !void {
27947 assert(!parent_block.is_comptime);
27841 assert(!parent_block.isComptime());
2794827842 const pt = sema.pt;
2794927843 const zcu = pt.zcu;
2795027844 const expected_sentinel_val = maybe_sentinel orelse return;
......@@ -27986,7 +27880,7 @@ fn addSafetyCheckCall(
2798627880 func_name: []const u8,
2798727881 args: []const Air.Inst.Ref,
2798827882) !void {
27989 assert(!parent_block.is_comptime);
27883 assert(!parent_block.isComptime());
2799027884 const gpa = sema.gpa;
2799127885 const pt = sema.pt;
2799227886 const zcu = pt.zcu;
......@@ -27997,7 +27891,7 @@ fn addSafetyCheckCall(
2799727891 .namespace = parent_block.namespace,
2799827892 .instructions = .{},
2799927893 .inlining = parent_block.inlining,
28000 .is_comptime = false,
27894 .comptime_reason = null,
2800127895 .src_base_inst = parent_block.src_base_inst,
2800227896 .type_name_ctx = parent_block.type_name_ctx,
2800327897 };
......@@ -29168,9 +29062,7 @@ fn elemPtr(
2916829062 .array, .vector => try sema.elemPtrArray(block, src, indexable_ptr_src, indexable_ptr, elem_index_src, elem_index, init, oob_safety),
2916929063 .@"struct" => blk: {
2917029064 // Tuple field access.
29171 const index_val = try sema.resolveConstDefinedValue(block, elem_index_src, elem_index, .{
29172 .needed_comptime_reason = "tuple field access index must be comptime-known",
29173 });
29065 const index_val = try sema.resolveConstDefinedValue(block, elem_index_src, elem_index, .{ .simple = .tuple_field_index });
2917429066 const index: u32 = @intCast(try index_val.toUnsignedIntSema(pt));
2917529067 break :blk try sema.tupleFieldPtr(block, src, indexable_ptr, elem_index_src, index, init);
2917629068 },
......@@ -29225,9 +29117,7 @@ fn elemPtrOneLayerOnly(
2922529117 .array, .vector => try sema.elemPtrArray(block, src, indexable_src, indexable, elem_index_src, elem_index, init, oob_safety),
2922629118 .@"struct" => blk: {
2922729119 assert(child_ty.isTuple(zcu));
29228 const index_val = try sema.resolveConstDefinedValue(block, elem_index_src, elem_index, .{
29229 .needed_comptime_reason = "tuple field access index must be comptime-known",
29230 });
29120 const index_val = try sema.resolveConstDefinedValue(block, elem_index_src, elem_index, .{ .simple = .tuple_field_index });
2923129121 const index: u32 = @intCast(try index_val.toUnsignedIntSema(pt));
2923229122 break :blk try sema.tupleFieldPtr(block, indexable_src, indexable, elem_index_src, index, false);
2923329123 },
......@@ -29305,9 +29195,7 @@ fn elemVal(
2930529195 },
2930629196 .@"struct" => {
2930729197 // Tuple field access.
29308 const index_val = try sema.resolveConstDefinedValue(block, elem_index_src, elem_index, .{
29309 .needed_comptime_reason = "tuple field access index must be comptime-known",
29310 });
29198 const index_val = try sema.resolveConstDefinedValue(block, elem_index_src, elem_index, .{ .simple = .tuple_field_index });
2931129199 const index: u32 = @intCast(try index_val.toUnsignedIntSema(pt));
2931229200 return sema.tupleField(block, indexable_src, indexable, elem_index_src, index);
2931329201 },
......@@ -30093,9 +29981,7 @@ fn coerceExtra(
3009329981 const val = maybe_inst_val orelse {
3009429982 if (dest_ty.zigTypeTag(zcu) == .comptime_int) {
3009529983 if (!opts.report_err) return error.NotCoercible;
30096 return sema.failWithNeededComptime(block, inst_src, .{
30097 .needed_comptime_reason = "value being casted to 'comptime_int' must be comptime-known",
30098 });
29984 return sema.failWithNeededComptime(block, inst_src, .{ .simple = .casted_to_comptime_int });
3009929985 }
3010029986 break :float;
3010129987 };
......@@ -30120,9 +30006,7 @@ fn coerceExtra(
3012030006 if (dest_ty.zigTypeTag(zcu) == .comptime_int) {
3012130007 if (!opts.report_err) return error.NotCoercible;
3012230008 if (opts.no_cast_to_comptime_int) return inst;
30123 return sema.failWithNeededComptime(block, inst_src, .{
30124 .needed_comptime_reason = "value being casted to 'comptime_int' must be comptime-known",
30125 });
30009 return sema.failWithNeededComptime(block, inst_src, .{ .simple = .casted_to_comptime_int });
3012630010 }
3012730011
3012830012 // integer widening
......@@ -30158,9 +30042,7 @@ fn coerceExtra(
3015830042 return Air.internedToRef(result_val.toIntern());
3015930043 } else if (dest_ty.zigTypeTag(zcu) == .comptime_float) {
3016030044 if (!opts.report_err) return error.NotCoercible;
30161 return sema.failWithNeededComptime(block, inst_src, .{
30162 .needed_comptime_reason = "value being casted to 'comptime_float' must be comptime-known",
30163 });
30045 return sema.failWithNeededComptime(block, inst_src, .{ .simple = .casted_to_comptime_float });
3016430046 }
3016530047
3016630048 // float widening
......@@ -30175,9 +30057,7 @@ fn coerceExtra(
3017530057 const val = maybe_inst_val orelse {
3017630058 if (dest_ty.zigTypeTag(zcu) == .comptime_float) {
3017730059 if (!opts.report_err) return error.NotCoercible;
30178 return sema.failWithNeededComptime(block, inst_src, .{
30179 .needed_comptime_reason = "value being casted to 'comptime_float' must be comptime-known",
30180 });
30060 return sema.failWithNeededComptime(block, inst_src, .{ .simple = .casted_to_comptime_float });
3018130061 }
3018230062 break :int;
3018330063 };
......@@ -32435,9 +32315,7 @@ fn coerceTupleToStruct(
3243532315 field_refs[struct_field_index] = coerced;
3243632316 if (struct_type.fieldIsComptime(ip, struct_field_index)) {
3243732317 const init_val = try sema.resolveValue(coerced) orelse {
32438 return sema.failWithNeededComptime(block, field_src, .{
32439 .needed_comptime_reason = "value stored in comptime field must be comptime-known",
32440 });
32318 return sema.failWithNeededComptime(block, field_src, .{ .simple = .stored_to_comptime_field });
3244132319 };
3244232320
3244332321 const field_init = Value.fromInterned(struct_type.field_inits.get(ip)[struct_field_index]);
......@@ -32550,9 +32428,7 @@ fn coerceTupleToTuple(
3255032428 field_refs[field_index] = coerced;
3255132429 if (default_val != .none) {
3255232430 const init_val = (try sema.resolveValue(coerced)) orelse {
32553 return sema.failWithNeededComptime(block, field_src, .{
32554 .needed_comptime_reason = "value stored in comptime field must be comptime-known",
32555 });
32431 return sema.failWithNeededComptime(block, field_src, .{ .simple = .stored_to_comptime_field });
3255632432 };
3255732433
3255832434 if (!init_val.eql(Value.fromInterned(default_val), Type.fromInterned(field_ty), pt.zcu)) {
......@@ -32816,12 +32692,12 @@ fn analyzeRef(
3281632692 // In a comptime context, the store would fail, since the operand is runtime-known. But that's
3281732693 // okay; we don't actually need this store to succeed, since we're creating a runtime value in a
3281832694 // comptime scope, so the value can never be used aside from to get its type.
32819 if (!block.is_comptime) {
32695 if (!block.isComptime()) {
3282032696 try sema.storePtr(block, src, alloc, operand);
3282132697 }
3282232698
3282332699 // Cast to the constant pointer type. We do this directly rather than going via `coerce` to
32824 // avoid errors in the `block.is_comptime` case.
32700 // avoid errors in the `block.isComptime()` case.
3282532701 return block.addBitCast(ptr_type, alloc);
3282632702}
3282732703
......@@ -33184,24 +33060,24 @@ fn analyzeSlice(
3318433060 array_ty = double_child_ty;
3318533061 elem_ty = double_child_ty.childType(zcu);
3318633062 } else {
33187 const bounds_error_message = "slice of single-item pointer must have comptime-known bounds [0..0], [0..1], or [1..1]";
3318833063 if (uncasted_end_opt == .none) {
33189 return sema.fail(block, src, bounds_error_message, .{});
33064 return sema.fail(block, src, "slice of single-item pointer must be bounded", .{});
3319033065 }
3319133066 const start_value = try sema.resolveConstDefinedValue(
3319233067 block,
3319333068 start_src,
3319433069 uncasted_start,
33195 .{ .needed_comptime_reason = bounds_error_message },
33070 .{ .simple = .slice_single_item_ptr_bounds },
3319633071 );
3319733072
3319833073 const end_value = try sema.resolveConstDefinedValue(
3319933074 block,
3320033075 end_src,
3320133076 uncasted_end_opt,
33202 .{ .needed_comptime_reason = bounds_error_message },
33077 .{ .simple = .slice_single_item_ptr_bounds },
3320333078 );
3320433079
33080 const bounds_error_message = "slice of single-item pointer must have bounds [0..0], [0..1], or [1..1]";
3320533081 if (try sema.compareScalar(start_value, .neq, end_value, Type.comptime_int)) {
3320633082 if (try sema.compareScalar(start_value, .neq, Value.zero_comptime_int, Type.comptime_int)) {
3320733083 const msg = msg: {
......@@ -33416,9 +33292,7 @@ fn analyzeSlice(
3341633292 if (sentinel_opt != .none) {
3341733293 const casted = try sema.coerce(block, elem_ty, sentinel_opt, sentinel_src);
3341833294 try checkSentinelType(sema, block, sentinel_src, elem_ty);
33419 break :s try sema.resolveConstDefinedValue(block, sentinel_src, casted, .{
33420 .needed_comptime_reason = "slice sentinel must be comptime-known",
33421 });
33295 break :s try sema.resolveConstDefinedValue(block, sentinel_src, casted, .{ .simple = .slice_sentinel });
3342233296 }
3342333297 // If we are slicing to the end of something that is sentinel-terminated
3342433298 // then the resulting slice type is also sentinel-terminated.
......@@ -33499,9 +33373,9 @@ fn analyzeSlice(
3349933373 runtime_src = end_src;
3350033374 }
3350133375
33502 if (!checked_start_lte_end and block.wantSafety() and !block.is_comptime) {
33376 if (!checked_start_lte_end and block.wantSafety() and !block.isComptime()) {
3350333377 // requirement: start <= end
33504 assert(!block.is_comptime);
33378 assert(!block.isComptime());
3350533379 try sema.requireRuntimeBlock(block, src, runtime_src.?);
3350633380 const ok = try block.addBinOp(.cmp_lte, start, end);
3350733381 try sema.addSafetyCheckCall(block, src, ok, "startGreaterThanEnd", &.{ start, end });
......@@ -35859,7 +35733,7 @@ fn backingIntType(
3585935733 .namespace = struct_type.namespace,
3586035734 .instructions = .{},
3586135735 .inlining = null,
35862 .is_comptime = true,
35736 .comptime_reason = null, // set below if needed
3586335737 .src_base_inst = struct_type.zir_index,
3586435738 .type_name_ctx = struct_type.name,
3586535739 };
......@@ -35899,6 +35773,10 @@ fn backingIntType(
3589935773 .base_node_inst = struct_type.zir_index,
3590035774 .offset = .{ .node_offset_container_tag = 0 },
3590135775 };
35776 block.comptime_reason = .{ .reason = .{
35777 .src = backing_int_src,
35778 .r = .{ .simple = .type },
35779 } };
3590235780 const backing_int_ty = blk: {
3590335781 if (backing_int_body_len == 0) {
3590435782 const backing_int_ref: Zir.Inst.Ref = @enumFromInt(zir.extra[extra_index]);
......@@ -36512,7 +36390,13 @@ fn structFields(
3651236390 .namespace = namespace_index,
3651336391 .instructions = .{},
3651436392 .inlining = null,
36515 .is_comptime = true,
36393 .comptime_reason = .{ .reason = .{
36394 .src = .{
36395 .base_node_inst = struct_type.zir_index,
36396 .offset = .nodeOffset(0),
36397 },
36398 .r = .{ .simple = .struct_fields },
36399 } },
3651636400 .src_base_inst = struct_type.zir_index,
3651736401 .type_name_ctx = struct_type.name,
3651836402 };
......@@ -36698,7 +36582,7 @@ fn structFieldInits(
3669836582 .namespace = namespace_index,
3669936583 .instructions = .{},
3670036584 .inlining = null,
36701 .is_comptime = true,
36585 .comptime_reason = undefined, // set when `block_scope` is used
3670236586 .src_base_inst = struct_type.zir_index,
3670336587 .type_name_ctx = struct_type.name,
3670436588 };
......@@ -36776,13 +36660,13 @@ fn structFieldInits(
3677636660 .offset = .{ .container_field_value = @intCast(field_i) },
3677736661 };
3677836662
36663 block_scope.comptime_reason = .{ .reason = .{
36664 .src = init_src,
36665 .r = .{ .simple = .struct_field_default_value },
36666 } };
3677936667 const init = try sema.resolveInlineBody(&block_scope, body, zir_index);
3678036668 const coerced = try sema.coerce(&block_scope, field_ty, init, init_src);
36781 const default_val = try sema.resolveValue(coerced) orelse {
36782 return sema.failWithNeededComptime(&block_scope, init_src, .{
36783 .needed_comptime_reason = "struct field default value must be comptime-known",
36784 });
36785 };
36669 const default_val = try sema.resolveConstValue(&block_scope, init_src, coerced, null);
3678636670
3678736671 if (default_val.canMutateComptimeVarState(zcu)) {
3678836672 return sema.fail(&block_scope, init_src, "field default value contains reference to comptime-mutable memory", .{});
......@@ -36850,20 +36734,26 @@ fn unionFields(
3685036734 const body = zir.bodySlice(extra_index, body_len);
3685136735 extra_index += body.len;
3685236736
36737 const src: LazySrcLoc = .{
36738 .base_node_inst = union_type.zir_index,
36739 .offset = .nodeOffset(0),
36740 };
36741
3685336742 var block_scope: Block = .{
3685436743 .parent = null,
3685536744 .sema = sema,
3685636745 .namespace = union_type.namespace,
3685736746 .instructions = .{},
3685836747 .inlining = null,
36859 .is_comptime = true,
36748 .comptime_reason = .{ .reason = .{
36749 .src = src,
36750 .r = .{ .simple = .union_fields },
36751 } },
3686036752 .src_base_inst = union_type.zir_index,
3686136753 .type_name_ctx = union_type.name,
3686236754 };
3686336755 defer assert(block_scope.instructions.items.len == 0);
3686436756
36865 const src = block_scope.nodeOffset(0);
36866
3686736757 if (body.len != 0) {
3686836758 _ = try sema.analyzeInlineBody(&block_scope, body, zir_index);
3686936759 }
......@@ -36993,9 +36883,7 @@ fn unionFields(
3699336883 if (enum_field_vals.capacity() > 0) {
3699436884 const enum_tag_val = if (tag_ref != .none) blk: {
3699536885 const coerced = try sema.coerce(&block_scope, int_tag_ty, tag_ref, value_src);
36996 const val = try sema.resolveConstDefinedValue(&block_scope, value_src, coerced, .{
36997 .needed_comptime_reason = "enum tag value must be comptime-known",
36998 });
36886 const val = try sema.resolveConstDefinedValue(&block_scope, value_src, coerced, .{ .simple = .enum_field_tag_value });
3699936887 last_tag_val = val;
3700036888
3700136889 break :blk val;
......@@ -37669,9 +37557,7 @@ pub fn analyzeAsAddressSpace(
3766937557 const zcu = pt.zcu;
3767037558 const addrspace_ty = try sema.getBuiltinType("AddressSpace");
3767137559 const coerced = try sema.coerce(block, addrspace_ty, air_ref, src);
37672 const addrspace_val = try sema.resolveConstDefinedValue(block, src, coerced, .{
37673 .needed_comptime_reason = "address space must be comptime-known",
37674 });
37560 const addrspace_val = try sema.resolveConstDefinedValue(block, src, coerced, .{ .simple = .@"addrspace" });
3767537561 const address_space = zcu.toEnum(std.builtin.AddressSpace, addrspace_val);
3767637562 const target = pt.zcu.getTarget();
3767737563 const arch = target.cpu.arch;
......@@ -38560,7 +38446,7 @@ fn sliceToIpString(
3856038446 block: *Block,
3856138447 src: LazySrcLoc,
3856238448 slice_val: Value,
38563 reason: NeededComptimeReason,
38449 reason: ComptimeReason,
3856438450) CompileError!InternPool.NullTerminatedString {
3856538451 const pt = sema.pt;
3856638452 const zcu = pt.zcu;
......@@ -38580,7 +38466,7 @@ fn derefSliceAsArray(
3858038466 block: *Block,
3858138467 src: LazySrcLoc,
3858238468 slice_val: Value,
38583 reason: NeededComptimeReason,
38469 reason: ComptimeReason,
3858438470) CompileError!Value {
3858538471 return try sema.maybeDerefSliceAsArray(block, src, slice_val) orelse {
3858638472 return sema.failWithNeededComptime(block, src, reason);
......@@ -38734,7 +38620,10 @@ pub fn resolveDeclaredEnum(
3873438620 .namespace = namespace,
3873538621 .instructions = .{},
3873638622 .inlining = null,
38737 .is_comptime = true,
38623 .comptime_reason = .{ .reason = .{
38624 .src = src,
38625 .r = .{ .simple = .enum_fields },
38626 } },
3873838627 .src_base_inst = tracked_inst,
3873938628 .type_name_ctx = type_name,
3874038629 };
......@@ -38798,9 +38687,7 @@ pub fn resolveDeclaredEnum(
3879838687 last_tag_val = try sema.resolveConstDefinedValue(&block, .{
3879938688 .base_node_inst = tracked_inst,
3880038689 .offset = .{ .container_field_name = field_i },
38801 }, tag_inst, .{
38802 .needed_comptime_reason = "enum tag value must be comptime-known",
38803 });
38690 }, tag_inst, .{ .simple = .enum_field_tag_value });
3880438691 if (!(try sema.intFitsInType(last_tag_val.?, int_tag_ty, null))) break :overflow true;
3880538692 last_tag_val = try pt.getCoerced(last_tag_val.?, int_tag_ty);
3880638693 if (wip_ty.nextField(ip, field_name, last_tag_val.?.toIntern())) |conflict| {
......@@ -38879,9 +38766,7 @@ fn getPanicInnerFn(
3887938766 const inner_name_ip = try ip.getOrPutString(gpa, pt.tid, inner_name, .no_embedded_nulls);
3888038767 const opt_fn_ref = try namespaceLookupVal(sema, block, src, outer_ty.getNamespaceIndex(zcu), inner_name_ip);
3888138768 const fn_ref = opt_fn_ref orelse return sema.fail(block, src, "std.builtin.Panic missing {s}", .{inner_name});
38882 const fn_val = try sema.resolveConstValue(block, src, fn_ref, .{
38883 .needed_comptime_reason = "panic handler must be comptime-known",
38884 });
38769 const fn_val = try sema.resolveConstValue(block, src, fn_ref, .{ .simple = .panic_handler });
3888538770 if (fn_val.typeOf(zcu).zigTypeTag(zcu) != .@"fn") {
3888638771 return sema.fail(block, src, "std.builtin.Panic.{s} is not a function", .{inner_name});
3888738772 }
......@@ -38963,9 +38848,7 @@ pub fn resolveNavPtrModifiers(
3896338848 const @"linksection": InternPool.OptionalNullTerminatedString = ls: {
3896438849 const linksection_body = zir_decl.linksection_body orelse break :ls .none;
3896538850 const linksection_ref = try sema.resolveInlineBody(block, linksection_body, decl_inst);
38966 const bytes = try sema.toConstString(block, section_src, linksection_ref, .{
38967 .needed_comptime_reason = "linksection must be comptime-known",
38968 });
38851 const bytes = try sema.toConstString(block, section_src, linksection_ref, .{ .simple = .@"linksection" });
3896938852 if (std.mem.indexOfScalar(u8, bytes, 0) != null) {
3897038853 return sema.fail(block, section_src, "linksection cannot contain null bytes", .{});
3897138854 } else if (bytes.len == 0) {
src/Zcu/PerThread.zig+22-6
......@@ -682,7 +682,13 @@ fn analyzeComptimeUnit(pt: Zcu.PerThread, cu_id: InternPool.ComptimeUnit.Id) Zcu
682682 .namespace = comptime_unit.namespace,
683683 .instructions = .{},
684684 .inlining = null,
685 .is_comptime = true,
685 .comptime_reason = .{ .reason = .{
686 .src = .{
687 .base_node_inst = comptime_unit.zir_index,
688 .offset = .{ .token_offset = 0 },
689 },
690 .r = .{ .simple = .comptime_keyword },
691 } },
686692 .src_base_inst = comptime_unit.zir_index,
687693 .type_name_ctx = try ip.getOrPutStringFmt(gpa, pt.tid, "{}.comptime", .{
688694 Type.fromInterned(zcu.namespacePtr(comptime_unit.namespace).owner_type).containerTypeName(ip).fmt(ip),
......@@ -878,7 +884,7 @@ fn analyzeNavVal(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zcu.CompileErr
878884 .namespace = old_nav.analysis.?.namespace,
879885 .instructions = .{},
880886 .inlining = null,
881 .is_comptime = true,
887 .comptime_reason = undefined, // set below
882888 .src_base_inst = old_nav.analysis.?.zir_index,
883889 .type_name_ctx = old_nav.fqn,
884890 };
......@@ -893,6 +899,11 @@ fn analyzeNavVal(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zcu.CompileErr
893899 const section_src = block.src(.{ .node_offset_var_decl_section = 0 });
894900 const addrspace_src = block.src(.{ .node_offset_var_decl_addrspace = 0 });
895901
902 block.comptime_reason = .{ .reason = .{
903 .src = init_src,
904 .r = .{ .simple = .container_var_init },
905 } };
906
896907 const maybe_ty: ?Type = if (zir_decl.type_body != null) ty: {
897908 // Since we have a type body, the type is resolved separately!
898909 // Of course, we need to make sure we depend on it properly.
......@@ -1253,7 +1264,7 @@ fn analyzeNavType(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zcu.CompileEr
12531264 .namespace = old_nav.analysis.?.namespace,
12541265 .instructions = .{},
12551266 .inlining = null,
1256 .is_comptime = true,
1267 .comptime_reason = undefined, // set below
12571268 .src_base_inst = old_nav.analysis.?.zir_index,
12581269 .type_name_ctx = old_nav.fqn,
12591270 };
......@@ -1262,6 +1273,13 @@ fn analyzeNavType(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zcu.CompileEr
12621273 const zir_decl = zir.getDeclaration(inst_resolved.inst);
12631274 assert(old_nav.is_usingnamespace == (zir_decl.kind == .@"usingnamespace"));
12641275
1276 const ty_src = block.src(.{ .node_offset_var_decl_ty = 0 });
1277
1278 block.comptime_reason = .{ .reason = .{
1279 .src = ty_src,
1280 .r = .{ .simple = .type },
1281 } };
1282
12651283 const type_body = zir_decl.type_body orelse {
12661284 // The type of this `Nav` is inferred from the value.
12671285 // In other words, this `nav_ty` depends on the corresponding `nav_val`.
......@@ -1279,8 +1297,6 @@ fn analyzeNavType(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zcu.CompileEr
12791297 return .{ .type_changed = true };
12801298 };
12811299
1282 const ty_src = block.src(.{ .node_offset_var_decl_ty = 0 });
1283
12841300 const resolved_ty: Type = ty: {
12851301 const uncoerced_type_ref = try sema.resolveInlineBody(&block, type_body, inst_resolved.inst);
12861302 const type_ref = try sema.coerce(&block, .type, uncoerced_type_ref, ty_src);
......@@ -2442,7 +2458,7 @@ fn analyzeFnBodyInner(pt: Zcu.PerThread, func_index: InternPool.Index) Zcu.SemaE
24422458 .namespace = decl_nav.analysis.?.namespace,
24432459 .instructions = .{},
24442460 .inlining = null,
2445 .is_comptime = false,
2461 .comptime_reason = null,
24462462 .src_base_inst = decl_nav.analysis.?.zir_index,
24472463 .type_name_ctx = func_nav.fqn,
24482464 };
src/print_zir.zig+10-4
......@@ -437,7 +437,6 @@ const Writer = struct {
437437 .field_call => try self.writeCall(stream, inst, .field),
438438
439439 .block,
440 .block_comptime,
441440 .block_inline,
442441 .suspend_block,
443442 .loop,
......@@ -445,6 +444,8 @@ const Writer = struct {
445444 .typeof_builtin,
446445 => try self.writeBlock(stream, inst),
447446
447 .block_comptime => try self.writeBlockComptime(stream, inst),
448
448449 .condbr,
449450 .condbr_inline,
450451 => try self.writeCondBr(stream, inst),
......@@ -1343,16 +1344,21 @@ const Writer = struct {
13431344
13441345 fn writeBlock(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
13451346 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1346 try self.writePlNodeBlockWithoutSrc(stream, inst);
1347 const extra = self.code.extraData(Zir.Inst.Block, inst_data.payload_index);
1348 const body = self.code.bodySlice(extra.end, extra.data.body_len);
1349 try self.writeBracedBody(stream, body);
1350 try stream.writeAll(") ");
13471351 try self.writeSrcNode(stream, inst_data.src_node);
13481352 }
13491353
1350 fn writePlNodeBlockWithoutSrc(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
1354 fn writeBlockComptime(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
13511355 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1352 const extra = self.code.extraData(Zir.Inst.Block, inst_data.payload_index);
1356 const extra = self.code.extraData(Zir.Inst.BlockComptime, inst_data.payload_index);
13531357 const body = self.code.bodySlice(extra.end, extra.data.body_len);
1358 try stream.print("reason={s}, ", .{@tagName(extra.data.reason)});
13541359 try self.writeBracedBody(stream, body);
13551360 try stream.writeAll(") ");
1361 try self.writeSrcNode(stream, inst_data.src_node);
13561362 }
13571363
13581364 fn writeCondBr(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {