authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-12-31 14:35:28+00:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-12-31 14:35:28+00:00
log0df1f3df2f25bf12071f11e9b738a356dfbd214d
treefd7830fb82c95f52a79fd9984af4caacb090f3fe
parent6d67658965bc298a697dc756a4e06bda144427de
parent106df881d3a3fe3b744f0563b99cff88d7ef6549
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #22352 from mlugg/zir-comptime-reason

Zir: attach reason to `block_comptime` and improve corresponding error reporting

39 files changed, 1157 insertions(+), 839 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+241-86
......@@ -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,13 +784,22 @@ 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 },
789791
790 .error_union => return simpleBinOp(gz, scope, ri, node, .error_union_type),
791 .merge_error_sets => return simpleBinOp(gz, scope, ri, node, .merge_error_sets),
792 .error_union, .merge_error_sets => |tag| {
793 const inst_tag: Zir.Inst.Tag = switch (tag) {
794 .error_union => .error_union_type,
795 .merge_error_sets => .merge_error_sets,
796 else => unreachable,
797 };
798 const lhs = try reachableTypeExpr(gz, scope, node_datas[node].lhs, node);
799 const rhs = try reachableTypeExpr(gz, scope, node_datas[node].rhs, node);
800 const result = try gz.addPlNode(inst_tag, node, Zir.Inst.Bin{ .lhs = lhs, .rhs = rhs });
801 return rvalue(gz, ri, result, node);
802 },
792803
793804 .bool_and => return boolBinOp(gz, scope, ri, node, .bool_br_and),
794805 .bool_or => return boolBinOp(gz, scope, ri, node, .bool_br_or),
......@@ -799,7 +810,7 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
799810 .negation => return negation(gz, scope, ri, node),
800811 .negation_wrap => return simpleUnOp(gz, scope, ri, node, .{ .rl = .none }, node_datas[node].lhs, .negate_wrap),
801812
802 .identifier => return identifier(gz, scope, ri, node),
813 .identifier => return identifier(gz, scope, ri, node, null),
803814
804815 .asm_simple,
805816 .@"asm",
......@@ -1364,6 +1375,7 @@ fn fnProtoExprInner(
13641375 assert(param_type_node != 0);
13651376 var param_gz = block_scope.makeSubBlock(scope);
13661377 defer param_gz.unstack();
1378 param_gz.is_comptime = true;
13671379 const param_type = try fullBodyExpr(&param_gz, scope, coerced_type_ri, param_type_node, .normal);
13681380 const param_inst_expected: Zir.Inst.Index = @enumFromInt(astgen.instructions.len + 1);
13691381 _ = try param_gz.addBreakWithSrcNode(.break_inline, param_inst_expected, param_type, param_type_node);
......@@ -1380,18 +1392,19 @@ fn fnProtoExprInner(
13801392 };
13811393
13821394 const cc: Zir.Inst.Ref = if (fn_proto.ast.callconv_expr != 0)
1383 try expr(
1395 try comptimeExpr(
13841396 &block_scope,
13851397 scope,
13861398 .{ .rl = .{ .coerced_ty = try block_scope.addBuiltinValue(fn_proto.ast.callconv_expr, .calling_convention) } },
13871399 fn_proto.ast.callconv_expr,
1400 .@"callconv",
13881401 )
13891402 else if (implicit_ccc)
13901403 try block_scope.addBuiltinValue(node, .calling_convention_c)
13911404 else
13921405 .none;
13931406
1394 const ret_ty = try expr(&block_scope, scope, coerced_type_ri, fn_proto.ast.return_type);
1407 const ret_ty = try comptimeExpr(&block_scope, scope, coerced_type_ri, fn_proto.ast.return_type, .function_ret_ty);
13951408
13961409 const result = try block_scope.addFunc(.{
13971410 .src_node = fn_proto.ast.proto_node,
......@@ -1453,7 +1466,7 @@ fn arrayInitExpr(
14531466 });
14541467 break :inst .{ array_type_inst, elem_type };
14551468 } else {
1456 const sentinel = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = elem_type } }, array_type.ast.sentinel);
1469 const sentinel = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = elem_type } }, array_type.ast.sentinel, .array_sentinel);
14571470 const array_type_inst = try gz.addPlNode(
14581471 .array_type_sentinel,
14591472 array_init.ast.type_expr,
......@@ -1721,7 +1734,7 @@ fn structInitExpr(
17211734 .rhs = elem_type,
17221735 });
17231736 } else blk: {
1724 const sentinel = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = elem_type } }, array_type.ast.sentinel);
1737 const sentinel = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = elem_type } }, array_type.ast.sentinel, .array_sentinel);
17251738 break :blk try gz.addPlNode(
17261739 .array_type_sentinel,
17271740 struct_init.ast.type_expr,
......@@ -1966,6 +1979,20 @@ fn comptimeExpr(
19661979 scope: *Scope,
19671980 ri: ResultInfo,
19681981 node: Ast.Node.Index,
1982 reason: std.zig.SimpleComptimeReason,
1983) InnerError!Zir.Inst.Ref {
1984 return comptimeExpr2(gz, scope, ri, node, node, reason);
1985}
1986
1987/// Like `comptimeExpr`, but draws a distinction between `node`, the expression to evaluate at comptime,
1988/// and `src_node`, the node to attach to the `block_comptime`.
1989fn comptimeExpr2(
1990 gz: *GenZir,
1991 scope: *Scope,
1992 ri: ResultInfo,
1993 node: Ast.Node.Index,
1994 src_node: Ast.Node.Index,
1995 reason: std.zig.SimpleComptimeReason,
19691996) InnerError!Zir.Inst.Ref {
19701997 if (gz.is_comptime) {
19711998 // No need to change anything!
......@@ -1979,19 +2006,50 @@ fn comptimeExpr(
19792006 const main_tokens = tree.nodes.items(.main_token);
19802007 const node_tags = tree.nodes.items(.tag);
19812008 switch (node_tags[node]) {
1982 // Any identifier in `primitive_instrs` is trivially comptime. In particular, this includes
1983 // some common types, so we can elide `block_comptime` for a few common type annotations.
19842009 .identifier => {
1985 const ident_token = main_tokens[node];
1986 const ident_name_raw = tree.tokenSlice(ident_token);
1987 if (primitive_instrs.get(ident_name_raw)) |zir_const_ref| {
1988 // No need to worry about result location here, we're not creating a comptime block!
1989 return rvalue(gz, ri, zir_const_ref, node);
1990 }
2010 // Many identifiers can be handled without a `block_comptime`, so `AstGen.identifier` has
2011 // special handling for this case.
2012 return identifier(gz, scope, ri, node, .{ .src_node = src_node, .reason = reason });
19912013 },
19922014
1993 // We can also avoid the block for a few trivial AST tags which are always comptime-known.
1994 .number_literal, .string_literal, .multiline_string_literal, .enum_literal, .error_value => {
2015 // These are leaf nodes which are always comptime-known.
2016 .number_literal,
2017 .char_literal,
2018 .string_literal,
2019 .multiline_string_literal,
2020 .enum_literal,
2021 .error_value,
2022 .anyframe_literal,
2023 .error_set_decl,
2024 // These nodes are not leaves, but will force comptime evaluation of all sub-expressions, and
2025 // hence behave the same regardless of whether they're in a comptime scope.
2026 .error_union,
2027 .merge_error_sets,
2028 .optional_type,
2029 .anyframe_type,
2030 .ptr_type_aligned,
2031 .ptr_type_sentinel,
2032 .ptr_type,
2033 .ptr_type_bit_range,
2034 .array_type,
2035 .array_type_sentinel,
2036 .fn_proto_simple,
2037 .fn_proto_multi,
2038 .fn_proto_one,
2039 .fn_proto,
2040 .container_decl,
2041 .container_decl_trailing,
2042 .container_decl_arg,
2043 .container_decl_arg_trailing,
2044 .container_decl_two,
2045 .container_decl_two_trailing,
2046 .tagged_union,
2047 .tagged_union_trailing,
2048 .tagged_union_enum_tag,
2049 .tagged_union_enum_tag_trailing,
2050 .tagged_union_two,
2051 .tagged_union_two_trailing,
2052 => {
19952053 // No need to worry about result location here, we're not creating a comptime block!
19962054 return expr(gz, scope, ri, node);
19972055 },
......@@ -2049,23 +2107,23 @@ fn comptimeExpr(
20492107 block_scope.is_comptime = true;
20502108 defer block_scope.unstack();
20512109
2052 const block_inst = try gz.makeBlockInst(.block_comptime, node);
2110 const block_inst = try gz.makeBlockInst(.block_comptime, src_node);
20532111 // Replace result location and copy back later - see above.
20542112 const ty_only_ri: ResultInfo = .{
20552113 .ctx = ri.ctx,
2056 .rl = if (try ri.rl.resultType(gz, node)) |res_ty|
2114 .rl = if (try ri.rl.resultType(gz, src_node)) |res_ty|
20572115 .{ .coerced_ty = res_ty }
20582116 else
20592117 .none,
20602118 };
20612119 const block_result = try fullBodyExpr(&block_scope, scope, ty_only_ri, node, .normal);
20622120 if (!gz.refIsNoReturn(block_result)) {
2063 _ = try block_scope.addBreak(.@"break", block_inst, block_result);
2121 _ = try block_scope.addBreak(.break_inline, block_inst, block_result);
20642122 }
2065 try block_scope.setBlockBody(block_inst);
2123 try block_scope.setBlockComptimeBody(block_inst, reason);
20662124 try gz.instructions.append(gz.astgen.gpa, block_inst);
20672125
2068 return rvalue(gz, ri, block_inst.toRef(), node);
2126 return rvalue(gz, ri, block_inst.toRef(), src_node);
20692127}
20702128
20712129/// This one is for an actual `comptime` syntax, and will emit a compile error if
......@@ -2084,7 +2142,7 @@ fn comptimeExprAst(
20842142 const tree = astgen.tree;
20852143 const node_datas = tree.nodes.items(.data);
20862144 const body_node = node_datas[node].lhs;
2087 return comptimeExpr(gz, scope, ri, body_node);
2145 return comptimeExpr2(gz, scope, ri, body_node, node, .comptime_keyword);
20882146}
20892147
20902148/// Restore the error return trace index. Performs the restore only if the result is a non-error or
......@@ -2494,10 +2552,10 @@ fn labeledBlockExpr(
24942552
24952553 // Reserve the Block ZIR instruction index so that we can put it into the GenZir struct
24962554 // 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);
2555 const block_inst = try gz.makeBlockInst(if (force_comptime) .block_comptime else .block, block_node);
24992556 try gz.instructions.append(astgen.gpa, block_inst);
25002557 var block_scope = gz.makeSubBlock(parent_scope);
2558 block_scope.is_inline = force_comptime;
25012559 block_scope.label = GenZir.Label{
25022560 .token = label_token,
25032561 .block_inst = block_inst,
......@@ -2511,14 +2569,20 @@ fn labeledBlockExpr(
25112569 // As our last action before the return, "pop" the error trace if needed
25122570 _ = try gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always, block_node);
25132571 const result = try rvalue(gz, block_scope.break_result_info, .void_value, block_node);
2514 _ = try block_scope.addBreak(.@"break", block_inst, result);
2572 const break_tag: Zir.Inst.Tag = if (force_comptime) .break_inline else .@"break";
2573 _ = try block_scope.addBreak(break_tag, block_inst, result);
25152574 }
25162575
25172576 if (!block_scope.label.?.used) {
25182577 try astgen.appendErrorTok(label_token, "unused block label", .{});
25192578 }
25202579
2521 try block_scope.setBlockBody(block_inst);
2580 if (force_comptime) {
2581 try block_scope.setBlockComptimeBody(block_inst, .comptime_keyword);
2582 } else {
2583 try block_scope.setBlockBody(block_inst);
2584 }
2585
25222586 if (need_result_rvalue) {
25232587 return rvalue(gz, ri, block_inst.toRef(), block_node);
25242588 } else {
......@@ -2941,6 +3005,7 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
29413005 .validate_array_init_result_ty,
29423006 .validate_ptr_array_init,
29433007 .validate_ref_ty,
3008 .validate_const,
29443009 .try_operand_ty,
29453010 .try_ref_operand_ty,
29463011 => break :b true,
......@@ -3255,9 +3320,10 @@ fn varDecl(
32553320 } else .{ .rl = .none, .ctx = .const_init };
32563321 const prev_anon_name_strategy = gz.anon_name_strategy;
32573322 gz.anon_name_strategy = .dbg_var;
3258 const init_inst = try reachableExprComptime(gz, scope, result_info, var_decl.ast.init_node, node, force_comptime);
3323 const init_inst = try reachableExprComptime(gz, scope, result_info, var_decl.ast.init_node, node, if (force_comptime) .comptime_keyword else null);
32593324 gz.anon_name_strategy = prev_anon_name_strategy;
32603325
3326 _ = try gz.addUnNode(.validate_const, init_inst, var_decl.ast.init_node);
32613327 try gz.addDbgVar(.dbg_var_val, ident_name, init_inst);
32623328
32633329 // The const init expression may have modified the error return trace, so signal
......@@ -3321,7 +3387,7 @@ fn varDecl(
33213387 const prev_anon_name_strategy = gz.anon_name_strategy;
33223388 gz.anon_name_strategy = .dbg_var;
33233389 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);
3390 const init_inst = try reachableExprComptime(gz, scope, init_result_info, var_decl.ast.init_node, node, if (force_comptime) .comptime_keyword else null);
33253391
33263392 // The const init expression may have modified the error return trace, so signal
33273393 // to Sema that it should save the new index for restoring later.
......@@ -3393,7 +3459,14 @@ fn varDecl(
33933459 };
33943460 const prev_anon_name_strategy = gz.anon_name_strategy;
33953461 gz.anon_name_strategy = .dbg_var;
3396 _ = try reachableExprComptime(gz, scope, result_info, var_decl.ast.init_node, node, is_comptime);
3462 _ = try reachableExprComptime(
3463 gz,
3464 scope,
3465 result_info,
3466 var_decl.ast.init_node,
3467 node,
3468 if (var_decl.comptime_token != null) .comptime_keyword else null,
3469 );
33973470 gz.anon_name_strategy = prev_anon_name_strategy;
33983471 const final_ptr: Zir.Inst.Ref = if (resolve_inferred) ptr: {
33993472 break :ptr try gz.addUnNode(.resolve_inferred_alloc, alloc, node);
......@@ -3501,8 +3574,8 @@ fn assignDestructure(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerErro
35013574
35023575 if (full.comptime_token) |_| {
35033576 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);
3577 _ = try inner_gz.addBreak(.break_inline, comptime_block_inst, .void_value);
3578 try inner_gz.setBlockComptimeBody(comptime_block_inst, .comptime_keyword);
35063579 try gz.instructions.append(gz.astgen.gpa, comptime_block_inst);
35073580 }
35083581}
......@@ -3673,8 +3746,8 @@ fn assignDestructureMaybeDecls(
36733746 // Finish the block_comptime. Inferred alloc resolution etc will occur
36743747 // in the parent block.
36753748 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);
3749 _ = try inner_gz.addBreak(.break_inline, comptime_block_inst, .void_value);
3750 try inner_gz.setBlockComptimeBody(comptime_block_inst, .comptime_keyword);
36783751 try gz.instructions.append(gz.astgen.gpa, comptime_block_inst);
36793752 }
36803753
......@@ -3867,7 +3940,16 @@ fn ptrType(
38673940 gz.astgen.source_line = source_line;
38683941 gz.astgen.source_column = source_column;
38693942
3870 sentinel_ref = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = elem_type } }, ptr_info.ast.sentinel);
3943 sentinel_ref = try comptimeExpr(
3944 gz,
3945 scope,
3946 .{ .rl = .{ .ty = elem_type } },
3947 ptr_info.ast.sentinel,
3948 switch (ptr_info.size) {
3949 .Slice => .slice_sentinel,
3950 else => .pointer_sentinel,
3951 },
3952 );
38713953 trailing_count += 1;
38723954 }
38733955 if (ptr_info.ast.addrspace_node != 0) {
......@@ -3876,7 +3958,7 @@ fn ptrType(
38763958 gz.astgen.source_column = source_column;
38773959
38783960 const addrspace_ty = try gz.addBuiltinValue(ptr_info.ast.addrspace_node, .address_space);
3879 addrspace_ref = try expr(gz, scope, .{ .rl = .{ .coerced_ty = addrspace_ty } }, ptr_info.ast.addrspace_node);
3961 addrspace_ref = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = addrspace_ty } }, ptr_info.ast.addrspace_node, .@"addrspace");
38803962 trailing_count += 1;
38813963 }
38823964 if (ptr_info.ast.align_node != 0) {
......@@ -3884,13 +3966,13 @@ fn ptrType(
38843966 gz.astgen.source_line = source_line;
38853967 gz.astgen.source_column = source_column;
38863968
3887 align_ref = try expr(gz, scope, coerced_align_ri, ptr_info.ast.align_node);
3969 align_ref = try comptimeExpr(gz, scope, coerced_align_ri, ptr_info.ast.align_node, .@"align");
38883970 trailing_count += 1;
38893971 }
38903972 if (ptr_info.ast.bit_range_start != 0) {
38913973 assert(ptr_info.ast.bit_range_end != 0);
3892 bit_start_ref = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .u16_type } }, ptr_info.ast.bit_range_start);
3893 bit_end_ref = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .u16_type } }, ptr_info.ast.bit_range_end);
3974 bit_start_ref = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u16_type } }, ptr_info.ast.bit_range_start, .type);
3975 bit_end_ref = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u16_type } }, ptr_info.ast.bit_range_end, .type);
38943976 trailing_count += 2;
38953977 }
38963978
......@@ -3953,7 +4035,7 @@ fn arrayType(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) !
39534035 {
39544036 return astgen.failNode(len_node, "unable to infer array size", .{});
39554037 }
3956 const len = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, len_node);
4038 const len = try reachableExprComptime(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, len_node, node, .type);
39574039 const elem_type = try typeExpr(gz, scope, node_datas[node].rhs);
39584040
39594041 const result = try gz.addPlNode(.array_type, node, Zir.Inst.Bin{
......@@ -3977,9 +4059,9 @@ fn arrayTypeSentinel(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.
39774059 {
39784060 return astgen.failNode(len_node, "unable to infer array size", .{});
39794061 }
3980 const len = try reachableExpr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, len_node, node);
4062 const len = try reachableExprComptime(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, len_node, node, .array_length);
39814063 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);
4064 const sentinel = try reachableExprComptime(gz, scope, .{ .rl = .{ .coerced_ty = elem_type } }, extra.sentinel, node, .array_sentinel);
39834065
39844066 const result = try gz.addPlNode(.array_type_sentinel, node, Zir.Inst.ArrayTypeSentinel{
39854067 .len = len,
......@@ -5321,7 +5403,7 @@ fn tupleDecl(
53215403 astgen.scratch.appendAssumeCapacity(@intFromEnum(field_type_ref));
53225404
53235405 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);
5406 const field_init_ref = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = field_type_ref } }, field.ast.value_expr, .tuple_field_default_value);
53255407 astgen.scratch.appendAssumeCapacity(@intFromEnum(field_init_ref));
53265408 } else {
53275409 astgen.scratch.appendAssumeCapacity(@intFromEnum(Zir.Inst.Ref.none));
......@@ -5693,7 +5775,7 @@ fn containerDecl(
56935775 namespace.base.tag = .namespace;
56945776
56955777 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)
5778 try comptimeExpr(&block_scope, &namespace.base, coerced_type_ri, container_decl.ast.arg, .type)
56975779 else
56985780 .none;
56995781
......@@ -7573,7 +7655,7 @@ fn switchExprErrUnion(
75737655 if (node_tags[item_node] == .switch_range) continue;
75747656 items_len += 1;
75757657
7576 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node);
7658 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node, .switch_item);
75777659 try payloads.append(gpa, @intFromEnum(item_inst));
75787660 }
75797661
......@@ -7583,8 +7665,8 @@ fn switchExprErrUnion(
75837665 if (node_tags[range] != .switch_range) continue;
75847666 ranges_len += 1;
75857667
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);
7668 const first = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].lhs, .switch_item);
7669 const last = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].rhs, .switch_item);
75887670 try payloads.appendSlice(gpa, &[_]u32{
75897671 @intFromEnum(first), @intFromEnum(last),
75907672 });
......@@ -7602,7 +7684,7 @@ fn switchExprErrUnion(
76027684 scalar_case_index += 1;
76037685 try payloads.resize(gpa, header_index + 2); // item, body_len
76047686 const item_node = case.ast.values[0];
7605 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node);
7687 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node, .switch_item);
76067688 payloads.items[header_index] = @intFromEnum(item_inst);
76077689 break :blk header_index + 1;
76087690 };
......@@ -8046,7 +8128,7 @@ fn switchExpr(
80468128 if (node_tags[item_node] == .switch_range) continue;
80478129 items_len += 1;
80488130
8049 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node);
8131 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node, .switch_item);
80508132 try payloads.append(gpa, @intFromEnum(item_inst));
80518133 }
80528134
......@@ -8056,8 +8138,8 @@ fn switchExpr(
80568138 if (node_tags[range] != .switch_range) continue;
80578139 ranges_len += 1;
80588140
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);
8141 const first = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].lhs, .switch_item);
8142 const last = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].rhs, .switch_item);
80618143 try payloads.appendSlice(gpa, &[_]u32{
80628144 @intFromEnum(first), @intFromEnum(last),
80638145 });
......@@ -8075,7 +8157,7 @@ fn switchExpr(
80758157 scalar_case_index += 1;
80768158 try payloads.resize(gpa, header_index + 2); // item, body_len
80778159 const item_node = case.ast.values[0];
8078 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node);
8160 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node, .switch_item);
80798161 payloads.items[header_index] = @intFromEnum(item_inst);
80808162 break :blk header_index + 1;
80818163 };
......@@ -8339,11 +8421,17 @@ fn parseBitCount(buf: []const u8) std.fmt.ParseIntError!u16 {
83398421 return x;
83408422}
83418423
8424const ComptimeBlockInfo = struct {
8425 src_node: Ast.Node.Index,
8426 reason: std.zig.SimpleComptimeReason,
8427};
8428
83428429fn identifier(
83438430 gz: *GenZir,
83448431 scope: *Scope,
83458432 ri: ResultInfo,
83468433 ident: Ast.Node.Index,
8434 force_comptime: ?ComptimeBlockInfo,
83478435) InnerError!Zir.Inst.Ref {
83488436 const astgen = gz.astgen;
83498437 const tree = astgen.tree;
......@@ -8362,6 +8450,7 @@ fn identifier(
83628450 }
83638451
83648452 if (ident_name_raw.len >= 2) integer: {
8453 // Keep in sync with logic in `comptimeExpr2`.
83658454 const first_c = ident_name_raw[0];
83668455 if (first_c == 'i' or first_c == 'u') {
83678456 const signedness: std.builtin.Signedness = switch (first_c == 'i') {
......@@ -8396,8 +8485,31 @@ fn identifier(
83968485 }
83978486 }
83988487
8399 // Local variables, including function parameters.
8400 return localVarRef(gz, scope, ri, ident, ident_token);
8488 // Local variables, including function parameters, and container-level declarations.
8489
8490 if (force_comptime) |fc| {
8491 // Mirrors the logic at the end of `comptimeExpr2`.
8492 const block_inst = try gz.makeBlockInst(.block_comptime, fc.src_node);
8493
8494 var comptime_gz = gz.makeSubBlock(scope);
8495 comptime_gz.is_comptime = true;
8496 defer comptime_gz.unstack();
8497
8498 const sub_ri: ResultInfo = .{
8499 .ctx = ri.ctx,
8500 .rl = .none, // no point providing a result type, it won't change anything
8501 };
8502 const block_result = try localVarRef(&comptime_gz, scope, sub_ri, ident, ident_token);
8503 assert(!comptime_gz.endsWithNoReturn());
8504 _ = try comptime_gz.addBreak(.break_inline, block_inst, block_result);
8505
8506 try comptime_gz.setBlockComptimeBody(block_inst, fc.reason);
8507 try gz.instructions.append(astgen.gpa, block_inst);
8508
8509 return rvalue(gz, ri, block_inst.toRef(), fc.src_node);
8510 } else {
8511 return localVarRef(gz, scope, ri, ident, ident_token);
8512 }
84018513}
84028514
84038515fn localVarRef(
......@@ -8836,7 +8948,7 @@ fn asmExpr(
88368948 },
88378949 else => .{
88388950 .tag = .asm_expr,
8839 .tmpl = @enumFromInt(@intFromEnum(try comptimeExpr(gz, scope, .{ .rl = .none }, full.ast.template))),
8951 .tmpl = @enumFromInt(@intFromEnum(try comptimeExpr(gz, scope, .{ .rl = .none }, full.ast.template, .inline_assembly_code))),
88408952 },
88418953 };
88428954
......@@ -8973,7 +9085,7 @@ fn unionInit(
89739085 params: []const Ast.Node.Index,
89749086) InnerError!Zir.Inst.Ref {
89759087 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]);
9088 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1], .union_field_name);
89779089 const field_type = try gz.addPlNode(.field_type_ref, node, Zir.Inst.FieldTypeRef{
89789090 .container_type = union_type,
89799091 .field_name = field_name,
......@@ -9078,7 +9190,7 @@ fn ptrCast(
90789190 const flags_int: FlagsInt = @bitCast(flags);
90799191 const cursor = maybeAdvanceSourceCursorToMainToken(gz, root_node);
90809192 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);
9193 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, node_datas[node].lhs, .field_name);
90829194 const field_ptr = try expr(gz, scope, .{ .rl = .none }, node_datas[node].rhs);
90839195 try emitDbgStmt(gz, cursor);
90849196 const result = try gz.addExtendedPayloadSmall(.field_parent_ptr, flags_int, Zir.Inst.FieldParentPtr{
......@@ -9279,7 +9391,7 @@ fn builtinCall(
92799391 return astgen.failNode(node, "'@branchHint' must appear as the first statement in a function or conditional branch", .{});
92809392 }
92819393 const hint_ty = try gz.addBuiltinValue(node, .branch_hint);
9282 const hint_val = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = hint_ty } }, params[0]);
9394 const hint_val = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = hint_ty } }, params[0], .operand_branchHint);
92839395 _ = try gz.addExtendedPayload(.branch_hint, Zir.Inst.UnNode{
92849396 .node = gz.nodeIndexToRelative(node),
92859397 .operand = hint_val,
......@@ -9326,18 +9438,18 @@ fn builtinCall(
93269438 if (ri.rl == .ref or ri.rl == .ref_coerced_ty) {
93279439 return gz.addPlNode(.field_ptr_named, node, Zir.Inst.FieldNamed{
93289440 .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]),
9441 .field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1], .field_name),
93309442 });
93319443 }
93329444 const result = try gz.addPlNode(.field_val_named, node, Zir.Inst.FieldNamed{
93339445 .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]),
9446 .field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1], .field_name),
93359447 });
93369448 return rvalue(gz, ri, result, node);
93379449 },
93389450 .FieldType => {
93399451 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]);
9452 const name_inst = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1], .field_name);
93419453 const result = try gz.addPlNode(.field_type_ref, node, Zir.Inst.FieldTypeRef{
93429454 .container_type = ty_inst,
93439455 .field_name = name_inst,
......@@ -9358,7 +9470,7 @@ fn builtinCall(
93589470 .@"export" => {
93599471 const exported = try expr(gz, scope, .{ .rl = .none }, params[0]);
93609472 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]);
9473 const options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = export_options_ty } }, params[1], .export_options);
93629474 _ = try gz.addPlNode(.@"export", node, Zir.Inst.Export{
93639475 .exported = exported,
93649476 .options = options,
......@@ -9368,7 +9480,7 @@ fn builtinCall(
93689480 .@"extern" => {
93699481 const type_inst = try typeExpr(gz, scope, params[0]);
93709482 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]);
9483 const options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = extern_options_ty } }, params[1], .extern_options);
93729484 const result = try gz.addExtendedPayload(.builtin_extern, Zir.Inst.BinNode{
93739485 .node = gz.nodeIndexToRelative(node),
93749486 .lhs = type_inst,
......@@ -9560,7 +9672,7 @@ fn builtinCall(
95609672 // zig fmt: on
95619673
95629674 .wasm_memory_size => {
9563 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);
9675 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .wasm_memory_index);
95649676 const result = try gz.addExtendedPayload(.wasm_memory_size, Zir.Inst.UnNode{
95659677 .node = gz.nodeIndexToRelative(node),
95669678 .operand = operand,
......@@ -9568,7 +9680,7 @@ fn builtinCall(
95689680 return rvalue(gz, ri, result, node);
95699681 },
95709682 .wasm_memory_grow => {
9571 const index_arg = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);
9683 const index_arg = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .wasm_memory_index);
95729684 const delta_arg = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, params[1]);
95739685 const result = try gz.addExtendedPayload(.wasm_memory_grow, Zir.Inst.BinNode{
95749686 .node = gz.nodeIndexToRelative(node),
......@@ -9579,8 +9691,8 @@ fn builtinCall(
95799691 },
95809692 .c_define => {
95819693 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]);
9694 const name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[0], .operand_cDefine_macro_name);
9695 const value = try comptimeExpr(gz, scope, .{ .rl = .none }, params[1], .operand_cDefine_macro_value);
95849696 const result = try gz.addExtendedPayload(.c_define, Zir.Inst.BinNode{
95859697 .node = gz.nodeIndexToRelative(node),
95869698 .lhs = name,
......@@ -9666,7 +9778,7 @@ fn builtinCall(
96669778 },
96679779 .call => {
96689780 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]);
9781 const modifier = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = call_modifier_ty } }, params[0], .call_modifier);
96709782 const callee = try expr(gz, scope, .{ .rl = .none }, params[1]);
96719783 const args = try expr(gz, scope, .{ .rl = .none }, params[2]);
96729784 const result = try gz.addPlNode(.builtin_call, node, Zir.Inst.BuiltinCall{
......@@ -9682,7 +9794,7 @@ fn builtinCall(
96829794 },
96839795 .field_parent_ptr => {
96849796 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]);
9797 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[0], .field_name);
96869798 const result = try gz.addExtendedPayloadSmall(.field_parent_ptr, 0, Zir.Inst.FieldParentPtr{
96879799 .src_node = gz.nodeIndexToRelative(node),
96889800 .parent_ptr_type = parent_ptr_type,
......@@ -9713,7 +9825,7 @@ fn builtinCall(
97139825 .elem_type = try typeExpr(gz, scope, params[0]),
97149826 .a = try expr(gz, scope, .{ .rl = .none }, params[1]),
97159827 .b = try expr(gz, scope, .{ .rl = .none }, params[2]),
9716 .mask = try comptimeExpr(gz, scope, .{ .rl = .none }, params[3]),
9828 .mask = try comptimeExpr(gz, scope, .{ .rl = .none }, params[3], .operand_shuffle_mask),
97179829 });
97189830 return rvalue(gz, ri, result, node);
97199831 },
......@@ -9739,7 +9851,7 @@ fn builtinCall(
97399851 },
97409852 .Vector => {
97419853 const result = try gz.addPlNode(.vector_type, node, Zir.Inst.Bin{
9742 .lhs = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]),
9854 .lhs = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .type),
97439855 .rhs = try typeExpr(gz, scope, params[1]),
97449856 });
97459857 return rvalue(gz, ri, result, node);
......@@ -9747,7 +9859,7 @@ fn builtinCall(
97479859 .prefetch => {
97489860 const prefetch_options_ty = try gz.addBuiltinValue(node, .prefetch_options);
97499861 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]);
9862 const options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = prefetch_options_ty } }, params[1], .prefetch_options);
97519863 _ = try gz.addExtendedPayload(.prefetch, Zir.Inst.BinNode{
97529864 .node = gz.nodeIndexToRelative(node),
97539865 .lhs = ptr,
......@@ -9785,7 +9897,7 @@ fn builtinCall(
97859897 },
97869898
97879899 .work_item_id => {
9788 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);
9900 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .work_group_dim_index);
97899901 const result = try gz.addExtendedPayload(.work_item_id, Zir.Inst.UnNode{
97909902 .node = gz.nodeIndexToRelative(node),
97919903 .operand = operand,
......@@ -9793,7 +9905,7 @@ fn builtinCall(
97939905 return rvalue(gz, ri, result, node);
97949906 },
97959907 .work_group_size => {
9796 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);
9908 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .work_group_dim_index);
97979909 const result = try gz.addExtendedPayload(.work_group_size, Zir.Inst.UnNode{
97989910 .node = gz.nodeIndexToRelative(node),
97999911 .operand = operand,
......@@ -9801,7 +9913,7 @@ fn builtinCall(
98019913 return rvalue(gz, ri, result, node);
98029914 },
98039915 .work_group_id => {
9804 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);
9916 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .work_group_dim_index);
98059917 const result = try gz.addExtendedPayload(.work_group_id, Zir.Inst.UnNode{
98069918 .node = gz.nodeIndexToRelative(node),
98079919 .operand = operand,
......@@ -9821,7 +9933,13 @@ fn hasDeclOrField(
98219933 tag: Zir.Inst.Tag,
98229934) InnerError!Zir.Inst.Ref {
98239935 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);
9936 const name = try comptimeExpr(
9937 gz,
9938 scope,
9939 .{ .rl = .{ .coerced_ty = .slice_const_u8_type } },
9940 rhs_node,
9941 if (tag == .has_decl) .decl_name else .field_name,
9942 );
98259943 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
98269944 .lhs = container_type,
98279945 .rhs = name,
......@@ -9874,7 +9992,7 @@ fn simpleUnOp(
98749992) InnerError!Zir.Inst.Ref {
98759993 const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
98769994 const operand = if (tag == .compile_error)
9877 try comptimeExpr(gz, scope, operand_ri, operand_node)
9995 try comptimeExpr(gz, scope, operand_ri, operand_node, .compile_error_string)
98789996 else
98799997 try expr(gz, scope, operand_ri, operand_node);
98809998 switch (tag) {
......@@ -9972,7 +10090,13 @@ fn simpleCBuiltin(
997210090) InnerError!Zir.Inst.Ref {
997310091 const name: []const u8 = if (tag == .c_undef) "C undef" else "C include";
997410092 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);
10093 const operand = try comptimeExpr(
10094 gz,
10095 scope,
10096 .{ .rl = .{ .coerced_ty = .slice_const_u8_type } },
10097 operand_node,
10098 if (tag == .c_undef) .operand_cUndef_macro_name else .operand_cInclude_file_name,
10099 );
997610100 _ = try gz.addExtendedPayload(tag, Zir.Inst.UnNode{
997710101 .node = gz.nodeIndexToRelative(node),
997810102 .operand = operand,
......@@ -9990,7 +10114,7 @@ fn offsetOf(
999010114 tag: Zir.Inst.Tag,
999110115) InnerError!Zir.Inst.Ref {
999210116 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);
10117 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, rhs_node, .field_name);
999410118 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
999510119 .lhs = type_inst,
999610120 .rhs = field_name,
......@@ -11996,11 +12120,16 @@ const GenZir = struct {
1199612120 }
1199712121
1199812122 /// Assumes nothing stacked on `gz`. Unstacks `gz`.
12123 /// Asserts `inst` is not a `block_comptime`.
1199912124 fn setBlockBody(gz: *GenZir, inst: Zir.Inst.Index) !void {
1200012125 const astgen = gz.astgen;
1200112126 const gpa = astgen.gpa;
1200212127 const body = gz.instructionsSlice();
1200312128 const body_len = astgen.countBodyLenAfterFixups(body);
12129
12130 const zir_tags = astgen.instructions.items(.tag);
12131 assert(zir_tags[@intFromEnum(inst)] != .block_comptime); // use `setComptimeBlockBody` instead
12132
1200412133 try astgen.extra.ensureUnusedCapacity(
1200512134 gpa,
1200612135 @typeInfo(Zir.Inst.Block).@"struct".fields.len + body_len,
......@@ -12013,6 +12142,32 @@ const GenZir = struct {
1201312142 gz.unstack();
1201412143 }
1201512144
12145 /// Assumes nothing stacked on `gz`. Unstacks `gz`.
12146 /// Asserts `inst` is a `block_comptime`.
12147 fn setBlockComptimeBody(gz: *GenZir, inst: Zir.Inst.Index, comptime_reason: std.zig.SimpleComptimeReason) !void {
12148 const astgen = gz.astgen;
12149 const gpa = astgen.gpa;
12150 const body = gz.instructionsSlice();
12151 const body_len = astgen.countBodyLenAfterFixups(body);
12152
12153 const zir_tags = astgen.instructions.items(.tag);
12154 assert(zir_tags[@intFromEnum(inst)] == .block_comptime); // use `setBlockBody` instead
12155
12156 try astgen.extra.ensureUnusedCapacity(
12157 gpa,
12158 @typeInfo(Zir.Inst.BlockComptime).@"struct".fields.len + body_len,
12159 );
12160 const zir_datas = astgen.instructions.items(.data);
12161 zir_datas[@intFromEnum(inst)].pl_node.payload_index = astgen.addExtraAssumeCapacity(
12162 Zir.Inst.BlockComptime{
12163 .reason = comptime_reason,
12164 .body_len = body_len,
12165 },
12166 );
12167 astgen.appendBodyWithFixups(body);
12168 gz.unstack();
12169 }
12170
1201612171 /// Assumes nothing stacked on `gz`. Unstacks `gz`.
1201712172 fn setTryBody(gz: *GenZir, inst: Zir.Inst.Index, operand: Zir.Inst.Ref) !void {
1201812173 const astgen = gz.astgen;
lib/std/zig/Zir.zig+26-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
......@@ -709,6 +711,12 @@ pub const Inst = struct {
709711 /// operator. Emit a compile error if not.
710712 /// Uses the `un_tok` union field. Token is the `&` operator. Operand is the type.
711713 validate_ref_ty,
714 /// Given a value, check whether it is a valid local constant in this scope.
715 /// In a runtime scope, this is always a nop.
716 /// In a comptime scope, raises a compile error if the value is runtime-known.
717 /// Result is always void.
718 /// Uses the `un_node` union field. Node is the initializer. Operand is the initializer value.
719 validate_const,
712720 /// Given a type `T`, construct the type `E!T`, where `E` is this function's error set, to be used
713721 /// as the result type of a `try` operand. Generic poison is propagated.
714722 /// Uses the `un_node` union field. Node is the `try` expression. Operand is the type `T`.
......@@ -1291,6 +1299,7 @@ pub const Inst = struct {
12911299 .array_init_elem_type,
12921300 .array_init_elem_ptr,
12931301 .validate_ref_ty,
1302 .validate_const,
12941303 .try_operand_ty,
12951304 .try_ref_operand_ty,
12961305 .restore_err_ret_index_unconditional,
......@@ -1351,6 +1360,7 @@ pub const Inst = struct {
13511360 .validate_array_init_result_ty,
13521361 .validate_ptr_array_init,
13531362 .validate_ref_ty,
1363 .validate_const,
13541364 .try_operand_ty,
13551365 .try_ref_operand_ty,
13561366 => true,
......@@ -1734,6 +1744,7 @@ pub const Inst = struct {
17341744 .opt_eu_base_ptr_init = .un_node,
17351745 .coerce_ptr_elem_ty = .pl_node,
17361746 .validate_ref_ty = .un_tok,
1747 .validate_const = .un_node,
17371748 .try_operand_ty = .un_node,
17381749 .try_ref_operand_ty = .un_node,
17391750
......@@ -2547,6 +2558,13 @@ pub const Inst = struct {
25472558 body_len: u32,
25482559 };
25492560
2561 /// Trailing:
2562 /// * inst: Index // for each `body_len`
2563 pub const BlockComptime = struct {
2564 reason: std.zig.SimpleComptimeReason,
2565 body_len: u32,
2566 };
2567
25502568 /// Trailing:
25512569 /// * inst: Index // for each `body_len`
25522570 pub const BoolBr = struct {
......@@ -4134,6 +4152,7 @@ fn findTrackableInner(
41344152 .opt_eu_base_ptr_init,
41354153 .coerce_ptr_elem_ty,
41364154 .validate_ref_ty,
4155 .validate_const,
41374156 .try_operand_ty,
41384157 .try_ref_operand_ty,
41394158 .struct_init_empty,
......@@ -4517,7 +4536,6 @@ fn findTrackableInner(
45174536 // Block instructions, recurse over the bodies.
45184537
45194538 .block,
4520 .block_comptime,
45214539 .block_inline,
45224540 .c_import,
45234541 .typeof_builtin,
......@@ -4528,6 +4546,12 @@ fn findTrackableInner(
45284546 const body = zir.bodySlice(extra.end, extra.data.body_len);
45294547 return zir.findTrackableBody(gpa, contents, defers, body);
45304548 },
4549 .block_comptime => {
4550 const inst_data = datas[@intFromEnum(inst)].pl_node;
4551 const extra = zir.extraData(Inst.BlockComptime, inst_data.payload_index);
4552 const body = zir.bodySlice(extra.end, extra.data.body_len);
4553 return zir.findTrackableBody(gpa, contents, defers, body);
4554 },
45314555 .condbr, .condbr_inline => {
45324556 const inst_data = datas[@intFromEnum(inst)].pl_node;
45334557 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+506-635
......@@ -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,83 @@ 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
848/// Represents the reason we are resolving a value or evaluating code at comptime.
849/// Most reasons are represented by a `std.zig.SimpleComptimeReason`, which provides a plain message.
850const ComptimeReason = union(enum) {
851 /// Evaluating at comptime for a reason in the `std.zig.SimpleComptimeReason` enum.
852 simple: std.zig.SimpleComptimeReason,
853
854 /// Evaluating at comptime because of a comptime-only type. This field is separate so that
855 /// the type in question can be included in the error message. AstGen could never emit this
856 /// reason, because it knows nothing of types.
857 /// The format string looks like "foo '{}' bar", where "{}" is the comptime-only type.
858 /// We will then explain why this type is comptime-only.
859 comptime_only: struct {
860 ty: Type,
861 msg: enum {
862 union_init,
863 struct_init,
864 tuple_init,
865 param_ty_arg,
866 ret_ty_call,
867 ret_ty_generic_call,
868 },
869 },
870
871 fn explain(reason: ComptimeReason, sema: *Sema, src: LazySrcLoc, err_msg: *Zcu.ErrorMsg) !void {
872 switch (reason) {
873 .simple => |simple| {
874 try sema.errNote(src, err_msg, "{s}", .{simple.message()});
875 },
876 .comptime_only => |co| {
877 const pre, const post = switch (co.msg) {
878 .union_init => .{ "initializer of comptime-only union", "must be comptime-known" },
879 .struct_init => .{ "initializer of comptime-only struct", "must be comptime-known" },
880 .tuple_init => .{ "initializer of comptime-only tuple", "must be comptime-known" },
881 .param_ty_arg => .{ "argument to parameter with comptime-only type", "must be comptime-known" },
882 .ret_ty_call => .{ "function with comptime-only return type", "is evaluated at comptime" },
883 .ret_ty_generic_call => .{ "generic function instantiated with comptime-only return type", "is evaluated at comptime" },
884 };
885 try sema.errNote(src, err_msg, "{s} '{}' {s}", .{ pre, co.ty.fmt(sema.pt), post });
886 try sema.explainWhyTypeIsComptime(err_msg, src, co.ty);
887 },
888 }
889 }
890};
891
892/// Represents the reason a `Block` is being evaluated at comptime.
893const BlockComptimeReason = union(enum) {
894 /// This block inherits being comptime-only from the `inlining` call site.
895 inlining_parent,
896
897 /// Comptime evaluation began somewhere in the current function for a given `ComptimeReason`.
898 reason: struct {
899 /// The source location which this reason originates from. `r` is reported here.
900 src: LazySrcLoc,
901 r: ComptimeReason,
902 },
863903};
864904
865905const LabeledBlock = struct {
......@@ -885,12 +925,6 @@ const InferredAlloc = struct {
885925 prongs: std.ArrayListUnmanaged(Air.Inst.Index) = .empty,
886926};
887927
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
894928pub fn deinit(sema: *Sema) void {
895929 const gpa = sema.gpa;
896930 sema.air_instructions.deinit(gpa);
......@@ -954,7 +988,7 @@ pub fn analyzeFnBody(
954988/// we are evaluating at comptime, semantically analyze the body and return the result from it.
955989/// Returns `null` if control flow did not break from this block, but instead terminated with some
956990/// 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`.
991/// return `error.ComptimeBreak`. If `block.isComptime()`, this function will never return `null`.
958992fn analyzeInlineBody(
959993 sema: *Sema,
960994 block: *Block,
......@@ -1003,7 +1037,7 @@ pub fn resolveInlineBody(
10031037/// If this function returns normally, the merges of `block` were populated with all possible
10041038/// (runtime) results of this block. Peer type resolution should be performed on the result,
10051039/// 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`.
1040/// `resolveAnalyzedBlock`. This form of return is impossible if `block.isComptime()`.
10071041///
10081042/// Alternatively, this function may return `error.ComptimeBreak`. This indicates that comptime
10091043/// control flow is happening, and we are breaking at comptime from a block indicated by the
......@@ -1340,7 +1374,7 @@ fn analyzeBodyInner(
13401374 continue;
13411375 },
13421376 .breakpoint => {
1343 if (!block.is_comptime) {
1377 if (!block.isComptime()) {
13441378 _ = try block.addNoOp(.breakpoint);
13451379 }
13461380 i += 1;
......@@ -1474,6 +1508,11 @@ fn analyzeBodyInner(
14741508 i += 1;
14751509 continue;
14761510 },
1511 .validate_const => {
1512 try sema.zirValidateConst(block, inst);
1513 i += 1;
1514 continue;
1515 },
14771516 .@"export" => {
14781517 try sema.zirExport(block, inst);
14791518 i += 1;
......@@ -1515,7 +1554,7 @@ fn analyzeBodyInner(
15151554 continue;
15161555 },
15171556 .check_comptime_control_flow => {
1518 if (!block.is_comptime) {
1557 if (!block.isComptime()) {
15191558 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
15201559 const src = block.nodeOffset(inst_data.src_node);
15211560 const inline_block = inst_data.operand.toIndex().?;
......@@ -1562,7 +1601,7 @@ fn analyzeBodyInner(
15621601
15631602 // Special case instructions to handle comptime control flow.
15641603 .@"break" => {
1565 if (block.is_comptime) {
1604 if (block.isComptime()) {
15661605 sema.comptime_break_inst = inst;
15671606 return error.ComptimeBreak;
15681607 } else {
......@@ -1575,7 +1614,7 @@ fn analyzeBodyInner(
15751614 return error.ComptimeBreak;
15761615 },
15771616 .repeat => {
1578 if (block.is_comptime) {
1617 if (block.isComptime()) {
15791618 // Send comptime control flow back to the beginning of this block.
15801619 const src = block.nodeOffset(datas[@intFromEnum(inst)].node);
15811620 try sema.emitBackwardBranch(block, src);
......@@ -1597,7 +1636,7 @@ fn analyzeBodyInner(
15971636 i = 0;
15981637 continue;
15991638 },
1600 .switch_continue => if (block.is_comptime) {
1639 .switch_continue => if (block.isComptime()) {
16011640 sema.comptime_break_inst = inst;
16021641 return error.ComptimeBreak;
16031642 } else {
......@@ -1605,17 +1644,40 @@ fn analyzeBodyInner(
16051644 break;
16061645 },
16071646
1608 .loop => if (block.is_comptime) {
1647 .loop => if (block.isComptime()) {
16091648 continue :inst .block_inline;
16101649 } else try sema.zirLoop(block, inst),
16111650
1612 .block => if (block.is_comptime) {
1651 .block => if (block.isComptime()) {
16131652 continue :inst .block_inline;
1614 } else try sema.zirBlock(block, inst, false),
1653 } else try sema.zirBlock(block, inst),
16151654
1616 .block_comptime => if (block.is_comptime) {
1617 continue :inst .block_inline;
1618 } else try sema.zirBlock(block, inst, true),
1655 .block_comptime => {
1656 const pl_node = datas[@intFromEnum(inst)].pl_node;
1657 const src = block.nodeOffset(pl_node.src_node);
1658 const extra = sema.code.extraData(Zir.Inst.BlockComptime, pl_node.payload_index);
1659 const block_body = sema.code.bodySlice(extra.end, extra.data.body_len);
1660
1661 if (block.isComptime()) {
1662 // No need for a sub-block; just resolve the other body directly!
1663 break :inst try sema.resolveInlineBody(block, block_body, inst);
1664 }
1665
1666 var child_block = block.makeSubBlock();
1667 defer child_block.instructions.deinit(sema.gpa);
1668 child_block.comptime_reason = .{ .reason = .{
1669 .src = src,
1670 .r = .{ .simple = extra.data.reason },
1671 } };
1672
1673 const result = try sema.resolveInlineBody(&child_block, block_body, inst);
1674
1675 if (!try sema.isComptimeKnown(result)) {
1676 return sema.failWithNeededComptime(&child_block, src, null);
1677 }
1678
1679 break :inst result;
1680 },
16191681
16201682 .block_inline => blk: {
16211683 // Directly analyze the block body without introducing a new block.
......@@ -1725,7 +1787,7 @@ fn analyzeBodyInner(
17251787 return error.ComptimeBreak;
17261788 }
17271789 },
1728 .condbr => if (block.is_comptime) {
1790 .condbr => if (block.isComptime()) {
17291791 continue :inst .condbr_inline;
17301792 } else {
17311793 try sema.zirCondbr(block, inst);
......@@ -1742,10 +1804,7 @@ fn analyzeBodyInner(
17421804 );
17431805 const uncasted_cond = try sema.resolveInst(extra.data.condition);
17441806 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 });
1807 const cond_val = try sema.resolveConstDefinedValue(block, cond_src, cond, null);
17491808 const inline_body = if (cond_val.toBool()) then_body else else_body;
17501809
17511810 try sema.maybeErrorUnwrapCondbr(block, inline_body, extra.data.condition, cond_src);
......@@ -1756,7 +1815,7 @@ fn analyzeBodyInner(
17561815 break :inst result;
17571816 },
17581817 .@"try" => blk: {
1759 if (!block.is_comptime) break :blk try sema.zirTry(block, inst);
1818 if (!block.isComptime()) break :blk try sema.zirTry(block, inst);
17601819 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
17611820 const src = block.nodeOffset(inst_data.src_node);
17621821 const operand_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node });
......@@ -1771,10 +1830,7 @@ fn analyzeBodyInner(
17711830 }
17721831 const is_non_err = try sema.analyzeIsNonErrComptimeOnly(block, operand_src, err_union);
17731832 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 });
1833 const is_non_err_val = try sema.resolveConstDefinedValue(block, operand_src, is_non_err, null);
17781834 if (is_non_err_val.toBool()) {
17791835 break :blk try sema.analyzeErrUnionPayload(block, src, err_union_ty, err_union, operand_src, false);
17801836 }
......@@ -1782,7 +1838,7 @@ fn analyzeBodyInner(
17821838 break :blk result;
17831839 },
17841840 .try_ptr => blk: {
1785 if (!block.is_comptime) break :blk try sema.zirTryPtr(block, inst);
1841 if (!block.isComptime()) break :blk try sema.zirTryPtr(block, inst);
17861842 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
17871843 const src = block.nodeOffset(inst_data.src_node);
17881844 const operand_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node });
......@@ -1792,10 +1848,7 @@ fn analyzeBodyInner(
17921848 const err_union = try sema.analyzeLoad(block, src, operand, operand_src);
17931849 const is_non_err = try sema.analyzeIsNonErrComptimeOnly(block, operand_src, err_union);
17941850 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 });
1851 const is_non_err_val = try sema.resolveConstDefinedValue(block, operand_src, is_non_err, null);
17991852 if (is_non_err_val.toBool()) {
18001853 break :blk try sema.analyzeErrUnionPayloadPtr(block, src, operand, false, false);
18011854 }
......@@ -1873,7 +1926,7 @@ fn resolveConstBool(
18731926 block: *Block,
18741927 src: LazySrcLoc,
18751928 zir_ref: Zir.Inst.Ref,
1876 reason: NeededComptimeReason,
1929 reason: ComptimeReason,
18771930) !bool {
18781931 const air_inst = try sema.resolveInst(zir_ref);
18791932 const wanted_type = Type.bool;
......@@ -1887,7 +1940,7 @@ fn resolveConstString(
18871940 block: *Block,
18881941 src: LazySrcLoc,
18891942 zir_ref: Zir.Inst.Ref,
1890 reason: NeededComptimeReason,
1943 reason: ComptimeReason,
18911944) ![]u8 {
18921945 const air_inst = try sema.resolveInst(zir_ref);
18931946 return sema.toConstString(block, src, air_inst, reason);
......@@ -1898,7 +1951,7 @@ pub fn toConstString(
18981951 block: *Block,
18991952 src: LazySrcLoc,
19001953 air_inst: Air.Inst.Ref,
1901 reason: NeededComptimeReason,
1954 reason: ComptimeReason,
19021955) ![]u8 {
19031956 const pt = sema.pt;
19041957 const coerced_inst = try sema.coerce(block, Type.slice_const_u8, air_inst, src);
......@@ -1912,7 +1965,7 @@ pub fn resolveConstStringIntern(
19121965 block: *Block,
19131966 src: LazySrcLoc,
19141967 zir_ref: Zir.Inst.Ref,
1915 reason: NeededComptimeReason,
1968 reason: ComptimeReason,
19161969) !InternPool.NullTerminatedString {
19171970 const air_inst = try sema.resolveInst(zir_ref);
19181971 const wanted_type = Type.slice_const_u8;
......@@ -2063,9 +2116,7 @@ fn analyzeAsType(
20632116) !Type {
20642117 const wanted_type = Type.type;
20652118 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 });
2119 const val = try sema.resolveConstDefinedValue(block, src, coerced_inst, .{ .simple = .type });
20692120 return val.toType();
20702121}
20712122
......@@ -2077,7 +2128,7 @@ pub fn setupErrorReturnTrace(sema: *Sema, block: *Block, last_arg_index: usize)
20772128 const ip = &zcu.intern_pool;
20782129 if (!comp.config.any_error_tracing) return;
20792130
2080 assert(!block.is_comptime);
2131 assert(!block.isComptime());
20812132 var err_trace_block = block.makeSubBlock();
20822133 defer err_trace_block.instructions.deinit(gpa);
20832134
......@@ -2148,7 +2199,7 @@ fn resolveConstValue(
21482199 block: *Block,
21492200 src: LazySrcLoc,
21502201 inst: Air.Inst.Ref,
2151 reason: NeededComptimeReason,
2202 reason: ?ComptimeReason,
21522203) CompileError!Value {
21532204 return try sema.resolveValue(inst) orelse {
21542205 return sema.failWithNeededComptime(block, src, reason);
......@@ -2177,7 +2228,7 @@ fn resolveConstDefinedValue(
21772228 block: *Block,
21782229 src: LazySrcLoc,
21792230 air_ref: Air.Inst.Ref,
2180 reason: NeededComptimeReason,
2231 reason: ?ComptimeReason,
21812232) CompileError!Value {
21822233 const val = try sema.resolveConstValue(block, src, air_ref, reason);
21832234 if (val.isUndef(sema.pt.zcu)) return sema.failWithUseOfUndef(block, src);
......@@ -2217,15 +2268,16 @@ pub fn resolveFinalDeclValue(
22172268 const val: Value = .fromInterned(ip_index);
22182269 break :rt_ptr val.isPtrRuntimeValue(zcu);
22192270 };
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;
22242271
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 });
2272 switch (sema.failWithNeededComptime(block, src, .{ .simple = .container_var_init })) {
2273 error.AnalysisFail => |e| {
2274 if (sema.err != null and is_runtime_ptr) {
2275 try sema.errNote(src, sema.err.?, "threadlocal and dll imported variables have runtime-known addresses", .{});
2276 }
2277 return e;
2278 },
2279 else => |e| return e,
2280 }
22292281 };
22302282
22312283 if (val.canMutateComptimeVarState(zcu)) {
......@@ -2235,21 +2287,19 @@ pub fn resolveFinalDeclValue(
22352287 return val;
22362288}
22372289
2238fn failWithNeededComptime(sema: *Sema, block: *Block, src: LazySrcLoc, reason: NeededComptimeReason) CompileError {
2239 const msg = msg: {
2290fn failWithNeededComptime(sema: *Sema, block: *Block, src: LazySrcLoc, reason: ?ComptimeReason) CompileError {
2291 const msg, const fail_block = msg: {
22402292 const msg = try sema.errMsg(src, "unable to resolve comptime value", .{});
22412293 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;
2294 const fail_block = if (reason) |r| b: {
2295 try r.explain(sema, src, msg);
2296 break :b block;
2297 } else b: {
2298 break :b try block.explainWhyBlockIsComptime(msg);
2299 };
2300 break :msg .{ msg, fail_block };
22512301 };
2252 return sema.failWithOwnedErrorMsg(block, msg);
2302 return sema.failWithOwnedErrorMsg(fail_block, msg);
22532303}
22542304
22552305fn failWithUseOfUndef(sema: *Sema, block: *Block, src: LazySrcLoc) CompileError {
......@@ -2578,9 +2628,7 @@ pub fn analyzeAsAlign(
25782628 src: LazySrcLoc,
25792629 air_ref: Air.Inst.Ref,
25802630) !Alignment {
2581 const alignment_big = try sema.analyzeAsInt(block, src, air_ref, align_ty, .{
2582 .needed_comptime_reason = "alignment must be comptime-known",
2583 });
2631 const alignment_big = try sema.analyzeAsInt(block, src, air_ref, align_ty, .{ .simple = .@"align" });
25842632 return sema.validateAlign(block, src, alignment_big);
25852633}
25862634
......@@ -2615,7 +2663,7 @@ fn resolveInt(
26152663 src: LazySrcLoc,
26162664 zir_ref: Zir.Inst.Ref,
26172665 dest_ty: Type,
2618 reason: NeededComptimeReason,
2666 reason: ComptimeReason,
26192667) !u64 {
26202668 const air_ref = try sema.resolveInst(zir_ref);
26212669 return sema.analyzeAsInt(block, src, air_ref, dest_ty, reason);
......@@ -2627,7 +2675,7 @@ fn analyzeAsInt(
26272675 src: LazySrcLoc,
26282676 air_ref: Air.Inst.Ref,
26292677 dest_ty: Type,
2630 reason: NeededComptimeReason,
2678 reason: ComptimeReason,
26312679) !u64 {
26322680 const coerced = try sema.coerce(block, dest_ty, air_ref, src);
26332681 const val = try sema.resolveConstDefinedValue(block, src, coerced, reason);
......@@ -2687,9 +2735,7 @@ fn zirTupleDecl(
26872735 if (zir_field_init != .none) {
26882736 const uncoerced_field_init = try sema.resolveInst(zir_field_init);
26892737 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 });
2738 const field_init_val = try sema.resolveConstDefinedValue(block, init_src, coerced_field_init, .{ .simple = .tuple_field_default_value });
26932739 if (field_init_val.canMutateComptimeVarState(zcu)) {
26942740 return sema.fail(block, init_src, "field default value contains reference to comptime-mutable memory", .{});
26952741 }
......@@ -3414,7 +3460,7 @@ fn zirRetPtr(sema: *Sema, block: *Block) CompileError!Air.Inst.Ref {
34143460
34153461 const pt = sema.pt;
34163462
3417 if (block.is_comptime or try sema.fn_ret_ty.comptimeOnlySema(pt)) {
3463 if (block.isComptime() or try sema.fn_ret_ty.comptimeOnlySema(pt)) {
34183464 try sema.fn_ret_ty.resolveFields(pt);
34193465 return sema.analyzeComptimeAlloc(block, sema.fn_ret_ty, .none);
34203466 }
......@@ -3608,7 +3654,7 @@ fn zirAllocExtended(
36083654 break :blk try sema.resolveAlign(block, align_src, align_ref);
36093655 } else .none;
36103656
3611 if (block.is_comptime or small.is_comptime) {
3657 if (block.isComptime() or small.is_comptime) {
36123658 if (small.has_type) {
36133659 return sema.analyzeComptimeAlloc(block, var_ty, alignment);
36143660 } else {
......@@ -4075,7 +4121,7 @@ fn zirAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
40754121 const ty_src = block.src(.{ .node_offset_var_decl_ty = inst_data.src_node });
40764122
40774123 const var_ty = try sema.resolveType(block, ty_src, inst_data.operand);
4078 if (block.is_comptime or try var_ty.comptimeOnlySema(pt)) {
4124 if (block.isComptime() or try var_ty.comptimeOnlySema(pt)) {
40794125 return sema.analyzeComptimeAlloc(block, var_ty, .none);
40804126 }
40814127 if (sema.func_is_naked and try var_ty.hasRuntimeBitsSema(pt)) {
......@@ -4103,7 +4149,7 @@ fn zirAllocMut(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
41034149 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
41044150 const ty_src = block.src(.{ .node_offset_var_decl_ty = inst_data.src_node });
41054151 const var_ty = try sema.resolveType(block, ty_src, inst_data.operand);
4106 if (block.is_comptime) {
4152 if (block.isComptime()) {
41074153 return sema.analyzeComptimeAlloc(block, var_ty, .none);
41084154 }
41094155 if (sema.func_is_naked and try var_ty.hasRuntimeBitsSema(pt)) {
......@@ -4129,7 +4175,7 @@ fn zirAllocInferred(
41294175
41304176 const gpa = sema.gpa;
41314177
4132 if (block.is_comptime) {
4178 if (block.isComptime()) {
41334179 try sema.air_instructions.append(gpa, .{
41344180 .tag = .inferred_alloc_comptime,
41354181 .data = .{ .inferred_alloc_comptime = .{
......@@ -4579,6 +4625,17 @@ fn zirValidateRefTy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
45794625 }
45804626}
45814627
4628fn zirValidateConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
4629 if (!block.isComptime()) return;
4630
4631 const un_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
4632 const src = block.nodeOffset(un_node.src_node);
4633 const init_ref = try sema.resolveInst(un_node.operand);
4634 if (!try sema.isComptimeKnown(init_ref)) {
4635 return sema.failWithNeededComptime(block, src, null);
4636 }
4637}
4638
45824639fn zirValidateArrayInitRefTy(
45834640 sema: *Sema,
45844641 block: *Block,
......@@ -4778,7 +4835,7 @@ fn validateUnionInit(
47784835 return sema.failWithOwnedErrorMsg(block, msg);
47794836 }
47804837
4781 if (block.is_comptime and
4838 if (block.isComptime() and
47824839 (try sema.resolveDefinedValue(block, init_src, union_ptr)) != null)
47834840 {
47844841 // In this case, comptime machinery already did everything. No work to do here.
......@@ -4897,9 +4954,11 @@ fn validateUnionInit(
48974954 try sema.storePtr2(block, init_src, union_ptr, init_src, union_init, init_src, .store);
48984955 return;
48994956 } 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 });
4957 const src = block.nodeOffset(field_ptr_data.src_node);
4958 return sema.failWithNeededComptime(block, src, .{ .comptime_only = .{
4959 .ty = union_ty,
4960 .msg = .union_init,
4961 } });
49034962 }
49044963 if (init_ref) |v| try sema.validateRuntimeValue(block, block.nodeOffset(field_ptr_data.src_node), v);
49054964
......@@ -4953,7 +5012,7 @@ fn validateStructInit(
49535012 errdefer if (root_msg) |msg| msg.destroy(sema.gpa);
49545013
49555014 const struct_ptr = try sema.resolveInst(struct_ptr_zir_ref);
4956 if (block.is_comptime and
5015 if (block.isComptime() and
49575016 (try sema.resolveDefinedValue(block, init_src, struct_ptr)) != null)
49585017 {
49595018 try struct_ty.resolveLayout(pt);
......@@ -5081,9 +5140,11 @@ fn validateStructInit(
50815140 field_values[i] = val.toIntern();
50825141 } else if (require_comptime) {
50835142 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 });
5143 const src = block.nodeOffset(field_ptr_data.src_node);
5144 return sema.failWithNeededComptime(block, src, .{ .comptime_only = .{
5145 .ty = struct_ty,
5146 .msg = .struct_init,
5147 } });
50875148 } else {
50885149 struct_is_comptime = false;
50895150 }
......@@ -5253,7 +5314,7 @@ fn zirValidatePtrArrayInit(
52535314 else => unreachable,
52545315 };
52555316
5256 if (block.is_comptime and
5317 if (block.isComptime() and
52575318 (try sema.resolveDefinedValue(block, init_src, array_ptr)) != null)
52585319 {
52595320 // In this case the comptime machinery will have evaluated the store instructions
......@@ -5629,9 +5690,7 @@ fn storeToInferredAllocComptime(
56295690 // There will be only one store_to_inferred_ptr because we are running at comptime.
56305691 // The alloc will turn into a Decl or a ComptimeAlloc.
56315692 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 });
5693 return sema.failWithNeededComptime(block, src, .{ .simple = .stored_to_comptime_var });
56355694 };
56365695 const alloc_ty = try pt.ptrTypeSema(.{
56375696 .child = operand_ty.toIntern(),
......@@ -5663,9 +5722,7 @@ fn storeToInferredAllocComptime(
56635722fn zirSetEvalBranchQuota(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
56645723 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
56655724 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 }));
5725 const quota: u32 = @intCast(try sema.resolveInt(block, src, inst_data.operand, .u32, .{ .simple = .operand_setEvalBranchQuota }));
56695726 sema.branch_quota = @max(sema.branch_quota, quota);
56705727 sema.allow_memoize = false;
56715728}
......@@ -5794,9 +5851,7 @@ fn zirCompileError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
57945851 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
57955852 const src = block.nodeOffset(inst_data.src_node);
57965853 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 });
5854 const msg = try sema.resolveConstString(block, operand_src, inst_data.operand, .{ .simple = .compile_error_string });
58005855 return sema.fail(block, src, "{s}", .{msg});
58015856}
58025857
......@@ -5848,7 +5903,7 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
58485903 // source location if we do it here.
58495904 const coerced_msg = try sema.coerce(block, Type.slice_const_u8, msg_inst, block.builtinCallArgSrc(inst_data.src_node, 0));
58505905
5851 if (block.is_comptime) {
5906 if (block.isComptime()) {
58525907 return sema.fail(block, src, "encountered @panic at comptime", .{});
58535908 }
58545909
......@@ -5864,7 +5919,7 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
58645919fn zirTrap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
58655920 const src_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].node;
58665921 const src = block.nodeOffset(src_node);
5867 if (block.is_comptime)
5922 if (block.isComptime())
58685923 return sema.fail(block, src, "encountered @trap at comptime", .{});
58695924 _ = try block.addNoOp(.trap);
58705925}
......@@ -5974,15 +6029,16 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
59746029 var c_import_buf = std.ArrayList(u8).init(gpa);
59756030 defer c_import_buf.deinit();
59766031
5977 const comptime_reason: Block.ComptimeReason = .{ .c_import = .{ .src = src } };
59786032 var child_block: Block = .{
59796033 .parent = parent_block,
59806034 .sema = sema,
59816035 .namespace = parent_block.namespace,
59826036 .instructions = .{},
59836037 .inlining = parent_block.inlining,
5984 .is_comptime = true,
5985 .comptime_reason = &comptime_reason,
6038 .comptime_reason = .{ .reason = .{
6039 .src = src,
6040 .r = .{ .simple = .operand_cImport },
6041 } },
59866042 .c_import_buf = &c_import_buf,
59876043 .runtime_cond = parent_block.runtime_cond,
59886044 .runtime_loop = parent_block.runtime_loop,
......@@ -6073,7 +6129,7 @@ fn zirSuspendBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) Comp
60736129 return sema.failWithUseOfAsync(parent_block, src);
60746130}
60756131
6076fn zirBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index, force_comptime: bool) CompileError!Air.Inst.Ref {
6132fn zirBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
60776133 const tracy = trace(@src());
60786134 defer tracy.end();
60796135
......@@ -6109,7 +6165,6 @@ fn zirBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index, force_compt
61096165 .instructions = .{},
61106166 .label = &label,
61116167 .inlining = parent_block.inlining,
6112 .is_comptime = parent_block.is_comptime or force_comptime,
61136168 .comptime_reason = parent_block.comptime_reason,
61146169 .is_typeof = parent_block.is_typeof,
61156170 .want_safety = parent_block.want_safety,
......@@ -6143,7 +6198,7 @@ fn resolveBlockBody(
61436198 body_inst: Zir.Inst.Index,
61446199 merges: *Block.Merges,
61456200) CompileError!Air.Inst.Ref {
6146 if (child_block.is_comptime) {
6201 if (child_block.isComptime()) {
61476202 return sema.resolveInlineBody(child_block, body, body_inst);
61486203 } else {
61496204 assert(sema.air_instructions.items(.tag)[@intFromEnum(merges.block_inst)] == .block);
......@@ -6303,7 +6358,7 @@ fn resolveAnalyzedBlock(
63036358 }
63046359 }
63056360 // 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.
6361 assert(!child_block.isComptime()); // Should already got a compile error in the condbr condition.
63076362
63086363 // Note that we'll always create an AIR block here, so `need_debug_scope` is irrelevant.
63096364
......@@ -6425,9 +6480,7 @@ fn zirExport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
64256480 const options_src = block.builtinCallArgSrc(inst_data.src_node, 1);
64266481
64276482 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 });
6483 const ptr_val = try sema.resolveConstDefinedValue(block, ptr_src, ptr, .{ .simple = .export_target });
64316484 const ptr_ty = ptr_val.typeOf(zcu);
64326485
64336486 const options = try sema.resolveExportOptions(block, options_src, extra.options);
......@@ -6553,17 +6606,13 @@ fn zirDisableInstrumentation(sema: *Sema) CompileError!void {
65536606fn zirSetFloatMode(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void {
65546607 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
65556608 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 });
6609 block.float_mode = try sema.resolveBuiltinEnum(block, src, extra.operand, "FloatMode", .{ .simple = .operand_setFloatMode });
65596610}
65606611
65616612fn zirSetRuntimeSafety(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
65626613 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
65636614 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 });
6615 block.want_safety = try sema.resolveConstBool(block, operand_src, inst_data.operand, .{ .simple = .operand_setRuntimeSafety });
65676616}
65686617
65696618fn zirBreak(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index) CompileError!void {
......@@ -6650,7 +6699,7 @@ fn zirSwitchContinue(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index) Com
66506699}
66516700
66526701fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
6653 if (block.is_comptime or block.ownerModule().strip) return;
6702 if (block.isComptime() or block.ownerModule().strip) return;
66546703
66556704 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;
66566705
......@@ -6676,7 +6725,7 @@ fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi
66766725}
66776726
66786727fn zirDbgEmptyStmt(_: *Sema, block: *Block, _: Zir.Inst.Index) CompileError!void {
6679 if (block.is_comptime or block.ownerModule().strip) return;
6728 if (block.isComptime() or block.ownerModule().strip) return;
66806729 _ = try block.addNoOp(.dbg_empty_stmt);
66816730}
66826731
......@@ -6699,7 +6748,7 @@ fn addDbgVar(
66996748 air_tag: Air.Inst.Tag,
67006749 name: []const u8,
67016750) CompileError!void {
6702 if (block.is_comptime or block.ownerModule().strip) return;
6751 if (block.isComptime() or block.ownerModule().strip) return;
67036752
67046753 const pt = sema.pt;
67056754 const zcu = pt.zcu;
......@@ -6931,7 +6980,7 @@ pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref
69316980 const zcu = pt.zcu;
69326981 const gpa = sema.gpa;
69336982
6934 if (block.is_comptime or block.is_typeof) {
6983 if (block.isComptime() or block.is_typeof) {
69356984 const index_val = try pt.intValue_u64(Type.usize, sema.comptime_err_ret_trace.items.len);
69366985 return Air.internedToRef(index_val.toIntern());
69376986 }
......@@ -7134,7 +7183,7 @@ fn zirCall(
71347183 }
71357184
71367185 if (block.ownerModule().error_tracing and
7137 !block.is_comptime and !block.is_typeof and (input_is_error or pop_error_return_trace))
7186 !block.isComptime() and !block.is_typeof and (input_is_error or pop_error_return_trace))
71387187 {
71397188 const return_ty = sema.typeOf(call_inst);
71407189 if (modifier != .always_tail and return_ty.isNoReturn(zcu))
......@@ -7404,12 +7453,14 @@ const CallArgsInfo = union(enum) {
74047453 };
74057454
74067455 // Generate args to comptime params in comptime block
7407 const parent_comptime = block.is_comptime;
7408 defer block.is_comptime = parent_comptime;
7456 const parent_comptime = block.comptime_reason;
7457 defer block.comptime_reason = parent_comptime;
74097458 // Note that we are indexing into parameters, not arguments, so use `arg_index` instead of `real_arg_idx`
74107459 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
7460 block.comptime_reason = .{ .reason = .{
7461 .src = cai.argSrc(block, arg_index),
7462 .r = .{ .simple = .comptime_param_arg },
7463 } };
74137464 }
74147465 // Give the arg its result type
74157466 const provide_param_ty = if (maybe_param_ty) |t| t else Type.generic_poison;
......@@ -7611,23 +7662,37 @@ fn analyzeCall(
76117662
76127663 const gpa = sema.gpa;
76137664
7665 const func_ret_ty_src: LazySrcLoc = if (try sema.funcDeclSrcInst(func)) |fn_decl_inst| .{
7666 .base_node_inst = fn_decl_inst,
7667 .offset = .{ .node_offset_fn_type_ret_ty = 0 },
7668 } else func_src;
7669
7670 // If this is not `null`, the call is comptime.
7671 var comptime_call_reason: ?BlockComptimeReason = cr: {
7672 if (block.comptime_reason) |r| break :cr r;
7673 if (modifier == .compile_time) break :cr .{ .reason = .{
7674 .src = call_src,
7675 .r = .{ .simple = .comptime_call_modifier },
7676 } };
7677 break :cr null;
7678 };
7679
76147680 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) {
7681 var is_inline_call = comptime_call_reason != null or modifier == .always_inline or func_ty_info.cc == .@"inline";
7682 if (!is_inline_call) {
76197683 if (try Type.fromInterned(func_ty_info.return_type).comptimeOnlySema(pt)) {
7620 is_comptime_call = true;
76217684 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),
7685 comptime_call_reason = .{ .reason = .{
7686 .src = func_ret_ty_src,
7687 .r = .{ .comptime_only = .{
7688 .ty = .fromInterned(func_ty_info.return_type),
7689 .msg = .ret_ty_call,
7690 } },
76267691 } };
76277692 }
76287693 }
76297694
7630 if (sema.func_is_naked and !is_inline_call and !is_comptime_call) {
7695 if (sema.func_is_naked and !is_inline_call) {
76317696 const msg = msg: {
76327697 const msg = try sema.errMsg(call_src, "runtime {s} not allowed in naked function", .{@tagName(operation)});
76337698 errdefer msg.destroy(sema.gpa);
......@@ -7642,6 +7707,7 @@ fn analyzeCall(
76427707 }
76437708
76447709 if (!is_inline_call and is_generic_call) {
7710 var comptime_ret_ty: Type = undefined;
76457711 if (sema.instantiateGenericCall(
76467712 block,
76477713 func,
......@@ -7651,6 +7717,7 @@ fn analyzeCall(
76517717 args_info,
76527718 call_tag,
76537719 call_dbg_node,
7720 &comptime_ret_ty,
76547721 )) |some| {
76557722 return some;
76567723 } else |err| switch (err) {
......@@ -7659,26 +7726,34 @@ fn analyzeCall(
76597726 },
76607727 error.ComptimeReturn => {
76617728 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),
7729 comptime_call_reason = .{ .reason = .{
7730 .src = func_ret_ty_src,
7731 .r = .{
7732 .comptime_only = .{
7733 .ty = comptime_ret_ty,
7734 .msg = .ret_ty_generic_call,
7735 },
7736 },
76677737 } };
76687738 },
76697739 else => |e| return e,
76707740 }
76717741 }
76727742
7743 const is_comptime_call = comptime_call_reason != null;
7744 // `comptime_call_reason` shouldn't be mutated again
7745 defer assert(is_comptime_call == (comptime_call_reason != null));
7746
76737747 if (is_comptime_call and modifier == .never_inline) {
76747748 return sema.fail(block, call_src, "unable to perform 'never_inline' call at compile-time", .{});
76757749 }
76767750
76777751 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 });
7752 const old_comptime_reason = block.comptime_reason;
7753 block.comptime_reason = comptime_call_reason;
7754 defer block.comptime_reason = old_comptime_reason;
7755
7756 const func_val = try sema.resolveConstDefinedValue(block, func_src, func, .{ .simple = .comptime_call_target });
76827757 const module_fn_index = switch (zcu.intern_pool.indexToKey(func_val.toIntern())) {
76837758 .@"extern" => return sema.fail(block, call_src, "{s} call of extern function", .{
76847759 @as([]const u8, if (is_comptime_call) "comptime" else "inline"),
......@@ -7767,8 +7842,7 @@ fn analyzeCall(
77677842 .label = null,
77687843 .inlining = &inlining,
77697844 .is_typeof = block.is_typeof,
7770 .is_comptime = is_comptime_call,
7771 .comptime_reason = comptime_reason,
7845 .comptime_reason = if (is_comptime_call) .inlining_parent else null,
77727846 .error_return_trace_index = block.error_return_trace_index,
77737847 .runtime_cond = block.runtime_cond,
77747848 .runtime_loop = block.runtime_loop,
......@@ -7857,11 +7931,16 @@ fn analyzeCall(
78577931 // on parameters, we must now do the same for the return type as we just did with
78587932 // each of the parameters, resolving the return type and providing it to the child
78597933 // `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);
78647934 const ret_ty_src: LazySrcLoc = .{ .base_node_inst = module_fn.zir_body_inst, .offset = .{ .node_offset_fn_type_ret_ty = 0 } };
7935 const ret_ty_inst = if (fn_info.ret_ty_body.len != 0) r: {
7936 const old_child_comptime_reason = child_block.comptime_reason;
7937 defer child_block.comptime_reason = old_child_comptime_reason;
7938 child_block.comptime_reason = .{ .reason = .{
7939 .src = ret_ty_src,
7940 .r = .{ .simple = .function_ret_ty },
7941 } };
7942 break :r try sema.resolveInlineBody(&child_block, fn_info.ret_ty_body, module_fn.zir_body_inst.resolve(ip) orelse return error.AnalysisFail);
7943 } else try sema.resolveInst(fn_info.ret_ty_ref);
78657944 sema.fn_ret_ty = try sema.analyzeAsType(&child_block, ret_ty_src, ret_ty_inst);
78667945 if (module_fn.analysisUnordered(ip).inferred_error_set) {
78677946 // Create a fresh inferred error set type for inline/comptime calls.
......@@ -8136,23 +8215,18 @@ fn analyzeInlineCallArg(
81368215 return casted_arg;
81378216 }
81388217 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 });
8218 if (zir_tags[@intFromEnum(inst)] == .param_comptime) {
8219 _ = try ics.caller().resolveConstValue(arg_block, arg_src, casted_arg, .{ .simple = .comptime_param_arg });
8220 } else if (!is_comptime_call and try Type.fromInterned(param_ty).comptimeOnlySema(ics.callee().pt)) {
8221 _ = try ics.caller().resolveConstValue(arg_block, arg_src, casted_arg, .{ .comptime_only = .{
8222 .ty = .fromInterned(param_ty),
8223 .msg = .param_ty_arg,
8224 } });
81488225 }
81498226
81508227 if (is_comptime_call) {
81518228 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 });
8229 const arg_val = try ics.caller().resolveConstValue(arg_block, arg_src, casted_arg, null);
81568230 switch (arg_val.toIntern()) {
81578231 .generic_poison, .generic_poison_type => {
81588232 // This function is currently evaluated as part of an as-of-yet unresolvable
......@@ -8188,10 +8262,7 @@ fn analyzeInlineCallArg(
81888262
81898263 if (is_comptime_call) {
81908264 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 });
8265 const arg_val = try ics.caller().resolveConstValue(arg_block, arg_src, uncasted_arg, null);
81958266 switch (arg_val.toIntern()) {
81968267 .generic_poison, .generic_poison_type => {
81978268 // This function is currently evaluated as part of an as-of-yet unresolvable
......@@ -8208,9 +8279,7 @@ fn analyzeInlineCallArg(
82088279 memoized_arg_values[arg_i.*] = resolved_arg_val.toIntern();
82098280 } else {
82108281 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 });
8282 _ = try ics.caller().resolveConstValue(arg_block, arg_src, uncasted_arg, .{ .simple = .comptime_param_arg });
82148283 }
82158284 ics.callee().inst_map.putAssumeCapacityNoClobber(inst, uncasted_arg);
82168285 }
......@@ -8237,15 +8306,18 @@ fn instantiateGenericCall(
82378306 args_info: CallArgsInfo,
82388307 call_tag: Air.Inst.Tag,
82398308 call_dbg_node: ?Zir.Inst.Index,
8309 /// Populated when `error.ComptimeReturn` is returned.
8310 comptime_ret_ty: *Type,
82408311) CompileError!Air.Inst.Ref {
82418312 const pt = sema.pt;
82428313 const zcu = pt.zcu;
82438314 const gpa = sema.gpa;
82448315 const ip = &zcu.intern_pool;
82458316
8246 const func_val = try sema.resolveConstDefinedValue(block, func_src, func, .{
8247 .needed_comptime_reason = "generic function being called must be comptime-known",
8248 });
8317 // Generic function pointers are comptime-only types, so `func` is definitely comptime-known.
8318 const func_val = (sema.resolveValue(func) catch unreachable).?;
8319 if (func_val.isUndef(zcu)) return sema.failWithUseOfUndef(block, func_src);
8320
82498321 const generic_owner = switch (zcu.intern_pool.indexToKey(func_val.toIntern())) {
82508322 .func => func_val.toIntern(),
82518323 .ptr => |ptr| ip.getNav(ptr.base_addr.nav).status.fully_resolved.val,
......@@ -8310,7 +8382,7 @@ fn instantiateGenericCall(
83108382 .namespace = fn_nav.analysis.?.namespace,
83118383 .instructions = .{},
83128384 .inlining = null,
8313 .is_comptime = true,
8385 .comptime_reason = undefined, // set as needed
83148386 .src_base_inst = fn_nav.analysis.?.zir_index,
83158387 .type_name_ctx = fn_nav.fqn,
83168388 };
......@@ -8354,12 +8426,13 @@ fn instantiateGenericCall(
83548426 child_sema.generic_call_src = prev_generic_call_src;
83558427 }
83568428
8429 const param_ty_src = child_block.tokenOffset(param_data.src_tok);
8430 child_block.comptime_reason = .{ .reason = .{
8431 .src = param_ty_src,
8432 .r = .{ .simple = .type },
8433 } };
83578434 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 );
8435 break :param_ty try child_sema.analyzeAsType(&child_block, param_ty_src, param_ty_inst);
83638436 },
83648437 else => unreachable,
83658438 }
......@@ -8452,6 +8525,10 @@ fn instantiateGenericCall(
84528525
84538526 // We've already handled parameters, so don't resolve the whole body. Instead, just
84548527 // do the instructions after the params (i.e. the func itself).
8528 child_block.comptime_reason = .{ .reason = .{
8529 .src = call_src,
8530 .r = .{ .simple = .type },
8531 } };
84558532 const new_func_inst = try child_sema.resolveInlineBody(&child_block, fn_info.param_body[args_info.count()..], fn_info.param_body_inst);
84568533 const callee_index = (child_sema.resolveConstDefinedValue(&child_block, LazySrcLoc.unneeded, new_func_inst, undefined) catch unreachable).toIntern();
84578534
......@@ -8465,6 +8542,7 @@ fn instantiateGenericCall(
84658542 // If the call evaluated to a return type that requires comptime, never mind
84668543 // our generic instantiation. Instead we need to perform a comptime call.
84678544 if (try Type.fromInterned(func_ty_info.return_type).comptimeOnlySema(pt)) {
8545 comptime_ret_ty.* = .fromInterned(func_ty_info.return_type);
84688546 return error.ComptimeReturn;
84698547 }
84708548 // Similarly, if the call evaluated to a generic type we need to instead
......@@ -8622,9 +8700,7 @@ fn zirVectorType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
86228700 const len_src = block.builtinCallArgSrc(inst_data.src_node, 0);
86238701 const elem_type_src = block.builtinCallArgSrc(inst_data.src_node, 1);
86248702 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 }));
8703 const len: u32 = @intCast(try sema.resolveInt(block, len_src, extra.lhs, Type.u32, .{ .simple = .vector_length }));
86288704 const elem_type = try sema.resolveType(block, elem_type_src, extra.rhs);
86298705 try sema.checkVectorElemType(block, elem_type_src, elem_type);
86308706 const vector_type = try sema.pt.vectorType(.{
......@@ -8642,9 +8718,7 @@ fn zirArrayType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
86428718 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
86438719 const len_src = block.src(.{ .node_offset_array_type_len = inst_data.src_node });
86448720 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 });
8721 const len = try sema.resolveInt(block, len_src, extra.lhs, Type.usize, .{ .simple = .array_length });
86488722 const elem_type = try sema.resolveType(block, elem_src, extra.rhs);
86498723 try sema.validateArrayElemType(block, elem_type, elem_src);
86508724 const array_ty = try sema.pt.arrayType(.{
......@@ -8664,16 +8738,12 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compil
86648738 const len_src = block.src(.{ .node_offset_array_type_len = inst_data.src_node });
86658739 const sentinel_src = block.src(.{ .node_offset_array_type_sentinel = inst_data.src_node });
86668740 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 });
8741 const len = try sema.resolveInt(block, len_src, extra.len, Type.usize, .{ .simple = .array_length });
86708742 const elem_type = try sema.resolveType(block, elem_src, extra.elem_type);
86718743 try sema.validateArrayElemType(block, elem_type, elem_src);
86728744 const uncasted_sentinel = try sema.resolveInst(extra.sentinel);
86738745 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 });
8746 const sentinel_val = try sema.resolveConstDefinedValue(block, sentinel_src, sentinel, .{ .simple = .array_sentinel });
86778747 const array_ty = try sema.pt.arrayType(.{
86788748 .len = len,
86798749 .sentinel = sentinel_val.toIntern(),
......@@ -9071,9 +9141,7 @@ fn zirEnumFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
90719141 }
90729142
90739143 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 });
9144 return sema.failWithNeededComptime(block, operand_src, .{ .simple = .casted_to_comptime_enum });
90779145 }
90789146
90799147 if (try sema.typeHasOnePossibleValue(dest_ty)) |opv| {
......@@ -9487,9 +9555,7 @@ fn zirFunc(
94879555 const ret_ty_body = sema.code.bodySlice(extra_index, extra.data.ret_body_len);
94889556 extra_index += ret_ty_body.len;
94899557
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 });
9558 const ret_ty_val = try sema.resolveGenericBody(block, ret_ty_src, ret_ty_body, inst, Type.type, .{ .simple = .function_ret_ty });
94939559 break :blk ret_ty_val.toType();
94949560 },
94959561 };
......@@ -9556,7 +9622,7 @@ fn resolveGenericBody(
95569622 body: []const Zir.Inst.Index,
95579623 func_inst: Zir.Inst.Index,
95589624 dest_ty: Type,
9559 reason: NeededComptimeReason,
9625 reason: ComptimeReason,
95609626) !Value {
95619627 assert(body.len != 0);
95629628
......@@ -9894,7 +9960,7 @@ fn funcCommon(
98949960 };
98959961 return sema.failWithOwnedErrorMsg(block, msg);
98969962 }
9897 if (is_source_decl and requires_comptime and !param_is_comptime and has_body and !block.is_comptime) {
9963 if (is_source_decl and requires_comptime and !param_is_comptime and has_body and !block.isComptime()) {
98989964 const msg = msg: {
98999965 const msg = try sema.errMsg(param_src, "parameter of type '{}' must be declared comptime", .{
99009966 param_ty.fmt(pt),
......@@ -10132,7 +10198,7 @@ fn finishFunc(
1013210198
1013310199 // If the return type is comptime-only but not dependent on parameters then
1013410200 // 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: {
10201 if (is_source_decl and opt_func_index != .none and ret_ty_requires_comptime and !block.isComptime()) comptime_check: {
1013610202 for (block.params.items(.is_comptime)) |is_comptime| {
1013710203 if (!is_comptime) break;
1013810204 } else break :comptime_check;
......@@ -10547,9 +10613,7 @@ fn zirFieldValNamed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
1054710613 const field_name_src = block.builtinCallArgSrc(inst_data.src_node, 1);
1054810614 const extra = sema.code.extraData(Zir.Inst.FieldNamed, inst_data.payload_index).data;
1054910615 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 });
10616 const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, .{ .simple = .field_name });
1055310617 return sema.fieldVal(block, src, object, field_name, field_name_src);
1055410618}
1055510619
......@@ -10562,9 +10626,7 @@ fn zirFieldPtrNamed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
1056210626 const field_name_src = block.builtinCallArgSrc(inst_data.src_node, 1);
1056310627 const extra = sema.code.extraData(Zir.Inst.FieldNamed, inst_data.payload_index).data;
1056410628 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 });
10629 const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, .{ .simple = .field_name });
1056810630 return sema.fieldPtr(block, src, object_ptr, field_name, field_name_src, false);
1056910631}
1057010632
......@@ -11923,7 +11985,6 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1192311985 .instructions = .{},
1192411986 .label = &label,
1192511987 .inlining = block.inlining,
11926 .is_comptime = block.is_comptime,
1192711988 .comptime_reason = block.comptime_reason,
1192811989 .is_typeof = block.is_typeof,
1192911990 .c_import_buf = block.c_import_buf,
......@@ -12027,11 +12088,8 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1202712088 };
1202812089 }
1202912090
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 });
12091 if (child_block.isComptime()) {
12092 _ = try sema.resolveConstDefinedValue(&child_block, main_operand_src, raw_operand_val, null);
1203512093 unreachable;
1203612094 }
1203712095
......@@ -12148,7 +12206,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1214812206
1214912207 const operand_ty = sema.typeOf(val);
1215012208
12151 if (extra.data.bits.has_continue and !block.is_comptime) {
12209 if (extra.data.bits.has_continue and !block.isComptime()) {
1215212210 // Even if the operand is comptime-known, this `switch` is runtime.
1215312211 if (try operand_ty.comptimeOnlySema(pt)) {
1215412212 return sema.failWithOwnedErrorMsg(block, msg: {
......@@ -12707,7 +12765,6 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1270712765 .instructions = .{},
1270812766 .label = &label,
1270912767 .inlining = block.inlining,
12710 .is_comptime = block.is_comptime,
1271112768 .comptime_reason = block.comptime_reason,
1271212769 .is_typeof = block.is_typeof,
1271312770 .c_import_buf = block.c_import_buf,
......@@ -12790,11 +12847,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1279012847 },
1279112848 }
1279212849
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 });
12850 if (child_block.isComptime()) {
12851 _ = try sema.resolveConstDefinedValue(&child_block, operand_src, operand.simple.cond, null);
1279812852 unreachable;
1279912853 }
1280012854
......@@ -13582,10 +13636,7 @@ fn resolveSwitchComptimeLoop(
1358213636
1358313637 const cond_ref = try sema.switchCond(child_block, src, val);
1358413638
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 });
13639 cond_val = try sema.resolveConstDefinedValue(child_block, src, cond_ref, null);
1358913640 spa.operand = .{ .simple = .{
1359013641 .by_val = val,
1359113642 .by_ref = ref,
......@@ -13825,9 +13876,7 @@ fn resolveSwitchItemVal(
1382513876
1382613877 const item = try sema.coerce(block, coerce_ty, uncoerced_item, item_src);
1382713878
13828 const maybe_lazy = try sema.resolveConstDefinedValue(block, item_src, item, .{
13829 .needed_comptime_reason = "switch prong values must be comptime-known",
13830 });
13879 const maybe_lazy = try sema.resolveConstDefinedValue(block, item_src, item, .{ .simple = .switch_item });
1383113880
1383213881 const val = try sema.resolveLazyValue(maybe_lazy);
1383313882 const new_item = if (val.toIntern() != maybe_lazy.toIntern()) blk: {
......@@ -14295,9 +14344,7 @@ fn zirHasField(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1429514344 const ty_src = block.builtinCallArgSrc(inst_data.src_node, 0);
1429614345 const name_src = block.builtinCallArgSrc(inst_data.src_node, 1);
1429714346 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 });
14347 const field_name = try sema.resolveConstStringIntern(block, name_src, extra.rhs, .{ .simple = .field_name });
1430114348 try ty.resolveFields(pt);
1430214349 const ip = &zcu.intern_pool;
1430314350
......@@ -14344,9 +14391,7 @@ fn zirHasDecl(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1434414391 const lhs_src = block.builtinCallArgSrc(inst_data.src_node, 0);
1434514392 const rhs_src = block.builtinCallArgSrc(inst_data.src_node, 1);
1434614393 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 });
14394 const decl_name = try sema.resolveConstStringIntern(block, rhs_src, extra.rhs, .{ .simple = .decl_name });
1435014395
1435114396 try sema.checkNamespaceType(block, lhs_src, container_type);
1435214397
......@@ -14399,9 +14444,7 @@ fn zirEmbedFile(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1439914444 const pt = sema.pt;
1440014445 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
1440114446 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 });
14447 const name = try sema.resolveConstString(block, operand_src, inst_data.operand, .{ .simple = .operand_embedFile });
1440514448
1440614449 if (name.len == 0) {
1440714450 return sema.fail(block, operand_src, "file path name cannot be empty", .{});
......@@ -14589,7 +14632,7 @@ fn zirShl(
1458914632 }),
1459014633 } },
1459114634 });
14592 const ov_bit = try sema.tupleFieldValByIndex(block, src, op_ov, 1, op_ov_tuple_ty);
14635 const ov_bit = try sema.tupleFieldValByIndex(block, op_ov, 1, op_ov_tuple_ty);
1459314636 const any_ov_bit = if (lhs_ty.zigTypeTag(zcu) == .vector)
1459414637 try block.addInst(.{
1459514638 .tag = if (block.float_mode == .optimized) .reduce_optimized else .reduce,
......@@ -14604,7 +14647,7 @@ fn zirShl(
1460414647 const no_ov = try block.addBinOp(.cmp_eq, any_ov_bit, zero_ov);
1460514648
1460614649 try sema.addSafetyCheck(block, src, no_ov, .shl_overflow);
14607 return sema.tupleFieldValByIndex(block, src, op_ov, 0, op_ov_tuple_ty);
14650 return sema.tupleFieldValByIndex(block, op_ov, 0, op_ov_tuple_ty);
1460814651 }
1460914652 }
1461014653 return block.addBinOp(air_tag, lhs, new_rhs);
......@@ -14932,20 +14975,12 @@ fn analyzeTupleCat(
1493214975 const element_refs = try sema.arena.alloc(Air.Inst.Ref, final_len);
1493314976 var i: u32 = 0;
1493414977 while (i < lhs_len) : (i += 1) {
14935 const operand_src = block.src(.{ .array_cat_lhs = .{
14936 .array_cat_offset = src_node,
14937 .elem_index = i,
14938 } });
14939 element_refs[i] = try sema.tupleFieldValByIndex(block, operand_src, lhs, i, lhs_ty);
14978 element_refs[i] = try sema.tupleFieldValByIndex(block, lhs, i, lhs_ty);
1494014979 }
1494114980 i = 0;
1494214981 while (i < rhs_len) : (i += 1) {
14943 const operand_src = block.src(.{ .array_cat_rhs = .{
14944 .array_cat_offset = src_node,
14945 .elem_index = i,
14946 } });
1494714982 element_refs[i + lhs_len] =
14948 try sema.tupleFieldValByIndex(block, operand_src, rhs, i, rhs_ty);
14983 try sema.tupleFieldValByIndex(block, rhs, i, rhs_ty);
1494914984 }
1495014985
1495114986 return block.addAggregateInit(Type.fromInterned(tuple_ty), element_refs);
......@@ -14985,7 +15020,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1498515020
1498615021 const resolved_elem_ty = t: {
1498715022 var trash_block = block.makeSubBlock();
14988 trash_block.is_comptime = false;
15023 trash_block.comptime_reason = null;
1498915024 defer trash_block.instructions.deinit(sema.gpa);
1499015025
1499115026 const instructions = [_]Air.Inst.Ref{
......@@ -15268,9 +15303,7 @@ fn getArrayCatInfo(sema: *Sema, block: *Block, src: LazySrcLoc, operand: Air.Ins
1526815303 const ptr_info = operand_ty.ptrInfo(zcu);
1526915304 switch (ptr_info.flags.size) {
1527015305 .Slice => {
15271 const val = try sema.resolveConstDefinedValue(block, src, operand, .{
15272 .needed_comptime_reason = "slice value being concatenated must be comptime-known",
15273 });
15306 const val = try sema.resolveConstDefinedValue(block, src, operand, .{ .simple = .slice_cat_operand });
1527415307 return Type.ArrayInfo{
1527515308 .elem_type = Type.fromInterned(ptr_info.child),
1527615309 .sentinel = switch (ptr_info.sentinel) {
......@@ -15365,11 +15398,7 @@ fn analyzeTupleMul(
1536515398 const element_refs = try sema.arena.alloc(Air.Inst.Ref, final_len);
1536615399 var i: u32 = 0;
1536715400 while (i < tuple_len) : (i += 1) {
15368 const operand_src = block.src(.{ .array_cat_lhs = .{
15369 .array_cat_offset = src_node,
15370 .elem_index = i,
15371 } });
15372 element_refs[i] = try sema.tupleFieldValByIndex(block, operand_src, operand, @intCast(i), operand_ty);
15401 element_refs[i] = try sema.tupleFieldValByIndex(block, operand, @intCast(i), operand_ty);
1537315402 }
1537415403 i = 1;
1537515404 while (i < factor) : (i += 1) {
......@@ -15431,9 +15460,7 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1543115460
1543215461 if (lhs_ty.isTuple(zcu)) {
1543315462 // 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 });
15463 const factor = try sema.resolveInt(block, rhs_src, extra.rhs, Type.usize, .{ .simple = .array_mul_factor });
1543715464 const factor_casted = try sema.usizeCast(block, rhs_src, factor);
1543815465 return sema.analyzeTupleMul(block, inst_data.src_node, lhs, factor_casted);
1543915466 }
......@@ -15455,9 +15482,7 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1545515482 };
1545615483
1545715484 // 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 });
15485 const factor = try sema.resolveInt(block, rhs_src, extra.rhs, Type.usize, .{ .simple = .array_mul_factor });
1546115486
1546215487 const result_len_u64 = std.math.mul(u64, lhs_info.len, factor) catch
1546315488 return sema.fail(block, rhs_src, "operation results in overflow", .{});
......@@ -17471,7 +17496,7 @@ fn analyzeArithmetic(
1747117496 }),
1747217497 } },
1747317498 });
17474 const ov_bit = try sema.tupleFieldValByIndex(block, src, op_ov, 1, op_ov_tuple_ty);
17499 const ov_bit = try sema.tupleFieldValByIndex(block, op_ov, 1, op_ov_tuple_ty);
1747517500 const any_ov_bit = if (resolved_type.zigTypeTag(zcu) == .vector)
1747617501 try block.addInst(.{
1747717502 .tag = if (block.float_mode == .optimized) .reduce_optimized else .reduce,
......@@ -17486,7 +17511,7 @@ fn analyzeArithmetic(
1748617511 const no_ov = try block.addBinOp(.cmp_eq, any_ov_bit, zero_ov);
1748717512
1748817513 try sema.addSafetyCheck(block, src, no_ov, .integer_overflow);
17489 return sema.tupleFieldValByIndex(block, src, op_ov, 0, op_ov_tuple_ty);
17514 return sema.tupleFieldValByIndex(block, op_ov, 0, op_ov_tuple_ty);
1749017515 }
1749117516 }
1749217517 }
......@@ -17635,12 +17660,9 @@ fn zirAsm(
1763517660 const is_global_assembly = sema.func_index == .none;
1763617661 const zir_tags = sema.code.instructions.items(.tag);
1763717662
17638 const asm_source: []const u8 = if (tmpl_is_expr) blk: {
17663 const asm_source: []const u8 = if (tmpl_is_expr) s: {
1763917664 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;
17665 break :s try sema.resolveConstString(block, src, tmpl, .{ .simple = .inline_assembly_code });
1764417666 } else sema.code.nullTerminatedString(extra.data.asm_source);
1764517667
1764617668 if (is_global_assembly) {
......@@ -18203,7 +18225,7 @@ fn zirClosureGet(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
1820318225 return sema.failWithOwnedErrorMsg(block, msg);
1820418226 }
1820518227
18206 if (!block.is_typeof and !block.is_comptime and sema.func_index != .none) {
18228 if (!block.is_typeof and !block.isComptime() and sema.func_index != .none) {
1820718229 const msg = msg: {
1820818230 const name = name: {
1820918231 const file, const src_base_node = Zcu.LazySrcLoc.resolveBaseNode(block.src_base_inst, zcu).?;
......@@ -18244,7 +18266,7 @@ fn zirRetAddr(
1824418266 extended: Zir.Inst.Extended.InstData,
1824518267) CompileError!Air.Inst.Ref {
1824618268 _ = extended;
18247 if (block.is_comptime) {
18269 if (block.isComptime()) {
1824818270 // TODO: we could give a meaningful lazy value here. #14938
1824918271 return sema.pt.intRef(Type.usize, 0);
1825018272 } else {
......@@ -19342,7 +19364,7 @@ fn zirTypeofBuiltin(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
1934219364 .namespace = block.namespace,
1934319365 .instructions = .{},
1934419366 .inlining = block.inlining,
19345 .is_comptime = false,
19367 .comptime_reason = null,
1934619368 .is_typeof = true,
1934719369 .want_safety = false,
1934819370 .error_return_trace_index = block.error_return_trace_index,
......@@ -19422,7 +19444,7 @@ fn zirTypeofPeer(
1942219444 .namespace = block.namespace,
1942319445 .instructions = .{},
1942419446 .inlining = block.inlining,
19425 .is_comptime = false,
19447 .comptime_reason = null,
1942619448 .is_typeof = true,
1942719449 .runtime_cond = block.runtime_cond,
1942819450 .runtime_loop = block.runtime_loop,
......@@ -19980,7 +20002,7 @@ fn ensurePostHoc(sema: *Sema, block: *Block, dest_block: Zir.Inst.Index) !*Label
1998020002 .instructions = .{},
1998120003 .label = &labeled_block.label,
1998220004 .inlining = block.inlining,
19983 .is_comptime = block.is_comptime,
20005 .comptime_reason = block.comptime_reason,
1998420006 .src_base_inst = block.src_base_inst,
1998520007 .type_name_ctx = block.type_name_ctx,
1998620008 },
......@@ -20013,7 +20035,7 @@ fn zirUnreachable(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
2001320035 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].@"unreachable";
2001420036 const src = block.nodeOffset(inst_data.src_node);
2001520037
20016 if (block.is_comptime) {
20038 if (block.isComptime()) {
2001720039 return sema.fail(block, src, "reached unreachable code", .{});
2001820040 }
2001920041 // TODO Add compile error for @optimizeFor occurring too late in a scope.
......@@ -20066,7 +20088,7 @@ fn zirRetImplicit(
2006620088 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_tok;
2006720089 const r_brace_src = block.tokenOffset(inst_data.src_tok);
2006820090 if (block.inlining == null and sema.func_is_naked) {
20069 assert(!block.is_comptime);
20091 assert(!block.isComptime());
2007020092 if (block.wantSafety()) {
2007120093 // Calling a safety function from a naked function would not be legal.
2007220094 _ = try block.addNoOp(.trap);
......@@ -20123,7 +20145,7 @@ fn zirRetLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi
2012320145 const src = block.nodeOffset(inst_data.src_node);
2012420146 const ret_ptr = try sema.resolveInst(inst_data.operand);
2012520147
20126 if (block.is_comptime or block.inlining != null or sema.func_is_naked) {
20148 if (block.isComptime() or block.inlining != null or sema.func_is_naked) {
2012720149 const operand = try sema.analyzeLoad(block, src, ret_ptr, src);
2012820150 return sema.analyzeRet(block, operand, src, block.src(.{ .node_offset_return_operand = inst_data.src_node }));
2012920151 }
......@@ -20215,7 +20237,7 @@ fn zirSaveErrRetIndex(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
2021520237 if (!block.ownerModule().error_tracing) return;
2021620238
2021720239 // This is only relevant at runtime.
20218 if (block.is_comptime or block.is_typeof) return;
20240 if (block.isComptime() or block.is_typeof) return;
2021920241
2022020242 const save_index = inst_data.operand == .none or b: {
2022120243 const operand = try sema.resolveInst(inst_data.operand);
......@@ -20268,7 +20290,7 @@ fn restoreErrRetIndex(sema: *Sema, start_block: *Block, src: LazySrcLoc, target_
2026820290
2026920291 const operand = try sema.resolveInstAllowNone(operand_zir);
2027020292
20271 if (start_block.is_comptime or start_block.is_typeof) {
20293 if (start_block.isComptime() or start_block.is_typeof) {
2027220294 const is_non_error = if (operand != .none) blk: {
2027320295 const is_non_error_inst = try sema.analyzeIsNonErr(start_block, src, operand);
2027420296 const cond_val = try sema.resolveDefinedValue(start_block, src, is_non_error_inst);
......@@ -20345,10 +20367,8 @@ fn analyzeRet(
2034520367 };
2034620368
2034720369 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 });
20370 if (block.isComptime()) {
20371 const ret_val = try sema.resolveConstValue(block, operand_src, operand, null);
2035220372 inlining.comptime_result = operand;
2035320373
2035420374 if (sema.fn_ret_ty.isError(zcu) and ret_val.getErrorName(zcu) != .none) {
......@@ -20362,7 +20382,7 @@ fn analyzeRet(
2036220382 try inlining.merges.br_list.append(sema.gpa, br_inst.toIndex().?);
2036320383 try inlining.merges.src_locs.append(sema.gpa, operand_src);
2036420384 return;
20365 } else if (block.is_comptime) {
20385 } else if (block.isComptime()) {
2036620386 return sema.fail(block, src, "function called at runtime cannot return value at comptime", .{});
2036720387 } else if (sema.func_is_naked) {
2036820388 const msg = msg: {
......@@ -20436,9 +20456,7 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
2043620456 const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]);
2043720457 extra_i += 1;
2043820458 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 });
20459 const val = try sema.resolveConstDefinedValue(block, sentinel_src, coerced, .{ .simple = .pointer_sentinel });
2044220460 try checkSentinelType(sema, block, sentinel_src, elem_ty);
2044320461 break :blk val.toIntern();
2044420462 } else .none;
......@@ -20447,9 +20465,7 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
2044720465 const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]);
2044820466 extra_i += 1;
2044920467 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 });
20468 const val = try sema.resolveConstDefinedValue(block, align_src, coerced, .{ .simple = .@"align" });
2045320469 // Check if this happens to be the lazy alignment of our element type, in
2045420470 // which case we can make this 0 without resolving it.
2045520471 switch (zcu.intern_pool.indexToKey(val.toIntern())) {
......@@ -20472,18 +20488,14 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
2047220488 const bit_offset: u16 = if (inst_data.flags.has_bit_range) blk: {
2047320489 const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]);
2047420490 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 });
20491 const bit_offset = try sema.resolveInt(block, bitoffset_src, ref, Type.u16, .{ .simple = .type });
2047820492 break :blk @intCast(bit_offset);
2047920493 } else 0;
2048020494
2048120495 const host_size: u16 = if (inst_data.flags.has_bit_range) blk: {
2048220496 const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]);
2048320497 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 });
20498 const host_size = try sema.resolveInt(block, hostsize_src, ref, Type.u16, .{ .simple = .type });
2048720499 break :blk @intCast(host_size);
2048820500 } else 0;
2048920501
......@@ -20671,9 +20683,7 @@ fn zirUnionInit(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2067120683 if (union_ty.zigTypeTag(pt.zcu) != .@"union") {
2067220684 return sema.fail(block, ty_src, "expected union type, found '{}'", .{union_ty.fmt(pt)});
2067320685 }
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 });
20686 const field_name = try sema.resolveConstStringIntern(block, field_src, extra.field_name, .{ .simple = .union_field_name });
2067720687 const init = try sema.resolveInst(extra.init);
2067820688 return sema.unionInit(block, init, init_src, union_ty, ty_src, field_name, field_src);
2067920689}
......@@ -20800,9 +20810,7 @@ fn zirStructInit(
2080020810 try resolved_ty.resolveStructFieldInits(pt);
2080120811 if (try resolved_ty.structFieldValueComptime(pt, field_index)) |default_value| {
2080220812 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 });
20813 return sema.failWithNeededComptime(block, field_src, .{ .simple = .stored_to_comptime_field });
2080620814 };
2080720815
2080820816 if (!init_val.eql(default_value, resolved_ty.fieldType(field_index, zcu), zcu)) {
......@@ -20862,9 +20870,10 @@ fn zirStructInit(
2086220870 }
2086320871
2086420872 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 });
20873 return sema.failWithNeededComptime(block, field_src, .{ .comptime_only = .{
20874 .ty = resolved_ty,
20875 .msg = .union_init,
20876 } });
2086820877 }
2086920878
2087020879 try sema.validateRuntimeValue(block, field_src, init_inst);
......@@ -21003,9 +21012,10 @@ fn finishStructInit(
2100321012 return sema.failWithNeededComptime(block, block.src(.{ .init_elem = .{
2100421013 .init_node_offset = init_src.offset.node_offset.x,
2100521014 .elem_index = @intCast(runtime_index),
21006 } }), .{
21007 .needed_comptime_reason = "initializer of comptime only struct must be comptime-known",
21008 });
21015 } }), .{ .comptime_only = .{
21016 .ty = struct_ty,
21017 .msg = .struct_init,
21018 } });
2100921019 }
2101021020
2101121021 for (field_inits) |field_init| {
......@@ -21315,11 +21325,7 @@ fn zirArrayInit(
2131521325 if (array_ty.structFieldIsComptime(i, zcu))
2131621326 try array_ty.resolveStructFieldInits(pt);
2131721327 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 };
21328 const init_val = try sema.resolveConstValue(block, elem_src, dest.*, .{ .simple = .stored_to_comptime_field });
2132321329 if (!field_val.eql(init_val, elem_ty, zcu)) {
2132421330 return sema.failWithInvalidComptimeFieldStore(block, elem_src, array_ty, i);
2132521331 }
......@@ -21508,9 +21514,7 @@ fn zirFieldTypeRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
2150821514 const ty_src = block.builtinCallArgSrc(inst_data.src_node, 0);
2150921515 const field_src = block.builtinCallArgSrc(inst_data.src_node, 1);
2151021516 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 });
21517 const field_name = try sema.resolveConstStringIntern(block, field_src, extra.field_name, .{ .simple = .field_name });
2151421518 return sema.fieldType(block, aggregate_ty, field_name, field_src, ty_src);
2151521519}
2151621520
......@@ -21890,9 +21894,7 @@ fn zirReify(
2189021894 const type_info_ty = try sema.getBuiltinType("Type");
2189121895 const uncasted_operand = try sema.resolveInst(extra.operand);
2189221896 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 });
21897 const val = try sema.resolveConstDefinedValue(block, operand_src, type_info, .{ .simple = .operand_Type });
2189621898 const union_val = ip.indexToKey(val.toIntern()).un;
2189721899 if (try sema.anyUndef(block, operand_src, Value.fromInterned(union_val.val))) {
2189821900 return sema.failWithUseOfUndef(block, operand_src);
......@@ -22136,9 +22138,7 @@ fn zirReify(
2213622138 const payload_val = Value.fromInterned(union_val.val).optionalValue(zcu) orelse
2213722139 return Air.internedToRef(Type.anyerror.toIntern());
2213822140
22139 const names_val = try sema.derefSliceAsArray(block, src, payload_val, .{
22140 .needed_comptime_reason = "error set contents must be comptime-known",
22141 });
22141 const names_val = try sema.derefSliceAsArray(block, src, payload_val, .{ .simple = .error_set_contents });
2214222142
2214322143 const len = try sema.usizeCast(block, src, names_val.typeOf(zcu).arrayLen(zcu));
2214422144 var names: InferredErrorSet.NameMap = .{};
......@@ -22151,9 +22151,7 @@ fn zirReify(
2215122151 try ip.getOrPutString(gpa, pt.tid, "name", .no_embedded_nulls),
2215222152 ).?);
2215322153
22154 const name = try sema.sliceToIpString(block, src, name_val, .{
22155 .needed_comptime_reason = "error set contents must be comptime-known",
22156 });
22154 const name = try sema.sliceToIpString(block, src, name_val, .{ .simple = .error_set_contents });
2215722155 _ = try pt.getErrorValue(name);
2215822156 const gop = names.getOrPutAssumeCapacity(name);
2215922157 if (gop.found_existing) {
......@@ -22200,9 +22198,7 @@ fn zirReify(
2220022198 return sema.fail(block, src, "non-packed struct does not support backing integer type", .{});
2220122199 }
2220222200
22203 const fields_arr = try sema.derefSliceAsArray(block, operand_src, fields_val, .{
22204 .needed_comptime_reason = "struct fields must be comptime-known",
22205 });
22201 const fields_arr = try sema.derefSliceAsArray(block, operand_src, fields_val, .{ .simple = .struct_fields });
2220622202
2220722203 if (is_tuple_val.toBool()) {
2220822204 switch (layout) {
......@@ -22238,9 +22234,7 @@ fn zirReify(
2223822234 return sema.fail(block, src, "reified enums must have no decls", .{});
2223922235 }
2224022236
22241 const fields_arr = try sema.derefSliceAsArray(block, operand_src, fields_val, .{
22242 .needed_comptime_reason = "enum fields must be comptime-known",
22243 });
22237 const fields_arr = try sema.derefSliceAsArray(block, operand_src, fields_val, .{ .simple = .enum_fields });
2224422238
2224522239 return sema.reifyEnum(block, inst, src, tag_type_val.toType(), is_exhaustive_val.toBool(), fields_arr, name_strategy);
2224622240 },
......@@ -22311,9 +22305,7 @@ fn zirReify(
2231122305 }
2231222306 const layout = zcu.toEnum(std.builtin.Type.ContainerLayout, layout_val);
2231322307
22314 const fields_arr = try sema.derefSliceAsArray(block, operand_src, fields_val, .{
22315 .needed_comptime_reason = "union fields must be comptime-known",
22316 });
22308 const fields_arr = try sema.derefSliceAsArray(block, operand_src, fields_val, .{ .simple = .union_fields });
2231722309
2231822310 return sema.reifyUnion(block, inst, src, layout, tag_type_val, fields_arr, name_strategy);
2231922311 },
......@@ -22354,9 +22346,7 @@ fn zirReify(
2235422346 const return_type = return_type_val.optionalValue(zcu) orelse
2235522347 return sema.fail(block, src, "Type.Fn.return_type must be non-null for @Type", .{});
2235622348
22357 const params_val = try sema.derefSliceAsArray(block, operand_src, params_slice_val, .{
22358 .needed_comptime_reason = "function parameters must be comptime-known",
22359 });
22349 const params_val = try sema.derefSliceAsArray(block, operand_src, params_slice_val, .{ .simple = .function_parameters });
2236022350
2236122351 const args_len = try sema.usizeCast(block, src, params_val.typeOf(zcu).arrayLen(zcu));
2236222352 const param_types = try sema.arena.alloc(InternPool.Index, args_len);
......@@ -22444,9 +22434,7 @@ fn reifyEnum(
2244422434 const field_name_val = try field_info.fieldValue(pt, 0);
2244522435 const field_value_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 1));
2244622436
22447 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{
22448 .needed_comptime_reason = "enum field name must be comptime-known",
22449 });
22437 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{ .simple = .enum_field_name });
2245022438
2245122439 std.hash.autoHash(&hasher, .{
2245222440 field_name,
......@@ -22591,9 +22579,7 @@ fn reifyUnion(
2259122579 const field_type_val = try field_info.fieldValue(pt, 1);
2259222580 const field_align_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 2));
2259322581
22594 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{
22595 .needed_comptime_reason = "union field name must be comptime-known",
22596 });
22582 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{ .simple = .union_field_name });
2259722583
2259822584 std.hash.autoHash(&hasher, .{
2259922585 field_name,
......@@ -22835,9 +22821,7 @@ fn reifyTuple(
2283522821 const field_is_comptime_val = try field_info.fieldValue(pt, 3);
2283622822 const field_alignment_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 4));
2283722823
22838 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{
22839 .needed_comptime_reason = "tuple field name must be comptime-known",
22840 });
22824 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{ .simple = .tuple_field_name });
2284122825 const field_type = field_type_val.toType();
2284222826 const field_default_value: InternPool.Index = if (field_default_value_val.optionalValue(zcu)) |ptr_val| d: {
2284322827 const ptr_ty = try pt.singleConstPtrType(field_type_val.toType());
......@@ -22845,7 +22829,7 @@ fn reifyTuple(
2284522829 const val = try sema.pointerDeref(block, src, ptr_val, ptr_ty) orelse return sema.failWithNeededComptime(
2284622830 block,
2284722831 src,
22848 .{ .needed_comptime_reason = "tuple field default value must be comptime-known" },
22832 .{ .simple = .tuple_field_default_value },
2284922833 );
2285022834 // Resolve the value so that lazy values do not create distinct types.
2285122835 break :d (try sema.resolveLazyValue(val)).toIntern();
......@@ -22951,9 +22935,7 @@ fn reifyStruct(
2295122935 const field_is_comptime_val = try field_info.fieldValue(pt, 3);
2295222936 const field_alignment_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 4));
2295322937
22954 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{
22955 .needed_comptime_reason = "struct field name must be comptime-known",
22956 });
22938 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{ .simple = .struct_field_name });
2295722939 const field_is_comptime = field_is_comptime_val.toBool();
2295822940 const field_default_value: InternPool.Index = if (field_default_value_val.optionalValue(zcu)) |ptr_val| d: {
2295922941 const ptr_ty = try pt.singleConstPtrType(field_type_val.toType());
......@@ -22961,7 +22943,7 @@ fn reifyStruct(
2296122943 const val = try sema.pointerDeref(block, src, ptr_val, ptr_ty) orelse return sema.failWithNeededComptime(
2296222944 block,
2296322945 src,
22964 .{ .needed_comptime_reason = "struct field default value must be comptime-known" },
22946 .{ .simple = .struct_field_default_value },
2296522947 );
2296622948 // Resolve the value so that lazy values do not create distinct types.
2296722949 break :d (try sema.resolveLazyValue(val)).toIntern();
......@@ -23285,9 +23267,7 @@ fn zirIntFromFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
2328523267 const result_val = try sema.intFromFloat(block, operand_src, operand_val, operand_ty, dest_ty, .truncate);
2328623268 return Air.internedToRef(result_val.toIntern());
2328723269 } 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 });
23270 return sema.failWithNeededComptime(block, operand_src, .{ .simple = .casted_to_comptime_int });
2329123271 }
2329223272
2329323273 try sema.requireRuntimeBlock(block, src, operand_src);
......@@ -23368,9 +23348,7 @@ fn zirFloatFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
2336823348 const result_val = try operand_val.floatFromIntAdvanced(sema.arena, operand_ty, dest_ty, pt, .sema);
2336923349 return Air.internedToRef(result_val.toIntern());
2337023350 } 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 });
23351 return sema.failWithNeededComptime(block, operand_src, .{ .simple = .casted_to_comptime_float });
2337423352 }
2337523353
2337623354 try sema.requireRuntimeBlock(block, src, operand_src);
......@@ -24394,9 +24372,7 @@ fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u6
2439424372 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
2439524373
2439624374 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 });
24375 const field_name = try sema.resolveConstStringIntern(block, rhs_src, extra.rhs, .{ .simple = .field_name });
2440024376
2440124377 const pt = sema.pt;
2440224378 const zcu = pt.zcu;
......@@ -24850,31 +24826,21 @@ fn resolveExportOptions(
2485024826 const visibility_src = block.src(.{ .init_field_visibility = src.offset.node_offset_builtin_call_arg.builtin_call_node });
2485124827
2485224828 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 });
24829 const name = try sema.toConstString(block, name_src, name_operand, .{ .simple = .export_options });
2485624830
2485724831 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 });
24832 const linkage_val = try sema.resolveConstDefinedValue(block, linkage_src, linkage_operand, .{ .simple = .export_options });
2486124833 const linkage = zcu.toEnum(std.builtin.GlobalLinkage, linkage_val);
2486224834
2486324835 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 });
24836 const section_opt_val = try sema.resolveConstDefinedValue(block, section_src, section_operand, .{ .simple = .export_options });
2486724837 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 })
24838 try sema.toConstString(block, section_src, Air.internedToRef(section_val.toIntern()), .{ .simple = .export_options })
2487124839 else
2487224840 null;
2487324841
2487424842 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 });
24843 const visibility_val = try sema.resolveConstDefinedValue(block, visibility_src, visibility_operand, .{ .simple = .export_options });
2487824844 const visibility = zcu.toEnum(std.builtin.SymbolVisibility, visibility_val);
2487924845
2488024846 if (name.len < 1) {
......@@ -24901,7 +24867,7 @@ fn resolveBuiltinEnum(
2490124867 src: LazySrcLoc,
2490224868 zir_ref: Zir.Inst.Ref,
2490324869 comptime name: []const u8,
24904 reason: NeededComptimeReason,
24870 reason: ComptimeReason,
2490524871) CompileError!@field(std.builtin, name) {
2490624872 const pt = sema.pt;
2490724873 const ty = try sema.getBuiltinType(name);
......@@ -24916,7 +24882,7 @@ fn resolveAtomicOrder(
2491624882 block: *Block,
2491724883 src: LazySrcLoc,
2491824884 zir_ref: Zir.Inst.Ref,
24919 reason: NeededComptimeReason,
24885 reason: ComptimeReason,
2492024886) CompileError!std.builtin.AtomicOrder {
2492124887 return sema.resolveBuiltinEnum(block, src, zir_ref, "AtomicOrder", reason);
2492224888}
......@@ -24927,9 +24893,7 @@ fn resolveAtomicRmwOp(
2492724893 src: LazySrcLoc,
2492824894 zir_ref: Zir.Inst.Ref,
2492924895) CompileError!std.builtin.AtomicRmwOp {
24930 return sema.resolveBuiltinEnum(block, src, zir_ref, "AtomicRmwOp", .{
24931 .needed_comptime_reason = "@atomicRmW operation must be comptime-known",
24932 });
24896 return sema.resolveBuiltinEnum(block, src, zir_ref, "AtomicRmwOp", .{ .simple = .operand_atomicRmw_operation });
2493324897}
2493424898
2493524899fn zirCmpxchg(
......@@ -24967,12 +24931,8 @@ fn zirCmpxchg(
2496724931 const uncasted_ptr = try sema.resolveInst(extra.ptr);
2496824932 const ptr = try sema.checkAtomicPtrOperand(block, elem_ty, elem_ty_src, uncasted_ptr, ptr_src, false);
2496924933 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 });
24934 const success_order = try sema.resolveAtomicOrder(block, success_order_src, extra.success_order, .{ .simple = .atomic_order });
24935 const failure_order = try sema.resolveAtomicOrder(block, failure_order_src, extra.failure_order, .{ .simple = .atomic_order });
2497624936
2497724937 if (@intFromEnum(success_order) < @intFromEnum(std.builtin.AtomicOrder.monotonic)) {
2497824938 return sema.fail(block, success_order_src, "success atomic ordering must be monotonic or stricter", .{});
......@@ -25113,9 +25073,7 @@ fn zirReduce(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
2511325073 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
2511425074 const op_src = block.builtinCallArgSrc(inst_data.src_node, 0);
2511525075 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 });
25076 const operation = try sema.resolveBuiltinEnum(block, op_src, extra.lhs, "ReduceOp", .{ .simple = .operand_reduce_operation });
2511925077 const operand = try sema.resolveInst(extra.rhs);
2512025078 const operand_ty = sema.typeOf(operand);
2512125079 const pt = sema.pt;
......@@ -25204,9 +25162,7 @@ fn zirShuffle(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
2520425162 .child = .i32_type,
2520525163 });
2520625164 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 });
25165 const mask_val = try sema.resolveConstValue(block, mask_src, mask, .{ .simple = .operand_shuffle_mask });
2521025166 return sema.analyzeShuffle(block, inst_data.src_node, elem_ty, a, b, mask_val, @intCast(mask_len));
2521125167}
2521225168
......@@ -25474,9 +25430,7 @@ fn zirAtomicLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
2547425430 const elem_ty = try sema.resolveType(block, elem_ty_src, extra.elem_type);
2547525431 const uncasted_ptr = try sema.resolveInst(extra.ptr);
2547625432 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 });
25433 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, .{ .simple = .atomic_order });
2548025434
2548125435 switch (order) {
2548225436 .release, .acq_rel => {
......@@ -25542,9 +25496,7 @@ fn zirAtomicRmw(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2554225496 },
2554325497 else => {},
2554425498 }
25545 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, .{
25546 .needed_comptime_reason = "atomic order of @atomicRmW must be comptime-known",
25547 });
25499 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, .{ .simple = .atomic_order });
2554825500
2554925501 if (order == .unordered) {
2555025502 return sema.fail(block, order_src, "@atomicRmw atomic ordering must not be unordered", .{});
......@@ -25611,9 +25563,7 @@ fn zirAtomicStore(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
2561125563 const elem_ty = sema.typeOf(operand);
2561225564 const uncasted_ptr = try sema.resolveInst(extra.ptr);
2561325565 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 });
25566 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, .{ .simple = .atomic_order });
2561725567
2561825568 const air_tag: Air.Inst.Tag = switch (order) {
2561925569 .acquire, .acq_rel => {
......@@ -25716,14 +25666,12 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
2571625666 const modifier_ty = try sema.getBuiltinType("CallModifier");
2571725667 const air_ref = try sema.resolveInst(extra.modifier);
2571825668 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 });
25669 const modifier_val = try sema.resolveConstDefinedValue(block, modifier_src, modifier_ref, .{ .simple = .call_modifier });
2572225670 var modifier = zcu.toEnum(std.builtin.CallModifier, modifier_val);
2572325671 switch (modifier) {
2572425672 // These can be upgraded to comptime or nosuspend calls.
2572525673 .auto, .never_tail, .no_async => {
25726 if (block.is_comptime) {
25674 if (block.isComptime()) {
2572725675 if (modifier == .never_tail) {
2572825676 return sema.fail(block, modifier_src, "unable to perform 'never_tail' call at compile-time", .{});
2572925677 }
......@@ -25738,12 +25686,12 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
2573825686 return sema.fail(block, func_src, "modifier '{s}' requires a comptime-known function", .{@tagName(modifier)});
2573925687 };
2574025688
25741 if (block.is_comptime) {
25689 if (block.isComptime()) {
2574225690 modifier = .compile_time;
2574325691 }
2574425692 },
2574525693 .always_tail => {
25746 if (block.is_comptime) {
25694 if (block.isComptime()) {
2574725695 modifier = .compile_time;
2574825696 }
2574925697 },
......@@ -25751,12 +25699,12 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
2575125699 if (extra.flags.is_nosuspend) {
2575225700 return sema.fail(block, modifier_src, "modifier 'async_kw' cannot be used inside nosuspend block", .{});
2575325701 }
25754 if (block.is_comptime) {
25702 if (block.isComptime()) {
2575525703 return sema.fail(block, modifier_src, "modifier 'async_kw' cannot be used in combination with comptime function call", .{});
2575625704 }
2575725705 },
2575825706 .never_inline => {
25759 if (block.is_comptime) {
25707 if (block.isComptime()) {
2576025708 return sema.fail(block, modifier_src, "unable to perform 'never_inline' call at compile-time", .{});
2576125709 }
2576225710 },
......@@ -25771,7 +25719,7 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
2577125719
2577225720 const resolved_args: []Air.Inst.Ref = try sema.arena.alloc(Air.Inst.Ref, args_ty.structFieldCount(zcu));
2577325721 for (resolved_args, 0..) |*resolved, i| {
25774 resolved.* = try sema.tupleFieldValByIndex(block, args_src, args, @intCast(i), args_ty);
25722 resolved.* = try sema.tupleFieldValByIndex(block, args, @intCast(i), args_ty);
2577525723 }
2577625724
2577725725 const callee_ty = sema.typeOf(func);
......@@ -25820,9 +25768,7 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Ins
2582025768 }
2582125769 try parent_ty.resolveLayout(pt);
2582225770
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 });
25771 const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, .{ .simple = .field_name });
2582625772 const field_index = switch (parent_ty.zigTypeTag(zcu)) {
2582725773 .@"struct" => blk: {
2582825774 if (parent_ty.isTuple(zcu)) {
......@@ -26680,9 +26626,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2668026626 extra_index += body.len;
2668126627
2668226628 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 });
26629 const val = try sema.resolveGenericBody(block, cc_src, body, inst, cc_ty, .{ .simple = .@"callconv" });
2668626630 break :blk try sema.analyzeValueAsCallconv(block, cc_src, val);
2668726631 } else if (extra.data.bits.has_cc_ref) blk: {
2668826632 const cc_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);
......@@ -26690,9 +26634,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2669026634 const cc_ty = try sema.getBuiltinType("CallingConvention");
2669126635 const uncoerced_cc = try sema.resolveInst(cc_ref);
2669226636 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 });
26637 const cc_val = try sema.resolveConstDefinedValue(block, cc_src, coerced_cc, .{ .simple = .@"callconv" });
2669626638 break :blk try sema.analyzeValueAsCallconv(block, cc_src, cc_val);
2669726639 } else cc: {
2669826640 if (has_body) {
......@@ -26730,9 +26672,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2673026672 const body = sema.code.bodySlice(extra_index, body_len);
2673126673 extra_index += body.len;
2673226674
26733 const val = try sema.resolveGenericBody(block, ret_src, body, inst, Type.type, .{
26734 .needed_comptime_reason = "return type must be comptime-known",
26735 });
26675 const val = try sema.resolveGenericBody(block, ret_src, body, inst, Type.type, .{ .simple = .function_ret_ty });
2673626676 const ty = val.toType();
2673726677 break :blk ty;
2673826678 } else if (extra.data.bits.has_ret_ty_ref) blk: {
......@@ -26742,9 +26682,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2674226682 error.GenericPoison => break :blk Type.generic_poison,
2674326683 else => |e| return e,
2674426684 };
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) {
26685 const ret_ty_val = sema.resolveConstDefinedValue(block, ret_src, ret_ty_air_ref, .{ .simple = .function_ret_ty }) catch |err| switch (err) {
2674826686 error.GenericPoison => break :blk Type.generic_poison,
2674926687 else => |e| return e,
2675026688 };
......@@ -26790,9 +26728,7 @@ fn zirCUndef(
2679026728 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
2679126729 const src = block.builtinCallArgSrc(extra.node, 0);
2679226730
26793 const name = try sema.resolveConstString(block, src, extra.operand, .{
26794 .needed_comptime_reason = "name of macro being undefined must be comptime-known",
26795 });
26731 const name = try sema.resolveConstString(block, src, extra.operand, .{ .simple = .operand_cUndef_macro_name });
2679626732 try block.c_import_buf.?.writer().print("#undef {s}\n", .{name});
2679726733 return .void_value;
2679826734}
......@@ -26805,9 +26741,7 @@ fn zirCInclude(
2680526741 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
2680626742 const src = block.builtinCallArgSrc(extra.node, 0);
2680726743
26808 const name = try sema.resolveConstString(block, src, extra.operand, .{
26809 .needed_comptime_reason = "path being included must be comptime-known",
26810 });
26744 const name = try sema.resolveConstString(block, src, extra.operand, .{ .simple = .operand_cInclude_file_name });
2681126745 try block.c_import_buf.?.writer().print("#include <{s}>\n", .{name});
2681226746 return .void_value;
2681326747}
......@@ -26823,14 +26757,10 @@ fn zirCDefine(
2682326757 const name_src = block.builtinCallArgSrc(extra.node, 0);
2682426758 const val_src = block.builtinCallArgSrc(extra.node, 1);
2682526759
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 });
26760 const name = try sema.resolveConstString(block, name_src, extra.lhs, .{ .simple = .operand_cDefine_macro_name });
2682926761 const rhs = try sema.resolveInst(extra.rhs);
2683026762 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 });
26763 const value = try sema.resolveConstString(block, val_src, extra.rhs, .{ .simple = .operand_cDefine_macro_value });
2683426764 try block.c_import_buf.?.writer().print("#define {s} {s}\n", .{ name, value });
2683526765 } else {
2683626766 try block.c_import_buf.?.writer().print("#define {s}\n", .{name});
......@@ -26851,9 +26781,7 @@ fn zirWasmMemorySize(
2685126781 return sema.fail(block, builtin_src, "builtin @wasmMemorySize is available when targeting WebAssembly; targeted CPU architecture is {s}", .{@tagName(target.cpu.arch)});
2685226782 }
2685326783
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 }));
26784 const index: u32 = @intCast(try sema.resolveInt(block, index_src, extra.operand, Type.u32, .{ .simple = .wasm_memory_index }));
2685726785 try sema.requireRuntimeBlock(block, builtin_src, null);
2685826786 return block.addInst(.{
2685926787 .tag = .wasm_memory_size,
......@@ -26878,9 +26806,7 @@ fn zirWasmMemoryGrow(
2687826806 return sema.fail(block, builtin_src, "builtin @wasmMemoryGrow is available when targeting WebAssembly; targeted CPU architecture is {s}", .{@tagName(target.cpu.arch)});
2687926807 }
2688026808
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 }));
26809 const index: u32 = @intCast(try sema.resolveInt(block, index_src, extra.lhs, Type.u32, .{ .simple = .wasm_memory_index }));
2688426810 const delta = try sema.coerce(block, Type.usize, try sema.resolveInst(extra.rhs), delta_src);
2688526811
2688626812 try sema.requireRuntimeBlock(block, builtin_src, null);
......@@ -26911,19 +26837,13 @@ fn resolvePrefetchOptions(
2691126837 const cache_src = block.src(.{ .init_field_cache = src.offset.node_offset_builtin_call_arg.builtin_call_node });
2691226838
2691326839 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 });
26840 const rw_val = try sema.resolveConstDefinedValue(block, rw_src, rw, .{ .simple = .prefetch_options });
2691726841
2691826842 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 });
26843 const locality_val = try sema.resolveConstDefinedValue(block, locality_src, locality, .{ .simple = .prefetch_options });
2692226844
2692326845 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 });
26846 const cache_val = try sema.resolveConstDefinedValue(block, cache_src, cache, .{ .simple = .prefetch_options });
2692726847
2692826848 return std.builtin.PrefetchOptions{
2692926849 .rw = zcu.toEnum(std.builtin.PrefetchOptions.Rw, rw_val),
......@@ -26945,7 +26865,7 @@ fn zirPrefetch(
2694526865
2694626866 const options = try sema.resolvePrefetchOptions(block, opts_src, extra.rhs);
2694726867
26948 if (!block.is_comptime) {
26868 if (!block.isComptime()) {
2694926869 _ = try block.addInst(.{
2695026870 .tag = .prefetch,
2695126871 .data = .{ .prefetch = .{
......@@ -26987,30 +26907,20 @@ fn resolveExternOptions(
2698726907 const dll_import_src = block.src(.{ .init_field_dll_import = src.offset.node_offset_builtin_call_arg.builtin_call_node });
2698826908
2698926909 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 });
26910 const name = try sema.toConstString(block, name_src, name_ref, .{ .simple = .extern_options });
2699326911
2699426912 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 });
26913 const library_name_val = try sema.resolveConstDefinedValue(block, library_src, library_name_inst, .{ .simple = .extern_options });
2699826914
2699926915 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 });
26916 const linkage_val = try sema.resolveConstDefinedValue(block, linkage_src, linkage_ref, .{ .simple = .extern_options });
2700326917 const linkage = zcu.toEnum(std.builtin.GlobalLinkage, linkage_val);
2700426918
2700526919 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 });
26920 const is_thread_local_val = try sema.resolveConstDefinedValue(block, thread_local_src, is_thread_local, .{ .simple = .extern_options });
2700926921
2701026922 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 });
26923 const library_name = try sema.toConstString(block, library_src, Air.internedToRef(library_name_payload.toIntern()), .{ .simple = .extern_options });
2701426924 if (library_name.len == 0) {
2701526925 return sema.fail(block, library_src, "library name cannot be empty", .{});
2701626926 }
......@@ -27019,9 +26929,7 @@ fn resolveExternOptions(
2701926929 } else null;
2702026930
2702126931 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 });
26932 const is_dll_import_val = try sema.resolveConstDefinedValue(block, dll_import_src, is_dll_import_ref, .{ .simple = .extern_options });
2702526933
2702626934 if (name.len == 0) {
2702726935 return sema.fail(block, name_src, "extern symbol name cannot be empty", .{});
......@@ -27134,9 +27042,7 @@ fn zirWorkItem(
2713427042 },
2713527043 }
2713627044
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 }));
27045 const dimension: u32 = @intCast(try sema.resolveInt(block, dimension_src, extra.operand, Type.u32, .{ .simple = .work_group_dim_index }));
2714027046 try sema.requireRuntimeBlock(block, builtin_src, null);
2714127047
2714227048 return block.addInst(.{
......@@ -27158,7 +27064,7 @@ fn zirInComptime(
2715827064 block: *Block,
2715927065) CompileError!Air.Inst.Ref {
2716027066 _ = sema;
27161 return if (block.is_comptime) .bool_true else .bool_false;
27067 return if (block.isComptime()) .bool_true else .bool_false;
2716227068}
2716327069
2716427070fn zirBuiltinValue(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
......@@ -27249,9 +27155,7 @@ fn zirBranchHint(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
2724927155
2725027156 const hint_ty = try sema.getBuiltinType("BranchHint");
2725127157 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 });
27158 const hint_val = try sema.resolveConstDefinedValue(block, operand_src, coerced_hint, .{ .simple = .operand_branchHint });
2725527159
2725627160 // We only apply the first hint in a branch.
2725727161 // This allows user-provided hints to override implicit cold hints.
......@@ -27261,20 +27165,20 @@ fn zirBranchHint(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
2726127165}
2726227166
2726327167fn requireRuntimeBlock(sema: *Sema, block: *Block, src: LazySrcLoc, runtime_src: ?LazySrcLoc) !void {
27264 if (block.is_comptime) {
27265 const msg = msg: {
27168 if (block.isComptime()) {
27169 const msg, const fail_block = msg: {
2726627170 const msg = try sema.errMsg(src, "unable to evaluate comptime expression", .{});
2726727171 errdefer msg.destroy(sema.gpa);
2726827172
2726927173 if (runtime_src) |some| {
2727027174 try sema.errNote(some, msg, "operation is runtime due to this operand", .{});
2727127175 }
27272 if (block.comptime_reason) |some| {
27273 try some.explain(sema, msg);
27274 }
27275 break :msg msg;
27176
27177 const fail_block = try block.explainWhyBlockIsComptime(msg);
27178
27179 break :msg .{ msg, fail_block };
2727627180 };
27277 return sema.failWithOwnedErrorMsg(block, msg);
27181 return sema.failWithOwnedErrorMsg(fail_block, msg);
2727827182 }
2727927183}
2728027184
......@@ -27759,7 +27663,7 @@ fn addSafetyCheck(
2775927663 panic_id: Zcu.PanicId,
2776027664) !void {
2776127665 const gpa = sema.gpa;
27762 assert(!parent_block.is_comptime);
27666 assert(!parent_block.isComptime());
2776327667
2776427668 var fail_block: Block = .{
2776527669 .parent = parent_block,
......@@ -27767,7 +27671,7 @@ fn addSafetyCheck(
2776727671 .namespace = parent_block.namespace,
2776827672 .instructions = .{},
2776927673 .inlining = parent_block.inlining,
27770 .is_comptime = false,
27674 .comptime_reason = null,
2777127675 .src_base_inst = parent_block.src_base_inst,
2777227676 .type_name_ctx = parent_block.type_name_ctx,
2777327677 };
......@@ -27874,7 +27778,7 @@ fn addSafetyCheckUnwrapError(
2787427778 unwrap_err_tag: Air.Inst.Tag,
2787527779 is_non_err_tag: Air.Inst.Tag,
2787627780) !void {
27877 assert(!parent_block.is_comptime);
27781 assert(!parent_block.isComptime());
2787827782 const ok = try parent_block.addUnOp(is_non_err_tag, operand);
2787927783 const gpa = sema.gpa;
2788027784
......@@ -27884,7 +27788,7 @@ fn addSafetyCheckUnwrapError(
2788427788 .namespace = parent_block.namespace,
2788527789 .instructions = .{},
2788627790 .inlining = parent_block.inlining,
27887 .is_comptime = false,
27791 .comptime_reason = null,
2788827792 .src_base_inst = parent_block.src_base_inst,
2788927793 .type_name_ctx = parent_block.type_name_ctx,
2789027794 };
......@@ -27918,7 +27822,7 @@ fn addSafetyCheckIndexOob(
2791827822 len: Air.Inst.Ref,
2791927823 cmp_op: Air.Inst.Tag,
2792027824) !void {
27921 assert(!parent_block.is_comptime);
27825 assert(!parent_block.isComptime());
2792227826 const ok = try parent_block.addBinOp(cmp_op, index, len);
2792327827 return addSafetyCheckCall(sema, parent_block, src, ok, "outOfBounds", &.{ index, len });
2792427828}
......@@ -27930,7 +27834,7 @@ fn addSafetyCheckInactiveUnionField(
2793027834 active_tag: Air.Inst.Ref,
2793127835 wanted_tag: Air.Inst.Ref,
2793227836) !void {
27933 assert(!parent_block.is_comptime);
27837 assert(!parent_block.isComptime());
2793427838 const ok = try parent_block.addBinOp(.cmp_eq, active_tag, wanted_tag);
2793527839 return addSafetyCheckCall(sema, parent_block, src, ok, "inactiveUnionField", &.{ active_tag, wanted_tag });
2793627840}
......@@ -27944,7 +27848,7 @@ fn addSafetyCheckSentinelMismatch(
2794427848 ptr: Air.Inst.Ref,
2794527849 sentinel_index: Air.Inst.Ref,
2794627850) !void {
27947 assert(!parent_block.is_comptime);
27851 assert(!parent_block.isComptime());
2794827852 const pt = sema.pt;
2794927853 const zcu = pt.zcu;
2795027854 const expected_sentinel_val = maybe_sentinel orelse return;
......@@ -27986,7 +27890,7 @@ fn addSafetyCheckCall(
2798627890 func_name: []const u8,
2798727891 args: []const Air.Inst.Ref,
2798827892) !void {
27989 assert(!parent_block.is_comptime);
27893 assert(!parent_block.isComptime());
2799027894 const gpa = sema.gpa;
2799127895 const pt = sema.pt;
2799227896 const zcu = pt.zcu;
......@@ -27997,7 +27901,7 @@ fn addSafetyCheckCall(
2799727901 .namespace = parent_block.namespace,
2799827902 .instructions = .{},
2799927903 .inlining = parent_block.inlining,
28000 .is_comptime = false,
27904 .comptime_reason = null,
2800127905 .src_base_inst = parent_block.src_base_inst,
2800227906 .type_name_ctx = parent_block.type_name_ctx,
2800327907 };
......@@ -28203,7 +28107,7 @@ fn fieldVal(
2820328107 const field_ptr = try sema.structFieldPtr(block, src, object, field_name, field_name_src, inner_ty, false);
2820428108 return sema.analyzeLoad(block, src, field_ptr, object_src);
2820528109 } else {
28206 return sema.structFieldVal(block, src, object, field_name, field_name_src, inner_ty);
28110 return sema.structFieldVal(block, object, field_name, field_name_src, inner_ty);
2820728111 },
2820828112 .@"union" => if (is_pointer_to) {
2820928113 // Avoid loading the entire union by fetching a pointer and loading that
......@@ -28823,14 +28727,12 @@ fn structFieldPtrByIndex(
2882328727 return Air.internedToRef(val);
2882428728 }
2882528729
28826 try sema.requireRuntimeBlock(block, src, null);
2882728730 return block.addStructFieldPtr(struct_ptr, field_index, ptr_field_ty);
2882828731}
2882928732
2883028733fn structFieldVal(
2883128734 sema: *Sema,
2883228735 block: *Block,
28833 src: LazySrcLoc,
2883428736 struct_byval: Air.Inst.Ref,
2883528737 field_name: InternPool.NullTerminatedString,
2883628738 field_name_src: LazySrcLoc,
......@@ -28866,12 +28768,11 @@ fn structFieldVal(
2886628768 return Air.internedToRef((try struct_val.fieldValue(pt, field_index)).toIntern());
2886728769 }
2886828770
28869 try sema.requireRuntimeBlock(block, src, null);
2887028771 try field_ty.resolveLayout(pt);
2887128772 return block.addStructFieldVal(struct_byval, field_index, field_ty);
2887228773 },
2887328774 .tuple_type => {
28874 return sema.tupleFieldVal(block, src, struct_byval, field_name, field_name_src, struct_ty);
28775 return sema.tupleFieldVal(block, struct_byval, field_name, field_name_src, struct_ty);
2887528776 },
2887628777 else => unreachable,
2887728778 }
......@@ -28880,7 +28781,6 @@ fn structFieldVal(
2888028781fn tupleFieldVal(
2888128782 sema: *Sema,
2888228783 block: *Block,
28883 src: LazySrcLoc,
2888428784 tuple_byval: Air.Inst.Ref,
2888528785 field_name: InternPool.NullTerminatedString,
2888628786 field_name_src: LazySrcLoc,
......@@ -28892,7 +28792,7 @@ fn tupleFieldVal(
2889228792 return pt.intRef(Type.usize, tuple_ty.structFieldCount(zcu));
2889328793 }
2889428794 const field_index = try sema.tupleFieldIndex(block, tuple_ty, field_name, field_name_src);
28895 return sema.tupleFieldValByIndex(block, src, tuple_byval, field_index, tuple_ty);
28795 return sema.tupleFieldValByIndex(block, tuple_byval, field_index, tuple_ty);
2889628796}
2889728797
2889828798/// Asserts that `field_name` is not "len".
......@@ -28921,7 +28821,6 @@ fn tupleFieldIndex(
2892128821fn tupleFieldValByIndex(
2892228822 sema: *Sema,
2892328823 block: *Block,
28924 src: LazySrcLoc,
2892528824 tuple_byval: Air.Inst.Ref,
2892628825 field_index: u32,
2892728826 tuple_ty: Type,
......@@ -28951,7 +28850,6 @@ fn tupleFieldValByIndex(
2895128850 };
2895228851 }
2895328852
28954 try sema.requireRuntimeBlock(block, src, null);
2895528853 try field_ty.resolveLayout(pt);
2895628854 return block.addStructFieldVal(tuple_byval, field_index, field_ty);
2895728855}
......@@ -29049,7 +28947,6 @@ fn unionFieldPtr(
2904928947 return Air.internedToRef(field_ptr_val.toIntern());
2905028948 }
2905128949
29052 try sema.requireRuntimeBlock(block, src, null);
2905328950 if (!initializing and union_obj.flagsUnordered(ip).layout == .auto and block.wantSafety() and
2905428951 union_ty.unionTagTypeSafety(zcu) != null and union_obj.field_types.len > 1)
2905528952 {
......@@ -29126,7 +29023,6 @@ fn unionFieldVal(
2912629023 }
2912729024 }
2912829025
29129 try sema.requireRuntimeBlock(block, src, null);
2913029026 if (union_obj.flagsUnordered(ip).layout == .auto and block.wantSafety() and
2913129027 union_ty.unionTagTypeSafety(zcu) != null and union_obj.field_types.len > 1)
2913229028 {
......@@ -29168,9 +29064,7 @@ fn elemPtr(
2916829064 .array, .vector => try sema.elemPtrArray(block, src, indexable_ptr_src, indexable_ptr, elem_index_src, elem_index, init, oob_safety),
2916929065 .@"struct" => blk: {
2917029066 // 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 });
29067 const index_val = try sema.resolveConstDefinedValue(block, elem_index_src, elem_index, .{ .simple = .tuple_field_index });
2917429068 const index: u32 = @intCast(try index_val.toUnsignedIntSema(pt));
2917529069 break :blk try sema.tupleFieldPtr(block, src, indexable_ptr, elem_index_src, index, init);
2917629070 },
......@@ -29207,16 +29101,15 @@ fn elemPtrOneLayerOnly(
2920729101 .Many, .C => {
2920829102 const maybe_ptr_val = try sema.resolveDefinedValue(block, indexable_src, indexable);
2920929103 const maybe_index_val = try sema.resolveDefinedValue(block, elem_index_src, elem_index);
29210 const runtime_src = rs: {
29211 const ptr_val = maybe_ptr_val orelse break :rs indexable_src;
29212 const index_val = maybe_index_val orelse break :rs elem_index_src;
29104 ct: {
29105 const ptr_val = maybe_ptr_val orelse break :ct;
29106 const index_val = maybe_index_val orelse break :ct;
2921329107 const index: usize = @intCast(try index_val.toUnsignedIntSema(pt));
2921429108 const elem_ptr = try ptr_val.ptrElem(index, pt);
2921529109 return Air.internedToRef(elem_ptr.toIntern());
29216 };
29110 }
2921729111 const result_ty = try indexable_ty.elemPtrType(null, pt);
2921829112
29219 try sema.requireRuntimeBlock(block, src, runtime_src);
2922029113 return block.addPtrElemPtr(indexable, elem_index, result_ty);
2922129114 },
2922229115 .One => {
......@@ -29225,9 +29118,7 @@ fn elemPtrOneLayerOnly(
2922529118 .array, .vector => try sema.elemPtrArray(block, src, indexable_src, indexable, elem_index_src, elem_index, init, oob_safety),
2922629119 .@"struct" => blk: {
2922729120 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 });
29121 const index_val = try sema.resolveConstDefinedValue(block, elem_index_src, elem_index, .{ .simple = .tuple_field_index });
2923129122 const index: u32 = @intCast(try index_val.toUnsignedIntSema(pt));
2923229123 break :blk try sema.tupleFieldPtr(block, indexable_src, indexable, elem_index_src, index, false);
2923329124 },
......@@ -29266,22 +29157,19 @@ fn elemVal(
2926629157 const maybe_indexable_val = try sema.resolveDefinedValue(block, indexable_src, indexable);
2926729158 const maybe_index_val = try sema.resolveDefinedValue(block, elem_index_src, elem_index);
2926829159
29269 const runtime_src = rs: {
29270 const indexable_val = maybe_indexable_val orelse break :rs indexable_src;
29271 const index_val = maybe_index_val orelse break :rs elem_index_src;
29160 ct: {
29161 const indexable_val = maybe_indexable_val orelse break :ct;
29162 const index_val = maybe_index_val orelse break :ct;
2927229163 const index: usize = @intCast(try index_val.toUnsignedIntSema(pt));
2927329164 const elem_ty = indexable_ty.elemType2(zcu);
2927429165 const many_ptr_ty = try pt.manyConstPtrType(elem_ty);
2927529166 const many_ptr_val = try pt.getCoerced(indexable_val, many_ptr_ty);
2927629167 const elem_ptr_ty = try pt.singleConstPtrType(elem_ty);
2927729168 const elem_ptr_val = try many_ptr_val.ptrElem(index, pt);
29278 if (try sema.pointerDeref(block, indexable_src, elem_ptr_val, elem_ptr_ty)) |elem_val| {
29279 return Air.internedToRef((try pt.getCoerced(elem_val, elem_ty)).toIntern());
29280 }
29281 break :rs indexable_src;
29282 };
29169 const elem_val = try sema.pointerDeref(block, indexable_src, elem_ptr_val, elem_ptr_ty) orelse break :ct;
29170 return Air.internedToRef((try pt.getCoerced(elem_val, elem_ty)).toIntern());
29171 }
2928329172
29284 try sema.requireRuntimeBlock(block, src, runtime_src);
2928529173 return block.addBinOp(.ptr_elem_val, indexable, elem_index);
2928629174 },
2928729175 .One => {
......@@ -29305,9 +29193,7 @@ fn elemVal(
2930529193 },
2930629194 .@"struct" => {
2930729195 // 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 });
29196 const index_val = try sema.resolveConstDefinedValue(block, elem_index_src, elem_index, .{ .simple = .tuple_field_index });
2931129197 const index: u32 = @intCast(try index_val.toUnsignedIntSema(pt));
2931229198 return sema.tupleField(block, indexable_src, indexable, elem_index_src, index);
2931329199 },
......@@ -29396,7 +29282,6 @@ fn tupleFieldPtr(
2939629282 try sema.validateRuntimeElemAccess(block, field_index_src, field_ty, tuple_ty, tuple_ptr_src);
2939729283 }
2939829284
29399 try sema.requireRuntimeBlock(block, tuple_ptr_src, null);
2940029285 return block.addStructFieldPtr(tuple_ptr, field_index, ptr_field_ty);
2940129286}
2940229287
......@@ -29439,7 +29324,6 @@ fn tupleField(
2943929324
2944029325 try sema.validateRuntimeElemAccess(block, field_index_src, field_ty, tuple_ty, tuple_src);
2944129326
29442 try sema.requireRuntimeBlock(block, tuple_src, null);
2944329327 try field_ty.resolveLayout(pt);
2944429328 return block.addStructFieldVal(tuple, field_index, field_ty);
2944529329}
......@@ -29495,7 +29379,6 @@ fn elemValArray(
2949529379
2949629380 try sema.validateRuntimeElemAccess(block, elem_index_src, elem_ty, array_ty, array_src);
2949729381
29498 const runtime_src = if (maybe_undef_array_val != null) elem_index_src else array_src;
2949929382 if (oob_safety and block.wantSafety()) {
2950029383 // Runtime check is only needed if unable to comptime check.
2950129384 if (maybe_index_val == null) {
......@@ -29508,7 +29391,6 @@ fn elemValArray(
2950829391 if (try sema.typeHasOnePossibleValue(elem_ty)) |elem_val|
2950929392 return Air.internedToRef(elem_val.toIntern());
2951029393
29511 try sema.requireRuntimeBlock(block, src, runtime_src);
2951229394 return block.addBinOp(.array_elem_val, array, elem_index);
2951329395}
2951429396
......@@ -29562,9 +29444,6 @@ fn elemPtrArray(
2956229444 try sema.validateRuntimeElemAccess(block, elem_index_src, array_ty.elemType2(zcu), array_ty, array_ptr_src);
2956329445 }
2956429446
29565 const runtime_src = if (maybe_undef_array_ptr_val != null) elem_index_src else array_ptr_src;
29566 try sema.requireRuntimeBlock(block, src, runtime_src);
29567
2956829447 // Runtime check is only needed if unable to comptime check.
2956929448 if (oob_safety and block.wantSafety() and offset == null) {
2957029449 const len_inst = try pt.intRef(Type.usize, array_len);
......@@ -29621,7 +29500,6 @@ fn elemValSlice(
2962129500
2962229501 try sema.validateRuntimeElemAccess(block, elem_index_src, elem_ty, slice_ty, slice_src);
2962329502
29624 try sema.requireRuntimeBlock(block, src, runtime_src);
2962529503 if (oob_safety and block.wantSafety()) {
2962629504 const len_inst = if (maybe_slice_val) |slice_val|
2962729505 try pt.intRef(Type.usize, try slice_val.sliceLen(pt))
......@@ -29678,8 +29556,6 @@ fn elemPtrSlice(
2967829556
2967929557 try sema.validateRuntimeElemAccess(block, elem_index_src, elem_ptr_ty, slice_ty, slice_src);
2968029558
29681 const runtime_src = if (maybe_undef_slice_val != null) elem_index_src else slice_src;
29682 try sema.requireRuntimeBlock(block, src, runtime_src);
2968329559 if (oob_safety and block.wantSafety()) {
2968429560 const len_inst = len: {
2968529561 if (maybe_undef_slice_val) |slice_val|
......@@ -30093,9 +29969,7 @@ fn coerceExtra(
3009329969 const val = maybe_inst_val orelse {
3009429970 if (dest_ty.zigTypeTag(zcu) == .comptime_int) {
3009529971 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 });
29972 return sema.failWithNeededComptime(block, inst_src, .{ .simple = .casted_to_comptime_int });
3009929973 }
3010029974 break :float;
3010129975 };
......@@ -30120,9 +29994,7 @@ fn coerceExtra(
3012029994 if (dest_ty.zigTypeTag(zcu) == .comptime_int) {
3012129995 if (!opts.report_err) return error.NotCoercible;
3012229996 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 });
29997 return sema.failWithNeededComptime(block, inst_src, .{ .simple = .casted_to_comptime_int });
3012629998 }
3012729999
3012830000 // integer widening
......@@ -30158,9 +30030,7 @@ fn coerceExtra(
3015830030 return Air.internedToRef(result_val.toIntern());
3015930031 } else if (dest_ty.zigTypeTag(zcu) == .comptime_float) {
3016030032 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 });
30033 return sema.failWithNeededComptime(block, inst_src, .{ .simple = .casted_to_comptime_float });
3016430034 }
3016530035
3016630036 // float widening
......@@ -30175,9 +30045,7 @@ fn coerceExtra(
3017530045 const val = maybe_inst_val orelse {
3017630046 if (dest_ty.zigTypeTag(zcu) == .comptime_float) {
3017730047 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 });
30048 return sema.failWithNeededComptime(block, inst_src, .{ .simple = .casted_to_comptime_float });
3018130049 }
3018230050 break :int;
3018330051 };
......@@ -32435,9 +32303,7 @@ fn coerceTupleToStruct(
3243532303 field_refs[struct_field_index] = coerced;
3243632304 if (struct_type.fieldIsComptime(ip, struct_field_index)) {
3243732305 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 });
32306 return sema.failWithNeededComptime(block, field_src, .{ .simple = .stored_to_comptime_field });
3244132307 };
3244232308
3244332309 const field_init = Value.fromInterned(struct_type.field_inits.get(ip)[struct_field_index]);
......@@ -32550,9 +32416,7 @@ fn coerceTupleToTuple(
3255032416 field_refs[field_index] = coerced;
3255132417 if (default_val != .none) {
3255232418 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 });
32419 return sema.failWithNeededComptime(block, field_src, .{ .simple = .stored_to_comptime_field });
3255632420 };
3255732421
3255832422 if (!init_val.eql(Value.fromInterned(default_val), Type.fromInterned(field_ty), pt.zcu)) {
......@@ -32816,12 +32680,12 @@ fn analyzeRef(
3281632680 // In a comptime context, the store would fail, since the operand is runtime-known. But that's
3281732681 // okay; we don't actually need this store to succeed, since we're creating a runtime value in a
3281832682 // comptime scope, so the value can never be used aside from to get its type.
32819 if (!block.is_comptime) {
32683 if (!block.isComptime()) {
3282032684 try sema.storePtr(block, src, alloc, operand);
3282132685 }
3282232686
3282332687 // 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.
32688 // avoid errors in the `block.isComptime()` case.
3282532689 return block.addBitCast(ptr_type, alloc);
3282632690}
3282732691
......@@ -33184,24 +33048,24 @@ fn analyzeSlice(
3318433048 array_ty = double_child_ty;
3318533049 elem_ty = double_child_ty.childType(zcu);
3318633050 } else {
33187 const bounds_error_message = "slice of single-item pointer must have comptime-known bounds [0..0], [0..1], or [1..1]";
3318833051 if (uncasted_end_opt == .none) {
33189 return sema.fail(block, src, bounds_error_message, .{});
33052 return sema.fail(block, src, "slice of single-item pointer must be bounded", .{});
3319033053 }
3319133054 const start_value = try sema.resolveConstDefinedValue(
3319233055 block,
3319333056 start_src,
3319433057 uncasted_start,
33195 .{ .needed_comptime_reason = bounds_error_message },
33058 .{ .simple = .slice_single_item_ptr_bounds },
3319633059 );
3319733060
3319833061 const end_value = try sema.resolveConstDefinedValue(
3319933062 block,
3320033063 end_src,
3320133064 uncasted_end_opt,
33202 .{ .needed_comptime_reason = bounds_error_message },
33065 .{ .simple = .slice_single_item_ptr_bounds },
3320333066 );
3320433067
33068 const bounds_error_message = "slice of single-item pointer must have bounds [0..0], [0..1], or [1..1]";
3320533069 if (try sema.compareScalar(start_value, .neq, end_value, Type.comptime_int)) {
3320633070 if (try sema.compareScalar(start_value, .neq, Value.zero_comptime_int, Type.comptime_int)) {
3320733071 const msg = msg: {
......@@ -33416,9 +33280,7 @@ fn analyzeSlice(
3341633280 if (sentinel_opt != .none) {
3341733281 const casted = try sema.coerce(block, elem_ty, sentinel_opt, sentinel_src);
3341833282 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 });
33283 break :s try sema.resolveConstDefinedValue(block, sentinel_src, casted, .{ .simple = .slice_sentinel });
3342233284 }
3342333285 // If we are slicing to the end of something that is sentinel-terminated
3342433286 // then the resulting slice type is also sentinel-terminated.
......@@ -33499,9 +33361,9 @@ fn analyzeSlice(
3349933361 runtime_src = end_src;
3350033362 }
3350133363
33502 if (!checked_start_lte_end and block.wantSafety() and !block.is_comptime) {
33364 if (!checked_start_lte_end and block.wantSafety() and !block.isComptime()) {
3350333365 // requirement: start <= end
33504 assert(!block.is_comptime);
33366 assert(!block.isComptime());
3350533367 try sema.requireRuntimeBlock(block, src, runtime_src.?);
3350633368 const ok = try block.addBinOp(.cmp_lte, start, end);
3350733369 try sema.addSafetyCheckCall(block, src, ok, "startGreaterThanEnd", &.{ start, end });
......@@ -35859,7 +35721,7 @@ fn backingIntType(
3585935721 .namespace = struct_type.namespace,
3586035722 .instructions = .{},
3586135723 .inlining = null,
35862 .is_comptime = true,
35724 .comptime_reason = null, // set below if needed
3586335725 .src_base_inst = struct_type.zir_index,
3586435726 .type_name_ctx = struct_type.name,
3586535727 };
......@@ -35899,6 +35761,10 @@ fn backingIntType(
3589935761 .base_node_inst = struct_type.zir_index,
3590035762 .offset = .{ .node_offset_container_tag = 0 },
3590135763 };
35764 block.comptime_reason = .{ .reason = .{
35765 .src = backing_int_src,
35766 .r = .{ .simple = .type },
35767 } };
3590235768 const backing_int_ty = blk: {
3590335769 if (backing_int_body_len == 0) {
3590435770 const backing_int_ref: Zir.Inst.Ref = @enumFromInt(zir.extra[extra_index]);
......@@ -36512,7 +36378,13 @@ fn structFields(
3651236378 .namespace = namespace_index,
3651336379 .instructions = .{},
3651436380 .inlining = null,
36515 .is_comptime = true,
36381 .comptime_reason = .{ .reason = .{
36382 .src = .{
36383 .base_node_inst = struct_type.zir_index,
36384 .offset = .nodeOffset(0),
36385 },
36386 .r = .{ .simple = .struct_fields },
36387 } },
3651636388 .src_base_inst = struct_type.zir_index,
3651736389 .type_name_ctx = struct_type.name,
3651836390 };
......@@ -36698,7 +36570,7 @@ fn structFieldInits(
3669836570 .namespace = namespace_index,
3669936571 .instructions = .{},
3670036572 .inlining = null,
36701 .is_comptime = true,
36573 .comptime_reason = undefined, // set when `block_scope` is used
3670236574 .src_base_inst = struct_type.zir_index,
3670336575 .type_name_ctx = struct_type.name,
3670436576 };
......@@ -36776,13 +36648,13 @@ fn structFieldInits(
3677636648 .offset = .{ .container_field_value = @intCast(field_i) },
3677736649 };
3677836650
36651 block_scope.comptime_reason = .{ .reason = .{
36652 .src = init_src,
36653 .r = .{ .simple = .struct_field_default_value },
36654 } };
3677936655 const init = try sema.resolveInlineBody(&block_scope, body, zir_index);
3678036656 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 };
36657 const default_val = try sema.resolveConstValue(&block_scope, init_src, coerced, null);
3678636658
3678736659 if (default_val.canMutateComptimeVarState(zcu)) {
3678836660 return sema.fail(&block_scope, init_src, "field default value contains reference to comptime-mutable memory", .{});
......@@ -36850,20 +36722,26 @@ fn unionFields(
3685036722 const body = zir.bodySlice(extra_index, body_len);
3685136723 extra_index += body.len;
3685236724
36725 const src: LazySrcLoc = .{
36726 .base_node_inst = union_type.zir_index,
36727 .offset = .nodeOffset(0),
36728 };
36729
3685336730 var block_scope: Block = .{
3685436731 .parent = null,
3685536732 .sema = sema,
3685636733 .namespace = union_type.namespace,
3685736734 .instructions = .{},
3685836735 .inlining = null,
36859 .is_comptime = true,
36736 .comptime_reason = .{ .reason = .{
36737 .src = src,
36738 .r = .{ .simple = .union_fields },
36739 } },
3686036740 .src_base_inst = union_type.zir_index,
3686136741 .type_name_ctx = union_type.name,
3686236742 };
3686336743 defer assert(block_scope.instructions.items.len == 0);
3686436744
36865 const src = block_scope.nodeOffset(0);
36866
3686736745 if (body.len != 0) {
3686836746 _ = try sema.analyzeInlineBody(&block_scope, body, zir_index);
3686936747 }
......@@ -36993,9 +36871,7 @@ fn unionFields(
3699336871 if (enum_field_vals.capacity() > 0) {
3699436872 const enum_tag_val = if (tag_ref != .none) blk: {
3699536873 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 });
36874 const val = try sema.resolveConstDefinedValue(&block_scope, value_src, coerced, .{ .simple = .enum_field_tag_value });
3699936875 last_tag_val = val;
3700036876
3700136877 break :blk val;
......@@ -37669,9 +37545,7 @@ pub fn analyzeAsAddressSpace(
3766937545 const zcu = pt.zcu;
3767037546 const addrspace_ty = try sema.getBuiltinType("AddressSpace");
3767137547 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 });
37548 const addrspace_val = try sema.resolveConstDefinedValue(block, src, coerced, .{ .simple = .@"addrspace" });
3767537549 const address_space = zcu.toEnum(std.builtin.AddressSpace, addrspace_val);
3767637550 const target = pt.zcu.getTarget();
3767737551 const arch = target.cpu.arch;
......@@ -38560,7 +38434,7 @@ fn sliceToIpString(
3856038434 block: *Block,
3856138435 src: LazySrcLoc,
3856238436 slice_val: Value,
38563 reason: NeededComptimeReason,
38437 reason: ComptimeReason,
3856438438) CompileError!InternPool.NullTerminatedString {
3856538439 const pt = sema.pt;
3856638440 const zcu = pt.zcu;
......@@ -38580,7 +38454,7 @@ fn derefSliceAsArray(
3858038454 block: *Block,
3858138455 src: LazySrcLoc,
3858238456 slice_val: Value,
38583 reason: NeededComptimeReason,
38457 reason: ComptimeReason,
3858438458) CompileError!Value {
3858538459 return try sema.maybeDerefSliceAsArray(block, src, slice_val) orelse {
3858638460 return sema.failWithNeededComptime(block, src, reason);
......@@ -38734,7 +38608,10 @@ pub fn resolveDeclaredEnum(
3873438608 .namespace = namespace,
3873538609 .instructions = .{},
3873638610 .inlining = null,
38737 .is_comptime = true,
38611 .comptime_reason = .{ .reason = .{
38612 .src = src,
38613 .r = .{ .simple = .enum_fields },
38614 } },
3873838615 .src_base_inst = tracked_inst,
3873938616 .type_name_ctx = type_name,
3874038617 };
......@@ -38798,9 +38675,7 @@ pub fn resolveDeclaredEnum(
3879838675 last_tag_val = try sema.resolveConstDefinedValue(&block, .{
3879938676 .base_node_inst = tracked_inst,
3880038677 .offset = .{ .container_field_name = field_i },
38801 }, tag_inst, .{
38802 .needed_comptime_reason = "enum tag value must be comptime-known",
38803 });
38678 }, tag_inst, .{ .simple = .enum_field_tag_value });
3880438679 if (!(try sema.intFitsInType(last_tag_val.?, int_tag_ty, null))) break :overflow true;
3880538680 last_tag_val = try pt.getCoerced(last_tag_val.?, int_tag_ty);
3880638681 if (wip_ty.nextField(ip, field_name, last_tag_val.?.toIntern())) |conflict| {
......@@ -38879,9 +38754,7 @@ fn getPanicInnerFn(
3887938754 const inner_name_ip = try ip.getOrPutString(gpa, pt.tid, inner_name, .no_embedded_nulls);
3888038755 const opt_fn_ref = try namespaceLookupVal(sema, block, src, outer_ty.getNamespaceIndex(zcu), inner_name_ip);
3888138756 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 });
38757 const fn_val = try sema.resolveConstValue(block, src, fn_ref, .{ .simple = .panic_handler });
3888538758 if (fn_val.typeOf(zcu).zigTypeTag(zcu) != .@"fn") {
3888638759 return sema.fail(block, src, "std.builtin.Panic.{s} is not a function", .{inner_name});
3888738760 }
......@@ -38963,9 +38836,7 @@ pub fn resolveNavPtrModifiers(
3896338836 const @"linksection": InternPool.OptionalNullTerminatedString = ls: {
3896438837 const linksection_body = zir_decl.linksection_body orelse break :ls .none;
3896538838 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 });
38839 const bytes = try sema.toConstString(block, section_src, linksection_ref, .{ .simple = .@"linksection" });
3896938840 if (std.mem.indexOfScalar(u8, bytes, 0) != null) {
3897038841 return sema.fail(block, section_src, "linksection cannot contain null bytes", .{});
3897138842 } 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+11-4
......@@ -273,6 +273,7 @@ const Writer = struct {
273273 .@"await",
274274 .make_ptr_const,
275275 .validate_deref,
276 .validate_const,
276277 .check_comptime_control_flow,
277278 .opt_eu_base_ptr_init,
278279 .restore_err_ret_index_unconditional,
......@@ -437,7 +438,6 @@ const Writer = struct {
437438 .field_call => try self.writeCall(stream, inst, .field),
438439
439440 .block,
440 .block_comptime,
441441 .block_inline,
442442 .suspend_block,
443443 .loop,
......@@ -445,6 +445,8 @@ const Writer = struct {
445445 .typeof_builtin,
446446 => try self.writeBlock(stream, inst),
447447
448 .block_comptime => try self.writeBlockComptime(stream, inst),
449
448450 .condbr,
449451 .condbr_inline,
450452 => try self.writeCondBr(stream, inst),
......@@ -1343,16 +1345,21 @@ const Writer = struct {
13431345
13441346 fn writeBlock(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
13451347 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1346 try self.writePlNodeBlockWithoutSrc(stream, inst);
1348 const extra = self.code.extraData(Zir.Inst.Block, inst_data.payload_index);
1349 const body = self.code.bodySlice(extra.end, extra.data.body_len);
1350 try self.writeBracedBody(stream, body);
1351 try stream.writeAll(") ");
13471352 try self.writeSrcNode(stream, inst_data.src_node);
13481353 }
13491354
1350 fn writePlNodeBlockWithoutSrc(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
1355 fn writeBlockComptime(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
13511356 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);
1357 const extra = self.code.extraData(Zir.Inst.BlockComptime, inst_data.payload_index);
13531358 const body = self.code.bodySlice(extra.end, extra.data.body_len);
1359 try stream.print("reason={s}, ", .{@tagName(extra.data.reason)});
13541360 try self.writeBracedBody(stream, body);
13551361 try stream.writeAll(") ");
1362 try self.writeSrcNode(stream, inst_data.src_node);
13561363 }
13571364
13581365 fn writeCondBr(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
test/cases/compile_errors/address_of_threadlocal_not_comptime_known.zig+2-2
......@@ -10,5 +10,5 @@ pub export fn entry() void {
1010// target=native
1111//
1212// :2:36: error: unable to resolve comptime value
13// :2:36: note: global variable initializer must be comptime-known
14// :2:36: note: thread local and dll imported variables have runtime-known addresses
13// :2:36: note: initializer of container-level variable must be comptime-known
14// :2:36: note: threadlocal and dll imported variables have runtime-known addresses
test/cases/compile_errors/anytype_param_requires_comptime.zig+3-3
......@@ -13,8 +13,8 @@ pub export fn entry() void {
1313}
1414
1515// error
16// backend=stage2
17// target=native
1816//
1917// :7:25: error: unable to resolve comptime value
20// :7:25: note: initializer of comptime only struct must be comptime-known
18// :7:25: note: initializer of comptime-only struct 'tmp.S.foo__anon_166.C' must be comptime-known
19// :4:16: note: struct requires comptime because of this field
20// :4:16: note: types are not available at runtime
test/cases/compile_errors/array_mult_with_number_type.zig+4-5
......@@ -1,10 +1,9 @@
1export fn entry(base: f32, exponent: f32) f32 {
1const exponent: f32 = 1.0;
2export fn entry(base: f32) f32 {
23 return base ** exponent;
34}
45
56// error
6// backend=stage2
7// target=native
87//
9// :2:12: error: expected indexable; found 'f32'
10// :2:17: note: this operator multiplies arrays; use std.math.pow for exponentiation
8// :3:12: error: expected indexable; found 'f32'
9// :3:17: note: this operator multiplies arrays; use std.math.pow for exponentiation
test/cases/compile_errors/asm_at_compile_time.zig+2-1
......@@ -15,4 +15,5 @@ fn doSomeAsm() void {
1515// target=native
1616//
1717// :6:5: error: unable to evaluate comptime expression
18// :2:14: note: called from here
18// :2:14: note: called at comptime from here
19// :1:1: note: 'comptime' keyword forces comptime evaluation
test/cases/compile_errors/attempted_double_pipe_on_boolean_values.zig+3-5
......@@ -1,13 +1,11 @@
1export fn entry(a: bool, b: bool) i32 {
2 if (a || b) {
1export fn entry() i32 {
2 if (true || false) {
33 return 1234;
44 }
55 return 5678;
66}
77
88// error
9// backend=stage2
10// target=native
119//
1210// :2:9: error: expected error set type, found 'bool'
13// :2:11: note: '||' merges error sets; 'or' performs boolean OR
11// :2:14: note: '||' merges error sets; 'or' performs boolean OR
test/cases/compile_errors/branch_in_comptime_only_scope_uses_condbr_inline.zig+2
......@@ -20,4 +20,6 @@ pub export fn entry2() void {
2020//
2121// :5:15: error: unable to evaluate comptime expression
2222// :5:13: note: operation is runtime due to this operand
23// :4:72: note: '@shuffle' mask must be comptime-known
2324// :13:11: error: unable to evaluate comptime expression
25// :12:72: note: '@shuffle' mask must be comptime-known
test/cases/compile_errors/builtin_extern_in_comptime_scope.zig+4-4
......@@ -11,8 +11,8 @@ pub export fn entry2() void {
1111// target=native
1212//
1313// :1:16: error: unable to resolve comptime value
14// :1:16: note: global variable initializer must be comptime-known
15// :1:16: note: thread local and dll imported variables have runtime-known addresses
14// :1:16: note: initializer of container-level variable must be comptime-known
15// :1:16: note: threadlocal and dll imported variables have runtime-known addresses
1616// :2:17: error: unable to resolve comptime value
17// :2:17: note: global variable initializer must be comptime-known
18// :2:17: note: thread local and dll imported variables have runtime-known addresses
17// :2:17: note: initializer of container-level variable must be comptime-known
18// :2:17: note: threadlocal and dll imported variables have runtime-known addresses
test/cases/compile_errors/compile_time_struct_field.zig+8-9
......@@ -4,16 +4,15 @@ const S = struct {
44};
55
66export fn a() void {
7 var value: u32 = 3;
8 const comptimeStruct = S {
9 .normal_ptr = &value,
10 };
11 _ = comptimeStruct;
7 var value: u32 = 3;
8 const comptimeStruct = S{
9 .normal_ptr = &value,
10 };
11 _ = comptimeStruct;
1212}
1313
1414// error
15// backend=stage2
16// target=native
1715//
18// 9:6: error: unable to resolve comptime value
19// 9:6: note: initializer of comptime only struct must be comptime-known
16// :9:10: error: unable to resolve comptime value
17// :9:10: note: initializer of comptime-only struct 'tmp.S' must be comptime-known
18// :2:21: note: struct requires comptime because of this field
test/cases/compile_errors/condition_comptime_reason_explained.zig+4-8
......@@ -33,18 +33,14 @@ pub export fn entry2() void {
3333}
3434
3535// error
36// backend=stage2
37// target=native
3836//
3937// :8:9: error: unable to resolve comptime value
40// :8:9: note: condition in comptime branch must be comptime-known
41// :7:13: note: expression is evaluated at comptime because the function returns a comptime-only type 'tmp.S'
38// :19:15: note: called at comptime from here
39// :7:13: note: function with comptime-only return type 'tmp.S' is evaluated at comptime
4240// :2:12: note: struct requires comptime because of this field
4341// :2:12: note: use '*const fn () void' for a function pointer type
44// :19:15: note: called from here
4542// :22:13: error: unable to resolve comptime value
46// :22:13: note: condition in comptime switch must be comptime-known
47// :21:17: note: expression is evaluated at comptime because the function returns a comptime-only type 'tmp.S'
43// :32:19: note: called at comptime from here
44// :21:17: note: function with comptime-only return type 'tmp.S' is evaluated at comptime
4845// :2:12: note: struct requires comptime because of this field
4946// :2:12: note: use '*const fn () void' for a function pointer type
50// :32:19: note: called from here
test/cases/compile_errors/enum_backed_by_comptime_int_must_be_casted_from_comptime_value.zig+1-1
......@@ -11,4 +11,4 @@ export fn entry() void {
1111// target=native
1212//
1313// :6:31: error: unable to resolve comptime value
14// :6:31: note: value being casted to enum with 'comptime_int' tag type must be comptime-known
14// :6:31: note: value casted to enum with 'comptime_int' tag type must be comptime-known
test/cases/compile_errors/error_in_typeof_param.zig+1-1
......@@ -11,4 +11,4 @@ pub export fn entry() void {
1111// target=native
1212//
1313// :6:31: error: unable to resolve comptime value
14// :6:31: note: value being casted to 'comptime_int' must be comptime-known
14// :6:31: note: value casted to 'comptime_int' must be comptime-known
test/cases/compile_errors/explain_why_fn_is_called_at_comptime.zig+2-5
......@@ -4,7 +4,7 @@ const S = struct {
44};
55fn bar() void {}
66
7fn foo(comptime a: *u8) S {
7fn foo(a: *u8) S {
88 return .{ .fnPtr = bar, .a = a.* };
99}
1010pub export fn entry() void {
......@@ -13,11 +13,8 @@ pub export fn entry() void {
1313}
1414
1515// error
16// backend=stage2
17// target=native
1816//
1917// :12:13: error: unable to resolve comptime value
20// :12:13: note: argument to function being called at comptime must be comptime-known
21// :7:25: note: expression is evaluated at comptime because the function returns a comptime-only type 'tmp.S'
18// :7:16: note: function with comptime-only return type 'tmp.S' is evaluated at comptime
2219// :2:12: note: struct requires comptime because of this field
2320// :2:12: note: use '*const fn () void' for a function pointer type
test/cases/compile_errors/explain_why_generic_fn_is_called_at_comptime.zig+3-4
......@@ -15,9 +15,8 @@ pub export fn entry() void {
1515 _ = foo(a, fn () void);
1616}
1717// error
18// backend=stage2
19// target=native
2018//
2119// :15:13: error: unable to resolve comptime value
22// :15:13: note: argument to function being called at comptime must be comptime-known
23// :9:38: note: expression is evaluated at comptime because the generic function was instantiated with a comptime-only return type
20// :9:38: note: generic function instantiated with comptime-only return type 'tmp.S(fn () void)' is evaluated at comptime
21// :3:16: note: struct requires comptime because of this field
22// :3:16: note: use '*const fn () void' for a function pointer type
test/cases/compile_errors/global_var_struct_init_in_comptim_block.zig deleted-14
......@@ -1,14 +0,0 @@
1const Foo = struct {
2 x: i32,
3};
4var x: Foo = .{ .x = 2 };
5comptime {
6 x = .{ .x = 3 };
7}
8
9// error
10// backend=stage2
11// target=native
12//
13// :6:17: error: unable to evaluate comptime expression
14// :6:17: note: operation is runtime due to this operand
test/cases/compile_errors/global_var_struct_init_in_comptime_block.zig created+15
......@@ -0,0 +1,15 @@
1const Foo = struct {
2 x: i32,
3};
4var x: Foo = .{ .x = 2 };
5comptime {
6 x = .{ .x = 3 };
7}
8
9// error
10// backend=stage2
11// target=native
12//
13// :6:17: error: unable to evaluate comptime expression
14// :6:17: note: operation is runtime due to this operand
15// :5:1: note: 'comptime' keyword forces comptime evaluation
test/cases/compile_errors/global_variable_stored_in_global_const.zig+1-3
......@@ -5,8 +5,6 @@ pub export fn entry() void {
55}
66
77// error
8// backend=stage2
9// target=native
108//
119// :2:11: error: unable to resolve comptime value
12// :2:11: note: global variable initializer must be comptime-known
10// :2:11: note: initializer of container-level variable must be comptime-known
test/cases/compile_errors/incorrect_pointer_dereference_syntax.zig+1-4
......@@ -1,12 +1,9 @@
11pub export fn entry() void {
2 var a: *u32 = undefined;
2 const a: *u32 = undefined;
33 _ = *a;
4 _ = &a;
54}
65
76// error
8// backend=stage2
9// target=native
107//
118// :3:10: error: expected type 'type', found '*u32'
129// :3:10: note: use '.*' to dereference pointer
test/cases/compile_errors/inline_call_runtime_value_to_comptime_param.zig+1-1
......@@ -14,4 +14,4 @@ pub export fn entry() void {
1414// target=native
1515//
1616// :5:18: error: unable to resolve comptime value
17// :5:18: note: parameter is comptime
17// :5:18: note: argument to comptime parameter must be comptime-known
test/cases/compile_errors/int-float_conversion_to_comptime_int-float.zig+2-4
......@@ -10,10 +10,8 @@ export fn bar() void {
1010}
1111
1212// error
13// backend=stage2
14// target=native
1513//
1614// :4:41: error: unable to resolve comptime value
17// :4:41: note: value being casted to 'comptime_int' must be comptime-known
15// :4:41: note: value casted to 'comptime_int' must be comptime-known
1816// :9:43: error: unable to resolve comptime value
19// :9:43: note: value being casted to 'comptime_float' must be comptime-known
17// :9:43: note: value casted to 'comptime_float' must be comptime-known
test/cases/compile_errors/non-const_expression_function_call_with_struct_return_value_outside_function.zig+2-3
......@@ -13,9 +13,8 @@ export fn entry() usize {
1313}
1414
1515// error
16// backend=stage2
17// target=native
1816//
1917// :6:24: error: unable to evaluate comptime expression
2018// :6:5: note: operation is runtime due to this operand
21// :4:17: note: called from here
19// :4:17: note: called at comptime from here
20// :4:17: note: initializer of container-level variable must be comptime-known
test/cases/compile_errors/non-pure_function_returns_type.zig+2-1
......@@ -23,4 +23,5 @@ export fn function_with_return_type_type() void {
2323//
2424// :3:7: error: unable to evaluate comptime expression
2525// :3:5: note: operation is runtime due to this operand
26// :16:19: note: called from here
26// :16:19: note: called at comptime from here
27// :16:19: note: types must be comptime-known
test/cases/compile_errors/non_comptime_param_in_comptime_function.zig+1-4
......@@ -9,10 +9,7 @@ export fn entry() void {
99}
1010
1111// error
12// backend=stage2
13// target=native
1412//
1513// :8:11: error: unable to resolve comptime value
16// :8:11: note: argument to function being called at comptime must be comptime-known
17// :1:20: note: expression is evaluated at comptime because the function returns a comptime-only type 'type'
14// :1:20: note: function with comptime-only return type 'type' is evaluated at comptime
1815// :1:20: note: types are not available at runtime
test/cases/compile_errors/non_constant_expression_in_array_size.zig+2-2
......@@ -15,5 +15,5 @@ export fn entry() usize {
1515// target=native
1616//
1717// :6:12: error: unable to resolve comptime value
18// :6:12: note: value being returned at comptime must be comptime-known
19// :2:12: note: called from here
18// :2:12: note: called at comptime from here
19// :1:13: note: struct fields must be comptime-known
test/cases/compile_errors/runtime_assignment_to_comptime_struct_type.zig+3-3
......@@ -10,8 +10,8 @@ export fn f() void {
1010}
1111
1212// error
13// backend=stage2
14// target=native
1513//
1614// :7:23: error: unable to resolve comptime value
17// :7:23: note: initializer of comptime only struct must be comptime-known
15// :7:23: note: initializer of comptime-only struct 'tmp.Foo' must be comptime-known
16// :3:10: note: struct requires comptime because of this field
17// :3:10: note: types are not available at runtime
test/cases/compile_errors/runtime_assignment_to_comptime_union_type.zig+3-3
......@@ -10,8 +10,8 @@ export fn f() void {
1010}
1111
1212// error
13// backend=stage2
14// target=native
1513//
1614// :8:23: error: unable to resolve comptime value
17// :8:23: note: initializer of comptime only union must be comptime-known
15// :8:23: note: initializer of comptime-only union 'tmp.Foo' must be comptime-known
16// :3:10: note: union requires comptime because of this field
17// :3:10: note: types are not available at runtime
test/cases/compile_errors/runtime_operation_in_comptime_scope.zig created+36
......@@ -0,0 +1,36 @@
1export fn entry1() void {
2 foo();
3}
4
5comptime {
6 qux();
7}
8
9inline fn foo() void {
10 _ = bar();
11}
12
13fn bar() type {
14 qux();
15 return u8;
16}
17
18fn qux() void {
19 rt = 123;
20}
21
22var rt: u32 = undefined;
23
24// error
25//
26// :19:8: error: unable to evaluate comptime expression
27// :19:5: note: operation is runtime due to this operand
28// :14:8: note: called at comptime from here
29// :10:12: note: called at comptime from here
30// :13:10: note: function with comptime-only return type 'type' is evaluated at comptime
31// :13:10: note: types are not available at runtime
32// :2:8: note: called from here
33// :19:8: error: unable to evaluate comptime expression
34// :19:5: note: operation is runtime due to this operand
35// :6:8: note: called at comptime from here
36// :5:1: note: 'comptime' keyword forces comptime evaluation
test/cases/compile_errors/runtime_to_comptime_num.zig+4-4
......@@ -26,10 +26,10 @@ pub export fn entry4() void {
2626// target=native
2727//
2828// :4:27: error: unable to resolve comptime value
29// :4:27: note: value being casted to 'comptime_int' must be comptime-known
29// :4:27: note: value casted to 'comptime_int' must be comptime-known
3030// :9:29: error: unable to resolve comptime value
31// :9:29: note: value being casted to 'comptime_float' must be comptime-known
31// :9:29: note: value casted to 'comptime_float' must be comptime-known
3232// :15:10: error: unable to resolve comptime value
33// :15:10: note: value being casted to 'comptime_float' must be comptime-known
33// :15:10: note: value casted to 'comptime_float' must be comptime-known
3434// :21:10: error: unable to resolve comptime value
35// :21:10: note: value being casted to 'comptime_int' must be comptime-known
35// :21:10: note: value casted to 'comptime_int' must be comptime-known
test/cases/compile_errors/runtime_value_in_comptime_scope.zig created+61
......@@ -0,0 +1,61 @@
1var rt_val: [5]u32 = .{ 1, 2, 3, 4, 5 };
2
3comptime {
4 _ = rt_val; // fine
5}
6
7comptime {
8 const a = rt_val; // error
9 _ = a;
10}
11
12comptime {
13 const l = rt_val.len; // fine
14 @compileLog(l);
15}
16
17export fn foo() void {
18 _ = comptime rt_val; // error
19}
20
21export fn bar() void {
22 const l = comptime rt_val.len; // fine
23 @compileLog(l);
24}
25
26export fn baz() void {
27 const S = struct {
28 fn inner() void {
29 _ = comptime rt_val;
30 }
31 };
32 comptime S.inner(); // fine; inner comptime is a nop
33 S.inner(); // error
34}
35
36export fn qux() void {
37 const S = struct {
38 fn inner() void {
39 const a = rt_val;
40 _ = a;
41 }
42 };
43 S.inner(); // fine; everything is runtime
44 comptime S.inner(); // error
45}
46
47// error
48//
49// :8:15: error: unable to resolve comptime value
50// :7:1: note: 'comptime' keyword forces comptime evaluation
51// :18:9: error: unable to resolve comptime value
52// :18:9: note: 'comptime' keyword forces comptime evaluation
53// :29:17: error: unable to resolve comptime value
54// :29:17: note: 'comptime' keyword forces comptime evaluation
55// :39:23: error: unable to resolve comptime value
56// :44:21: note: called at comptime from here
57// :44:5: note: 'comptime' keyword forces comptime evaluation
58//
59// Compile Log Output:
60// @as(usize, 5)
61// @as(usize, 5)
test/cases/compile_errors/slice_of_single-item_pointer_bounds.zig+5-5
......@@ -31,13 +31,13 @@ export fn entry2() void {
3131
3232// error
3333//
34// :5:12: error: slice of single-item pointer must have comptime-known bounds [0..0], [0..1], or [1..1]
35// :9:13: error: slice of single-item pointer must have comptime-known bounds [0..0], [0..1], or [1..1]
34// :5:12: error: slice of single-item pointer must be bounded
35// :9:13: error: slice of single-item pointer must have bounds [0..0], [0..1], or [1..1]
3636// :9:13: note: expected '0', found '1'
37// :13:16: error: slice of single-item pointer must have comptime-known bounds [0..0], [0..1], or [1..1]
37// :13:16: error: slice of single-item pointer must have bounds [0..0], [0..1], or [1..1]
3838// :13:16: note: expected '1', found '2'
3939// :17:16: error: end index 2 out of bounds for slice of single-item pointer
4040// :23:13: error: unable to resolve comptime value
41// :23:13: note: slice of single-item pointer must have comptime-known bounds [0..0], [0..1], or [1..1]
41// :23:13: note: slice of single-item pointer must have comptime-known bounds
4242// :29:16: error: unable to resolve comptime value
43// :29:16: note: slice of single-item pointer must have comptime-known bounds [0..0], [0..1], or [1..1]
43// :29:16: note: slice of single-item pointer must have comptime-known bounds
test/cases/compile_errors/unable_to_evaluate_comptime_expr.zig+3-2
......@@ -33,12 +33,13 @@ pub export fn entry3() void {
3333}
3434
3535// error
36// backend=stage2
37// target=native
3836//
3937// :7:13: error: unable to evaluate comptime expression
4038// :7:16: note: operation is runtime due to this operand
39// :7:13: note: initializer of container-level variable must be comptime-known
4140// :13:13: error: unable to evaluate comptime expression
4241// :13:16: note: operation is runtime due to this operand
42// :13:13: note: initializer of container-level variable must be comptime-known
4343// :22:9: error: unable to evaluate comptime expression
4444// :22:21: note: operation is runtime due to this operand
45// :21:13: note: enum fields must be comptime-known
test/cases/compile_errors/unable_to_evaluate_expr_inside_cimport.zig+1-1
......@@ -12,4 +12,4 @@ export fn entry() void {
1212//
1313// :2:11: error: unable to evaluate comptime expression
1414// :2:13: note: operation is runtime due to this operand
15// :1:11: note: expression is evaluated at comptime because it is inside a @cImport
15// :1:11: note: operand to '@cImport' is evaluated at comptime
test/compile_errors.zig+5-3
......@@ -57,8 +57,8 @@ pub fn addCases(ctx: *Cases, b: *std.Build) !void {
5757 \\}
5858 , &[_][]const u8{
5959 ":3:12: error: unable to resolve comptime value",
60 ":3:12: note: argument to function being called at comptime must be comptime-known",
61 ":2:55: note: expression is evaluated at comptime because the generic function was instantiated with a comptime-only return type",
60 ":2:55: note: generic function instantiated with comptime-only return type '?fn () void' is evaluated at comptime",
61 ":2:55: note: use '*const fn () void' for a function pointer type",
6262 });
6363 case.addSourceFile("b.zig",
6464 \\pub const ElfDynLib = struct {
......@@ -198,7 +198,9 @@ pub fn addCases(ctx: *Cases, b: *std.Build) !void {
198198 ":8:36: error: runtime-known argument passed to comptime parameter",
199199 ":2:41: note: declared comptime here",
200200 ":13:32: error: unable to resolve comptime value",
201 ":13:32: note: initializer of comptime only struct must be comptime-known",
201 ":13:32: note: initializer of comptime-only struct 'tmp.callAnytypeFunctionWithRuntimeComptimeOnlyType.S' must be comptime-known",
202 ":12:35: note: struct requires comptime because of this field",
203 ":12:35: note: types are not available at runtime",
202204 });
203205
204206 case.addSourceFile("import.zig",