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

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

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

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

lib/std/zig.zig+159
...@@ -718,6 +718,165 @@ pub const EnvVar = enum {...@@ -718,6 +718,165 @@ pub const EnvVar = enum {
718 }718 }
719};719};
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
721test {880test {
722 _ = Ast;881 _ = Ast;
723 _ = AstRlAnnotate;882 _ = AstRlAnnotate;
lib/std/zig/AstGen.zig+146-65
...@@ -97,6 +97,7 @@ fn setExtra(astgen: *AstGen, index: usize, extra: anytype) void {...@@ -97,6 +97,7 @@ fn setExtra(astgen: *AstGen, index: usize, extra: anytype) void {
97 Zir.Inst.Ref,97 Zir.Inst.Ref,
98 Zir.Inst.Index,98 Zir.Inst.Index,
99 Zir.Inst.Declaration.Name,99 Zir.Inst.Declaration.Name,
100 std.zig.SimpleComptimeReason,
100 Zir.NullTerminatedString,101 Zir.NullTerminatedString,
101 => @intFromEnum(@field(extra, field.name)),102 => @intFromEnum(@field(extra, field.name)),
102103
...@@ -379,7 +380,7 @@ const coerced_type_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .type_type } };...@@ -379,7 +380,7 @@ const coerced_type_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .type_type } };
379const coerced_bool_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .bool_type } };380const coerced_bool_ri: ResultInfo = .{ .rl = .{ .coerced_ty = .bool_type } };
380381
381fn typeExpr(gz: *GenZir, scope: *Scope, type_node: Ast.Node.Index) InnerError!Zir.Inst.Ref {382fn 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);
383}384}
384385
385fn reachableTypeExpr(386fn reachableTypeExpr(
...@@ -388,7 +389,7 @@ fn reachableTypeExpr(...@@ -388,7 +389,7 @@ fn reachableTypeExpr(
388 type_node: Ast.Node.Index,389 type_node: Ast.Node.Index,
389 reachable_node: Ast.Node.Index,390 reachable_node: Ast.Node.Index,
390) InnerError!Zir.Inst.Ref {391) 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);
392}393}
393394
394/// Same as `expr` but fails with a compile error if the result type is `noreturn`.395/// Same as `expr` but fails with a compile error if the result type is `noreturn`.
...@@ -399,7 +400,7 @@ fn reachableExpr(...@@ -399,7 +400,7 @@ fn reachableExpr(
399 node: Ast.Node.Index,400 node: Ast.Node.Index,
400 reachable_node: Ast.Node.Index,401 reachable_node: Ast.Node.Index,
401) InnerError!Zir.Inst.Ref {402) InnerError!Zir.Inst.Ref {
402 return reachableExprComptime(gz, scope, ri, node, reachable_node, false);403 return reachableExprComptime(gz, scope, ri, node, reachable_node, null);
403}404}
404405
405fn reachableExprComptime(406fn reachableExprComptime(
...@@ -408,10 +409,11 @@ fn reachableExprComptime(...@@ -408,10 +409,11 @@ fn reachableExprComptime(
408 ri: ResultInfo,409 ri: ResultInfo,
409 node: Ast.Node.Index,410 node: Ast.Node.Index,
410 reachable_node: Ast.Node.Index,411 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,
412) InnerError!Zir.Inst.Ref {414) InnerError!Zir.Inst.Ref {
413 const result_inst = if (force_comptime)415 const result_inst = if (comptime_reason) |r|
414 try comptimeExpr(gz, scope, ri, node)416 try comptimeExpr(gz, scope, ri, node, r)
415 else417 else
416 try expr(gz, scope, ri, node);418 try expr(gz, scope, ri, node);
417419
...@@ -782,7 +784,7 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE...@@ -782,7 +784,7 @@ fn expr(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) InnerE
782 const result = try gz.addPlNode(.array_mul, node, Zir.Inst.ArrayMul{784 const result = try gz.addPlNode(.array_mul, node, Zir.Inst.ArrayMul{
783 .res_ty = if (try ri.rl.resultType(gz, node)) |t| t else .none,785 .res_ty = if (try ri.rl.resultType(gz, node)) |t| t else .none,
784 .lhs = try expr(gz, scope, .{ .rl = .none }, node_datas[node].lhs),786 .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),
786 });788 });
787 return rvalue(gz, ri, result, node);789 return rvalue(gz, ri, result, node);
788 },790 },
...@@ -1453,7 +1455,7 @@ fn arrayInitExpr(...@@ -1453,7 +1455,7 @@ fn arrayInitExpr(
1453 });1455 });
1454 break :inst .{ array_type_inst, elem_type };1456 break :inst .{ array_type_inst, elem_type };
1455 } else {1457 } else {
1456 const sentinel = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = elem_type } }, array_type.ast.sentinel);1458 const sentinel = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = elem_type } }, array_type.ast.sentinel, .array_sentinel);
1457 const array_type_inst = try gz.addPlNode(1459 const array_type_inst = try gz.addPlNode(
1458 .array_type_sentinel,1460 .array_type_sentinel,
1459 array_init.ast.type_expr,1461 array_init.ast.type_expr,
...@@ -1721,7 +1723,7 @@ fn structInitExpr(...@@ -1721,7 +1723,7 @@ fn structInitExpr(
1721 .rhs = elem_type,1723 .rhs = elem_type,
1722 });1724 });
1723 } else blk: {1725 } else blk: {
1724 const sentinel = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = elem_type } }, array_type.ast.sentinel);1726 const sentinel = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = elem_type } }, array_type.ast.sentinel, .array_sentinel);
1725 break :blk try gz.addPlNode(1727 break :blk try gz.addPlNode(
1726 .array_type_sentinel,1728 .array_type_sentinel,
1727 struct_init.ast.type_expr,1729 struct_init.ast.type_expr,
...@@ -1966,6 +1968,20 @@ fn comptimeExpr(...@@ -1966,6 +1968,20 @@ fn comptimeExpr(
1966 scope: *Scope,1968 scope: *Scope,
1967 ri: ResultInfo,1969 ri: ResultInfo,
1968 node: Ast.Node.Index,1970 node: Ast.Node.Index,
1971 reason: std.zig.SimpleComptimeReason,
1972) InnerError!Zir.Inst.Ref {
1973 return comptimeExpr2(gz, scope, ri, node, node, reason);
1974}
1975
1976/// Like `comptimeExpr`, but draws a distinction between `node`, the expression to evaluate at comptime,
1977/// and `src_node`, the node to attach to the `block_comptime`.
1978fn comptimeExpr2(
1979 gz: *GenZir,
1980 scope: *Scope,
1981 ri: ResultInfo,
1982 node: Ast.Node.Index,
1983 src_node: Ast.Node.Index,
1984 reason: std.zig.SimpleComptimeReason,
1969) InnerError!Zir.Inst.Ref {1985) InnerError!Zir.Inst.Ref {
1970 if (gz.is_comptime) {1986 if (gz.is_comptime) {
1971 // No need to change anything!1987 // No need to change anything!
...@@ -2049,23 +2065,23 @@ fn comptimeExpr(...@@ -2049,23 +2065,23 @@ fn comptimeExpr(
2049 block_scope.is_comptime = true;2065 block_scope.is_comptime = true;
2050 defer block_scope.unstack();2066 defer block_scope.unstack();
20512067
2052 const block_inst = try gz.makeBlockInst(.block_comptime, node);2068 const block_inst = try gz.makeBlockInst(.block_comptime, src_node);
2053 // Replace result location and copy back later - see above.2069 // Replace result location and copy back later - see above.
2054 const ty_only_ri: ResultInfo = .{2070 const ty_only_ri: ResultInfo = .{
2055 .ctx = ri.ctx,2071 .ctx = ri.ctx,
2056 .rl = if (try ri.rl.resultType(gz, node)) |res_ty|2072 .rl = if (try ri.rl.resultType(gz, src_node)) |res_ty|
2057 .{ .coerced_ty = res_ty }2073 .{ .coerced_ty = res_ty }
2058 else2074 else
2059 .none,2075 .none,
2060 };2076 };
2061 const block_result = try fullBodyExpr(&block_scope, scope, ty_only_ri, node, .normal);2077 const block_result = try fullBodyExpr(&block_scope, scope, ty_only_ri, node, .normal);
2062 if (!gz.refIsNoReturn(block_result)) {2078 if (!gz.refIsNoReturn(block_result)) {
2063 _ = try block_scope.addBreak(.@"break", block_inst, block_result);2079 _ = try block_scope.addBreak(.break_inline, block_inst, block_result);
2064 }2080 }
2065 try block_scope.setBlockBody(block_inst);2081 try block_scope.setBlockComptimeBody(block_inst, reason);
2066 try gz.instructions.append(gz.astgen.gpa, block_inst);2082 try gz.instructions.append(gz.astgen.gpa, block_inst);
20672083
2068 return rvalue(gz, ri, block_inst.toRef(), node);2084 return rvalue(gz, ri, block_inst.toRef(), src_node);
2069}2085}
20702086
2071/// This one is for an actual `comptime` syntax, and will emit a compile error if2087/// This one is for an actual `comptime` syntax, and will emit a compile error if
...@@ -2084,7 +2100,7 @@ fn comptimeExprAst(...@@ -2084,7 +2100,7 @@ fn comptimeExprAst(
2084 const tree = astgen.tree;2100 const tree = astgen.tree;
2085 const node_datas = tree.nodes.items(.data);2101 const node_datas = tree.nodes.items(.data);
2086 const body_node = node_datas[node].lhs;2102 const body_node = node_datas[node].lhs;
2087 return comptimeExpr(gz, scope, ri, body_node);2103 return comptimeExpr2(gz, scope, ri, body_node, node, .comptime_keyword);
2088}2104}
20892105
2090/// Restore the error return trace index. Performs the restore only if the result is a non-error or2106/// Restore the error return trace index. Performs the restore only if the result is a non-error or
...@@ -2494,10 +2510,10 @@ fn labeledBlockExpr(...@@ -2494,10 +2510,10 @@ fn labeledBlockExpr(
24942510
2495 // Reserve the Block ZIR instruction index so that we can put it into the GenZir struct2511 // Reserve the Block ZIR instruction index so that we can put it into the GenZir struct
2496 // so that break statements can reference it.2512 // so that break statements can reference it.
2497 const block_tag: Zir.Inst.Tag = if (force_comptime) .block_comptime else .block;2513 const block_inst = try gz.makeBlockInst(if (force_comptime) .block_comptime else .block, block_node);
2498 const block_inst = try gz.makeBlockInst(block_tag, block_node);
2499 try gz.instructions.append(astgen.gpa, block_inst);2514 try gz.instructions.append(astgen.gpa, block_inst);
2500 var block_scope = gz.makeSubBlock(parent_scope);2515 var block_scope = gz.makeSubBlock(parent_scope);
2516 block_scope.is_inline = force_comptime;
2501 block_scope.label = GenZir.Label{2517 block_scope.label = GenZir.Label{
2502 .token = label_token,2518 .token = label_token,
2503 .block_inst = block_inst,2519 .block_inst = block_inst,
...@@ -2511,14 +2527,20 @@ fn labeledBlockExpr(...@@ -2511,14 +2527,20 @@ fn labeledBlockExpr(
2511 // As our last action before the return, "pop" the error trace if needed2527 // As our last action before the return, "pop" the error trace if needed
2512 _ = try gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always, block_node);2528 _ = try gz.addRestoreErrRetIndex(.{ .block = block_inst }, .always, block_node);
2513 const result = try rvalue(gz, block_scope.break_result_info, .void_value, block_node);2529 const result = try rvalue(gz, block_scope.break_result_info, .void_value, block_node);
2514 _ = try block_scope.addBreak(.@"break", block_inst, result);2530 const break_tag: Zir.Inst.Tag = if (force_comptime) .break_inline else .@"break";
2531 _ = try block_scope.addBreak(break_tag, block_inst, result);
2515 }2532 }
25162533
2517 if (!block_scope.label.?.used) {2534 if (!block_scope.label.?.used) {
2518 try astgen.appendErrorTok(label_token, "unused block label", .{});2535 try astgen.appendErrorTok(label_token, "unused block label", .{});
2519 }2536 }
25202537
2521 try block_scope.setBlockBody(block_inst);2538 if (force_comptime) {
2539 try block_scope.setBlockComptimeBody(block_inst, .comptime_keyword);
2540 } else {
2541 try block_scope.setBlockBody(block_inst);
2542 }
2543
2522 if (need_result_rvalue) {2544 if (need_result_rvalue) {
2523 return rvalue(gz, ri, block_inst.toRef(), block_node);2545 return rvalue(gz, ri, block_inst.toRef(), block_node);
2524 } else {2546 } else {
...@@ -3255,7 +3277,7 @@ fn varDecl(...@@ -3255,7 +3277,7 @@ fn varDecl(
3255 } else .{ .rl = .none, .ctx = .const_init };3277 } else .{ .rl = .none, .ctx = .const_init };
3256 const prev_anon_name_strategy = gz.anon_name_strategy;3278 const prev_anon_name_strategy = gz.anon_name_strategy;
3257 gz.anon_name_strategy = .dbg_var;3279 gz.anon_name_strategy = .dbg_var;
3258 const init_inst = try reachableExprComptime(gz, scope, result_info, var_decl.ast.init_node, node, force_comptime);3280 const init_inst = try reachableExprComptime(gz, scope, result_info, var_decl.ast.init_node, node, if (force_comptime) .comptime_keyword else null);
3259 gz.anon_name_strategy = prev_anon_name_strategy;3281 gz.anon_name_strategy = prev_anon_name_strategy;
32603282
3261 try gz.addDbgVar(.dbg_var_val, ident_name, init_inst);3283 try gz.addDbgVar(.dbg_var_val, ident_name, init_inst);
...@@ -3321,7 +3343,7 @@ fn varDecl(...@@ -3321,7 +3343,7 @@ fn varDecl(
3321 const prev_anon_name_strategy = gz.anon_name_strategy;3343 const prev_anon_name_strategy = gz.anon_name_strategy;
3322 gz.anon_name_strategy = .dbg_var;3344 gz.anon_name_strategy = .dbg_var;
3323 defer gz.anon_name_strategy = prev_anon_name_strategy;3345 defer gz.anon_name_strategy = prev_anon_name_strategy;
3324 const init_inst = try reachableExprComptime(gz, scope, init_result_info, var_decl.ast.init_node, node, force_comptime);3346 const init_inst = try reachableExprComptime(gz, scope, init_result_info, var_decl.ast.init_node, node, if (force_comptime) .comptime_keyword else null);
33253347
3326 // The const init expression may have modified the error return trace, so signal3348 // The const init expression may have modified the error return trace, so signal
3327 // to Sema that it should save the new index for restoring later.3349 // to Sema that it should save the new index for restoring later.
...@@ -3393,7 +3415,14 @@ fn varDecl(...@@ -3393,7 +3415,14 @@ fn varDecl(
3393 };3415 };
3394 const prev_anon_name_strategy = gz.anon_name_strategy;3416 const prev_anon_name_strategy = gz.anon_name_strategy;
3395 gz.anon_name_strategy = .dbg_var;3417 gz.anon_name_strategy = .dbg_var;
3396 _ = try reachableExprComptime(gz, scope, result_info, var_decl.ast.init_node, node, is_comptime);3418 _ = try reachableExprComptime(
3419 gz,
3420 scope,
3421 result_info,
3422 var_decl.ast.init_node,
3423 node,
3424 if (var_decl.comptime_token != null) .comptime_keyword else null,
3425 );
3397 gz.anon_name_strategy = prev_anon_name_strategy;3426 gz.anon_name_strategy = prev_anon_name_strategy;
3398 const final_ptr: Zir.Inst.Ref = if (resolve_inferred) ptr: {3427 const final_ptr: Zir.Inst.Ref = if (resolve_inferred) ptr: {
3399 break :ptr try gz.addUnNode(.resolve_inferred_alloc, alloc, node);3428 break :ptr try gz.addUnNode(.resolve_inferred_alloc, alloc, node);
...@@ -3501,8 +3530,8 @@ fn assignDestructure(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerErro...@@ -3501,8 +3530,8 @@ fn assignDestructure(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerErro
35013530
3502 if (full.comptime_token) |_| {3531 if (full.comptime_token) |_| {
3503 const comptime_block_inst = try gz.makeBlockInst(.block_comptime, node);3532 const comptime_block_inst = try gz.makeBlockInst(.block_comptime, node);
3504 _ = try inner_gz.addBreak(.@"break", comptime_block_inst, .void_value);3533 _ = try inner_gz.addBreak(.break_inline, comptime_block_inst, .void_value);
3505 try inner_gz.setBlockBody(comptime_block_inst);3534 try inner_gz.setBlockComptimeBody(comptime_block_inst, .comptime_keyword);
3506 try gz.instructions.append(gz.astgen.gpa, comptime_block_inst);3535 try gz.instructions.append(gz.astgen.gpa, comptime_block_inst);
3507 }3536 }
3508}3537}
...@@ -3673,8 +3702,8 @@ fn assignDestructureMaybeDecls(...@@ -3673,8 +3702,8 @@ fn assignDestructureMaybeDecls(
3673 // Finish the block_comptime. Inferred alloc resolution etc will occur3702 // Finish the block_comptime. Inferred alloc resolution etc will occur
3674 // in the parent block.3703 // in the parent block.
3675 const comptime_block_inst = try gz.makeBlockInst(.block_comptime, node);3704 const comptime_block_inst = try gz.makeBlockInst(.block_comptime, node);
3676 _ = try inner_gz.addBreak(.@"break", comptime_block_inst, .void_value);3705 _ = try inner_gz.addBreak(.break_inline, comptime_block_inst, .void_value);
3677 try inner_gz.setBlockBody(comptime_block_inst);3706 try inner_gz.setBlockComptimeBody(comptime_block_inst, .comptime_keyword);
3678 try gz.instructions.append(gz.astgen.gpa, comptime_block_inst);3707 try gz.instructions.append(gz.astgen.gpa, comptime_block_inst);
3679 }3708 }
36803709
...@@ -3867,7 +3896,16 @@ fn ptrType(...@@ -3867,7 +3896,16 @@ fn ptrType(
3867 gz.astgen.source_line = source_line;3896 gz.astgen.source_line = source_line;
3868 gz.astgen.source_column = source_column;3897 gz.astgen.source_column = source_column;
38693898
3870 sentinel_ref = try comptimeExpr(gz, scope, .{ .rl = .{ .ty = elem_type } }, ptr_info.ast.sentinel);3899 sentinel_ref = try comptimeExpr(
3900 gz,
3901 scope,
3902 .{ .rl = .{ .ty = elem_type } },
3903 ptr_info.ast.sentinel,
3904 switch (ptr_info.size) {
3905 .Slice => .slice_sentinel,
3906 else => .pointer_sentinel,
3907 },
3908 );
3871 trailing_count += 1;3909 trailing_count += 1;
3872 }3910 }
3873 if (ptr_info.ast.addrspace_node != 0) {3911 if (ptr_info.ast.addrspace_node != 0) {
...@@ -3953,7 +3991,7 @@ fn arrayType(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) !...@@ -3953,7 +3991,7 @@ fn arrayType(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.Index) !
3953 {3991 {
3954 return astgen.failNode(len_node, "unable to infer array size", .{});3992 return astgen.failNode(len_node, "unable to infer array size", .{});
3955 }3993 }
3956 const len = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, len_node);3994 const len = try reachableExprComptime(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, len_node, node, .type);
3957 const elem_type = try typeExpr(gz, scope, node_datas[node].rhs);3995 const elem_type = try typeExpr(gz, scope, node_datas[node].rhs);
39583996
3959 const result = try gz.addPlNode(.array_type, node, Zir.Inst.Bin{3997 const result = try gz.addPlNode(.array_type, node, Zir.Inst.Bin{
...@@ -3977,9 +4015,9 @@ fn arrayTypeSentinel(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node....@@ -3977,9 +4015,9 @@ fn arrayTypeSentinel(gz: *GenZir, scope: *Scope, ri: ResultInfo, node: Ast.Node.
3977 {4015 {
3978 return astgen.failNode(len_node, "unable to infer array size", .{});4016 return astgen.failNode(len_node, "unable to infer array size", .{});
3979 }4017 }
3980 const len = try reachableExpr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, len_node, node);4018 const len = try reachableExprComptime(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, len_node, node, .array_length);
3981 const elem_type = try typeExpr(gz, scope, extra.elem_type);4019 const elem_type = try typeExpr(gz, scope, extra.elem_type);
3982 const sentinel = try reachableExprComptime(gz, scope, .{ .rl = .{ .coerced_ty = elem_type } }, extra.sentinel, node, true);4020 const sentinel = try reachableExprComptime(gz, scope, .{ .rl = .{ .coerced_ty = elem_type } }, extra.sentinel, node, .array_sentinel);
39834021
3984 const result = try gz.addPlNode(.array_type_sentinel, node, Zir.Inst.ArrayTypeSentinel{4022 const result = try gz.addPlNode(.array_type_sentinel, node, Zir.Inst.ArrayTypeSentinel{
3985 .len = len,4023 .len = len,
...@@ -5321,7 +5359,7 @@ fn tupleDecl(...@@ -5321,7 +5359,7 @@ fn tupleDecl(
5321 astgen.scratch.appendAssumeCapacity(@intFromEnum(field_type_ref));5359 astgen.scratch.appendAssumeCapacity(@intFromEnum(field_type_ref));
53225360
5323 if (field.ast.value_expr != 0) {5361 if (field.ast.value_expr != 0) {
5324 const field_init_ref = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = field_type_ref } }, field.ast.value_expr);5362 const field_init_ref = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = field_type_ref } }, field.ast.value_expr, .tuple_field_default_value);
5325 astgen.scratch.appendAssumeCapacity(@intFromEnum(field_init_ref));5363 astgen.scratch.appendAssumeCapacity(@intFromEnum(field_init_ref));
5326 } else {5364 } else {
5327 astgen.scratch.appendAssumeCapacity(@intFromEnum(Zir.Inst.Ref.none));5365 astgen.scratch.appendAssumeCapacity(@intFromEnum(Zir.Inst.Ref.none));
...@@ -5693,7 +5731,7 @@ fn containerDecl(...@@ -5693,7 +5731,7 @@ fn containerDecl(
5693 namespace.base.tag = .namespace;5731 namespace.base.tag = .namespace;
56945732
5695 const arg_inst: Zir.Inst.Ref = if (container_decl.ast.arg != 0)5733 const arg_inst: Zir.Inst.Ref = if (container_decl.ast.arg != 0)
5696 try comptimeExpr(&block_scope, &namespace.base, coerced_type_ri, container_decl.ast.arg)5734 try comptimeExpr(&block_scope, &namespace.base, coerced_type_ri, container_decl.ast.arg, .type)
5697 else5735 else
5698 .none;5736 .none;
56995737
...@@ -7573,7 +7611,7 @@ fn switchExprErrUnion(...@@ -7573,7 +7611,7 @@ fn switchExprErrUnion(
7573 if (node_tags[item_node] == .switch_range) continue;7611 if (node_tags[item_node] == .switch_range) continue;
7574 items_len += 1;7612 items_len += 1;
75757613
7576 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node);7614 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node, .switch_item);
7577 try payloads.append(gpa, @intFromEnum(item_inst));7615 try payloads.append(gpa, @intFromEnum(item_inst));
7578 }7616 }
75797617
...@@ -7583,8 +7621,8 @@ fn switchExprErrUnion(...@@ -7583,8 +7621,8 @@ fn switchExprErrUnion(
7583 if (node_tags[range] != .switch_range) continue;7621 if (node_tags[range] != .switch_range) continue;
7584 ranges_len += 1;7622 ranges_len += 1;
75857623
7586 const first = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].lhs);7624 const first = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].lhs, .switch_item);
7587 const last = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].rhs);7625 const last = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].rhs, .switch_item);
7588 try payloads.appendSlice(gpa, &[_]u32{7626 try payloads.appendSlice(gpa, &[_]u32{
7589 @intFromEnum(first), @intFromEnum(last),7627 @intFromEnum(first), @intFromEnum(last),
7590 });7628 });
...@@ -7602,7 +7640,7 @@ fn switchExprErrUnion(...@@ -7602,7 +7640,7 @@ fn switchExprErrUnion(
7602 scalar_case_index += 1;7640 scalar_case_index += 1;
7603 try payloads.resize(gpa, header_index + 2); // item, body_len7641 try payloads.resize(gpa, header_index + 2); // item, body_len
7604 const item_node = case.ast.values[0];7642 const item_node = case.ast.values[0];
7605 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node);7643 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node, .switch_item);
7606 payloads.items[header_index] = @intFromEnum(item_inst);7644 payloads.items[header_index] = @intFromEnum(item_inst);
7607 break :blk header_index + 1;7645 break :blk header_index + 1;
7608 };7646 };
...@@ -8046,7 +8084,7 @@ fn switchExpr(...@@ -8046,7 +8084,7 @@ fn switchExpr(
8046 if (node_tags[item_node] == .switch_range) continue;8084 if (node_tags[item_node] == .switch_range) continue;
8047 items_len += 1;8085 items_len += 1;
80488086
8049 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node);8087 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node, .switch_item);
8050 try payloads.append(gpa, @intFromEnum(item_inst));8088 try payloads.append(gpa, @intFromEnum(item_inst));
8051 }8089 }
80528090
...@@ -8056,8 +8094,8 @@ fn switchExpr(...@@ -8056,8 +8094,8 @@ fn switchExpr(
8056 if (node_tags[range] != .switch_range) continue;8094 if (node_tags[range] != .switch_range) continue;
8057 ranges_len += 1;8095 ranges_len += 1;
80588096
8059 const first = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].lhs);8097 const first = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].lhs, .switch_item);
8060 const last = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].rhs);8098 const last = try comptimeExpr(parent_gz, scope, item_ri, node_datas[range].rhs, .switch_item);
8061 try payloads.appendSlice(gpa, &[_]u32{8099 try payloads.appendSlice(gpa, &[_]u32{
8062 @intFromEnum(first), @intFromEnum(last),8100 @intFromEnum(first), @intFromEnum(last),
8063 });8101 });
...@@ -8075,7 +8113,7 @@ fn switchExpr(...@@ -8075,7 +8113,7 @@ fn switchExpr(
8075 scalar_case_index += 1;8113 scalar_case_index += 1;
8076 try payloads.resize(gpa, header_index + 2); // item, body_len8114 try payloads.resize(gpa, header_index + 2); // item, body_len
8077 const item_node = case.ast.values[0];8115 const item_node = case.ast.values[0];
8078 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node);8116 const item_inst = try comptimeExpr(parent_gz, scope, item_ri, item_node, .switch_item);
8079 payloads.items[header_index] = @intFromEnum(item_inst);8117 payloads.items[header_index] = @intFromEnum(item_inst);
8080 break :blk header_index + 1;8118 break :blk header_index + 1;
8081 };8119 };
...@@ -8836,7 +8874,7 @@ fn asmExpr(...@@ -8836,7 +8874,7 @@ fn asmExpr(
8836 },8874 },
8837 else => .{8875 else => .{
8838 .tag = .asm_expr,8876 .tag = .asm_expr,
8839 .tmpl = @enumFromInt(@intFromEnum(try comptimeExpr(gz, scope, .{ .rl = .none }, full.ast.template))),8877 .tmpl = @enumFromInt(@intFromEnum(try comptimeExpr(gz, scope, .{ .rl = .none }, full.ast.template, .inline_assembly_code))),
8840 },8878 },
8841 };8879 };
88428880
...@@ -8973,7 +9011,7 @@ fn unionInit(...@@ -8973,7 +9011,7 @@ fn unionInit(
8973 params: []const Ast.Node.Index,9011 params: []const Ast.Node.Index,
8974) InnerError!Zir.Inst.Ref {9012) InnerError!Zir.Inst.Ref {
8975 const union_type = try typeExpr(gz, scope, params[0]);9013 const union_type = try typeExpr(gz, scope, params[0]);
8976 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1]);9014 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1], .union_field_name);
8977 const field_type = try gz.addPlNode(.field_type_ref, node, Zir.Inst.FieldTypeRef{9015 const field_type = try gz.addPlNode(.field_type_ref, node, Zir.Inst.FieldTypeRef{
8978 .container_type = union_type,9016 .container_type = union_type,
8979 .field_name = field_name,9017 .field_name = field_name,
...@@ -9078,7 +9116,7 @@ fn ptrCast(...@@ -9078,7 +9116,7 @@ fn ptrCast(
9078 const flags_int: FlagsInt = @bitCast(flags);9116 const flags_int: FlagsInt = @bitCast(flags);
9079 const cursor = maybeAdvanceSourceCursorToMainToken(gz, root_node);9117 const cursor = maybeAdvanceSourceCursorToMainToken(gz, root_node);
9080 const parent_ptr_type = try ri.rl.resultTypeForCast(gz, root_node, "@alignCast");9118 const parent_ptr_type = try ri.rl.resultTypeForCast(gz, root_node, "@alignCast");
9081 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, node_datas[node].lhs);9119 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, node_datas[node].lhs, .field_name);
9082 const field_ptr = try expr(gz, scope, .{ .rl = .none }, node_datas[node].rhs);9120 const field_ptr = try expr(gz, scope, .{ .rl = .none }, node_datas[node].rhs);
9083 try emitDbgStmt(gz, cursor);9121 try emitDbgStmt(gz, cursor);
9084 const result = try gz.addExtendedPayloadSmall(.field_parent_ptr, flags_int, Zir.Inst.FieldParentPtr{9122 const result = try gz.addExtendedPayloadSmall(.field_parent_ptr, flags_int, Zir.Inst.FieldParentPtr{
...@@ -9279,7 +9317,7 @@ fn builtinCall(...@@ -9279,7 +9317,7 @@ fn builtinCall(
9279 return astgen.failNode(node, "'@branchHint' must appear as the first statement in a function or conditional branch", .{});9317 return astgen.failNode(node, "'@branchHint' must appear as the first statement in a function or conditional branch", .{});
9280 }9318 }
9281 const hint_ty = try gz.addBuiltinValue(node, .branch_hint);9319 const hint_ty = try gz.addBuiltinValue(node, .branch_hint);
9282 const hint_val = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = hint_ty } }, params[0]);9320 const hint_val = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = hint_ty } }, params[0], .operand_branchHint);
9283 _ = try gz.addExtendedPayload(.branch_hint, Zir.Inst.UnNode{9321 _ = try gz.addExtendedPayload(.branch_hint, Zir.Inst.UnNode{
9284 .node = gz.nodeIndexToRelative(node),9322 .node = gz.nodeIndexToRelative(node),
9285 .operand = hint_val,9323 .operand = hint_val,
...@@ -9326,18 +9364,18 @@ fn builtinCall(...@@ -9326,18 +9364,18 @@ fn builtinCall(
9326 if (ri.rl == .ref or ri.rl == .ref_coerced_ty) {9364 if (ri.rl == .ref or ri.rl == .ref_coerced_ty) {
9327 return gz.addPlNode(.field_ptr_named, node, Zir.Inst.FieldNamed{9365 return gz.addPlNode(.field_ptr_named, node, Zir.Inst.FieldNamed{
9328 .lhs = try expr(gz, scope, .{ .rl = .ref }, params[0]),9366 .lhs = try expr(gz, scope, .{ .rl = .ref }, params[0]),
9329 .field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1]),9367 .field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1], .field_name),
9330 });9368 });
9331 }9369 }
9332 const result = try gz.addPlNode(.field_val_named, node, Zir.Inst.FieldNamed{9370 const result = try gz.addPlNode(.field_val_named, node, Zir.Inst.FieldNamed{
9333 .lhs = try expr(gz, scope, .{ .rl = .none }, params[0]),9371 .lhs = try expr(gz, scope, .{ .rl = .none }, params[0]),
9334 .field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1]),9372 .field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1], .field_name),
9335 });9373 });
9336 return rvalue(gz, ri, result, node);9374 return rvalue(gz, ri, result, node);
9337 },9375 },
9338 .FieldType => {9376 .FieldType => {
9339 const ty_inst = try typeExpr(gz, scope, params[0]);9377 const ty_inst = try typeExpr(gz, scope, params[0]);
9340 const name_inst = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1]);9378 const name_inst = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1], .field_name);
9341 const result = try gz.addPlNode(.field_type_ref, node, Zir.Inst.FieldTypeRef{9379 const result = try gz.addPlNode(.field_type_ref, node, Zir.Inst.FieldTypeRef{
9342 .container_type = ty_inst,9380 .container_type = ty_inst,
9343 .field_name = name_inst,9381 .field_name = name_inst,
...@@ -9358,7 +9396,7 @@ fn builtinCall(...@@ -9358,7 +9396,7 @@ fn builtinCall(
9358 .@"export" => {9396 .@"export" => {
9359 const exported = try expr(gz, scope, .{ .rl = .none }, params[0]);9397 const exported = try expr(gz, scope, .{ .rl = .none }, params[0]);
9360 const export_options_ty = try gz.addBuiltinValue(node, .export_options);9398 const export_options_ty = try gz.addBuiltinValue(node, .export_options);
9361 const options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = export_options_ty } }, params[1]);9399 const options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = export_options_ty } }, params[1], .export_options);
9362 _ = try gz.addPlNode(.@"export", node, Zir.Inst.Export{9400 _ = try gz.addPlNode(.@"export", node, Zir.Inst.Export{
9363 .exported = exported,9401 .exported = exported,
9364 .options = options,9402 .options = options,
...@@ -9368,7 +9406,7 @@ fn builtinCall(...@@ -9368,7 +9406,7 @@ fn builtinCall(
9368 .@"extern" => {9406 .@"extern" => {
9369 const type_inst = try typeExpr(gz, scope, params[0]);9407 const type_inst = try typeExpr(gz, scope, params[0]);
9370 const extern_options_ty = try gz.addBuiltinValue(node, .extern_options);9408 const extern_options_ty = try gz.addBuiltinValue(node, .extern_options);
9371 const options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = extern_options_ty } }, params[1]);9409 const options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = extern_options_ty } }, params[1], .extern_options);
9372 const result = try gz.addExtendedPayload(.builtin_extern, Zir.Inst.BinNode{9410 const result = try gz.addExtendedPayload(.builtin_extern, Zir.Inst.BinNode{
9373 .node = gz.nodeIndexToRelative(node),9411 .node = gz.nodeIndexToRelative(node),
9374 .lhs = type_inst,9412 .lhs = type_inst,
...@@ -9560,7 +9598,7 @@ fn builtinCall(...@@ -9560,7 +9598,7 @@ fn builtinCall(
9560 // zig fmt: on9598 // zig fmt: on
95619599
9562 .wasm_memory_size => {9600 .wasm_memory_size => {
9563 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);9601 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .wasm_memory_index);
9564 const result = try gz.addExtendedPayload(.wasm_memory_size, Zir.Inst.UnNode{9602 const result = try gz.addExtendedPayload(.wasm_memory_size, Zir.Inst.UnNode{
9565 .node = gz.nodeIndexToRelative(node),9603 .node = gz.nodeIndexToRelative(node),
9566 .operand = operand,9604 .operand = operand,
...@@ -9568,7 +9606,7 @@ fn builtinCall(...@@ -9568,7 +9606,7 @@ fn builtinCall(
9568 return rvalue(gz, ri, result, node);9606 return rvalue(gz, ri, result, node);
9569 },9607 },
9570 .wasm_memory_grow => {9608 .wasm_memory_grow => {
9571 const index_arg = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);9609 const index_arg = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .wasm_memory_index);
9572 const delta_arg = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, params[1]);9610 const delta_arg = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, params[1]);
9573 const result = try gz.addExtendedPayload(.wasm_memory_grow, Zir.Inst.BinNode{9611 const result = try gz.addExtendedPayload(.wasm_memory_grow, Zir.Inst.BinNode{
9574 .node = gz.nodeIndexToRelative(node),9612 .node = gz.nodeIndexToRelative(node),
...@@ -9579,8 +9617,8 @@ fn builtinCall(...@@ -9579,8 +9617,8 @@ fn builtinCall(
9579 },9617 },
9580 .c_define => {9618 .c_define => {
9581 if (!gz.c_import) return gz.astgen.failNode(node, "C define valid only inside C import block", .{});9619 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]);9620 const name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[0], .operand_cDefine_macro_name);
9583 const value = try comptimeExpr(gz, scope, .{ .rl = .none }, params[1]);9621 const value = try comptimeExpr(gz, scope, .{ .rl = .none }, params[1], .operand_cDefine_macro_value);
9584 const result = try gz.addExtendedPayload(.c_define, Zir.Inst.BinNode{9622 const result = try gz.addExtendedPayload(.c_define, Zir.Inst.BinNode{
9585 .node = gz.nodeIndexToRelative(node),9623 .node = gz.nodeIndexToRelative(node),
9586 .lhs = name,9624 .lhs = name,
...@@ -9666,7 +9704,7 @@ fn builtinCall(...@@ -9666,7 +9704,7 @@ fn builtinCall(
9666 },9704 },
9667 .call => {9705 .call => {
9668 const call_modifier_ty = try gz.addBuiltinValue(node, .call_modifier);9706 const call_modifier_ty = try gz.addBuiltinValue(node, .call_modifier);
9669 const modifier = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = call_modifier_ty } }, params[0]);9707 const modifier = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = call_modifier_ty } }, params[0], .call_modifier);
9670 const callee = try expr(gz, scope, .{ .rl = .none }, params[1]);9708 const callee = try expr(gz, scope, .{ .rl = .none }, params[1]);
9671 const args = try expr(gz, scope, .{ .rl = .none }, params[2]);9709 const args = try expr(gz, scope, .{ .rl = .none }, params[2]);
9672 const result = try gz.addPlNode(.builtin_call, node, Zir.Inst.BuiltinCall{9710 const result = try gz.addPlNode(.builtin_call, node, Zir.Inst.BuiltinCall{
...@@ -9682,7 +9720,7 @@ fn builtinCall(...@@ -9682,7 +9720,7 @@ fn builtinCall(
9682 },9720 },
9683 .field_parent_ptr => {9721 .field_parent_ptr => {
9684 const parent_ptr_type = try ri.rl.resultTypeForCast(gz, node, builtin_name);9722 const parent_ptr_type = try ri.rl.resultTypeForCast(gz, node, builtin_name);
9685 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[0]);9723 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[0], .field_name);
9686 const result = try gz.addExtendedPayloadSmall(.field_parent_ptr, 0, Zir.Inst.FieldParentPtr{9724 const result = try gz.addExtendedPayloadSmall(.field_parent_ptr, 0, Zir.Inst.FieldParentPtr{
9687 .src_node = gz.nodeIndexToRelative(node),9725 .src_node = gz.nodeIndexToRelative(node),
9688 .parent_ptr_type = parent_ptr_type,9726 .parent_ptr_type = parent_ptr_type,
...@@ -9713,7 +9751,7 @@ fn builtinCall(...@@ -9713,7 +9751,7 @@ fn builtinCall(
9713 .elem_type = try typeExpr(gz, scope, params[0]),9751 .elem_type = try typeExpr(gz, scope, params[0]),
9714 .a = try expr(gz, scope, .{ .rl = .none }, params[1]),9752 .a = try expr(gz, scope, .{ .rl = .none }, params[1]),
9715 .b = try expr(gz, scope, .{ .rl = .none }, params[2]),9753 .b = try expr(gz, scope, .{ .rl = .none }, params[2]),
9716 .mask = try comptimeExpr(gz, scope, .{ .rl = .none }, params[3]),9754 .mask = try comptimeExpr(gz, scope, .{ .rl = .none }, params[3], .operand_shuffle_mask),
9717 });9755 });
9718 return rvalue(gz, ri, result, node);9756 return rvalue(gz, ri, result, node);
9719 },9757 },
...@@ -9739,7 +9777,7 @@ fn builtinCall(...@@ -9739,7 +9777,7 @@ fn builtinCall(
9739 },9777 },
9740 .Vector => {9778 .Vector => {
9741 const result = try gz.addPlNode(.vector_type, node, Zir.Inst.Bin{9779 const result = try gz.addPlNode(.vector_type, node, Zir.Inst.Bin{
9742 .lhs = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]),9780 .lhs = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .type),
9743 .rhs = try typeExpr(gz, scope, params[1]),9781 .rhs = try typeExpr(gz, scope, params[1]),
9744 });9782 });
9745 return rvalue(gz, ri, result, node);9783 return rvalue(gz, ri, result, node);
...@@ -9747,7 +9785,7 @@ fn builtinCall(...@@ -9747,7 +9785,7 @@ fn builtinCall(
9747 .prefetch => {9785 .prefetch => {
9748 const prefetch_options_ty = try gz.addBuiltinValue(node, .prefetch_options);9786 const prefetch_options_ty = try gz.addBuiltinValue(node, .prefetch_options);
9749 const ptr = try expr(gz, scope, .{ .rl = .none }, params[0]);9787 const ptr = try expr(gz, scope, .{ .rl = .none }, params[0]);
9750 const options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = prefetch_options_ty } }, params[1]);9788 const options = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = prefetch_options_ty } }, params[1], .prefetch_options);
9751 _ = try gz.addExtendedPayload(.prefetch, Zir.Inst.BinNode{9789 _ = try gz.addExtendedPayload(.prefetch, Zir.Inst.BinNode{
9752 .node = gz.nodeIndexToRelative(node),9790 .node = gz.nodeIndexToRelative(node),
9753 .lhs = ptr,9791 .lhs = ptr,
...@@ -9785,7 +9823,7 @@ fn builtinCall(...@@ -9785,7 +9823,7 @@ fn builtinCall(
9785 },9823 },
97869824
9787 .work_item_id => {9825 .work_item_id => {
9788 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);9826 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .work_group_dim_index);
9789 const result = try gz.addExtendedPayload(.work_item_id, Zir.Inst.UnNode{9827 const result = try gz.addExtendedPayload(.work_item_id, Zir.Inst.UnNode{
9790 .node = gz.nodeIndexToRelative(node),9828 .node = gz.nodeIndexToRelative(node),
9791 .operand = operand,9829 .operand = operand,
...@@ -9793,7 +9831,7 @@ fn builtinCall(...@@ -9793,7 +9831,7 @@ fn builtinCall(
9793 return rvalue(gz, ri, result, node);9831 return rvalue(gz, ri, result, node);
9794 },9832 },
9795 .work_group_size => {9833 .work_group_size => {
9796 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);9834 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .work_group_dim_index);
9797 const result = try gz.addExtendedPayload(.work_group_size, Zir.Inst.UnNode{9835 const result = try gz.addExtendedPayload(.work_group_size, Zir.Inst.UnNode{
9798 .node = gz.nodeIndexToRelative(node),9836 .node = gz.nodeIndexToRelative(node),
9799 .operand = operand,9837 .operand = operand,
...@@ -9801,7 +9839,7 @@ fn builtinCall(...@@ -9801,7 +9839,7 @@ fn builtinCall(
9801 return rvalue(gz, ri, result, node);9839 return rvalue(gz, ri, result, node);
9802 },9840 },
9803 .work_group_id => {9841 .work_group_id => {
9804 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0]);9842 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .u32_type } }, params[0], .work_group_dim_index);
9805 const result = try gz.addExtendedPayload(.work_group_id, Zir.Inst.UnNode{9843 const result = try gz.addExtendedPayload(.work_group_id, Zir.Inst.UnNode{
9806 .node = gz.nodeIndexToRelative(node),9844 .node = gz.nodeIndexToRelative(node),
9807 .operand = operand,9845 .operand = operand,
...@@ -9821,7 +9859,13 @@ fn hasDeclOrField(...@@ -9821,7 +9859,13 @@ fn hasDeclOrField(
9821 tag: Zir.Inst.Tag,9859 tag: Zir.Inst.Tag,
9822) InnerError!Zir.Inst.Ref {9860) InnerError!Zir.Inst.Ref {
9823 const container_type = try typeExpr(gz, scope, lhs_node);9861 const container_type = try typeExpr(gz, scope, lhs_node);
9824 const name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, rhs_node);9862 const name = try comptimeExpr(
9863 gz,
9864 scope,
9865 .{ .rl = .{ .coerced_ty = .slice_const_u8_type } },
9866 rhs_node,
9867 if (tag == .has_decl) .decl_name else .field_name,
9868 );
9825 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{9869 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
9826 .lhs = container_type,9870 .lhs = container_type,
9827 .rhs = name,9871 .rhs = name,
...@@ -9874,7 +9918,7 @@ fn simpleUnOp(...@@ -9874,7 +9918,7 @@ fn simpleUnOp(
9874) InnerError!Zir.Inst.Ref {9918) InnerError!Zir.Inst.Ref {
9875 const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);9919 const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
9876 const operand = if (tag == .compile_error)9920 const operand = if (tag == .compile_error)
9877 try comptimeExpr(gz, scope, operand_ri, operand_node)9921 try comptimeExpr(gz, scope, operand_ri, operand_node, .compile_error_string)
9878 else9922 else
9879 try expr(gz, scope, operand_ri, operand_node);9923 try expr(gz, scope, operand_ri, operand_node);
9880 switch (tag) {9924 switch (tag) {
...@@ -9972,7 +10016,13 @@ fn simpleCBuiltin(...@@ -9972,7 +10016,13 @@ fn simpleCBuiltin(
9972) InnerError!Zir.Inst.Ref {10016) InnerError!Zir.Inst.Ref {
9973 const name: []const u8 = if (tag == .c_undef) "C undef" else "C include";10017 const name: []const u8 = if (tag == .c_undef) "C undef" else "C include";
9974 if (!gz.c_import) return gz.astgen.failNode(node, "{s} valid only inside C import block", .{name});10018 if (!gz.c_import) return gz.astgen.failNode(node, "{s} valid only inside C import block", .{name});
9975 const operand = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, operand_node);10019 const operand = try comptimeExpr(
10020 gz,
10021 scope,
10022 .{ .rl = .{ .coerced_ty = .slice_const_u8_type } },
10023 operand_node,
10024 if (tag == .c_undef) .operand_cUndef_macro_name else .operand_cInclude_file_name,
10025 );
9976 _ = try gz.addExtendedPayload(tag, Zir.Inst.UnNode{10026 _ = try gz.addExtendedPayload(tag, Zir.Inst.UnNode{
9977 .node = gz.nodeIndexToRelative(node),10027 .node = gz.nodeIndexToRelative(node),
9978 .operand = operand,10028 .operand = operand,
...@@ -9990,7 +10040,7 @@ fn offsetOf(...@@ -9990,7 +10040,7 @@ fn offsetOf(
9990 tag: Zir.Inst.Tag,10040 tag: Zir.Inst.Tag,
9991) InnerError!Zir.Inst.Ref {10041) InnerError!Zir.Inst.Ref {
9992 const type_inst = try typeExpr(gz, scope, lhs_node);10042 const type_inst = try typeExpr(gz, scope, lhs_node);
9993 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, rhs_node);10043 const field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, rhs_node, .field_name);
9994 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{10044 const result = try gz.addPlNode(tag, node, Zir.Inst.Bin{
9995 .lhs = type_inst,10045 .lhs = type_inst,
9996 .rhs = field_name,10046 .rhs = field_name,
...@@ -11996,11 +12046,16 @@ const GenZir = struct {...@@ -11996,11 +12046,16 @@ const GenZir = struct {
11996 }12046 }
1199712047
11998 /// Assumes nothing stacked on `gz`. Unstacks `gz`.12048 /// Assumes nothing stacked on `gz`. Unstacks `gz`.
12049 /// Asserts `inst` is not a `block_comptime`.
11999 fn setBlockBody(gz: *GenZir, inst: Zir.Inst.Index) !void {12050 fn setBlockBody(gz: *GenZir, inst: Zir.Inst.Index) !void {
12000 const astgen = gz.astgen;12051 const astgen = gz.astgen;
12001 const gpa = astgen.gpa;12052 const gpa = astgen.gpa;
12002 const body = gz.instructionsSlice();12053 const body = gz.instructionsSlice();
12003 const body_len = astgen.countBodyLenAfterFixups(body);12054 const body_len = astgen.countBodyLenAfterFixups(body);
12055
12056 const zir_tags = astgen.instructions.items(.tag);
12057 assert(zir_tags[@intFromEnum(inst)] != .block_comptime); // use `setComptimeBlockBody` instead
12058
12004 try astgen.extra.ensureUnusedCapacity(12059 try astgen.extra.ensureUnusedCapacity(
12005 gpa,12060 gpa,
12006 @typeInfo(Zir.Inst.Block).@"struct".fields.len + body_len,12061 @typeInfo(Zir.Inst.Block).@"struct".fields.len + body_len,
...@@ -12013,6 +12068,32 @@ const GenZir = struct {...@@ -12013,6 +12068,32 @@ const GenZir = struct {
12013 gz.unstack();12068 gz.unstack();
12014 }12069 }
1201512070
12071 /// Assumes nothing stacked on `gz`. Unstacks `gz`.
12072 /// Asserts `inst` is a `block_comptime`.
12073 fn setBlockComptimeBody(gz: *GenZir, inst: Zir.Inst.Index, comptime_reason: std.zig.SimpleComptimeReason) !void {
12074 const astgen = gz.astgen;
12075 const gpa = astgen.gpa;
12076 const body = gz.instructionsSlice();
12077 const body_len = astgen.countBodyLenAfterFixups(body);
12078
12079 const zir_tags = astgen.instructions.items(.tag);
12080 assert(zir_tags[@intFromEnum(inst)] == .block_comptime); // use `setBlockBody` instead
12081
12082 try astgen.extra.ensureUnusedCapacity(
12083 gpa,
12084 @typeInfo(Zir.Inst.BlockComptime).@"struct".fields.len + body_len,
12085 );
12086 const zir_datas = astgen.instructions.items(.data);
12087 zir_datas[@intFromEnum(inst)].pl_node.payload_index = astgen.addExtraAssumeCapacity(
12088 Zir.Inst.BlockComptime{
12089 .reason = comptime_reason,
12090 .body_len = body_len,
12091 },
12092 );
12093 astgen.appendBodyWithFixups(body);
12094 gz.unstack();
12095 }
12096
12016 /// Assumes nothing stacked on `gz`. Unstacks `gz`.12097 /// Assumes nothing stacked on `gz`. Unstacks `gz`.
12017 fn setTryBody(gz: *GenZir, inst: Zir.Inst.Index, operand: Zir.Inst.Ref) !void {12098 fn setTryBody(gz: *GenZir, inst: Zir.Inst.Index, operand: Zir.Inst.Ref) !void {
12018 const astgen = gz.astgen;12099 const astgen = gz.astgen;
lib/std/zig/Zir.zig+16-2
...@@ -78,6 +78,7 @@ pub fn extraData(code: Zir, comptime T: type, index: usize) ExtraData(T) {...@@ -78,6 +78,7 @@ pub fn extraData(code: Zir, comptime T: type, index: usize) ExtraData(T) {
78 Inst.Ref,78 Inst.Ref,
79 Inst.Index,79 Inst.Index,
80 Inst.Declaration.Name,80 Inst.Declaration.Name,
81 std.zig.SimpleComptimeReason,
81 NullTerminatedString,82 NullTerminatedString,
82 => @enumFromInt(code.extra[i]),83 => @enumFromInt(code.extra[i]),
8384
...@@ -291,7 +292,8 @@ pub const Inst = struct {...@@ -291,7 +292,8 @@ pub const Inst = struct {
291 /// Uses the `pl_node` union field. Payload is `Block`.292 /// Uses the `pl_node` union field. Payload is `Block`.
292 block,293 block,
293 /// Like `block`, but forces full evaluation of its contents at compile-time.294 /// 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`.
295 block_comptime,297 block_comptime,
296 /// A list of instructions which are analyzed in the parent context, without298 /// A list of instructions which are analyzed in the parent context, without
297 /// generating a runtime block. Must terminate with an "inline" variant of299 /// generating a runtime block. Must terminate with an "inline" variant of
...@@ -2547,6 +2549,13 @@ pub const Inst = struct {...@@ -2547,6 +2549,13 @@ pub const Inst = struct {
2547 body_len: u32,2549 body_len: u32,
2548 };2550 };
25492551
2552 /// Trailing:
2553 /// * inst: Index // for each `body_len`
2554 pub const BlockComptime = struct {
2555 reason: std.zig.SimpleComptimeReason,
2556 body_len: u32,
2557 };
2558
2550 /// Trailing:2559 /// Trailing:
2551 /// * inst: Index // for each `body_len`2560 /// * inst: Index // for each `body_len`
2552 pub const BoolBr = struct {2561 pub const BoolBr = struct {
...@@ -4517,7 +4526,6 @@ fn findTrackableInner(...@@ -4517,7 +4526,6 @@ fn findTrackableInner(
4517 // Block instructions, recurse over the bodies.4526 // Block instructions, recurse over the bodies.
45184527
4519 .block,4528 .block,
4520 .block_comptime,
4521 .block_inline,4529 .block_inline,
4522 .c_import,4530 .c_import,
4523 .typeof_builtin,4531 .typeof_builtin,
...@@ -4528,6 +4536,12 @@ fn findTrackableInner(...@@ -4528,6 +4536,12 @@ fn findTrackableInner(
4528 const body = zir.bodySlice(extra.end, extra.data.body_len);4536 const body = zir.bodySlice(extra.end, extra.data.body_len);
4529 return zir.findTrackableBody(gpa, contents, defers, body);4537 return zir.findTrackableBody(gpa, contents, defers, body);
4530 },4538 },
4539 .block_comptime => {
4540 const inst_data = datas[@intFromEnum(inst)].pl_node;
4541 const extra = zir.extraData(Inst.BlockComptime, inst_data.payload_index);
4542 const body = zir.bodySlice(extra.end, extra.data.body_len);
4543 return zir.findTrackableBody(gpa, contents, defers, body);
4544 },
4531 .condbr, .condbr_inline => {4545 .condbr, .condbr_inline => {
4532 const inst_data = datas[@intFromEnum(inst)].pl_node;4546 const inst_data = datas[@intFromEnum(inst)].pl_node;
4533 const extra = zir.extraData(Inst.CondBr, inst_data.payload_index);4547 const extra = zir.extraData(Inst.CondBr, inst_data.payload_index);
src/Compilation.zig+5-1
...@@ -3435,6 +3435,7 @@ pub fn addModuleErrorMsg(...@@ -3435,6 +3435,7 @@ pub fn addModuleErrorMsg(
3435 var notes: std.ArrayHashMapUnmanaged(ErrorBundle.ErrorMessage, void, ErrorNoteHashContext, true) = .empty;3435 var notes: std.ArrayHashMapUnmanaged(ErrorBundle.ErrorMessage, void, ErrorNoteHashContext, true) = .empty;
3436 defer notes.deinit(gpa);3436 defer notes.deinit(gpa);
34373437
3438 var last_note_loc: ?std.zig.Loc = null;
3438 for (module_err_msg.notes) |module_note| {3439 for (module_err_msg.notes) |module_note| {
3439 const note_src_loc = module_note.src_loc.upgrade(zcu);3440 const note_src_loc = module_note.src_loc.upgrade(zcu);
3440 const source = try note_src_loc.file_scope.getSource(gpa);3441 const source = try note_src_loc.file_scope.getSource(gpa);
...@@ -3443,6 +3444,9 @@ pub fn addModuleErrorMsg(...@@ -3443,6 +3444,9 @@ pub fn addModuleErrorMsg(
3443 const note_file_path = try note_src_loc.file_scope.fullPath(gpa);3444 const note_file_path = try note_src_loc.file_scope.fullPath(gpa);
3444 defer gpa.free(note_file_path);3445 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
3446 const gop = try notes.getOrPutContext(gpa, .{3450 const gop = try notes.getOrPutContext(gpa, .{
3447 .msg = try eb.addString(module_note.msg),3451 .msg = try eb.addString(module_note.msg),
3448 .src_loc = try eb.addSourceLocation(.{3452 .src_loc = try eb.addSourceLocation(.{
...@@ -3452,7 +3456,7 @@ pub fn addModuleErrorMsg(...@@ -3452,7 +3456,7 @@ pub fn addModuleErrorMsg(
3452 .span_end = span.end,3456 .span_end = span.end,
3453 .line = @intCast(loc.line),3457 .line = @intCast(loc.line),
3454 .column = @intCast(loc.column),3458 .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),
3456 }),3460 }),
3457 }, .{ .eb = eb });3461 }, .{ .eb = eb });
3458 if (gop.found_existing) {3462 if (gop.found_existing) {
src/Sema.zig+463-580
...@@ -377,9 +377,7 @@ pub const Block = struct {...@@ -377,9 +377,7 @@ pub const Block = struct {
377 runtime_index: RuntimeIndex = .zero,377 runtime_index: RuntimeIndex = .zero,
378 inline_block: Zir.Inst.OptionalIndex = .none,378 inline_block: Zir.Inst.OptionalIndex = .none,
379379
380 comptime_reason: ?*const ComptimeReason = null,380 comptime_reason: ?BlockComptimeReason = null,
381 // TODO is_comptime and comptime_reason should probably be merged together.
382 is_comptime: bool,
383 is_typeof: bool = false,381 is_typeof: bool = false,
384382
385 /// Keep track of the active error return trace index around blocks so that we can correctly383 /// Keep track of the active error return trace index around blocks so that we can correctly
...@@ -419,6 +417,10 @@ pub const Block = struct {...@@ -419,6 +417,10 @@ pub const Block = struct {
419 };417 };
420 }418 }
421419
420 fn isComptime(block: Block) bool {
421 return block.comptime_reason != null;
422 }
423
422 fn builtinCallArgSrc(block: *Block, builtin_call_node: i32, arg_index: u32) LazySrcLoc {424 fn builtinCallArgSrc(block: *Block, builtin_call_node: i32, arg_index: u32) LazySrcLoc {
423 return block.src(.{ .node_offset_builtin_call_arg = .{425 return block.src(.{ .node_offset_builtin_call_arg = .{
424 .builtin_call_node = builtin_call_node,426 .builtin_call_node = builtin_call_node,
...@@ -434,44 +436,6 @@ pub const Block = struct {...@@ -434,44 +436,6 @@ pub const Block = struct {
434 return block.src(.{ .token_offset = tok_offset });436 return block.src(.{ .token_offset = tok_offset });
435 }437 }
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
475 const Param = struct {439 const Param = struct {
476 /// `none` means `anytype`.440 /// `none` means `anytype`.
477 ty: InternPool.Index,441 ty: InternPool.Index,
...@@ -539,7 +503,6 @@ pub const Block = struct {...@@ -539,7 +503,6 @@ pub const Block = struct {
539 .instructions = .{},503 .instructions = .{},
540 .label = null,504 .label = null,
541 .inlining = parent.inlining,505 .inlining = parent.inlining,
542 .is_comptime = parent.is_comptime,
543 .comptime_reason = parent.comptime_reason,506 .comptime_reason = parent.comptime_reason,
544 .is_typeof = parent.is_typeof,507 .is_typeof = parent.is_typeof,
545 .runtime_cond = parent.runtime_cond,508 .runtime_cond = parent.runtime_cond,
...@@ -860,6 +823,77 @@ pub const Block = struct {...@@ -860,6 +823,77 @@ pub const Block = struct {
860 .inst = inst,823 .inst = inst,
861 });824 });
862 }825 }
826
827 /// Returns the `*Block` that should be passed to `Sema.failWithOwnedErrorMsg`, because all inline
828 /// calls below it have already been reported with "called at comptime from here" notes.
829 fn explainWhyBlockIsComptime(start_block: *Block, err_msg: *Zcu.ErrorMsg) !*Block {
830 const sema = start_block.sema;
831 var block = start_block;
832 while (true) {
833 switch (block.comptime_reason.?) {
834 .inlining_parent => {
835 const inlining = block.inlining.?;
836 try sema.errNote(inlining.call_src, err_msg, "called at comptime from here", .{});
837 block = inlining.call_block;
838 },
839 .reason => |r| {
840 try r.r.explain(sema, r.src, err_msg);
841 return block;
842 },
843 }
844 }
845 }
846};
847
848const ComptimeReason = union(enum) {
849 /// Evaluating at comptime for a reason in the `std.zig.SimpleComptimeReason` enum.
850 simple: std.zig.SimpleComptimeReason,
851
852 /// Evaluating at comptime because of a comptime-only type.
853 /// The format string looks like "foo '{}' bar", where "{}" is the comptime-only type.
854 /// We will then explain why this type is comptime-only.
855 comptime_only: struct {
856 ty: Type,
857 msg: enum {
858 union_init,
859 struct_init,
860 tuple_init,
861 param_ty_arg,
862 ret_ty_call,
863 ret_ty_generic_call,
864 },
865 },
866
867 fn explain(reason: ComptimeReason, sema: *Sema, src: LazySrcLoc, err_msg: *Zcu.ErrorMsg) !void {
868 switch (reason) {
869 .simple => |simple| {
870 try sema.errNote(src, err_msg, "{s}", .{simple.message()});
871 },
872 .comptime_only => |co| {
873 const pre, const post = switch (co.msg) {
874 .union_init => .{ "initializer of comptime-only union", "must be comptime-known" },
875 .struct_init => .{ "initializer of comptime-only struct", "must be comptime-known" },
876 .tuple_init => .{ "initializer of comptime-only tuple", "must be comptime-known" },
877 .param_ty_arg => .{ "argument to parameter with comptime-only type", "must be comptime-known" },
878 .ret_ty_call => .{ "function with comptime-only return type", "is evaluated at comptime" },
879 .ret_ty_generic_call => .{ "generic function instantiated with comptime-only return type", "is evaluated at comptime" },
880 };
881 try sema.errNote(src, err_msg, "{s} '{}' {s}", .{ pre, co.ty.fmt(sema.pt), post });
882 try sema.explainWhyTypeIsComptime(err_msg, src, co.ty);
883 },
884 }
885 }
886};
887
888const BlockComptimeReason = union(enum) {
889 /// This block inherits being comptime-only from the `inlining` call site.
890 inlining_parent,
891
892 /// This block is comptime for the given reason at the given source location.
893 reason: struct {
894 src: LazySrcLoc,
895 r: ComptimeReason,
896 },
863};897};
864898
865const LabeledBlock = struct {899const LabeledBlock = struct {
...@@ -885,12 +919,6 @@ const InferredAlloc = struct {...@@ -885,12 +919,6 @@ const InferredAlloc = struct {
885 prongs: std.ArrayListUnmanaged(Air.Inst.Index) = .empty,919 prongs: std.ArrayListUnmanaged(Air.Inst.Index) = .empty,
886};920};
887921
888const NeededComptimeReason = struct {
889 needed_comptime_reason: []const u8,
890 value_comptime_reason: ?[]const u8 = null,
891 block_comptime_reason: ?*const Block.ComptimeReason = null,
892};
893
894pub fn deinit(sema: *Sema) void {922pub fn deinit(sema: *Sema) void {
895 const gpa = sema.gpa;923 const gpa = sema.gpa;
896 sema.air_instructions.deinit(gpa);924 sema.air_instructions.deinit(gpa);
...@@ -954,7 +982,7 @@ pub fn analyzeFnBody(...@@ -954,7 +982,7 @@ pub fn analyzeFnBody(
954/// we are evaluating at comptime, semantically analyze the body and return the result from it.982/// we are evaluating at comptime, semantically analyze the body and return the result from it.
955/// Returns `null` if control flow did not break from this block, but instead terminated with some983/// Returns `null` if control flow did not break from this block, but instead terminated with some
956/// other runtime noreturn instruction. Compile-time breaks to blocks further up the stack still984/// other runtime noreturn instruction. Compile-time breaks to blocks further up the stack still
957/// return `error.ComptimeBreak`. If `block.is_comptime`, this function will never return `null`.985/// return `error.ComptimeBreak`. If `block.isComptime()`, this function will never return `null`.
958fn analyzeInlineBody(986fn analyzeInlineBody(
959 sema: *Sema,987 sema: *Sema,
960 block: *Block,988 block: *Block,
...@@ -1003,7 +1031,7 @@ pub fn resolveInlineBody(...@@ -1003,7 +1031,7 @@ pub fn resolveInlineBody(
1003/// If this function returns normally, the merges of `block` were populated with all possible1031/// If this function returns normally, the merges of `block` were populated with all possible
1004/// (runtime) results of this block. Peer type resolution should be performed on the result,1032/// (runtime) results of this block. Peer type resolution should be performed on the result,
1005/// and relevant runtime instructions written to perform necessary coercions and breaks. See1033/// and relevant runtime instructions written to perform necessary coercions and breaks. See
1006/// `resolveAnalyzedBlock`. This form of return is impossible if `block.is_comptime == true`.1034/// `resolveAnalyzedBlock`. This form of return is impossible if `block.isComptime()`.
1007///1035///
1008/// Alternatively, this function may return `error.ComptimeBreak`. This indicates that comptime1036/// Alternatively, this function may return `error.ComptimeBreak`. This indicates that comptime
1009/// control flow is happening, and we are breaking at comptime from a block indicated by the1037/// control flow is happening, and we are breaking at comptime from a block indicated by the
...@@ -1340,7 +1368,7 @@ fn analyzeBodyInner(...@@ -1340,7 +1368,7 @@ fn analyzeBodyInner(
1340 continue;1368 continue;
1341 },1369 },
1342 .breakpoint => {1370 .breakpoint => {
1343 if (!block.is_comptime) {1371 if (!block.isComptime()) {
1344 _ = try block.addNoOp(.breakpoint);1372 _ = try block.addNoOp(.breakpoint);
1345 }1373 }
1346 i += 1;1374 i += 1;
...@@ -1515,7 +1543,7 @@ fn analyzeBodyInner(...@@ -1515,7 +1543,7 @@ fn analyzeBodyInner(
1515 continue;1543 continue;
1516 },1544 },
1517 .check_comptime_control_flow => {1545 .check_comptime_control_flow => {
1518 if (!block.is_comptime) {1546 if (!block.isComptime()) {
1519 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;1547 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
1520 const src = block.nodeOffset(inst_data.src_node);1548 const src = block.nodeOffset(inst_data.src_node);
1521 const inline_block = inst_data.operand.toIndex().?;1549 const inline_block = inst_data.operand.toIndex().?;
...@@ -1562,7 +1590,7 @@ fn analyzeBodyInner(...@@ -1562,7 +1590,7 @@ fn analyzeBodyInner(
15621590
1563 // Special case instructions to handle comptime control flow.1591 // Special case instructions to handle comptime control flow.
1564 .@"break" => {1592 .@"break" => {
1565 if (block.is_comptime) {1593 if (block.isComptime()) {
1566 sema.comptime_break_inst = inst;1594 sema.comptime_break_inst = inst;
1567 return error.ComptimeBreak;1595 return error.ComptimeBreak;
1568 } else {1596 } else {
...@@ -1575,7 +1603,7 @@ fn analyzeBodyInner(...@@ -1575,7 +1603,7 @@ fn analyzeBodyInner(
1575 return error.ComptimeBreak;1603 return error.ComptimeBreak;
1576 },1604 },
1577 .repeat => {1605 .repeat => {
1578 if (block.is_comptime) {1606 if (block.isComptime()) {
1579 // Send comptime control flow back to the beginning of this block.1607 // Send comptime control flow back to the beginning of this block.
1580 const src = block.nodeOffset(datas[@intFromEnum(inst)].node);1608 const src = block.nodeOffset(datas[@intFromEnum(inst)].node);
1581 try sema.emitBackwardBranch(block, src);1609 try sema.emitBackwardBranch(block, src);
...@@ -1597,7 +1625,7 @@ fn analyzeBodyInner(...@@ -1597,7 +1625,7 @@ fn analyzeBodyInner(
1597 i = 0;1625 i = 0;
1598 continue;1626 continue;
1599 },1627 },
1600 .switch_continue => if (block.is_comptime) {1628 .switch_continue => if (block.isComptime()) {
1601 sema.comptime_break_inst = inst;1629 sema.comptime_break_inst = inst;
1602 return error.ComptimeBreak;1630 return error.ComptimeBreak;
1603 } else {1631 } else {
...@@ -1605,17 +1633,40 @@ fn analyzeBodyInner(...@@ -1605,17 +1633,40 @@ fn analyzeBodyInner(
1605 break;1633 break;
1606 },1634 },
16071635
1608 .loop => if (block.is_comptime) {1636 .loop => if (block.isComptime()) {
1609 continue :inst .block_inline;1637 continue :inst .block_inline;
1610 } else try sema.zirLoop(block, inst),1638 } else try sema.zirLoop(block, inst),
16111639
1612 .block => if (block.is_comptime) {1640 .block => if (block.isComptime()) {
1613 continue :inst .block_inline;1641 continue :inst .block_inline;
1614 } else try sema.zirBlock(block, inst, false),1642 } else try sema.zirBlock(block, inst),
16151643
1616 .block_comptime => if (block.is_comptime) {1644 .block_comptime => {
1617 continue :inst .block_inline;1645 const pl_node = datas[@intFromEnum(inst)].pl_node;
1618 } else try sema.zirBlock(block, inst, true),1646 const src = block.nodeOffset(pl_node.src_node);
1647 const extra = sema.code.extraData(Zir.Inst.BlockComptime, pl_node.payload_index);
1648 const block_body = sema.code.bodySlice(extra.end, extra.data.body_len);
1649
1650 if (block.isComptime()) {
1651 // No need for a sub-block; just resolve the other body directly!
1652 break :inst try sema.resolveInlineBody(block, block_body, inst);
1653 }
1654
1655 var child_block = block.makeSubBlock();
1656 defer child_block.instructions.deinit(sema.gpa);
1657 child_block.comptime_reason = .{ .reason = .{
1658 .src = src,
1659 .r = .{ .simple = extra.data.reason },
1660 } };
1661
1662 const result = try sema.resolveInlineBody(&child_block, block_body, inst);
1663
1664 if (!try sema.isComptimeKnown(result)) {
1665 return sema.failWithNeededComptime(&child_block, src, null);
1666 }
1667
1668 break :inst result;
1669 },
16191670
1620 .block_inline => blk: {1671 .block_inline => blk: {
1621 // Directly analyze the block body without introducing a new block.1672 // Directly analyze the block body without introducing a new block.
...@@ -1725,7 +1776,7 @@ fn analyzeBodyInner(...@@ -1725,7 +1776,7 @@ fn analyzeBodyInner(
1725 return error.ComptimeBreak;1776 return error.ComptimeBreak;
1726 }1777 }
1727 },1778 },
1728 .condbr => if (block.is_comptime) {1779 .condbr => if (block.isComptime()) {
1729 continue :inst .condbr_inline;1780 continue :inst .condbr_inline;
1730 } else {1781 } else {
1731 try sema.zirCondbr(block, inst);1782 try sema.zirCondbr(block, inst);
...@@ -1742,10 +1793,7 @@ fn analyzeBodyInner(...@@ -1742,10 +1793,7 @@ fn analyzeBodyInner(
1742 );1793 );
1743 const uncasted_cond = try sema.resolveInst(extra.data.condition);1794 const uncasted_cond = try sema.resolveInst(extra.data.condition);
1744 const cond = try sema.coerce(block, Type.bool, uncasted_cond, cond_src);1795 const cond = try sema.coerce(block, Type.bool, uncasted_cond, cond_src);
1745 const cond_val = try sema.resolveConstDefinedValue(block, cond_src, cond, .{1796 const cond_val = try sema.resolveConstDefinedValue(block, cond_src, cond, null);
1746 .needed_comptime_reason = "condition in comptime branch must be comptime-known",
1747 .block_comptime_reason = block.comptime_reason,
1748 });
1749 const inline_body = if (cond_val.toBool()) then_body else else_body;1797 const inline_body = if (cond_val.toBool()) then_body else else_body;
17501798
1751 try sema.maybeErrorUnwrapCondbr(block, inline_body, extra.data.condition, cond_src);1799 try sema.maybeErrorUnwrapCondbr(block, inline_body, extra.data.condition, cond_src);
...@@ -1756,7 +1804,7 @@ fn analyzeBodyInner(...@@ -1756,7 +1804,7 @@ fn analyzeBodyInner(
1756 break :inst result;1804 break :inst result;
1757 },1805 },
1758 .@"try" => blk: {1806 .@"try" => blk: {
1759 if (!block.is_comptime) break :blk try sema.zirTry(block, inst);1807 if (!block.isComptime()) break :blk try sema.zirTry(block, inst);
1760 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;1808 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1761 const src = block.nodeOffset(inst_data.src_node);1809 const src = block.nodeOffset(inst_data.src_node);
1762 const operand_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node });1810 const operand_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node });
...@@ -1771,10 +1819,7 @@ fn analyzeBodyInner(...@@ -1771,10 +1819,7 @@ fn analyzeBodyInner(
1771 }1819 }
1772 const is_non_err = try sema.analyzeIsNonErrComptimeOnly(block, operand_src, err_union);1820 const is_non_err = try sema.analyzeIsNonErrComptimeOnly(block, operand_src, err_union);
1773 assert(is_non_err != .none);1821 assert(is_non_err != .none);
1774 const is_non_err_val = try sema.resolveConstDefinedValue(block, operand_src, is_non_err, .{1822 const is_non_err_val = try sema.resolveConstDefinedValue(block, operand_src, is_non_err, null);
1775 .needed_comptime_reason = "try operand inside comptime block must be comptime-known",
1776 .block_comptime_reason = block.comptime_reason,
1777 });
1778 if (is_non_err_val.toBool()) {1823 if (is_non_err_val.toBool()) {
1779 break :blk try sema.analyzeErrUnionPayload(block, src, err_union_ty, err_union, operand_src, false);1824 break :blk try sema.analyzeErrUnionPayload(block, src, err_union_ty, err_union, operand_src, false);
1780 }1825 }
...@@ -1782,7 +1827,7 @@ fn analyzeBodyInner(...@@ -1782,7 +1827,7 @@ fn analyzeBodyInner(
1782 break :blk result;1827 break :blk result;
1783 },1828 },
1784 .try_ptr => blk: {1829 .try_ptr => blk: {
1785 if (!block.is_comptime) break :blk try sema.zirTryPtr(block, inst);1830 if (!block.isComptime()) break :blk try sema.zirTryPtr(block, inst);
1786 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;1831 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1787 const src = block.nodeOffset(inst_data.src_node);1832 const src = block.nodeOffset(inst_data.src_node);
1788 const operand_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node });1833 const operand_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node });
...@@ -1792,10 +1837,7 @@ fn analyzeBodyInner(...@@ -1792,10 +1837,7 @@ fn analyzeBodyInner(
1792 const err_union = try sema.analyzeLoad(block, src, operand, operand_src);1837 const err_union = try sema.analyzeLoad(block, src, operand, operand_src);
1793 const is_non_err = try sema.analyzeIsNonErrComptimeOnly(block, operand_src, err_union);1838 const is_non_err = try sema.analyzeIsNonErrComptimeOnly(block, operand_src, err_union);
1794 assert(is_non_err != .none);1839 assert(is_non_err != .none);
1795 const is_non_err_val = try sema.resolveConstDefinedValue(block, operand_src, is_non_err, .{1840 const is_non_err_val = try sema.resolveConstDefinedValue(block, operand_src, is_non_err, null);
1796 .needed_comptime_reason = "try operand inside comptime block must be comptime-known",
1797 .block_comptime_reason = block.comptime_reason,
1798 });
1799 if (is_non_err_val.toBool()) {1841 if (is_non_err_val.toBool()) {
1800 break :blk try sema.analyzeErrUnionPayloadPtr(block, src, operand, false, false);1842 break :blk try sema.analyzeErrUnionPayloadPtr(block, src, operand, false, false);
1801 }1843 }
...@@ -1873,7 +1915,7 @@ fn resolveConstBool(...@@ -1873,7 +1915,7 @@ fn resolveConstBool(
1873 block: *Block,1915 block: *Block,
1874 src: LazySrcLoc,1916 src: LazySrcLoc,
1875 zir_ref: Zir.Inst.Ref,1917 zir_ref: Zir.Inst.Ref,
1876 reason: NeededComptimeReason,1918 reason: ComptimeReason,
1877) !bool {1919) !bool {
1878 const air_inst = try sema.resolveInst(zir_ref);1920 const air_inst = try sema.resolveInst(zir_ref);
1879 const wanted_type = Type.bool;1921 const wanted_type = Type.bool;
...@@ -1887,7 +1929,7 @@ fn resolveConstString(...@@ -1887,7 +1929,7 @@ fn resolveConstString(
1887 block: *Block,1929 block: *Block,
1888 src: LazySrcLoc,1930 src: LazySrcLoc,
1889 zir_ref: Zir.Inst.Ref,1931 zir_ref: Zir.Inst.Ref,
1890 reason: NeededComptimeReason,1932 reason: ComptimeReason,
1891) ![]u8 {1933) ![]u8 {
1892 const air_inst = try sema.resolveInst(zir_ref);1934 const air_inst = try sema.resolveInst(zir_ref);
1893 return sema.toConstString(block, src, air_inst, reason);1935 return sema.toConstString(block, src, air_inst, reason);
...@@ -1898,7 +1940,7 @@ pub fn toConstString(...@@ -1898,7 +1940,7 @@ pub fn toConstString(
1898 block: *Block,1940 block: *Block,
1899 src: LazySrcLoc,1941 src: LazySrcLoc,
1900 air_inst: Air.Inst.Ref,1942 air_inst: Air.Inst.Ref,
1901 reason: NeededComptimeReason,1943 reason: ComptimeReason,
1902) ![]u8 {1944) ![]u8 {
1903 const pt = sema.pt;1945 const pt = sema.pt;
1904 const coerced_inst = try sema.coerce(block, Type.slice_const_u8, air_inst, src);1946 const coerced_inst = try sema.coerce(block, Type.slice_const_u8, air_inst, src);
...@@ -1912,7 +1954,7 @@ pub fn resolveConstStringIntern(...@@ -1912,7 +1954,7 @@ pub fn resolveConstStringIntern(
1912 block: *Block,1954 block: *Block,
1913 src: LazySrcLoc,1955 src: LazySrcLoc,
1914 zir_ref: Zir.Inst.Ref,1956 zir_ref: Zir.Inst.Ref,
1915 reason: NeededComptimeReason,1957 reason: ComptimeReason,
1916) !InternPool.NullTerminatedString {1958) !InternPool.NullTerminatedString {
1917 const air_inst = try sema.resolveInst(zir_ref);1959 const air_inst = try sema.resolveInst(zir_ref);
1918 const wanted_type = Type.slice_const_u8;1960 const wanted_type = Type.slice_const_u8;
...@@ -2063,9 +2105,7 @@ fn analyzeAsType(...@@ -2063,9 +2105,7 @@ fn analyzeAsType(
2063) !Type {2105) !Type {
2064 const wanted_type = Type.type;2106 const wanted_type = Type.type;
2065 const coerced_inst = try sema.coerce(block, wanted_type, air_inst, src);2107 const coerced_inst = try sema.coerce(block, wanted_type, air_inst, src);
2066 const val = try sema.resolveConstDefinedValue(block, src, coerced_inst, .{2108 const val = try sema.resolveConstDefinedValue(block, src, coerced_inst, .{ .simple = .type });
2067 .needed_comptime_reason = "types must be comptime-known",
2068 });
2069 return val.toType();2109 return val.toType();
2070}2110}
20712111
...@@ -2077,7 +2117,7 @@ pub fn setupErrorReturnTrace(sema: *Sema, block: *Block, last_arg_index: usize)...@@ -2077,7 +2117,7 @@ pub fn setupErrorReturnTrace(sema: *Sema, block: *Block, last_arg_index: usize)
2077 const ip = &zcu.intern_pool;2117 const ip = &zcu.intern_pool;
2078 if (!comp.config.any_error_tracing) return;2118 if (!comp.config.any_error_tracing) return;
20792119
2080 assert(!block.is_comptime);2120 assert(!block.isComptime());
2081 var err_trace_block = block.makeSubBlock();2121 var err_trace_block = block.makeSubBlock();
2082 defer err_trace_block.instructions.deinit(gpa);2122 defer err_trace_block.instructions.deinit(gpa);
20832123
...@@ -2148,7 +2188,7 @@ fn resolveConstValue(...@@ -2148,7 +2188,7 @@ fn resolveConstValue(
2148 block: *Block,2188 block: *Block,
2149 src: LazySrcLoc,2189 src: LazySrcLoc,
2150 inst: Air.Inst.Ref,2190 inst: Air.Inst.Ref,
2151 reason: NeededComptimeReason,2191 reason: ?ComptimeReason,
2152) CompileError!Value {2192) CompileError!Value {
2153 return try sema.resolveValue(inst) orelse {2193 return try sema.resolveValue(inst) orelse {
2154 return sema.failWithNeededComptime(block, src, reason);2194 return sema.failWithNeededComptime(block, src, reason);
...@@ -2177,7 +2217,7 @@ fn resolveConstDefinedValue(...@@ -2177,7 +2217,7 @@ fn resolveConstDefinedValue(
2177 block: *Block,2217 block: *Block,
2178 src: LazySrcLoc,2218 src: LazySrcLoc,
2179 air_ref: Air.Inst.Ref,2219 air_ref: Air.Inst.Ref,
2180 reason: NeededComptimeReason,2220 reason: ?ComptimeReason,
2181) CompileError!Value {2221) CompileError!Value {
2182 const val = try sema.resolveConstValue(block, src, air_ref, reason);2222 const val = try sema.resolveConstValue(block, src, air_ref, reason);
2183 if (val.isUndef(sema.pt.zcu)) return sema.failWithUseOfUndef(block, src);2223 if (val.isUndef(sema.pt.zcu)) return sema.failWithUseOfUndef(block, src);
...@@ -2217,15 +2257,16 @@ pub fn resolveFinalDeclValue(...@@ -2217,15 +2257,16 @@ pub fn resolveFinalDeclValue(
2217 const val: Value = .fromInterned(ip_index);2257 const val: Value = .fromInterned(ip_index);
2218 break :rt_ptr val.isPtrRuntimeValue(zcu);2258 break :rt_ptr val.isPtrRuntimeValue(zcu);
2219 };2259 };
2220 const value_comptime_reason: ?[]const u8 = if (is_runtime_ptr)
2221 "thread local and dll imported variables have runtime-known addresses"
2222 else
2223 null;
22242260
2225 return sema.failWithNeededComptime(block, src, .{2261 switch (sema.failWithNeededComptime(block, src, .{ .simple = .container_var_init })) {
2226 .needed_comptime_reason = "global variable initializer must be comptime-known",2262 error.AnalysisFail => |e| {
2227 .value_comptime_reason = value_comptime_reason,2263 if (sema.err != null and is_runtime_ptr) {
2228 });2264 try sema.errNote(src, sema.err.?, "threadlocal and dll imported variables have runtime-known addresses", .{});
2265 }
2266 return e;
2267 },
2268 else => |e| return e,
2269 }
2229 };2270 };
22302271
2231 if (val.canMutateComptimeVarState(zcu)) {2272 if (val.canMutateComptimeVarState(zcu)) {
...@@ -2235,21 +2276,19 @@ pub fn resolveFinalDeclValue(...@@ -2235,21 +2276,19 @@ pub fn resolveFinalDeclValue(
2235 return val;2276 return val;
2236}2277}
22372278
2238fn failWithNeededComptime(sema: *Sema, block: *Block, src: LazySrcLoc, reason: NeededComptimeReason) CompileError {2279fn failWithNeededComptime(sema: *Sema, block: *Block, src: LazySrcLoc, reason: ?ComptimeReason) CompileError {
2239 const msg = msg: {2280 const msg, const fail_block = msg: {
2240 const msg = try sema.errMsg(src, "unable to resolve comptime value", .{});2281 const msg = try sema.errMsg(src, "unable to resolve comptime value", .{});
2241 errdefer msg.destroy(sema.gpa);2282 errdefer msg.destroy(sema.gpa);
2242 try sema.errNote(src, msg, "{s}", .{reason.needed_comptime_reason});2283 const fail_block = if (reason) |r| b: {
2243 if (reason.value_comptime_reason) |value_comptime_reason| {2284 try r.explain(sema, src, msg);
2244 try sema.errNote(src, msg, "{s}", .{value_comptime_reason});2285 break :b block;
2245 }2286 } else b: {
22462287 break :b try block.explainWhyBlockIsComptime(msg);
2247 if (reason.block_comptime_reason) |block_comptime_reason| {2288 };
2248 try block_comptime_reason.explain(sema, msg);2289 break :msg .{ msg, fail_block };
2249 }
2250 break :msg msg;
2251 };2290 };
2252 return sema.failWithOwnedErrorMsg(block, msg);2291 return sema.failWithOwnedErrorMsg(fail_block, msg);
2253}2292}
22542293
2255fn failWithUseOfUndef(sema: *Sema, block: *Block, src: LazySrcLoc) CompileError {2294fn failWithUseOfUndef(sema: *Sema, block: *Block, src: LazySrcLoc) CompileError {
...@@ -2578,9 +2617,7 @@ pub fn analyzeAsAlign(...@@ -2578,9 +2617,7 @@ pub fn analyzeAsAlign(
2578 src: LazySrcLoc,2617 src: LazySrcLoc,
2579 air_ref: Air.Inst.Ref,2618 air_ref: Air.Inst.Ref,
2580) !Alignment {2619) !Alignment {
2581 const alignment_big = try sema.analyzeAsInt(block, src, air_ref, align_ty, .{2620 const alignment_big = try sema.analyzeAsInt(block, src, air_ref, align_ty, .{ .simple = .@"align" });
2582 .needed_comptime_reason = "alignment must be comptime-known",
2583 });
2584 return sema.validateAlign(block, src, alignment_big);2621 return sema.validateAlign(block, src, alignment_big);
2585}2622}
25862623
...@@ -2615,7 +2652,7 @@ fn resolveInt(...@@ -2615,7 +2652,7 @@ fn resolveInt(
2615 src: LazySrcLoc,2652 src: LazySrcLoc,
2616 zir_ref: Zir.Inst.Ref,2653 zir_ref: Zir.Inst.Ref,
2617 dest_ty: Type,2654 dest_ty: Type,
2618 reason: NeededComptimeReason,2655 reason: ComptimeReason,
2619) !u64 {2656) !u64 {
2620 const air_ref = try sema.resolveInst(zir_ref);2657 const air_ref = try sema.resolveInst(zir_ref);
2621 return sema.analyzeAsInt(block, src, air_ref, dest_ty, reason);2658 return sema.analyzeAsInt(block, src, air_ref, dest_ty, reason);
...@@ -2627,7 +2664,7 @@ fn analyzeAsInt(...@@ -2627,7 +2664,7 @@ fn analyzeAsInt(
2627 src: LazySrcLoc,2664 src: LazySrcLoc,
2628 air_ref: Air.Inst.Ref,2665 air_ref: Air.Inst.Ref,
2629 dest_ty: Type,2666 dest_ty: Type,
2630 reason: NeededComptimeReason,2667 reason: ComptimeReason,
2631) !u64 {2668) !u64 {
2632 const coerced = try sema.coerce(block, dest_ty, air_ref, src);2669 const coerced = try sema.coerce(block, dest_ty, air_ref, src);
2633 const val = try sema.resolveConstDefinedValue(block, src, coerced, reason);2670 const val = try sema.resolveConstDefinedValue(block, src, coerced, reason);
...@@ -2687,9 +2724,7 @@ fn zirTupleDecl(...@@ -2687,9 +2724,7 @@ fn zirTupleDecl(
2687 if (zir_field_init != .none) {2724 if (zir_field_init != .none) {
2688 const uncoerced_field_init = try sema.resolveInst(zir_field_init);2725 const uncoerced_field_init = try sema.resolveInst(zir_field_init);
2689 const coerced_field_init = try sema.coerce(block, field_type, uncoerced_field_init, init_src);2726 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, .{2727 const field_init_val = try sema.resolveConstDefinedValue(block, init_src, coerced_field_init, .{ .simple = .tuple_field_default_value });
2691 .needed_comptime_reason = "tuple field default value must be comptime-known",
2692 });
2693 if (field_init_val.canMutateComptimeVarState(zcu)) {2728 if (field_init_val.canMutateComptimeVarState(zcu)) {
2694 return sema.fail(block, init_src, "field default value contains reference to comptime-mutable memory", .{});2729 return sema.fail(block, init_src, "field default value contains reference to comptime-mutable memory", .{});
2695 }2730 }
...@@ -3414,7 +3449,7 @@ fn zirRetPtr(sema: *Sema, block: *Block) CompileError!Air.Inst.Ref {...@@ -3414,7 +3449,7 @@ fn zirRetPtr(sema: *Sema, block: *Block) CompileError!Air.Inst.Ref {
34143449
3415 const pt = sema.pt;3450 const pt = sema.pt;
34163451
3417 if (block.is_comptime or try sema.fn_ret_ty.comptimeOnlySema(pt)) {3452 if (block.isComptime() or try sema.fn_ret_ty.comptimeOnlySema(pt)) {
3418 try sema.fn_ret_ty.resolveFields(pt);3453 try sema.fn_ret_ty.resolveFields(pt);
3419 return sema.analyzeComptimeAlloc(block, sema.fn_ret_ty, .none);3454 return sema.analyzeComptimeAlloc(block, sema.fn_ret_ty, .none);
3420 }3455 }
...@@ -3608,7 +3643,7 @@ fn zirAllocExtended(...@@ -3608,7 +3643,7 @@ fn zirAllocExtended(
3608 break :blk try sema.resolveAlign(block, align_src, align_ref);3643 break :blk try sema.resolveAlign(block, align_src, align_ref);
3609 } else .none;3644 } else .none;
36103645
3611 if (block.is_comptime or small.is_comptime) {3646 if (block.isComptime() or small.is_comptime) {
3612 if (small.has_type) {3647 if (small.has_type) {
3613 return sema.analyzeComptimeAlloc(block, var_ty, alignment);3648 return sema.analyzeComptimeAlloc(block, var_ty, alignment);
3614 } else {3649 } else {
...@@ -4075,7 +4110,7 @@ fn zirAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I...@@ -4075,7 +4110,7 @@ fn zirAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
4075 const ty_src = block.src(.{ .node_offset_var_decl_ty = inst_data.src_node });4110 const ty_src = block.src(.{ .node_offset_var_decl_ty = inst_data.src_node });
40764111
4077 const var_ty = try sema.resolveType(block, ty_src, inst_data.operand);4112 const var_ty = try sema.resolveType(block, ty_src, inst_data.operand);
4078 if (block.is_comptime or try var_ty.comptimeOnlySema(pt)) {4113 if (block.isComptime() or try var_ty.comptimeOnlySema(pt)) {
4079 return sema.analyzeComptimeAlloc(block, var_ty, .none);4114 return sema.analyzeComptimeAlloc(block, var_ty, .none);
4080 }4115 }
4081 if (sema.func_is_naked and try var_ty.hasRuntimeBitsSema(pt)) {4116 if (sema.func_is_naked and try var_ty.hasRuntimeBitsSema(pt)) {
...@@ -4103,7 +4138,7 @@ fn zirAllocMut(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -4103,7 +4138,7 @@ fn zirAllocMut(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
4103 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;4138 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
4104 const ty_src = block.src(.{ .node_offset_var_decl_ty = inst_data.src_node });4139 const ty_src = block.src(.{ .node_offset_var_decl_ty = inst_data.src_node });
4105 const var_ty = try sema.resolveType(block, ty_src, inst_data.operand);4140 const var_ty = try sema.resolveType(block, ty_src, inst_data.operand);
4106 if (block.is_comptime) {4141 if (block.isComptime()) {
4107 return sema.analyzeComptimeAlloc(block, var_ty, .none);4142 return sema.analyzeComptimeAlloc(block, var_ty, .none);
4108 }4143 }
4109 if (sema.func_is_naked and try var_ty.hasRuntimeBitsSema(pt)) {4144 if (sema.func_is_naked and try var_ty.hasRuntimeBitsSema(pt)) {
...@@ -4129,7 +4164,7 @@ fn zirAllocInferred(...@@ -4129,7 +4164,7 @@ fn zirAllocInferred(
41294164
4130 const gpa = sema.gpa;4165 const gpa = sema.gpa;
41314166
4132 if (block.is_comptime) {4167 if (block.isComptime()) {
4133 try sema.air_instructions.append(gpa, .{4168 try sema.air_instructions.append(gpa, .{
4134 .tag = .inferred_alloc_comptime,4169 .tag = .inferred_alloc_comptime,
4135 .data = .{ .inferred_alloc_comptime = .{4170 .data = .{ .inferred_alloc_comptime = .{
...@@ -4778,7 +4813,7 @@ fn validateUnionInit(...@@ -4778,7 +4813,7 @@ fn validateUnionInit(
4778 return sema.failWithOwnedErrorMsg(block, msg);4813 return sema.failWithOwnedErrorMsg(block, msg);
4779 }4814 }
47804815
4781 if (block.is_comptime and4816 if (block.isComptime() and
4782 (try sema.resolveDefinedValue(block, init_src, union_ptr)) != null)4817 (try sema.resolveDefinedValue(block, init_src, union_ptr)) != null)
4783 {4818 {
4784 // In this case, comptime machinery already did everything. No work to do here.4819 // In this case, comptime machinery already did everything. No work to do here.
...@@ -4897,9 +4932,11 @@ fn validateUnionInit(...@@ -4897,9 +4932,11 @@ fn validateUnionInit(
4897 try sema.storePtr2(block, init_src, union_ptr, init_src, union_init, init_src, .store);4932 try sema.storePtr2(block, init_src, union_ptr, init_src, union_init, init_src, .store);
4898 return;4933 return;
4899 } else if (try union_ty.comptimeOnlySema(pt)) {4934 } else if (try union_ty.comptimeOnlySema(pt)) {
4900 return sema.failWithNeededComptime(block, block.nodeOffset(field_ptr_data.src_node), .{4935 const src = block.nodeOffset(field_ptr_data.src_node);
4901 .needed_comptime_reason = "initializer of comptime only union must be comptime-known",4936 return sema.failWithNeededComptime(block, src, .{ .comptime_only = .{
4902 });4937 .ty = union_ty,
4938 .msg = .union_init,
4939 } });
4903 }4940 }
4904 if (init_ref) |v| try sema.validateRuntimeValue(block, block.nodeOffset(field_ptr_data.src_node), v);4941 if (init_ref) |v| try sema.validateRuntimeValue(block, block.nodeOffset(field_ptr_data.src_node), v);
49054942
...@@ -4953,7 +4990,7 @@ fn validateStructInit(...@@ -4953,7 +4990,7 @@ fn validateStructInit(
4953 errdefer if (root_msg) |msg| msg.destroy(sema.gpa);4990 errdefer if (root_msg) |msg| msg.destroy(sema.gpa);
49544991
4955 const struct_ptr = try sema.resolveInst(struct_ptr_zir_ref);4992 const struct_ptr = try sema.resolveInst(struct_ptr_zir_ref);
4956 if (block.is_comptime and4993 if (block.isComptime() and
4957 (try sema.resolveDefinedValue(block, init_src, struct_ptr)) != null)4994 (try sema.resolveDefinedValue(block, init_src, struct_ptr)) != null)
4958 {4995 {
4959 try struct_ty.resolveLayout(pt);4996 try struct_ty.resolveLayout(pt);
...@@ -5081,9 +5118,11 @@ fn validateStructInit(...@@ -5081,9 +5118,11 @@ fn validateStructInit(
5081 field_values[i] = val.toIntern();5118 field_values[i] = val.toIntern();
5082 } else if (require_comptime) {5119 } else if (require_comptime) {
5083 const field_ptr_data = sema.code.instructions.items(.data)[@intFromEnum(field_ptr)].pl_node;5120 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), .{5121 const src = block.nodeOffset(field_ptr_data.src_node);
5085 .needed_comptime_reason = "initializer of comptime only struct must be comptime-known",5122 return sema.failWithNeededComptime(block, src, .{ .comptime_only = .{
5086 });5123 .ty = struct_ty,
5124 .msg = .struct_init,
5125 } });
5087 } else {5126 } else {
5088 struct_is_comptime = false;5127 struct_is_comptime = false;
5089 }5128 }
...@@ -5253,7 +5292,7 @@ fn zirValidatePtrArrayInit(...@@ -5253,7 +5292,7 @@ fn zirValidatePtrArrayInit(
5253 else => unreachable,5292 else => unreachable,
5254 };5293 };
52555294
5256 if (block.is_comptime and5295 if (block.isComptime() and
5257 (try sema.resolveDefinedValue(block, init_src, array_ptr)) != null)5296 (try sema.resolveDefinedValue(block, init_src, array_ptr)) != null)
5258 {5297 {
5259 // In this case the comptime machinery will have evaluated the store instructions5298 // In this case the comptime machinery will have evaluated the store instructions
...@@ -5629,9 +5668,7 @@ fn storeToInferredAllocComptime(...@@ -5629,9 +5668,7 @@ fn storeToInferredAllocComptime(
5629 // There will be only one store_to_inferred_ptr because we are running at comptime.5668 // There will be only one store_to_inferred_ptr because we are running at comptime.
5630 // The alloc will turn into a Decl or a ComptimeAlloc.5669 // The alloc will turn into a Decl or a ComptimeAlloc.
5631 const operand_val = try sema.resolveValue(operand) orelse {5670 const operand_val = try sema.resolveValue(operand) orelse {
5632 return sema.failWithNeededComptime(block, src, .{5671 return sema.failWithNeededComptime(block, src, .{ .simple = .stored_to_comptime_var });
5633 .needed_comptime_reason = "value being stored to a comptime variable must be comptime-known",
5634 });
5635 };5672 };
5636 const alloc_ty = try pt.ptrTypeSema(.{5673 const alloc_ty = try pt.ptrTypeSema(.{
5637 .child = operand_ty.toIntern(),5674 .child = operand_ty.toIntern(),
...@@ -5663,9 +5700,7 @@ fn storeToInferredAllocComptime(...@@ -5663,9 +5700,7 @@ fn storeToInferredAllocComptime(
5663fn zirSetEvalBranchQuota(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {5700fn zirSetEvalBranchQuota(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
5664 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;5701 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
5665 const src = block.nodeOffset(inst_data.src_node);5702 const src = block.nodeOffset(inst_data.src_node);
5666 const quota: u32 = @intCast(try sema.resolveInt(block, src, inst_data.operand, Type.u32, .{5703 const quota: u32 = @intCast(try sema.resolveInt(block, src, inst_data.operand, .u32, .{ .simple = .operand_setEvalBranchQuota }));
5667 .needed_comptime_reason = "eval branch quota must be comptime-known",
5668 }));
5669 sema.branch_quota = @max(sema.branch_quota, quota);5704 sema.branch_quota = @max(sema.branch_quota, quota);
5670 sema.allow_memoize = false;5705 sema.allow_memoize = false;
5671}5706}
...@@ -5794,9 +5829,7 @@ fn zirCompileError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro...@@ -5794,9 +5829,7 @@ fn zirCompileError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
5794 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;5829 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
5795 const src = block.nodeOffset(inst_data.src_node);5830 const src = block.nodeOffset(inst_data.src_node);
5796 const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0);5831 const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0);
5797 const msg = try sema.resolveConstString(block, operand_src, inst_data.operand, .{5832 const msg = try sema.resolveConstString(block, operand_src, inst_data.operand, .{ .simple = .compile_error_string });
5798 .needed_comptime_reason = "compile error string must be comptime-known",
5799 });
5800 return sema.fail(block, src, "{s}", .{msg});5833 return sema.fail(block, src, "{s}", .{msg});
5801}5834}
58025835
...@@ -5848,7 +5881,7 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void...@@ -5848,7 +5881,7 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
5848 // source location if we do it here.5881 // source location if we do it here.
5849 const coerced_msg = try sema.coerce(block, Type.slice_const_u8, msg_inst, block.builtinCallArgSrc(inst_data.src_node, 0));5882 const coerced_msg = try sema.coerce(block, Type.slice_const_u8, msg_inst, block.builtinCallArgSrc(inst_data.src_node, 0));
58505883
5851 if (block.is_comptime) {5884 if (block.isComptime()) {
5852 return sema.fail(block, src, "encountered @panic at comptime", .{});5885 return sema.fail(block, src, "encountered @panic at comptime", .{});
5853 }5886 }
58545887
...@@ -5864,7 +5897,7 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void...@@ -5864,7 +5897,7 @@ fn zirPanic(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
5864fn zirTrap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {5897fn zirTrap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
5865 const src_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].node;5898 const src_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].node;
5866 const src = block.nodeOffset(src_node);5899 const src = block.nodeOffset(src_node);
5867 if (block.is_comptime)5900 if (block.isComptime())
5868 return sema.fail(block, src, "encountered @trap at comptime", .{});5901 return sema.fail(block, src, "encountered @trap at comptime", .{});
5869 _ = try block.addNoOp(.trap);5902 _ = try block.addNoOp(.trap);
5870}5903}
...@@ -5974,15 +6007,16 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr...@@ -5974,15 +6007,16 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
5974 var c_import_buf = std.ArrayList(u8).init(gpa);6007 var c_import_buf = std.ArrayList(u8).init(gpa);
5975 defer c_import_buf.deinit();6008 defer c_import_buf.deinit();
59766009
5977 const comptime_reason: Block.ComptimeReason = .{ .c_import = .{ .src = src } };
5978 var child_block: Block = .{6010 var child_block: Block = .{
5979 .parent = parent_block,6011 .parent = parent_block,
5980 .sema = sema,6012 .sema = sema,
5981 .namespace = parent_block.namespace,6013 .namespace = parent_block.namespace,
5982 .instructions = .{},6014 .instructions = .{},
5983 .inlining = parent_block.inlining,6015 .inlining = parent_block.inlining,
5984 .is_comptime = true,6016 .comptime_reason = .{ .reason = .{
5985 .comptime_reason = &comptime_reason,6017 .src = src,
6018 .r = .{ .simple = .operand_cImport },
6019 } },
5986 .c_import_buf = &c_import_buf,6020 .c_import_buf = &c_import_buf,
5987 .runtime_cond = parent_block.runtime_cond,6021 .runtime_cond = parent_block.runtime_cond,
5988 .runtime_loop = parent_block.runtime_loop,6022 .runtime_loop = parent_block.runtime_loop,
...@@ -6073,7 +6107,7 @@ fn zirSuspendBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) Comp...@@ -6073,7 +6107,7 @@ fn zirSuspendBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) Comp
6073 return sema.failWithUseOfAsync(parent_block, src);6107 return sema.failWithUseOfAsync(parent_block, src);
6074}6108}
60756109
6076fn zirBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index, force_comptime: bool) CompileError!Air.Inst.Ref {6110fn zirBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
6077 const tracy = trace(@src());6111 const tracy = trace(@src());
6078 defer tracy.end();6112 defer tracy.end();
60796113
...@@ -6109,7 +6143,6 @@ fn zirBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index, force_compt...@@ -6109,7 +6143,6 @@ fn zirBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index, force_compt
6109 .instructions = .{},6143 .instructions = .{},
6110 .label = &label,6144 .label = &label,
6111 .inlining = parent_block.inlining,6145 .inlining = parent_block.inlining,
6112 .is_comptime = parent_block.is_comptime or force_comptime,
6113 .comptime_reason = parent_block.comptime_reason,6146 .comptime_reason = parent_block.comptime_reason,
6114 .is_typeof = parent_block.is_typeof,6147 .is_typeof = parent_block.is_typeof,
6115 .want_safety = parent_block.want_safety,6148 .want_safety = parent_block.want_safety,
...@@ -6143,7 +6176,7 @@ fn resolveBlockBody(...@@ -6143,7 +6176,7 @@ fn resolveBlockBody(
6143 body_inst: Zir.Inst.Index,6176 body_inst: Zir.Inst.Index,
6144 merges: *Block.Merges,6177 merges: *Block.Merges,
6145) CompileError!Air.Inst.Ref {6178) CompileError!Air.Inst.Ref {
6146 if (child_block.is_comptime) {6179 if (child_block.isComptime()) {
6147 return sema.resolveInlineBody(child_block, body, body_inst);6180 return sema.resolveInlineBody(child_block, body, body_inst);
6148 } else {6181 } else {
6149 assert(sema.air_instructions.items(.tag)[@intFromEnum(merges.block_inst)] == .block);6182 assert(sema.air_instructions.items(.tag)[@intFromEnum(merges.block_inst)] == .block);
...@@ -6303,7 +6336,7 @@ fn resolveAnalyzedBlock(...@@ -6303,7 +6336,7 @@ fn resolveAnalyzedBlock(
6303 }6336 }
6304 }6337 }
6305 // It is impossible to have the number of results be > 1 in a comptime scope.6338 // It is impossible to have the number of results be > 1 in a comptime scope.
6306 assert(!child_block.is_comptime); // Should already got a compile error in the condbr condition.6339 assert(!child_block.isComptime()); // Should already got a compile error in the condbr condition.
63076340
6308 // Note that we'll always create an AIR block here, so `need_debug_scope` is irrelevant.6341 // Note that we'll always create an AIR block here, so `need_debug_scope` is irrelevant.
63096342
...@@ -6425,9 +6458,7 @@ fn zirExport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void...@@ -6425,9 +6458,7 @@ fn zirExport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
6425 const options_src = block.builtinCallArgSrc(inst_data.src_node, 1);6458 const options_src = block.builtinCallArgSrc(inst_data.src_node, 1);
64266459
6427 const ptr = try sema.resolveInst(extra.exported);6460 const ptr = try sema.resolveInst(extra.exported);
6428 const ptr_val = try sema.resolveConstDefinedValue(block, ptr_src, ptr, .{6461 const ptr_val = try sema.resolveConstDefinedValue(block, ptr_src, ptr, .{ .simple = .export_target });
6429 .needed_comptime_reason = "export target must be comptime-known",
6430 });
6431 const ptr_ty = ptr_val.typeOf(zcu);6462 const ptr_ty = ptr_val.typeOf(zcu);
64326463
6433 const options = try sema.resolveExportOptions(block, options_src, extra.options);6464 const options = try sema.resolveExportOptions(block, options_src, extra.options);
...@@ -6553,17 +6584,13 @@ fn zirDisableInstrumentation(sema: *Sema) CompileError!void {...@@ -6553,17 +6584,13 @@ fn zirDisableInstrumentation(sema: *Sema) CompileError!void {
6553fn zirSetFloatMode(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void {6584fn zirSetFloatMode(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void {
6554 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;6585 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
6555 const src = block.builtinCallArgSrc(extra.node, 0);6586 const src = block.builtinCallArgSrc(extra.node, 0);
6556 block.float_mode = try sema.resolveBuiltinEnum(block, src, extra.operand, "FloatMode", .{6587 block.float_mode = try sema.resolveBuiltinEnum(block, src, extra.operand, "FloatMode", .{ .simple = .operand_setFloatMode });
6557 .needed_comptime_reason = "operand to @setFloatMode must be comptime-known",
6558 });
6559}6588}
65606589
6561fn zirSetRuntimeSafety(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {6590fn zirSetRuntimeSafety(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
6562 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;6591 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
6563 const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0);6592 const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0);
6564 block.want_safety = try sema.resolveConstBool(block, operand_src, inst_data.operand, .{6593 block.want_safety = try sema.resolveConstBool(block, operand_src, inst_data.operand, .{ .simple = .operand_setRuntimeSafety });
6565 .needed_comptime_reason = "operand to @setRuntimeSafety must be comptime-known",
6566 });
6567}6594}
65686595
6569fn zirBreak(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index) CompileError!void {6596fn zirBreak(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index) CompileError!void {
...@@ -6650,7 +6677,7 @@ fn zirSwitchContinue(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index) Com...@@ -6650,7 +6677,7 @@ fn zirSwitchContinue(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index) Com
6650}6677}
66516678
6652fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {6679fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
6653 if (block.is_comptime or block.ownerModule().strip) return;6680 if (block.isComptime() or block.ownerModule().strip) return;
66546681
6655 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;6682 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;
66566683
...@@ -6676,7 +6703,7 @@ fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi...@@ -6676,7 +6703,7 @@ fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi
6676}6703}
66776704
6678fn zirDbgEmptyStmt(_: *Sema, block: *Block, _: Zir.Inst.Index) CompileError!void {6705fn zirDbgEmptyStmt(_: *Sema, block: *Block, _: Zir.Inst.Index) CompileError!void {
6679 if (block.is_comptime or block.ownerModule().strip) return;6706 if (block.isComptime() or block.ownerModule().strip) return;
6680 _ = try block.addNoOp(.dbg_empty_stmt);6707 _ = try block.addNoOp(.dbg_empty_stmt);
6681}6708}
66826709
...@@ -6699,7 +6726,7 @@ fn addDbgVar(...@@ -6699,7 +6726,7 @@ fn addDbgVar(
6699 air_tag: Air.Inst.Tag,6726 air_tag: Air.Inst.Tag,
6700 name: []const u8,6727 name: []const u8,
6701) CompileError!void {6728) CompileError!void {
6702 if (block.is_comptime or block.ownerModule().strip) return;6729 if (block.isComptime() or block.ownerModule().strip) return;
67036730
6704 const pt = sema.pt;6731 const pt = sema.pt;
6705 const zcu = pt.zcu;6732 const zcu = pt.zcu;
...@@ -6931,7 +6958,7 @@ pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref...@@ -6931,7 +6958,7 @@ pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref
6931 const zcu = pt.zcu;6958 const zcu = pt.zcu;
6932 const gpa = sema.gpa;6959 const gpa = sema.gpa;
69336960
6934 if (block.is_comptime or block.is_typeof) {6961 if (block.isComptime() or block.is_typeof) {
6935 const index_val = try pt.intValue_u64(Type.usize, sema.comptime_err_ret_trace.items.len);6962 const index_val = try pt.intValue_u64(Type.usize, sema.comptime_err_ret_trace.items.len);
6936 return Air.internedToRef(index_val.toIntern());6963 return Air.internedToRef(index_val.toIntern());
6937 }6964 }
...@@ -7134,7 +7161,7 @@ fn zirCall(...@@ -7134,7 +7161,7 @@ fn zirCall(
7134 }7161 }
71357162
7136 if (block.ownerModule().error_tracing and7163 if (block.ownerModule().error_tracing and
7137 !block.is_comptime and !block.is_typeof and (input_is_error or pop_error_return_trace))7164 !block.isComptime() and !block.is_typeof and (input_is_error or pop_error_return_trace))
7138 {7165 {
7139 const return_ty = sema.typeOf(call_inst);7166 const return_ty = sema.typeOf(call_inst);
7140 if (modifier != .always_tail and return_ty.isNoReturn(zcu))7167 if (modifier != .always_tail and return_ty.isNoReturn(zcu))
...@@ -7404,12 +7431,14 @@ const CallArgsInfo = union(enum) {...@@ -7404,12 +7431,14 @@ const CallArgsInfo = union(enum) {
7404 };7431 };
74057432
7406 // Generate args to comptime params in comptime block7433 // Generate args to comptime params in comptime block
7407 const parent_comptime = block.is_comptime;7434 const parent_comptime = block.comptime_reason;
7408 defer block.is_comptime = parent_comptime;7435 defer block.comptime_reason = parent_comptime;
7409 // Note that we are indexing into parameters, not arguments, so use `arg_index` instead of `real_arg_idx`7436 // Note that we are indexing into parameters, not arguments, so use `arg_index` instead of `real_arg_idx`
7410 if (arg_index < @min(param_count, 32) and func_ty_info.paramIsComptime(@intCast(arg_index))) {7437 if (arg_index < @min(param_count, 32) and func_ty_info.paramIsComptime(@intCast(arg_index))) {
7411 block.is_comptime = true;7438 block.comptime_reason = .{ .reason = .{
7412 // TODO set comptime_reason7439 .src = cai.argSrc(block, arg_index),
7440 .r = .{ .simple = .comptime_param_arg },
7441 } };
7413 }7442 }
7414 // Give the arg its result type7443 // Give the arg its result type
7415 const provide_param_ty = if (maybe_param_ty) |t| t else Type.generic_poison;7444 const provide_param_ty = if (maybe_param_ty) |t| t else Type.generic_poison;
...@@ -7611,23 +7640,37 @@ fn analyzeCall(...@@ -7611,23 +7640,37 @@ fn analyzeCall(
76117640
7612 const gpa = sema.gpa;7641 const gpa = sema.gpa;
76137642
7643 const func_ret_ty_src: LazySrcLoc = if (try sema.funcDeclSrcInst(func)) |fn_decl_inst| .{
7644 .base_node_inst = fn_decl_inst,
7645 .offset = .{ .node_offset_fn_type_ret_ty = 0 },
7646 } else func_src;
7647
7648 // If this is not `null`, the call is comptime.
7649 var comptime_call_reason: ?BlockComptimeReason = cr: {
7650 if (block.comptime_reason) |r| break :cr r;
7651 if (modifier == .compile_time) break :cr .{ .reason = .{
7652 .src = call_src,
7653 .r = .{ .simple = .comptime_call_modifier },
7654 } };
7655 break :cr null;
7656 };
7657
7614 const is_generic_call = func_ty_info.is_generic;7658 const is_generic_call = func_ty_info.is_generic;
7615 var is_comptime_call = block.is_comptime or modifier == .compile_time;7659 var is_inline_call = comptime_call_reason != null or modifier == .always_inline or func_ty_info.cc == .@"inline";
7616 var is_inline_call = is_comptime_call or modifier == .always_inline or func_ty_info.cc == .@"inline";7660 if (!is_inline_call) {
7617 var comptime_reason: ?*const Block.ComptimeReason = null;
7618 if (!is_inline_call and !is_comptime_call) {
7619 if (try Type.fromInterned(func_ty_info.return_type).comptimeOnlySema(pt)) {7661 if (try Type.fromInterned(func_ty_info.return_type).comptimeOnlySema(pt)) {
7620 is_comptime_call = true;
7621 is_inline_call = true;7662 is_inline_call = true;
7622 comptime_reason = &.{ .comptime_ret_ty = .{7663 comptime_call_reason = .{ .reason = .{
7623 .func = func,7664 .src = func_ret_ty_src,
7624 .func_src = func_src,7665 .r = .{ .comptime_only = .{
7625 .return_ty = Type.fromInterned(func_ty_info.return_type),7666 .ty = .fromInterned(func_ty_info.return_type),
7667 .msg = .ret_ty_call,
7668 } },
7626 } };7669 } };
7627 }7670 }
7628 }7671 }
76297672
7630 if (sema.func_is_naked and !is_inline_call and !is_comptime_call) {7673 if (sema.func_is_naked and !is_inline_call) {
7631 const msg = msg: {7674 const msg = msg: {
7632 const msg = try sema.errMsg(call_src, "runtime {s} not allowed in naked function", .{@tagName(operation)});7675 const msg = try sema.errMsg(call_src, "runtime {s} not allowed in naked function", .{@tagName(operation)});
7633 errdefer msg.destroy(sema.gpa);7676 errdefer msg.destroy(sema.gpa);
...@@ -7642,6 +7685,7 @@ fn analyzeCall(...@@ -7642,6 +7685,7 @@ fn analyzeCall(
7642 }7685 }
76437686
7644 if (!is_inline_call and is_generic_call) {7687 if (!is_inline_call and is_generic_call) {
7688 var comptime_ret_ty: Type = undefined;
7645 if (sema.instantiateGenericCall(7689 if (sema.instantiateGenericCall(
7646 block,7690 block,
7647 func,7691 func,
...@@ -7651,6 +7695,7 @@ fn analyzeCall(...@@ -7651,6 +7695,7 @@ fn analyzeCall(
7651 args_info,7695 args_info,
7652 call_tag,7696 call_tag,
7653 call_dbg_node,7697 call_dbg_node,
7698 &comptime_ret_ty,
7654 )) |some| {7699 )) |some| {
7655 return some;7700 return some;
7656 } else |err| switch (err) {7701 } else |err| switch (err) {
...@@ -7659,26 +7704,34 @@ fn analyzeCall(...@@ -7659,26 +7704,34 @@ fn analyzeCall(
7659 },7704 },
7660 error.ComptimeReturn => {7705 error.ComptimeReturn => {
7661 is_inline_call = true;7706 is_inline_call = true;
7662 is_comptime_call = true;7707 comptime_call_reason = .{ .reason = .{
7663 comptime_reason = &.{ .comptime_ret_ty = .{7708 .src = func_ret_ty_src,
7664 .func = func,7709 .r = .{
7665 .func_src = func_src,7710 .comptime_only = .{
7666 .return_ty = Type.fromInterned(func_ty_info.return_type),7711 .ty = comptime_ret_ty,
7712 .msg = .ret_ty_generic_call,
7713 },
7714 },
7667 } };7715 } };
7668 },7716 },
7669 else => |e| return e,7717 else => |e| return e,
7670 }7718 }
7671 }7719 }
76727720
7721 const is_comptime_call = comptime_call_reason != null;
7722 // `comptime_call_reason` shouldn't be mutated again
7723 defer assert(is_comptime_call == (comptime_call_reason != null));
7724
7673 if (is_comptime_call and modifier == .never_inline) {7725 if (is_comptime_call and modifier == .never_inline) {
7674 return sema.fail(block, call_src, "unable to perform 'never_inline' call at compile-time", .{});7726 return sema.fail(block, call_src, "unable to perform 'never_inline' call at compile-time", .{});
7675 }7727 }
76767728
7677 const result: Air.Inst.Ref = if (is_inline_call) res: {7729 const result: Air.Inst.Ref = if (is_inline_call) res: {
7678 const func_val = try sema.resolveConstDefinedValue(block, func_src, func, .{7730 const old_comptime_reason = block.comptime_reason;
7679 .needed_comptime_reason = "function being called at comptime must be comptime-known",7731 block.comptime_reason = comptime_call_reason;
7680 .block_comptime_reason = comptime_reason,7732 defer block.comptime_reason = old_comptime_reason;
7681 });7733
7734 const func_val = try sema.resolveConstDefinedValue(block, func_src, func, .{ .simple = .comptime_call_target });
7682 const module_fn_index = switch (zcu.intern_pool.indexToKey(func_val.toIntern())) {7735 const module_fn_index = switch (zcu.intern_pool.indexToKey(func_val.toIntern())) {
7683 .@"extern" => return sema.fail(block, call_src, "{s} call of extern function", .{7736 .@"extern" => return sema.fail(block, call_src, "{s} call of extern function", .{
7684 @as([]const u8, if (is_comptime_call) "comptime" else "inline"),7737 @as([]const u8, if (is_comptime_call) "comptime" else "inline"),
...@@ -7767,8 +7820,7 @@ fn analyzeCall(...@@ -7767,8 +7820,7 @@ fn analyzeCall(
7767 .label = null,7820 .label = null,
7768 .inlining = &inlining,7821 .inlining = &inlining,
7769 .is_typeof = block.is_typeof,7822 .is_typeof = block.is_typeof,
7770 .is_comptime = is_comptime_call,7823 .comptime_reason = if (is_comptime_call) .inlining_parent else null,
7771 .comptime_reason = comptime_reason,
7772 .error_return_trace_index = block.error_return_trace_index,7824 .error_return_trace_index = block.error_return_trace_index,
7773 .runtime_cond = block.runtime_cond,7825 .runtime_cond = block.runtime_cond,
7774 .runtime_loop = block.runtime_loop,7826 .runtime_loop = block.runtime_loop,
...@@ -7857,11 +7909,16 @@ fn analyzeCall(...@@ -7857,11 +7909,16 @@ fn analyzeCall(
7857 // on parameters, we must now do the same for the return type as we just did with7909 // on parameters, we must now do the same for the return type as we just did with
7858 // each of the parameters, resolving the return type and providing it to the child7910 // each of the parameters, resolving the return type and providing it to the child
7859 // `Sema` so that it can be used for the `ret_ptr` instruction.7911 // `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);
7864 const ret_ty_src: LazySrcLoc = .{ .base_node_inst = module_fn.zir_body_inst, .offset = .{ .node_offset_fn_type_ret_ty = 0 } };7912 const ret_ty_src: LazySrcLoc = .{ .base_node_inst = module_fn.zir_body_inst, .offset = .{ .node_offset_fn_type_ret_ty = 0 } };
7913 const ret_ty_inst = if (fn_info.ret_ty_body.len != 0) r: {
7914 const old_child_comptime_reason = child_block.comptime_reason;
7915 defer child_block.comptime_reason = old_child_comptime_reason;
7916 child_block.comptime_reason = .{ .reason = .{
7917 .src = ret_ty_src,
7918 .r = .{ .simple = .function_ret_ty },
7919 } };
7920 break :r try sema.resolveInlineBody(&child_block, fn_info.ret_ty_body, module_fn.zir_body_inst.resolve(ip) orelse return error.AnalysisFail);
7921 } else try sema.resolveInst(fn_info.ret_ty_ref);
7865 sema.fn_ret_ty = try sema.analyzeAsType(&child_block, ret_ty_src, ret_ty_inst);7922 sema.fn_ret_ty = try sema.analyzeAsType(&child_block, ret_ty_src, ret_ty_inst);
7866 if (module_fn.analysisUnordered(ip).inferred_error_set) {7923 if (module_fn.analysisUnordered(ip).inferred_error_set) {
7867 // Create a fresh inferred error set type for inline/comptime calls.7924 // Create a fresh inferred error set type for inline/comptime calls.
...@@ -8136,23 +8193,18 @@ fn analyzeInlineCallArg(...@@ -8136,23 +8193,18 @@ fn analyzeInlineCallArg(
8136 return casted_arg;8193 return casted_arg;
8137 }8194 }
8138 const arg_src = args_info.argSrc(arg_block, arg_i.*);8195 const arg_src = args_info.argSrc(arg_block, arg_i.*);
8139 if (try Type.fromInterned(param_ty).comptimeOnlySema(ics.callee().pt)) {8196 if (zir_tags[@intFromEnum(inst)] == .param_comptime) {
8140 _ = try ics.caller().resolveConstValue(arg_block, arg_src, casted_arg, .{8197 _ = try ics.caller().resolveConstValue(arg_block, arg_src, casted_arg, .{ .simple = .comptime_param_arg });
8141 .needed_comptime_reason = "argument to parameter with comptime-only type must be comptime-known",8198 } else if (!is_comptime_call and try Type.fromInterned(param_ty).comptimeOnlySema(ics.callee().pt)) {
8142 .block_comptime_reason = param_block.comptime_reason,8199 _ = try ics.caller().resolveConstValue(arg_block, arg_src, casted_arg, .{ .comptime_only = .{
8143 });8200 .ty = .fromInterned(param_ty),
8144 } else if (!is_comptime_call and zir_tags[@intFromEnum(inst)] == .param_comptime) {8201 .msg = .param_ty_arg,
8145 _ = try ics.caller().resolveConstValue(arg_block, arg_src, casted_arg, .{8202 } });
8146 .needed_comptime_reason = "parameter is comptime",
8147 });
8148 }8203 }
81498204
8150 if (is_comptime_call) {8205 if (is_comptime_call) {
8151 ics.callee().inst_map.putAssumeCapacityNoClobber(inst, casted_arg);8206 ics.callee().inst_map.putAssumeCapacityNoClobber(inst, casted_arg);
8152 const arg_val = try ics.caller().resolveConstValue(arg_block, arg_src, casted_arg, .{8207 const arg_val = try ics.caller().resolveConstValue(arg_block, arg_src, casted_arg, null);
8153 .needed_comptime_reason = "argument to function being called at comptime must be comptime-known",
8154 .block_comptime_reason = param_block.comptime_reason,
8155 });
8156 switch (arg_val.toIntern()) {8208 switch (arg_val.toIntern()) {
8157 .generic_poison, .generic_poison_type => {8209 .generic_poison, .generic_poison_type => {
8158 // This function is currently evaluated as part of an as-of-yet unresolvable8210 // This function is currently evaluated as part of an as-of-yet unresolvable
...@@ -8188,10 +8240,7 @@ fn analyzeInlineCallArg(...@@ -8188,10 +8240,7 @@ fn analyzeInlineCallArg(
81888240
8189 if (is_comptime_call) {8241 if (is_comptime_call) {
8190 ics.callee().inst_map.putAssumeCapacityNoClobber(inst, uncasted_arg);8242 ics.callee().inst_map.putAssumeCapacityNoClobber(inst, uncasted_arg);
8191 const arg_val = try ics.caller().resolveConstValue(arg_block, arg_src, uncasted_arg, .{8243 const arg_val = try ics.caller().resolveConstValue(arg_block, arg_src, uncasted_arg, null);
8192 .needed_comptime_reason = "argument to function being called at comptime must be comptime-known",
8193 .block_comptime_reason = param_block.comptime_reason,
8194 });
8195 switch (arg_val.toIntern()) {8244 switch (arg_val.toIntern()) {
8196 .generic_poison, .generic_poison_type => {8245 .generic_poison, .generic_poison_type => {
8197 // This function is currently evaluated as part of an as-of-yet unresolvable8246 // This function is currently evaluated as part of an as-of-yet unresolvable
...@@ -8208,9 +8257,7 @@ fn analyzeInlineCallArg(...@@ -8208,9 +8257,7 @@ fn analyzeInlineCallArg(
8208 memoized_arg_values[arg_i.*] = resolved_arg_val.toIntern();8257 memoized_arg_values[arg_i.*] = resolved_arg_val.toIntern();
8209 } else {8258 } else {
8210 if (zir_tags[@intFromEnum(inst)] == .param_anytype_comptime) {8259 if (zir_tags[@intFromEnum(inst)] == .param_anytype_comptime) {
8211 _ = try ics.caller().resolveConstValue(arg_block, arg_src, uncasted_arg, .{8260 _ = try ics.caller().resolveConstValue(arg_block, arg_src, uncasted_arg, .{ .simple = .comptime_param_arg });
8212 .needed_comptime_reason = "parameter is comptime",
8213 });
8214 }8261 }
8215 ics.callee().inst_map.putAssumeCapacityNoClobber(inst, uncasted_arg);8262 ics.callee().inst_map.putAssumeCapacityNoClobber(inst, uncasted_arg);
8216 }8263 }
...@@ -8237,15 +8284,18 @@ fn instantiateGenericCall(...@@ -8237,15 +8284,18 @@ fn instantiateGenericCall(
8237 args_info: CallArgsInfo,8284 args_info: CallArgsInfo,
8238 call_tag: Air.Inst.Tag,8285 call_tag: Air.Inst.Tag,
8239 call_dbg_node: ?Zir.Inst.Index,8286 call_dbg_node: ?Zir.Inst.Index,
8287 /// Populated when `error.ComptimeReturn` is returned.
8288 comptime_ret_ty: *Type,
8240) CompileError!Air.Inst.Ref {8289) CompileError!Air.Inst.Ref {
8241 const pt = sema.pt;8290 const pt = sema.pt;
8242 const zcu = pt.zcu;8291 const zcu = pt.zcu;
8243 const gpa = sema.gpa;8292 const gpa = sema.gpa;
8244 const ip = &zcu.intern_pool;8293 const ip = &zcu.intern_pool;
82458294
8246 const func_val = try sema.resolveConstDefinedValue(block, func_src, func, .{8295 // Generic function pointers are comptime-only types, so `func` is definitely comptime-known.
8247 .needed_comptime_reason = "generic function being called must be comptime-known",8296 const func_val = (sema.resolveValue(func) catch unreachable).?;
8248 });8297 if (func_val.isUndef(zcu)) return sema.failWithUseOfUndef(block, func_src);
8298
8249 const generic_owner = switch (zcu.intern_pool.indexToKey(func_val.toIntern())) {8299 const generic_owner = switch (zcu.intern_pool.indexToKey(func_val.toIntern())) {
8250 .func => func_val.toIntern(),8300 .func => func_val.toIntern(),
8251 .ptr => |ptr| ip.getNav(ptr.base_addr.nav).status.fully_resolved.val,8301 .ptr => |ptr| ip.getNav(ptr.base_addr.nav).status.fully_resolved.val,
...@@ -8310,7 +8360,7 @@ fn instantiateGenericCall(...@@ -8310,7 +8360,7 @@ fn instantiateGenericCall(
8310 .namespace = fn_nav.analysis.?.namespace,8360 .namespace = fn_nav.analysis.?.namespace,
8311 .instructions = .{},8361 .instructions = .{},
8312 .inlining = null,8362 .inlining = null,
8313 .is_comptime = true,8363 .comptime_reason = undefined, // set as needed
8314 .src_base_inst = fn_nav.analysis.?.zir_index,8364 .src_base_inst = fn_nav.analysis.?.zir_index,
8315 .type_name_ctx = fn_nav.fqn,8365 .type_name_ctx = fn_nav.fqn,
8316 };8366 };
...@@ -8354,12 +8404,13 @@ fn instantiateGenericCall(...@@ -8354,12 +8404,13 @@ fn instantiateGenericCall(
8354 child_sema.generic_call_src = prev_generic_call_src;8404 child_sema.generic_call_src = prev_generic_call_src;
8355 }8405 }
83568406
8407 const param_ty_src = child_block.tokenOffset(param_data.src_tok);
8408 child_block.comptime_reason = .{ .reason = .{
8409 .src = param_ty_src,
8410 .r = .{ .simple = .type },
8411 } };
8357 const param_ty_inst = try child_sema.resolveInlineBody(&child_block, param_ty_body, param_inst);8412 const param_ty_inst = try child_sema.resolveInlineBody(&child_block, param_ty_body, param_inst);
8358 break :param_ty try child_sema.analyzeAsType(8413 break :param_ty try child_sema.analyzeAsType(&child_block, param_ty_src, param_ty_inst);
8359 &child_block,
8360 child_block.tokenOffset(param_data.src_tok),
8361 param_ty_inst,
8362 );
8363 },8414 },
8364 else => unreachable,8415 else => unreachable,
8365 }8416 }
...@@ -8452,6 +8503,10 @@ fn instantiateGenericCall(...@@ -8452,6 +8503,10 @@ fn instantiateGenericCall(
84528503
8453 // We've already handled parameters, so don't resolve the whole body. Instead, just8504 // We've already handled parameters, so don't resolve the whole body. Instead, just
8454 // do the instructions after the params (i.e. the func itself).8505 // do the instructions after the params (i.e. the func itself).
8506 child_block.comptime_reason = .{ .reason = .{
8507 .src = call_src,
8508 .r = .{ .simple = .type },
8509 } };
8455 const new_func_inst = try child_sema.resolveInlineBody(&child_block, fn_info.param_body[args_info.count()..], fn_info.param_body_inst);8510 const new_func_inst = try child_sema.resolveInlineBody(&child_block, fn_info.param_body[args_info.count()..], fn_info.param_body_inst);
8456 const callee_index = (child_sema.resolveConstDefinedValue(&child_block, LazySrcLoc.unneeded, new_func_inst, undefined) catch unreachable).toIntern();8511 const callee_index = (child_sema.resolveConstDefinedValue(&child_block, LazySrcLoc.unneeded, new_func_inst, undefined) catch unreachable).toIntern();
84578512
...@@ -8465,6 +8520,7 @@ fn instantiateGenericCall(...@@ -8465,6 +8520,7 @@ fn instantiateGenericCall(
8465 // If the call evaluated to a return type that requires comptime, never mind8520 // If the call evaluated to a return type that requires comptime, never mind
8466 // our generic instantiation. Instead we need to perform a comptime call.8521 // our generic instantiation. Instead we need to perform a comptime call.
8467 if (try Type.fromInterned(func_ty_info.return_type).comptimeOnlySema(pt)) {8522 if (try Type.fromInterned(func_ty_info.return_type).comptimeOnlySema(pt)) {
8523 comptime_ret_ty.* = .fromInterned(func_ty_info.return_type);
8468 return error.ComptimeReturn;8524 return error.ComptimeReturn;
8469 }8525 }
8470 // Similarly, if the call evaluated to a generic type we need to instead8526 // Similarly, if the call evaluated to a generic type we need to instead
...@@ -8622,9 +8678,7 @@ fn zirVectorType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!...@@ -8622,9 +8678,7 @@ fn zirVectorType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
8622 const len_src = block.builtinCallArgSrc(inst_data.src_node, 0);8678 const len_src = block.builtinCallArgSrc(inst_data.src_node, 0);
8623 const elem_type_src = block.builtinCallArgSrc(inst_data.src_node, 1);8679 const elem_type_src = block.builtinCallArgSrc(inst_data.src_node, 1);
8624 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;8680 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, .{8681 const len: u32 = @intCast(try sema.resolveInt(block, len_src, extra.lhs, Type.u32, .{ .simple = .vector_length }));
8626 .needed_comptime_reason = "vector length must be comptime-known",
8627 }));
8628 const elem_type = try sema.resolveType(block, elem_type_src, extra.rhs);8682 const elem_type = try sema.resolveType(block, elem_type_src, extra.rhs);
8629 try sema.checkVectorElemType(block, elem_type_src, elem_type);8683 try sema.checkVectorElemType(block, elem_type_src, elem_type);
8630 const vector_type = try sema.pt.vectorType(.{8684 const vector_type = try sema.pt.vectorType(.{
...@@ -8642,9 +8696,7 @@ fn zirArrayType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -8642,9 +8696,7 @@ fn zirArrayType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
8642 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;8696 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
8643 const len_src = block.src(.{ .node_offset_array_type_len = inst_data.src_node });8697 const len_src = block.src(.{ .node_offset_array_type_len = inst_data.src_node });
8644 const elem_src = block.src(.{ .node_offset_array_type_elem = inst_data.src_node });8698 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, .{8699 const len = try sema.resolveInt(block, len_src, extra.lhs, Type.usize, .{ .simple = .array_length });
8646 .needed_comptime_reason = "array length must be comptime-known",
8647 });
8648 const elem_type = try sema.resolveType(block, elem_src, extra.rhs);8700 const elem_type = try sema.resolveType(block, elem_src, extra.rhs);
8649 try sema.validateArrayElemType(block, elem_type, elem_src);8701 try sema.validateArrayElemType(block, elem_type, elem_src);
8650 const array_ty = try sema.pt.arrayType(.{8702 const array_ty = try sema.pt.arrayType(.{
...@@ -8664,16 +8716,12 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compil...@@ -8664,16 +8716,12 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compil
8664 const len_src = block.src(.{ .node_offset_array_type_len = inst_data.src_node });8716 const len_src = block.src(.{ .node_offset_array_type_len = inst_data.src_node });
8665 const sentinel_src = block.src(.{ .node_offset_array_type_sentinel = inst_data.src_node });8717 const sentinel_src = block.src(.{ .node_offset_array_type_sentinel = inst_data.src_node });
8666 const elem_src = block.src(.{ .node_offset_array_type_elem = inst_data.src_node });8718 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, .{8719 const len = try sema.resolveInt(block, len_src, extra.len, Type.usize, .{ .simple = .array_length });
8668 .needed_comptime_reason = "array length must be comptime-known",
8669 });
8670 const elem_type = try sema.resolveType(block, elem_src, extra.elem_type);8720 const elem_type = try sema.resolveType(block, elem_src, extra.elem_type);
8671 try sema.validateArrayElemType(block, elem_type, elem_src);8721 try sema.validateArrayElemType(block, elem_type, elem_src);
8672 const uncasted_sentinel = try sema.resolveInst(extra.sentinel);8722 const uncasted_sentinel = try sema.resolveInst(extra.sentinel);
8673 const sentinel = try sema.coerce(block, elem_type, uncasted_sentinel, sentinel_src);8723 const sentinel = try sema.coerce(block, elem_type, uncasted_sentinel, sentinel_src);
8674 const sentinel_val = try sema.resolveConstDefinedValue(block, sentinel_src, sentinel, .{8724 const sentinel_val = try sema.resolveConstDefinedValue(block, sentinel_src, sentinel, .{ .simple = .array_sentinel });
8675 .needed_comptime_reason = "array sentinel value must be comptime-known",
8676 });
8677 const array_ty = try sema.pt.arrayType(.{8725 const array_ty = try sema.pt.arrayType(.{
8678 .len = len,8726 .len = len,
8679 .sentinel = sentinel_val.toIntern(),8727 .sentinel = sentinel_val.toIntern(),
...@@ -9071,9 +9119,7 @@ fn zirEnumFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError...@@ -9071,9 +9119,7 @@ fn zirEnumFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
9071 }9119 }
90729120
9073 if (dest_ty.intTagType(zcu).zigTypeTag(zcu) == .comptime_int) {9121 if (dest_ty.intTagType(zcu).zigTypeTag(zcu) == .comptime_int) {
9074 return sema.failWithNeededComptime(block, operand_src, .{9122 return sema.failWithNeededComptime(block, operand_src, .{ .simple = .casted_to_comptime_enum });
9075 .needed_comptime_reason = "value being casted to enum with 'comptime_int' tag type must be comptime-known",
9076 });
9077 }9123 }
90789124
9079 if (try sema.typeHasOnePossibleValue(dest_ty)) |opv| {9125 if (try sema.typeHasOnePossibleValue(dest_ty)) |opv| {
...@@ -9487,9 +9533,7 @@ fn zirFunc(...@@ -9487,9 +9533,7 @@ fn zirFunc(
9487 const ret_ty_body = sema.code.bodySlice(extra_index, extra.data.ret_body_len);9533 const ret_ty_body = sema.code.bodySlice(extra_index, extra.data.ret_body_len);
9488 extra_index += ret_ty_body.len;9534 extra_index += ret_ty_body.len;
94899535
9490 const ret_ty_val = try sema.resolveGenericBody(block, ret_ty_src, ret_ty_body, inst, Type.type, .{9536 const ret_ty_val = try sema.resolveGenericBody(block, ret_ty_src, ret_ty_body, inst, Type.type, .{ .simple = .function_ret_ty });
9491 .needed_comptime_reason = "return type must be comptime-known",
9492 });
9493 break :blk ret_ty_val.toType();9537 break :blk ret_ty_val.toType();
9494 },9538 },
9495 };9539 };
...@@ -9556,7 +9600,7 @@ fn resolveGenericBody(...@@ -9556,7 +9600,7 @@ fn resolveGenericBody(
9556 body: []const Zir.Inst.Index,9600 body: []const Zir.Inst.Index,
9557 func_inst: Zir.Inst.Index,9601 func_inst: Zir.Inst.Index,
9558 dest_ty: Type,9602 dest_ty: Type,
9559 reason: NeededComptimeReason,9603 reason: ComptimeReason,
9560) !Value {9604) !Value {
9561 assert(body.len != 0);9605 assert(body.len != 0);
95629606
...@@ -9894,7 +9938,7 @@ fn funcCommon(...@@ -9894,7 +9938,7 @@ fn funcCommon(
9894 };9938 };
9895 return sema.failWithOwnedErrorMsg(block, msg);9939 return sema.failWithOwnedErrorMsg(block, msg);
9896 }9940 }
9897 if (is_source_decl and requires_comptime and !param_is_comptime and has_body and !block.is_comptime) {9941 if (is_source_decl and requires_comptime and !param_is_comptime and has_body and !block.isComptime()) {
9898 const msg = msg: {9942 const msg = msg: {
9899 const msg = try sema.errMsg(param_src, "parameter of type '{}' must be declared comptime", .{9943 const msg = try sema.errMsg(param_src, "parameter of type '{}' must be declared comptime", .{
9900 param_ty.fmt(pt),9944 param_ty.fmt(pt),
...@@ -10132,7 +10176,7 @@ fn finishFunc(...@@ -10132,7 +10176,7 @@ fn finishFunc(
1013210176
10133 // If the return type is comptime-only but not dependent on parameters then10177 // If the return type is comptime-only but not dependent on parameters then
10134 // all parameter types also need to be comptime.10178 // all parameter types also need to be comptime.
10135 if (is_source_decl and opt_func_index != .none and ret_ty_requires_comptime and !block.is_comptime) comptime_check: {10179 if (is_source_decl and opt_func_index != .none and ret_ty_requires_comptime and !block.isComptime()) comptime_check: {
10136 for (block.params.items(.is_comptime)) |is_comptime| {10180 for (block.params.items(.is_comptime)) |is_comptime| {
10137 if (!is_comptime) break;10181 if (!is_comptime) break;
10138 } else break :comptime_check;10182 } else break :comptime_check;
...@@ -10547,9 +10591,7 @@ fn zirFieldValNamed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr...@@ -10547,9 +10591,7 @@ fn zirFieldValNamed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
10547 const field_name_src = block.builtinCallArgSrc(inst_data.src_node, 1);10591 const field_name_src = block.builtinCallArgSrc(inst_data.src_node, 1);
10548 const extra = sema.code.extraData(Zir.Inst.FieldNamed, inst_data.payload_index).data;10592 const extra = sema.code.extraData(Zir.Inst.FieldNamed, inst_data.payload_index).data;
10549 const object = try sema.resolveInst(extra.lhs);10593 const object = try sema.resolveInst(extra.lhs);
10550 const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, .{10594 const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, .{ .simple = .field_name });
10551 .needed_comptime_reason = "field name must be comptime-known",
10552 });
10553 return sema.fieldVal(block, src, object, field_name, field_name_src);10595 return sema.fieldVal(block, src, object, field_name, field_name_src);
10554}10596}
1055510597
...@@ -10562,9 +10604,7 @@ fn zirFieldPtrNamed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr...@@ -10562,9 +10604,7 @@ fn zirFieldPtrNamed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
10562 const field_name_src = block.builtinCallArgSrc(inst_data.src_node, 1);10604 const field_name_src = block.builtinCallArgSrc(inst_data.src_node, 1);
10563 const extra = sema.code.extraData(Zir.Inst.FieldNamed, inst_data.payload_index).data;10605 const extra = sema.code.extraData(Zir.Inst.FieldNamed, inst_data.payload_index).data;
10564 const object_ptr = try sema.resolveInst(extra.lhs);10606 const object_ptr = try sema.resolveInst(extra.lhs);
10565 const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, .{10607 const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, .{ .simple = .field_name });
10566 .needed_comptime_reason = "field name must be comptime-known",
10567 });
10568 return sema.fieldPtr(block, src, object_ptr, field_name, field_name_src, false);10608 return sema.fieldPtr(block, src, object_ptr, field_name, field_name_src, false);
10569}10609}
1057010610
...@@ -11923,7 +11963,6 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp...@@ -11923,7 +11963,6 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
11923 .instructions = .{},11963 .instructions = .{},
11924 .label = &label,11964 .label = &label,
11925 .inlining = block.inlining,11965 .inlining = block.inlining,
11926 .is_comptime = block.is_comptime,
11927 .comptime_reason = block.comptime_reason,11966 .comptime_reason = block.comptime_reason,
11928 .is_typeof = block.is_typeof,11967 .is_typeof = block.is_typeof,
11929 .c_import_buf = block.c_import_buf,11968 .c_import_buf = block.c_import_buf,
...@@ -12027,11 +12066,8 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp...@@ -12027,11 +12066,8 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
12027 };12066 };
12028 }12067 }
1202912068
12030 if (child_block.is_comptime) {12069 if (child_block.isComptime()) {
12031 _ = try sema.resolveConstDefinedValue(&child_block, main_operand_src, raw_operand_val, .{12070 _ = try sema.resolveConstDefinedValue(&child_block, main_operand_src, raw_operand_val, null);
12032 .needed_comptime_reason = "condition in comptime switch must be comptime-known",
12033 .block_comptime_reason = child_block.comptime_reason,
12034 });
12035 unreachable;12071 unreachable;
12036 }12072 }
1203712073
...@@ -12148,7 +12184,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r...@@ -12148,7 +12184,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1214812184
12149 const operand_ty = sema.typeOf(val);12185 const operand_ty = sema.typeOf(val);
1215012186
12151 if (extra.data.bits.has_continue and !block.is_comptime) {12187 if (extra.data.bits.has_continue and !block.isComptime()) {
12152 // Even if the operand is comptime-known, this `switch` is runtime.12188 // Even if the operand is comptime-known, this `switch` is runtime.
12153 if (try operand_ty.comptimeOnlySema(pt)) {12189 if (try operand_ty.comptimeOnlySema(pt)) {
12154 return sema.failWithOwnedErrorMsg(block, msg: {12190 return sema.failWithOwnedErrorMsg(block, msg: {
...@@ -12707,7 +12743,6 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r...@@ -12707,7 +12743,6 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
12707 .instructions = .{},12743 .instructions = .{},
12708 .label = &label,12744 .label = &label,
12709 .inlining = block.inlining,12745 .inlining = block.inlining,
12710 .is_comptime = block.is_comptime,
12711 .comptime_reason = block.comptime_reason,12746 .comptime_reason = block.comptime_reason,
12712 .is_typeof = block.is_typeof,12747 .is_typeof = block.is_typeof,
12713 .c_import_buf = block.c_import_buf,12748 .c_import_buf = block.c_import_buf,
...@@ -12790,11 +12825,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r...@@ -12790,11 +12825,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
12790 },12825 },
12791 }12826 }
1279212827
12793 if (child_block.is_comptime) {12828 if (child_block.isComptime()) {
12794 _ = try sema.resolveConstDefinedValue(&child_block, operand_src, operand.simple.cond, .{12829 _ = try sema.resolveConstDefinedValue(&child_block, operand_src, operand.simple.cond, null);
12795 .needed_comptime_reason = "condition in comptime switch must be comptime-known",
12796 .block_comptime_reason = child_block.comptime_reason,
12797 });
12798 unreachable;12830 unreachable;
12799 }12831 }
1280012832
...@@ -13582,10 +13614,7 @@ fn resolveSwitchComptimeLoop(...@@ -13582,10 +13614,7 @@ fn resolveSwitchComptimeLoop(
1358213614
13583 const cond_ref = try sema.switchCond(child_block, src, val);13615 const cond_ref = try sema.switchCond(child_block, src, val);
1358413616
13585 cond_val = try sema.resolveConstDefinedValue(child_block, src, cond_ref, .{13617 cond_val = try sema.resolveConstDefinedValue(child_block, src, cond_ref, null);
13586 .needed_comptime_reason = "condition in comptime switch must be comptime-known",
13587 .block_comptime_reason = child_block.comptime_reason,
13588 });
13589 spa.operand = .{ .simple = .{13618 spa.operand = .{ .simple = .{
13590 .by_val = val,13619 .by_val = val,
13591 .by_ref = ref,13620 .by_ref = ref,
...@@ -13825,9 +13854,7 @@ fn resolveSwitchItemVal(...@@ -13825,9 +13854,7 @@ fn resolveSwitchItemVal(
1382513854
13826 const item = try sema.coerce(block, coerce_ty, uncoerced_item, item_src);13855 const item = try sema.coerce(block, coerce_ty, uncoerced_item, item_src);
1382713856
13828 const maybe_lazy = try sema.resolveConstDefinedValue(block, item_src, item, .{13857 const maybe_lazy = try sema.resolveConstDefinedValue(block, item_src, item, .{ .simple = .switch_item });
13829 .needed_comptime_reason = "switch prong values must be comptime-known",
13830 });
1383113858
13832 const val = try sema.resolveLazyValue(maybe_lazy);13859 const val = try sema.resolveLazyValue(maybe_lazy);
13833 const new_item = if (val.toIntern() != maybe_lazy.toIntern()) blk: {13860 const new_item = if (val.toIntern() != maybe_lazy.toIntern()) blk: {
...@@ -14295,9 +14322,7 @@ fn zirHasField(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -14295,9 +14322,7 @@ fn zirHasField(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
14295 const ty_src = block.builtinCallArgSrc(inst_data.src_node, 0);14322 const ty_src = block.builtinCallArgSrc(inst_data.src_node, 0);
14296 const name_src = block.builtinCallArgSrc(inst_data.src_node, 1);14323 const name_src = block.builtinCallArgSrc(inst_data.src_node, 1);
14297 const ty = try sema.resolveType(block, ty_src, extra.lhs);14324 const ty = try sema.resolveType(block, ty_src, extra.lhs);
14298 const field_name = try sema.resolveConstStringIntern(block, name_src, extra.rhs, .{14325 const field_name = try sema.resolveConstStringIntern(block, name_src, extra.rhs, .{ .simple = .field_name });
14299 .needed_comptime_reason = "field name must be comptime-known",
14300 });
14301 try ty.resolveFields(pt);14326 try ty.resolveFields(pt);
14302 const ip = &zcu.intern_pool;14327 const ip = &zcu.intern_pool;
1430314328
...@@ -14344,9 +14369,7 @@ fn zirHasDecl(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -14344,9 +14369,7 @@ fn zirHasDecl(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
14344 const lhs_src = block.builtinCallArgSrc(inst_data.src_node, 0);14369 const lhs_src = block.builtinCallArgSrc(inst_data.src_node, 0);
14345 const rhs_src = block.builtinCallArgSrc(inst_data.src_node, 1);14370 const rhs_src = block.builtinCallArgSrc(inst_data.src_node, 1);
14346 const container_type = try sema.resolveType(block, lhs_src, extra.lhs);14371 const container_type = try sema.resolveType(block, lhs_src, extra.lhs);
14347 const decl_name = try sema.resolveConstStringIntern(block, rhs_src, extra.rhs, .{14372 const decl_name = try sema.resolveConstStringIntern(block, rhs_src, extra.rhs, .{ .simple = .decl_name });
14348 .needed_comptime_reason = "decl name must be comptime-known",
14349 });
1435014373
14351 try sema.checkNamespaceType(block, lhs_src, container_type);14374 try sema.checkNamespaceType(block, lhs_src, container_type);
1435214375
...@@ -14399,9 +14422,7 @@ fn zirEmbedFile(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -14399,9 +14422,7 @@ fn zirEmbedFile(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
14399 const pt = sema.pt;14422 const pt = sema.pt;
14400 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;14423 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
14401 const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0);14424 const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0);
14402 const name = try sema.resolveConstString(block, operand_src, inst_data.operand, .{14425 const name = try sema.resolveConstString(block, operand_src, inst_data.operand, .{ .simple = .operand_embedFile });
14403 .needed_comptime_reason = "file path name must be comptime-known",
14404 });
1440514426
14406 if (name.len == 0) {14427 if (name.len == 0) {
14407 return sema.fail(block, operand_src, "file path name cannot be empty", .{});14428 return sema.fail(block, operand_src, "file path name cannot be empty", .{});
...@@ -14985,7 +15006,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -14985,7 +15006,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1498515006
14986 const resolved_elem_ty = t: {15007 const resolved_elem_ty = t: {
14987 var trash_block = block.makeSubBlock();15008 var trash_block = block.makeSubBlock();
14988 trash_block.is_comptime = false;15009 trash_block.comptime_reason = null;
14989 defer trash_block.instructions.deinit(sema.gpa);15010 defer trash_block.instructions.deinit(sema.gpa);
1499015011
14991 const instructions = [_]Air.Inst.Ref{15012 const instructions = [_]Air.Inst.Ref{
...@@ -15268,9 +15289,7 @@ fn getArrayCatInfo(sema: *Sema, block: *Block, src: LazySrcLoc, operand: Air.Ins...@@ -15268,9 +15289,7 @@ fn getArrayCatInfo(sema: *Sema, block: *Block, src: LazySrcLoc, operand: Air.Ins
15268 const ptr_info = operand_ty.ptrInfo(zcu);15289 const ptr_info = operand_ty.ptrInfo(zcu);
15269 switch (ptr_info.flags.size) {15290 switch (ptr_info.flags.size) {
15270 .Slice => {15291 .Slice => {
15271 const val = try sema.resolveConstDefinedValue(block, src, operand, .{15292 const val = try sema.resolveConstDefinedValue(block, src, operand, .{ .simple = .slice_cat_operand });
15272 .needed_comptime_reason = "slice value being concatenated must be comptime-known",
15273 });
15274 return Type.ArrayInfo{15293 return Type.ArrayInfo{
15275 .elem_type = Type.fromInterned(ptr_info.child),15294 .elem_type = Type.fromInterned(ptr_info.child),
15276 .sentinel = switch (ptr_info.sentinel) {15295 .sentinel = switch (ptr_info.sentinel) {
...@@ -15431,9 +15450,7 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -15431,9 +15450,7 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1543115450
15432 if (lhs_ty.isTuple(zcu)) {15451 if (lhs_ty.isTuple(zcu)) {
15433 // In `**` rhs must be comptime-known, but lhs can be runtime-known15452 // 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, .{15453 const factor = try sema.resolveInt(block, rhs_src, extra.rhs, Type.usize, .{ .simple = .array_mul_factor });
15435 .needed_comptime_reason = "array multiplication factor must be comptime-known",
15436 });
15437 const factor_casted = try sema.usizeCast(block, rhs_src, factor);15454 const factor_casted = try sema.usizeCast(block, rhs_src, factor);
15438 return sema.analyzeTupleMul(block, inst_data.src_node, lhs, factor_casted);15455 return sema.analyzeTupleMul(block, inst_data.src_node, lhs, factor_casted);
15439 }15456 }
...@@ -15455,9 +15472,7 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -15455,9 +15472,7 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
15455 };15472 };
1545615473
15457 // In `**` rhs must be comptime-known, but lhs can be runtime-known15474 // 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, .{15475 const factor = try sema.resolveInt(block, rhs_src, extra.rhs, Type.usize, .{ .simple = .array_mul_factor });
15459 .needed_comptime_reason = "array multiplication factor must be comptime-known",
15460 });
1546115476
15462 const result_len_u64 = std.math.mul(u64, lhs_info.len, factor) catch15477 const result_len_u64 = std.math.mul(u64, lhs_info.len, factor) catch
15463 return sema.fail(block, rhs_src, "operation results in overflow", .{});15478 return sema.fail(block, rhs_src, "operation results in overflow", .{});
...@@ -17635,12 +17650,9 @@ fn zirAsm(...@@ -17635,12 +17650,9 @@ fn zirAsm(
17635 const is_global_assembly = sema.func_index == .none;17650 const is_global_assembly = sema.func_index == .none;
17636 const zir_tags = sema.code.instructions.items(.tag);17651 const zir_tags = sema.code.instructions.items(.tag);
1763717652
17638 const asm_source: []const u8 = if (tmpl_is_expr) blk: {17653 const asm_source: []const u8 = if (tmpl_is_expr) s: {
17639 const tmpl: Zir.Inst.Ref = @enumFromInt(@intFromEnum(extra.data.asm_source));17654 const tmpl: Zir.Inst.Ref = @enumFromInt(@intFromEnum(extra.data.asm_source));
17640 const s: []const u8 = try sema.resolveConstString(block, src, tmpl, .{17655 break :s try sema.resolveConstString(block, src, tmpl, .{ .simple = .inline_assembly_code });
17641 .needed_comptime_reason = "assembly code must be comptime-known",
17642 });
17643 break :blk s;
17644 } else sema.code.nullTerminatedString(extra.data.asm_source);17656 } else sema.code.nullTerminatedString(extra.data.asm_source);
1764517657
17646 if (is_global_assembly) {17658 if (is_global_assembly) {
...@@ -18203,7 +18215,7 @@ fn zirClosureGet(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat...@@ -18203,7 +18215,7 @@ fn zirClosureGet(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
18203 return sema.failWithOwnedErrorMsg(block, msg);18215 return sema.failWithOwnedErrorMsg(block, msg);
18204 }18216 }
1820518217
18206 if (!block.is_typeof and !block.is_comptime and sema.func_index != .none) {18218 if (!block.is_typeof and !block.isComptime() and sema.func_index != .none) {
18207 const msg = msg: {18219 const msg = msg: {
18208 const name = name: {18220 const name = name: {
18209 const file, const src_base_node = Zcu.LazySrcLoc.resolveBaseNode(block.src_base_inst, zcu).?;18221 const file, const src_base_node = Zcu.LazySrcLoc.resolveBaseNode(block.src_base_inst, zcu).?;
...@@ -18244,7 +18256,7 @@ fn zirRetAddr(...@@ -18244,7 +18256,7 @@ fn zirRetAddr(
18244 extended: Zir.Inst.Extended.InstData,18256 extended: Zir.Inst.Extended.InstData,
18245) CompileError!Air.Inst.Ref {18257) CompileError!Air.Inst.Ref {
18246 _ = extended;18258 _ = extended;
18247 if (block.is_comptime) {18259 if (block.isComptime()) {
18248 // TODO: we could give a meaningful lazy value here. #1493818260 // TODO: we could give a meaningful lazy value here. #14938
18249 return sema.pt.intRef(Type.usize, 0);18261 return sema.pt.intRef(Type.usize, 0);
18250 } else {18262 } else {
...@@ -19342,7 +19354,7 @@ fn zirTypeofBuiltin(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr...@@ -19342,7 +19354,7 @@ fn zirTypeofBuiltin(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
19342 .namespace = block.namespace,19354 .namespace = block.namespace,
19343 .instructions = .{},19355 .instructions = .{},
19344 .inlining = block.inlining,19356 .inlining = block.inlining,
19345 .is_comptime = false,19357 .comptime_reason = null,
19346 .is_typeof = true,19358 .is_typeof = true,
19347 .want_safety = false,19359 .want_safety = false,
19348 .error_return_trace_index = block.error_return_trace_index,19360 .error_return_trace_index = block.error_return_trace_index,
...@@ -19422,7 +19434,7 @@ fn zirTypeofPeer(...@@ -19422,7 +19434,7 @@ fn zirTypeofPeer(
19422 .namespace = block.namespace,19434 .namespace = block.namespace,
19423 .instructions = .{},19435 .instructions = .{},
19424 .inlining = block.inlining,19436 .inlining = block.inlining,
19425 .is_comptime = false,19437 .comptime_reason = null,
19426 .is_typeof = true,19438 .is_typeof = true,
19427 .runtime_cond = block.runtime_cond,19439 .runtime_cond = block.runtime_cond,
19428 .runtime_loop = block.runtime_loop,19440 .runtime_loop = block.runtime_loop,
...@@ -19980,7 +19992,7 @@ fn ensurePostHoc(sema: *Sema, block: *Block, dest_block: Zir.Inst.Index) !*Label...@@ -19980,7 +19992,7 @@ fn ensurePostHoc(sema: *Sema, block: *Block, dest_block: Zir.Inst.Index) !*Label
19980 .instructions = .{},19992 .instructions = .{},
19981 .label = &labeled_block.label,19993 .label = &labeled_block.label,
19982 .inlining = block.inlining,19994 .inlining = block.inlining,
19983 .is_comptime = block.is_comptime,19995 .comptime_reason = block.comptime_reason,
19984 .src_base_inst = block.src_base_inst,19996 .src_base_inst = block.src_base_inst,
19985 .type_name_ctx = block.type_name_ctx,19997 .type_name_ctx = block.type_name_ctx,
19986 },19998 },
...@@ -20013,7 +20025,7 @@ fn zirUnreachable(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError...@@ -20013,7 +20025,7 @@ fn zirUnreachable(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
20013 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].@"unreachable";20025 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].@"unreachable";
20014 const src = block.nodeOffset(inst_data.src_node);20026 const src = block.nodeOffset(inst_data.src_node);
2001520027
20016 if (block.is_comptime) {20028 if (block.isComptime()) {
20017 return sema.fail(block, src, "reached unreachable code", .{});20029 return sema.fail(block, src, "reached unreachable code", .{});
20018 }20030 }
20019 // TODO Add compile error for @optimizeFor occurring too late in a scope.20031 // TODO Add compile error for @optimizeFor occurring too late in a scope.
...@@ -20066,7 +20078,7 @@ fn zirRetImplicit(...@@ -20066,7 +20078,7 @@ fn zirRetImplicit(
20066 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_tok;20078 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_tok;
20067 const r_brace_src = block.tokenOffset(inst_data.src_tok);20079 const r_brace_src = block.tokenOffset(inst_data.src_tok);
20068 if (block.inlining == null and sema.func_is_naked) {20080 if (block.inlining == null and sema.func_is_naked) {
20069 assert(!block.is_comptime);20081 assert(!block.isComptime());
20070 if (block.wantSafety()) {20082 if (block.wantSafety()) {
20071 // Calling a safety function from a naked function would not be legal.20083 // Calling a safety function from a naked function would not be legal.
20072 _ = try block.addNoOp(.trap);20084 _ = try block.addNoOp(.trap);
...@@ -20123,7 +20135,7 @@ fn zirRetLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi...@@ -20123,7 +20135,7 @@ fn zirRetLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi
20123 const src = block.nodeOffset(inst_data.src_node);20135 const src = block.nodeOffset(inst_data.src_node);
20124 const ret_ptr = try sema.resolveInst(inst_data.operand);20136 const ret_ptr = try sema.resolveInst(inst_data.operand);
2012520137
20126 if (block.is_comptime or block.inlining != null or sema.func_is_naked) {20138 if (block.isComptime() or block.inlining != null or sema.func_is_naked) {
20127 const operand = try sema.analyzeLoad(block, src, ret_ptr, src);20139 const operand = try sema.analyzeLoad(block, src, ret_ptr, src);
20128 return sema.analyzeRet(block, operand, src, block.src(.{ .node_offset_return_operand = inst_data.src_node }));20140 return sema.analyzeRet(block, operand, src, block.src(.{ .node_offset_return_operand = inst_data.src_node }));
20129 }20141 }
...@@ -20215,7 +20227,7 @@ fn zirSaveErrRetIndex(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE...@@ -20215,7 +20227,7 @@ fn zirSaveErrRetIndex(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
20215 if (!block.ownerModule().error_tracing) return;20227 if (!block.ownerModule().error_tracing) return;
2021620228
20217 // This is only relevant at runtime.20229 // This is only relevant at runtime.
20218 if (block.is_comptime or block.is_typeof) return;20230 if (block.isComptime() or block.is_typeof) return;
2021920231
20220 const save_index = inst_data.operand == .none or b: {20232 const save_index = inst_data.operand == .none or b: {
20221 const operand = try sema.resolveInst(inst_data.operand);20233 const operand = try sema.resolveInst(inst_data.operand);
...@@ -20268,7 +20280,7 @@ fn restoreErrRetIndex(sema: *Sema, start_block: *Block, src: LazySrcLoc, target_...@@ -20268,7 +20280,7 @@ fn restoreErrRetIndex(sema: *Sema, start_block: *Block, src: LazySrcLoc, target_
2026820280
20269 const operand = try sema.resolveInstAllowNone(operand_zir);20281 const operand = try sema.resolveInstAllowNone(operand_zir);
2027020282
20271 if (start_block.is_comptime or start_block.is_typeof) {20283 if (start_block.isComptime() or start_block.is_typeof) {
20272 const is_non_error = if (operand != .none) blk: {20284 const is_non_error = if (operand != .none) blk: {
20273 const is_non_error_inst = try sema.analyzeIsNonErr(start_block, src, operand);20285 const is_non_error_inst = try sema.analyzeIsNonErr(start_block, src, operand);
20274 const cond_val = try sema.resolveDefinedValue(start_block, src, is_non_error_inst);20286 const cond_val = try sema.resolveDefinedValue(start_block, src, is_non_error_inst);
...@@ -20345,10 +20357,8 @@ fn analyzeRet(...@@ -20345,10 +20357,8 @@ fn analyzeRet(
20345 };20357 };
2034620358
20347 if (block.inlining) |inlining| {20359 if (block.inlining) |inlining| {
20348 if (block.is_comptime) {20360 if (block.isComptime()) {
20349 const ret_val = try sema.resolveConstValue(block, operand_src, operand, .{20361 const ret_val = try sema.resolveConstValue(block, operand_src, operand, null);
20350 .needed_comptime_reason = "value being returned at comptime must be comptime-known",
20351 });
20352 inlining.comptime_result = operand;20362 inlining.comptime_result = operand;
2035320363
20354 if (sema.fn_ret_ty.isError(zcu) and ret_val.getErrorName(zcu) != .none) {20364 if (sema.fn_ret_ty.isError(zcu) and ret_val.getErrorName(zcu) != .none) {
...@@ -20362,7 +20372,7 @@ fn analyzeRet(...@@ -20362,7 +20372,7 @@ fn analyzeRet(
20362 try inlining.merges.br_list.append(sema.gpa, br_inst.toIndex().?);20372 try inlining.merges.br_list.append(sema.gpa, br_inst.toIndex().?);
20363 try inlining.merges.src_locs.append(sema.gpa, operand_src);20373 try inlining.merges.src_locs.append(sema.gpa, operand_src);
20364 return;20374 return;
20365 } else if (block.is_comptime) {20375 } else if (block.isComptime()) {
20366 return sema.fail(block, src, "function called at runtime cannot return value at comptime", .{});20376 return sema.fail(block, src, "function called at runtime cannot return value at comptime", .{});
20367 } else if (sema.func_is_naked) {20377 } else if (sema.func_is_naked) {
20368 const msg = msg: {20378 const msg = msg: {
...@@ -20436,9 +20446,7 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -20436,9 +20446,7 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
20436 const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]);20446 const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]);
20437 extra_i += 1;20447 extra_i += 1;
20438 const coerced = try sema.coerce(block, elem_ty, try sema.resolveInst(ref), sentinel_src);20448 const coerced = try sema.coerce(block, elem_ty, try sema.resolveInst(ref), sentinel_src);
20439 const val = try sema.resolveConstDefinedValue(block, sentinel_src, coerced, .{20449 const val = try sema.resolveConstDefinedValue(block, sentinel_src, coerced, .{ .simple = .pointer_sentinel });
20440 .needed_comptime_reason = "pointer sentinel value must be comptime-known",
20441 });
20442 try checkSentinelType(sema, block, sentinel_src, elem_ty);20450 try checkSentinelType(sema, block, sentinel_src, elem_ty);
20443 break :blk val.toIntern();20451 break :blk val.toIntern();
20444 } else .none;20452 } else .none;
...@@ -20447,9 +20455,7 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -20447,9 +20455,7 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
20447 const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]);20455 const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]);
20448 extra_i += 1;20456 extra_i += 1;
20449 const coerced = try sema.coerce(block, Type.u32, try sema.resolveInst(ref), align_src);20457 const coerced = try sema.coerce(block, Type.u32, try sema.resolveInst(ref), align_src);
20450 const val = try sema.resolveConstDefinedValue(block, align_src, coerced, .{20458 const val = try sema.resolveConstDefinedValue(block, align_src, coerced, .{ .simple = .@"align" });
20451 .needed_comptime_reason = "pointer alignment must be comptime-known",
20452 });
20453 // Check if this happens to be the lazy alignment of our element type, in20459 // Check if this happens to be the lazy alignment of our element type, in
20454 // which case we can make this 0 without resolving it.20460 // which case we can make this 0 without resolving it.
20455 switch (zcu.intern_pool.indexToKey(val.toIntern())) {20461 switch (zcu.intern_pool.indexToKey(val.toIntern())) {
...@@ -20472,18 +20478,14 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -20472,18 +20478,14 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
20472 const bit_offset: u16 = if (inst_data.flags.has_bit_range) blk: {20478 const bit_offset: u16 = if (inst_data.flags.has_bit_range) blk: {
20473 const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]);20479 const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]);
20474 extra_i += 1;20480 extra_i += 1;
20475 const bit_offset = try sema.resolveInt(block, bitoffset_src, ref, Type.u16, .{20481 const bit_offset = try sema.resolveInt(block, bitoffset_src, ref, Type.u16, .{ .simple = .type });
20476 .needed_comptime_reason = "pointer bit-offset must be comptime-known",
20477 });
20478 break :blk @intCast(bit_offset);20482 break :blk @intCast(bit_offset);
20479 } else 0;20483 } else 0;
2048020484
20481 const host_size: u16 = if (inst_data.flags.has_bit_range) blk: {20485 const host_size: u16 = if (inst_data.flags.has_bit_range) blk: {
20482 const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]);20486 const ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_i]);
20483 extra_i += 1;20487 extra_i += 1;
20484 const host_size = try sema.resolveInt(block, hostsize_src, ref, Type.u16, .{20488 const host_size = try sema.resolveInt(block, hostsize_src, ref, Type.u16, .{ .simple = .type });
20485 .needed_comptime_reason = "pointer host size must be comptime-known",
20486 });
20487 break :blk @intCast(host_size);20489 break :blk @intCast(host_size);
20488 } else 0;20490 } else 0;
2048920491
...@@ -20671,9 +20673,7 @@ fn zirUnionInit(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -20671,9 +20673,7 @@ fn zirUnionInit(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
20671 if (union_ty.zigTypeTag(pt.zcu) != .@"union") {20673 if (union_ty.zigTypeTag(pt.zcu) != .@"union") {
20672 return sema.fail(block, ty_src, "expected union type, found '{}'", .{union_ty.fmt(pt)});20674 return sema.fail(block, ty_src, "expected union type, found '{}'", .{union_ty.fmt(pt)});
20673 }20675 }
20674 const field_name = try sema.resolveConstStringIntern(block, field_src, extra.field_name, .{20676 const field_name = try sema.resolveConstStringIntern(block, field_src, extra.field_name, .{ .simple = .union_field_name });
20675 .needed_comptime_reason = "name of field being initialized must be comptime-known",
20676 });
20677 const init = try sema.resolveInst(extra.init);20677 const init = try sema.resolveInst(extra.init);
20678 return sema.unionInit(block, init, init_src, union_ty, ty_src, field_name, field_src);20678 return sema.unionInit(block, init, init_src, union_ty, ty_src, field_name, field_src);
20679}20679}
...@@ -20800,9 +20800,7 @@ fn zirStructInit(...@@ -20800,9 +20800,7 @@ fn zirStructInit(
20800 try resolved_ty.resolveStructFieldInits(pt);20800 try resolved_ty.resolveStructFieldInits(pt);
20801 if (try resolved_ty.structFieldValueComptime(pt, field_index)) |default_value| {20801 if (try resolved_ty.structFieldValueComptime(pt, field_index)) |default_value| {
20802 const init_val = (try sema.resolveValue(field_inits[field_index])) orelse {20802 const init_val = (try sema.resolveValue(field_inits[field_index])) orelse {
20803 return sema.failWithNeededComptime(block, field_src, .{20803 return sema.failWithNeededComptime(block, field_src, .{ .simple = .stored_to_comptime_field });
20804 .needed_comptime_reason = "value stored in comptime field must be comptime-known",
20805 });
20806 };20804 };
2080720805
20808 if (!init_val.eql(default_value, resolved_ty.fieldType(field_index, zcu), zcu)) {20806 if (!init_val.eql(default_value, resolved_ty.fieldType(field_index, zcu), zcu)) {
...@@ -20862,9 +20860,10 @@ fn zirStructInit(...@@ -20862,9 +20860,10 @@ fn zirStructInit(
20862 }20860 }
2086320861
20864 if (try resolved_ty.comptimeOnlySema(pt)) {20862 if (try resolved_ty.comptimeOnlySema(pt)) {
20865 return sema.failWithNeededComptime(block, field_src, .{20863 return sema.failWithNeededComptime(block, field_src, .{ .comptime_only = .{
20866 .needed_comptime_reason = "initializer of comptime only union must be comptime-known",20864 .ty = resolved_ty,
20867 });20865 .msg = .union_init,
20866 } });
20868 }20867 }
2086920868
20870 try sema.validateRuntimeValue(block, field_src, init_inst);20869 try sema.validateRuntimeValue(block, field_src, init_inst);
...@@ -21003,9 +21002,10 @@ fn finishStructInit(...@@ -21003,9 +21002,10 @@ fn finishStructInit(
21003 return sema.failWithNeededComptime(block, block.src(.{ .init_elem = .{21002 return sema.failWithNeededComptime(block, block.src(.{ .init_elem = .{
21004 .init_node_offset = init_src.offset.node_offset.x,21003 .init_node_offset = init_src.offset.node_offset.x,
21005 .elem_index = @intCast(runtime_index),21004 .elem_index = @intCast(runtime_index),
21006 } }), .{21005 } }), .{ .comptime_only = .{
21007 .needed_comptime_reason = "initializer of comptime only struct must be comptime-known",21006 .ty = struct_ty,
21008 });21007 .msg = .struct_init,
21008 } });
21009 }21009 }
2101021010
21011 for (field_inits) |field_init| {21011 for (field_inits) |field_init| {
...@@ -21315,11 +21315,7 @@ fn zirArrayInit(...@@ -21315,11 +21315,7 @@ fn zirArrayInit(
21315 if (array_ty.structFieldIsComptime(i, zcu))21315 if (array_ty.structFieldIsComptime(i, zcu))
21316 try array_ty.resolveStructFieldInits(pt);21316 try array_ty.resolveStructFieldInits(pt);
21317 if (try array_ty.structFieldValueComptime(pt, i)) |field_val| {21317 if (try array_ty.structFieldValueComptime(pt, i)) |field_val| {
21318 const init_val = try sema.resolveValue(dest.*) orelse {21318 const init_val = try sema.resolveConstValue(block, elem_src, dest.*, .{ .simple = .stored_to_comptime_field });
21319 return sema.failWithNeededComptime(block, elem_src, .{
21320 .needed_comptime_reason = "value stored in comptime field must be comptime-known",
21321 });
21322 };
21323 if (!field_val.eql(init_val, elem_ty, zcu)) {21319 if (!field_val.eql(init_val, elem_ty, zcu)) {
21324 return sema.failWithInvalidComptimeFieldStore(block, elem_src, array_ty, i);21320 return sema.failWithInvalidComptimeFieldStore(block, elem_src, array_ty, i);
21325 }21321 }
...@@ -21508,9 +21504,7 @@ fn zirFieldTypeRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro...@@ -21508,9 +21504,7 @@ fn zirFieldTypeRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
21508 const ty_src = block.builtinCallArgSrc(inst_data.src_node, 0);21504 const ty_src = block.builtinCallArgSrc(inst_data.src_node, 0);
21509 const field_src = block.builtinCallArgSrc(inst_data.src_node, 1);21505 const field_src = block.builtinCallArgSrc(inst_data.src_node, 1);
21510 const aggregate_ty = try sema.resolveType(block, ty_src, extra.container_type);21506 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, .{21507 const field_name = try sema.resolveConstStringIntern(block, field_src, extra.field_name, .{ .simple = .field_name });
21512 .needed_comptime_reason = "field name must be comptime-known",
21513 });
21514 return sema.fieldType(block, aggregate_ty, field_name, field_src, ty_src);21508 return sema.fieldType(block, aggregate_ty, field_name, field_src, ty_src);
21515}21509}
2151621510
...@@ -21890,9 +21884,7 @@ fn zirReify(...@@ -21890,9 +21884,7 @@ fn zirReify(
21890 const type_info_ty = try sema.getBuiltinType("Type");21884 const type_info_ty = try sema.getBuiltinType("Type");
21891 const uncasted_operand = try sema.resolveInst(extra.operand);21885 const uncasted_operand = try sema.resolveInst(extra.operand);
21892 const type_info = try sema.coerce(block, type_info_ty, uncasted_operand, operand_src);21886 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, .{21887 const val = try sema.resolveConstDefinedValue(block, operand_src, type_info, .{ .simple = .operand_Type });
21894 .needed_comptime_reason = "operand to @Type must be comptime-known",
21895 });
21896 const union_val = ip.indexToKey(val.toIntern()).un;21888 const union_val = ip.indexToKey(val.toIntern()).un;
21897 if (try sema.anyUndef(block, operand_src, Value.fromInterned(union_val.val))) {21889 if (try sema.anyUndef(block, operand_src, Value.fromInterned(union_val.val))) {
21898 return sema.failWithUseOfUndef(block, operand_src);21890 return sema.failWithUseOfUndef(block, operand_src);
...@@ -22136,9 +22128,7 @@ fn zirReify(...@@ -22136,9 +22128,7 @@ fn zirReify(
22136 const payload_val = Value.fromInterned(union_val.val).optionalValue(zcu) orelse22128 const payload_val = Value.fromInterned(union_val.val).optionalValue(zcu) orelse
22137 return Air.internedToRef(Type.anyerror.toIntern());22129 return Air.internedToRef(Type.anyerror.toIntern());
2213822130
22139 const names_val = try sema.derefSliceAsArray(block, src, payload_val, .{22131 const names_val = try sema.derefSliceAsArray(block, src, payload_val, .{ .simple = .error_set_contents });
22140 .needed_comptime_reason = "error set contents must be comptime-known",
22141 });
2214222132
22143 const len = try sema.usizeCast(block, src, names_val.typeOf(zcu).arrayLen(zcu));22133 const len = try sema.usizeCast(block, src, names_val.typeOf(zcu).arrayLen(zcu));
22144 var names: InferredErrorSet.NameMap = .{};22134 var names: InferredErrorSet.NameMap = .{};
...@@ -22151,9 +22141,7 @@ fn zirReify(...@@ -22151,9 +22141,7 @@ fn zirReify(
22151 try ip.getOrPutString(gpa, pt.tid, "name", .no_embedded_nulls),22141 try ip.getOrPutString(gpa, pt.tid, "name", .no_embedded_nulls),
22152 ).?);22142 ).?);
2215322143
22154 const name = try sema.sliceToIpString(block, src, name_val, .{22144 const name = try sema.sliceToIpString(block, src, name_val, .{ .simple = .error_set_contents });
22155 .needed_comptime_reason = "error set contents must be comptime-known",
22156 });
22157 _ = try pt.getErrorValue(name);22145 _ = try pt.getErrorValue(name);
22158 const gop = names.getOrPutAssumeCapacity(name);22146 const gop = names.getOrPutAssumeCapacity(name);
22159 if (gop.found_existing) {22147 if (gop.found_existing) {
...@@ -22200,9 +22188,7 @@ fn zirReify(...@@ -22200,9 +22188,7 @@ fn zirReify(
22200 return sema.fail(block, src, "non-packed struct does not support backing integer type", .{});22188 return sema.fail(block, src, "non-packed struct does not support backing integer type", .{});
22201 }22189 }
2220222190
22203 const fields_arr = try sema.derefSliceAsArray(block, operand_src, fields_val, .{22191 const fields_arr = try sema.derefSliceAsArray(block, operand_src, fields_val, .{ .simple = .struct_fields });
22204 .needed_comptime_reason = "struct fields must be comptime-known",
22205 });
2220622192
22207 if (is_tuple_val.toBool()) {22193 if (is_tuple_val.toBool()) {
22208 switch (layout) {22194 switch (layout) {
...@@ -22238,9 +22224,7 @@ fn zirReify(...@@ -22238,9 +22224,7 @@ fn zirReify(
22238 return sema.fail(block, src, "reified enums must have no decls", .{});22224 return sema.fail(block, src, "reified enums must have no decls", .{});
22239 }22225 }
2224022226
22241 const fields_arr = try sema.derefSliceAsArray(block, operand_src, fields_val, .{22227 const fields_arr = try sema.derefSliceAsArray(block, operand_src, fields_val, .{ .simple = .enum_fields });
22242 .needed_comptime_reason = "enum fields must be comptime-known",
22243 });
2224422228
22245 return sema.reifyEnum(block, inst, src, tag_type_val.toType(), is_exhaustive_val.toBool(), fields_arr, name_strategy);22229 return sema.reifyEnum(block, inst, src, tag_type_val.toType(), is_exhaustive_val.toBool(), fields_arr, name_strategy);
22246 },22230 },
...@@ -22311,9 +22295,7 @@ fn zirReify(...@@ -22311,9 +22295,7 @@ fn zirReify(
22311 }22295 }
22312 const layout = zcu.toEnum(std.builtin.Type.ContainerLayout, layout_val);22296 const layout = zcu.toEnum(std.builtin.Type.ContainerLayout, layout_val);
2231322297
22314 const fields_arr = try sema.derefSliceAsArray(block, operand_src, fields_val, .{22298 const fields_arr = try sema.derefSliceAsArray(block, operand_src, fields_val, .{ .simple = .union_fields });
22315 .needed_comptime_reason = "union fields must be comptime-known",
22316 });
2231722299
22318 return sema.reifyUnion(block, inst, src, layout, tag_type_val, fields_arr, name_strategy);22300 return sema.reifyUnion(block, inst, src, layout, tag_type_val, fields_arr, name_strategy);
22319 },22301 },
...@@ -22354,9 +22336,7 @@ fn zirReify(...@@ -22354,9 +22336,7 @@ fn zirReify(
22354 const return_type = return_type_val.optionalValue(zcu) orelse22336 const return_type = return_type_val.optionalValue(zcu) orelse
22355 return sema.fail(block, src, "Type.Fn.return_type must be non-null for @Type", .{});22337 return sema.fail(block, src, "Type.Fn.return_type must be non-null for @Type", .{});
2235622338
22357 const params_val = try sema.derefSliceAsArray(block, operand_src, params_slice_val, .{22339 const params_val = try sema.derefSliceAsArray(block, operand_src, params_slice_val, .{ .simple = .function_parameters });
22358 .needed_comptime_reason = "function parameters must be comptime-known",
22359 });
2236022340
22361 const args_len = try sema.usizeCast(block, src, params_val.typeOf(zcu).arrayLen(zcu));22341 const args_len = try sema.usizeCast(block, src, params_val.typeOf(zcu).arrayLen(zcu));
22362 const param_types = try sema.arena.alloc(InternPool.Index, args_len);22342 const param_types = try sema.arena.alloc(InternPool.Index, args_len);
...@@ -22444,9 +22424,7 @@ fn reifyEnum(...@@ -22444,9 +22424,7 @@ fn reifyEnum(
22444 const field_name_val = try field_info.fieldValue(pt, 0);22424 const field_name_val = try field_info.fieldValue(pt, 0);
22445 const field_value_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 1));22425 const field_value_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 1));
2244622426
22447 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{22427 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{ .simple = .enum_field_name });
22448 .needed_comptime_reason = "enum field name must be comptime-known",
22449 });
2245022428
22451 std.hash.autoHash(&hasher, .{22429 std.hash.autoHash(&hasher, .{
22452 field_name,22430 field_name,
...@@ -22591,9 +22569,7 @@ fn reifyUnion(...@@ -22591,9 +22569,7 @@ fn reifyUnion(
22591 const field_type_val = try field_info.fieldValue(pt, 1);22569 const field_type_val = try field_info.fieldValue(pt, 1);
22592 const field_align_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 2));22570 const field_align_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 2));
2259322571
22594 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{22572 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{ .simple = .union_field_name });
22595 .needed_comptime_reason = "union field name must be comptime-known",
22596 });
2259722573
22598 std.hash.autoHash(&hasher, .{22574 std.hash.autoHash(&hasher, .{
22599 field_name,22575 field_name,
...@@ -22835,9 +22811,7 @@ fn reifyTuple(...@@ -22835,9 +22811,7 @@ fn reifyTuple(
22835 const field_is_comptime_val = try field_info.fieldValue(pt, 3);22811 const field_is_comptime_val = try field_info.fieldValue(pt, 3);
22836 const field_alignment_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 4));22812 const field_alignment_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 4));
2283722813
22838 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{22814 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{ .simple = .tuple_field_name });
22839 .needed_comptime_reason = "tuple field name must be comptime-known",
22840 });
22841 const field_type = field_type_val.toType();22815 const field_type = field_type_val.toType();
22842 const field_default_value: InternPool.Index = if (field_default_value_val.optionalValue(zcu)) |ptr_val| d: {22816 const field_default_value: InternPool.Index = if (field_default_value_val.optionalValue(zcu)) |ptr_val| d: {
22843 const ptr_ty = try pt.singleConstPtrType(field_type_val.toType());22817 const ptr_ty = try pt.singleConstPtrType(field_type_val.toType());
...@@ -22845,7 +22819,7 @@ fn reifyTuple(...@@ -22845,7 +22819,7 @@ fn reifyTuple(
22845 const val = try sema.pointerDeref(block, src, ptr_val, ptr_ty) orelse return sema.failWithNeededComptime(22819 const val = try sema.pointerDeref(block, src, ptr_val, ptr_ty) orelse return sema.failWithNeededComptime(
22846 block,22820 block,
22847 src,22821 src,
22848 .{ .needed_comptime_reason = "tuple field default value must be comptime-known" },22822 .{ .simple = .tuple_field_default_value },
22849 );22823 );
22850 // Resolve the value so that lazy values do not create distinct types.22824 // Resolve the value so that lazy values do not create distinct types.
22851 break :d (try sema.resolveLazyValue(val)).toIntern();22825 break :d (try sema.resolveLazyValue(val)).toIntern();
...@@ -22951,9 +22925,7 @@ fn reifyStruct(...@@ -22951,9 +22925,7 @@ fn reifyStruct(
22951 const field_is_comptime_val = try field_info.fieldValue(pt, 3);22925 const field_is_comptime_val = try field_info.fieldValue(pt, 3);
22952 const field_alignment_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 4));22926 const field_alignment_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 4));
2295322927
22954 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{22928 const field_name = try sema.sliceToIpString(block, src, field_name_val, .{ .simple = .struct_field_name });
22955 .needed_comptime_reason = "struct field name must be comptime-known",
22956 });
22957 const field_is_comptime = field_is_comptime_val.toBool();22929 const field_is_comptime = field_is_comptime_val.toBool();
22958 const field_default_value: InternPool.Index = if (field_default_value_val.optionalValue(zcu)) |ptr_val| d: {22930 const field_default_value: InternPool.Index = if (field_default_value_val.optionalValue(zcu)) |ptr_val| d: {
22959 const ptr_ty = try pt.singleConstPtrType(field_type_val.toType());22931 const ptr_ty = try pt.singleConstPtrType(field_type_val.toType());
...@@ -22961,7 +22933,7 @@ fn reifyStruct(...@@ -22961,7 +22933,7 @@ fn reifyStruct(
22961 const val = try sema.pointerDeref(block, src, ptr_val, ptr_ty) orelse return sema.failWithNeededComptime(22933 const val = try sema.pointerDeref(block, src, ptr_val, ptr_ty) orelse return sema.failWithNeededComptime(
22962 block,22934 block,
22963 src,22935 src,
22964 .{ .needed_comptime_reason = "struct field default value must be comptime-known" },22936 .{ .simple = .struct_field_default_value },
22965 );22937 );
22966 // Resolve the value so that lazy values do not create distinct types.22938 // Resolve the value so that lazy values do not create distinct types.
22967 break :d (try sema.resolveLazyValue(val)).toIntern();22939 break :d (try sema.resolveLazyValue(val)).toIntern();
...@@ -23285,9 +23257,7 @@ fn zirIntFromFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro...@@ -23285,9 +23257,7 @@ fn zirIntFromFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
23285 const result_val = try sema.intFromFloat(block, operand_src, operand_val, operand_ty, dest_ty, .truncate);23257 const result_val = try sema.intFromFloat(block, operand_src, operand_val, operand_ty, dest_ty, .truncate);
23286 return Air.internedToRef(result_val.toIntern());23258 return Air.internedToRef(result_val.toIntern());
23287 } else if (dest_scalar_ty.zigTypeTag(zcu) == .comptime_int) {23259 } else if (dest_scalar_ty.zigTypeTag(zcu) == .comptime_int) {
23288 return sema.failWithNeededComptime(block, operand_src, .{23260 return sema.failWithNeededComptime(block, operand_src, .{ .simple = .casted_to_comptime_int });
23289 .needed_comptime_reason = "value being casted to 'comptime_int' must be comptime-known",
23290 });
23291 }23261 }
2329223262
23293 try sema.requireRuntimeBlock(block, src, operand_src);23263 try sema.requireRuntimeBlock(block, src, operand_src);
...@@ -23368,9 +23338,7 @@ fn zirFloatFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro...@@ -23368,9 +23338,7 @@ fn zirFloatFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
23368 const result_val = try operand_val.floatFromIntAdvanced(sema.arena, operand_ty, dest_ty, pt, .sema);23338 const result_val = try operand_val.floatFromIntAdvanced(sema.arena, operand_ty, dest_ty, pt, .sema);
23369 return Air.internedToRef(result_val.toIntern());23339 return Air.internedToRef(result_val.toIntern());
23370 } else if (dest_scalar_ty.zigTypeTag(zcu) == .comptime_float) {23340 } else if (dest_scalar_ty.zigTypeTag(zcu) == .comptime_float) {
23371 return sema.failWithNeededComptime(block, operand_src, .{23341 return sema.failWithNeededComptime(block, operand_src, .{ .simple = .casted_to_comptime_float });
23372 .needed_comptime_reason = "value being casted to 'comptime_float' must be comptime-known",
23373 });
23374 }23342 }
2337523343
23376 try sema.requireRuntimeBlock(block, src, operand_src);23344 try sema.requireRuntimeBlock(block, src, operand_src);
...@@ -24394,9 +24362,7 @@ fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u6...@@ -24394,9 +24362,7 @@ fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u6
24394 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;24362 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
2439524363
24396 const ty = try sema.resolveType(block, lhs_src, extra.lhs);24364 const ty = try sema.resolveType(block, lhs_src, extra.lhs);
24397 const field_name = try sema.resolveConstStringIntern(block, rhs_src, extra.rhs, .{24365 const field_name = try sema.resolveConstStringIntern(block, rhs_src, extra.rhs, .{ .simple = .field_name });
24398 .needed_comptime_reason = "name of field must be comptime-known",
24399 });
2440024366
24401 const pt = sema.pt;24367 const pt = sema.pt;
24402 const zcu = pt.zcu;24368 const zcu = pt.zcu;
...@@ -24850,31 +24816,21 @@ fn resolveExportOptions(...@@ -24850,31 +24816,21 @@ fn resolveExportOptions(
24850 const visibility_src = block.src(.{ .init_field_visibility = src.offset.node_offset_builtin_call_arg.builtin_call_node });24816 const visibility_src = block.src(.{ .init_field_visibility = src.offset.node_offset_builtin_call_arg.builtin_call_node });
2485124817
24852 const name_operand = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "name", .no_embedded_nulls), name_src);24818 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, .{24819 const name = try sema.toConstString(block, name_src, name_operand, .{ .simple = .export_options });
24854 .needed_comptime_reason = "name of exported value must be comptime-known",
24855 });
2485624820
24857 const linkage_operand = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "linkage", .no_embedded_nulls), linkage_src);24821 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, .{24822 const linkage_val = try sema.resolveConstDefinedValue(block, linkage_src, linkage_operand, .{ .simple = .export_options });
24859 .needed_comptime_reason = "linkage of exported value must be comptime-known",
24860 });
24861 const linkage = zcu.toEnum(std.builtin.GlobalLinkage, linkage_val);24823 const linkage = zcu.toEnum(std.builtin.GlobalLinkage, linkage_val);
2486224824
24863 const section_operand = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "section", .no_embedded_nulls), section_src);24825 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, .{24826 const section_opt_val = try sema.resolveConstDefinedValue(block, section_src, section_operand, .{ .simple = .export_options });
24865 .needed_comptime_reason = "linksection of exported value must be comptime-known",
24866 });
24867 const section = if (section_opt_val.optionalValue(zcu)) |section_val|24827 const section = if (section_opt_val.optionalValue(zcu)) |section_val|
24868 try sema.toConstString(block, section_src, Air.internedToRef(section_val.toIntern()), .{24828 try sema.toConstString(block, section_src, Air.internedToRef(section_val.toIntern()), .{ .simple = .export_options })
24869 .needed_comptime_reason = "linksection of exported value must be comptime-known",
24870 })
24871 else24829 else
24872 null;24830 null;
2487324831
24874 const visibility_operand = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "visibility", .no_embedded_nulls), visibility_src);24832 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, .{24833 const visibility_val = try sema.resolveConstDefinedValue(block, visibility_src, visibility_operand, .{ .simple = .export_options });
24876 .needed_comptime_reason = "visibility of exported value must be comptime-known",
24877 });
24878 const visibility = zcu.toEnum(std.builtin.SymbolVisibility, visibility_val);24834 const visibility = zcu.toEnum(std.builtin.SymbolVisibility, visibility_val);
2487924835
24880 if (name.len < 1) {24836 if (name.len < 1) {
...@@ -24901,7 +24857,7 @@ fn resolveBuiltinEnum(...@@ -24901,7 +24857,7 @@ fn resolveBuiltinEnum(
24901 src: LazySrcLoc,24857 src: LazySrcLoc,
24902 zir_ref: Zir.Inst.Ref,24858 zir_ref: Zir.Inst.Ref,
24903 comptime name: []const u8,24859 comptime name: []const u8,
24904 reason: NeededComptimeReason,24860 reason: ComptimeReason,
24905) CompileError!@field(std.builtin, name) {24861) CompileError!@field(std.builtin, name) {
24906 const pt = sema.pt;24862 const pt = sema.pt;
24907 const ty = try sema.getBuiltinType(name);24863 const ty = try sema.getBuiltinType(name);
...@@ -24916,7 +24872,7 @@ fn resolveAtomicOrder(...@@ -24916,7 +24872,7 @@ fn resolveAtomicOrder(
24916 block: *Block,24872 block: *Block,
24917 src: LazySrcLoc,24873 src: LazySrcLoc,
24918 zir_ref: Zir.Inst.Ref,24874 zir_ref: Zir.Inst.Ref,
24919 reason: NeededComptimeReason,24875 reason: ComptimeReason,
24920) CompileError!std.builtin.AtomicOrder {24876) CompileError!std.builtin.AtomicOrder {
24921 return sema.resolveBuiltinEnum(block, src, zir_ref, "AtomicOrder", reason);24877 return sema.resolveBuiltinEnum(block, src, zir_ref, "AtomicOrder", reason);
24922}24878}
...@@ -24927,9 +24883,7 @@ fn resolveAtomicRmwOp(...@@ -24927,9 +24883,7 @@ fn resolveAtomicRmwOp(
24927 src: LazySrcLoc,24883 src: LazySrcLoc,
24928 zir_ref: Zir.Inst.Ref,24884 zir_ref: Zir.Inst.Ref,
24929) CompileError!std.builtin.AtomicRmwOp {24885) CompileError!std.builtin.AtomicRmwOp {
24930 return sema.resolveBuiltinEnum(block, src, zir_ref, "AtomicRmwOp", .{24886 return sema.resolveBuiltinEnum(block, src, zir_ref, "AtomicRmwOp", .{ .simple = .operand_atomicRmw_operation });
24931 .needed_comptime_reason = "@atomicRmW operation must be comptime-known",
24932 });
24933}24887}
2493424888
24935fn zirCmpxchg(24889fn zirCmpxchg(
...@@ -24967,12 +24921,8 @@ fn zirCmpxchg(...@@ -24967,12 +24921,8 @@ fn zirCmpxchg(
24967 const uncasted_ptr = try sema.resolveInst(extra.ptr);24921 const uncasted_ptr = try sema.resolveInst(extra.ptr);
24968 const ptr = try sema.checkAtomicPtrOperand(block, elem_ty, elem_ty_src, uncasted_ptr, ptr_src, false);24922 const ptr = try sema.checkAtomicPtrOperand(block, elem_ty, elem_ty_src, uncasted_ptr, ptr_src, false);
24969 const new_value = try sema.coerce(block, elem_ty, try sema.resolveInst(extra.new_value), new_value_src);24923 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, .{24924 const success_order = try sema.resolveAtomicOrder(block, success_order_src, extra.success_order, .{ .simple = .atomic_order });
24971 .needed_comptime_reason = "atomic order of cmpxchg success must be comptime-known",24925 const failure_order = try sema.resolveAtomicOrder(block, failure_order_src, extra.failure_order, .{ .simple = .atomic_order });
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 });
2497624926
24977 if (@intFromEnum(success_order) < @intFromEnum(std.builtin.AtomicOrder.monotonic)) {24927 if (@intFromEnum(success_order) < @intFromEnum(std.builtin.AtomicOrder.monotonic)) {
24978 return sema.fail(block, success_order_src, "success atomic ordering must be monotonic or stricter", .{});24928 return sema.fail(block, success_order_src, "success atomic ordering must be monotonic or stricter", .{});
...@@ -25113,9 +25063,7 @@ fn zirReduce(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air....@@ -25113,9 +25063,7 @@ fn zirReduce(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
25113 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;25063 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
25114 const op_src = block.builtinCallArgSrc(inst_data.src_node, 0);25064 const op_src = block.builtinCallArgSrc(inst_data.src_node, 0);
25115 const operand_src = block.builtinCallArgSrc(inst_data.src_node, 1);25065 const operand_src = block.builtinCallArgSrc(inst_data.src_node, 1);
25116 const operation = try sema.resolveBuiltinEnum(block, op_src, extra.lhs, "ReduceOp", .{25066 const operation = try sema.resolveBuiltinEnum(block, op_src, extra.lhs, "ReduceOp", .{ .simple = .operand_reduce_operation });
25117 .needed_comptime_reason = "@reduce operation must be comptime-known",
25118 });
25119 const operand = try sema.resolveInst(extra.rhs);25067 const operand = try sema.resolveInst(extra.rhs);
25120 const operand_ty = sema.typeOf(operand);25068 const operand_ty = sema.typeOf(operand);
25121 const pt = sema.pt;25069 const pt = sema.pt;
...@@ -25204,9 +25152,7 @@ fn zirShuffle(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -25204,9 +25152,7 @@ fn zirShuffle(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
25204 .child = .i32_type,25152 .child = .i32_type,
25205 });25153 });
25206 mask = try sema.coerce(block, mask_ty, mask, mask_src);25154 mask = try sema.coerce(block, mask_ty, mask, mask_src);
25207 const mask_val = try sema.resolveConstValue(block, mask_src, mask, .{25155 const mask_val = try sema.resolveConstValue(block, mask_src, mask, .{ .simple = .operand_shuffle_mask });
25208 .needed_comptime_reason = "shuffle mask must be comptime-known",
25209 });
25210 return sema.analyzeShuffle(block, inst_data.src_node, elem_ty, a, b, mask_val, @intCast(mask_len));25156 return sema.analyzeShuffle(block, inst_data.src_node, elem_ty, a, b, mask_val, @intCast(mask_len));
25211}25157}
2521225158
...@@ -25474,9 +25420,7 @@ fn zirAtomicLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!...@@ -25474,9 +25420,7 @@ fn zirAtomicLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
25474 const elem_ty = try sema.resolveType(block, elem_ty_src, extra.elem_type);25420 const elem_ty = try sema.resolveType(block, elem_ty_src, extra.elem_type);
25475 const uncasted_ptr = try sema.resolveInst(extra.ptr);25421 const uncasted_ptr = try sema.resolveInst(extra.ptr);
25476 const ptr = try sema.checkAtomicPtrOperand(block, elem_ty, elem_ty_src, uncasted_ptr, ptr_src, true);25422 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, .{25423 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, .{ .simple = .atomic_order });
25478 .needed_comptime_reason = "atomic order of @atomicLoad must be comptime-known",
25479 });
2548025424
25481 switch (order) {25425 switch (order) {
25482 .release, .acq_rel => {25426 .release, .acq_rel => {
...@@ -25542,9 +25486,7 @@ fn zirAtomicRmw(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -25542,9 +25486,7 @@ fn zirAtomicRmw(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
25542 },25486 },
25543 else => {},25487 else => {},
25544 }25488 }
25545 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, .{25489 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, .{ .simple = .atomic_order });
25546 .needed_comptime_reason = "atomic order of @atomicRmW must be comptime-known",
25547 });
2554825490
25549 if (order == .unordered) {25491 if (order == .unordered) {
25550 return sema.fail(block, order_src, "@atomicRmw atomic ordering must not be unordered", .{});25492 return sema.fail(block, order_src, "@atomicRmw atomic ordering must not be unordered", .{});
...@@ -25611,9 +25553,7 @@ fn zirAtomicStore(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError...@@ -25611,9 +25553,7 @@ fn zirAtomicStore(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
25611 const elem_ty = sema.typeOf(operand);25553 const elem_ty = sema.typeOf(operand);
25612 const uncasted_ptr = try sema.resolveInst(extra.ptr);25554 const uncasted_ptr = try sema.resolveInst(extra.ptr);
25613 const ptr = try sema.checkAtomicPtrOperand(block, elem_ty, elem_ty_src, uncasted_ptr, ptr_src, false);25555 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, .{25556 const order = try sema.resolveAtomicOrder(block, order_src, extra.ordering, .{ .simple = .atomic_order });
25615 .needed_comptime_reason = "atomic order of @atomicStore must be comptime-known",
25616 });
2561725557
25618 const air_tag: Air.Inst.Tag = switch (order) {25558 const air_tag: Air.Inst.Tag = switch (order) {
25619 .acquire, .acq_rel => {25559 .acquire, .acq_rel => {
...@@ -25716,14 +25656,12 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError...@@ -25716,14 +25656,12 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
25716 const modifier_ty = try sema.getBuiltinType("CallModifier");25656 const modifier_ty = try sema.getBuiltinType("CallModifier");
25717 const air_ref = try sema.resolveInst(extra.modifier);25657 const air_ref = try sema.resolveInst(extra.modifier);
25718 const modifier_ref = try sema.coerce(block, modifier_ty, air_ref, modifier_src);25658 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, .{25659 const modifier_val = try sema.resolveConstDefinedValue(block, modifier_src, modifier_ref, .{ .simple = .call_modifier });
25720 .needed_comptime_reason = "call modifier must be comptime-known",
25721 });
25722 var modifier = zcu.toEnum(std.builtin.CallModifier, modifier_val);25660 var modifier = zcu.toEnum(std.builtin.CallModifier, modifier_val);
25723 switch (modifier) {25661 switch (modifier) {
25724 // These can be upgraded to comptime or nosuspend calls.25662 // These can be upgraded to comptime or nosuspend calls.
25725 .auto, .never_tail, .no_async => {25663 .auto, .never_tail, .no_async => {
25726 if (block.is_comptime) {25664 if (block.isComptime()) {
25727 if (modifier == .never_tail) {25665 if (modifier == .never_tail) {
25728 return sema.fail(block, modifier_src, "unable to perform 'never_tail' call at compile-time", .{});25666 return sema.fail(block, modifier_src, "unable to perform 'never_tail' call at compile-time", .{});
25729 }25667 }
...@@ -25738,12 +25676,12 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError...@@ -25738,12 +25676,12 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
25738 return sema.fail(block, func_src, "modifier '{s}' requires a comptime-known function", .{@tagName(modifier)});25676 return sema.fail(block, func_src, "modifier '{s}' requires a comptime-known function", .{@tagName(modifier)});
25739 };25677 };
2574025678
25741 if (block.is_comptime) {25679 if (block.isComptime()) {
25742 modifier = .compile_time;25680 modifier = .compile_time;
25743 }25681 }
25744 },25682 },
25745 .always_tail => {25683 .always_tail => {
25746 if (block.is_comptime) {25684 if (block.isComptime()) {
25747 modifier = .compile_time;25685 modifier = .compile_time;
25748 }25686 }
25749 },25687 },
...@@ -25751,12 +25689,12 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError...@@ -25751,12 +25689,12 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
25751 if (extra.flags.is_nosuspend) {25689 if (extra.flags.is_nosuspend) {
25752 return sema.fail(block, modifier_src, "modifier 'async_kw' cannot be used inside nosuspend block", .{});25690 return sema.fail(block, modifier_src, "modifier 'async_kw' cannot be used inside nosuspend block", .{});
25753 }25691 }
25754 if (block.is_comptime) {25692 if (block.isComptime()) {
25755 return sema.fail(block, modifier_src, "modifier 'async_kw' cannot be used in combination with comptime function call", .{});25693 return sema.fail(block, modifier_src, "modifier 'async_kw' cannot be used in combination with comptime function call", .{});
25756 }25694 }
25757 },25695 },
25758 .never_inline => {25696 .never_inline => {
25759 if (block.is_comptime) {25697 if (block.isComptime()) {
25760 return sema.fail(block, modifier_src, "unable to perform 'never_inline' call at compile-time", .{});25698 return sema.fail(block, modifier_src, "unable to perform 'never_inline' call at compile-time", .{});
25761 }25699 }
25762 },25700 },
...@@ -25820,9 +25758,7 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Ins...@@ -25820,9 +25758,7 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Ins
25820 }25758 }
25821 try parent_ty.resolveLayout(pt);25759 try parent_ty.resolveLayout(pt);
2582225760
25823 const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, .{25761 const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, .{ .simple = .field_name });
25824 .needed_comptime_reason = "field name must be comptime-known",
25825 });
25826 const field_index = switch (parent_ty.zigTypeTag(zcu)) {25762 const field_index = switch (parent_ty.zigTypeTag(zcu)) {
25827 .@"struct" => blk: {25763 .@"struct" => blk: {
25828 if (parent_ty.isTuple(zcu)) {25764 if (parent_ty.isTuple(zcu)) {
...@@ -26680,9 +26616,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -26680,9 +26616,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
26680 extra_index += body.len;26616 extra_index += body.len;
2668126617
26682 const cc_ty = try sema.getBuiltinType("CallingConvention");26618 const cc_ty = try sema.getBuiltinType("CallingConvention");
26683 const val = try sema.resolveGenericBody(block, cc_src, body, inst, cc_ty, .{26619 const val = try sema.resolveGenericBody(block, cc_src, body, inst, cc_ty, .{ .simple = .@"callconv" });
26684 .needed_comptime_reason = "calling convention must be comptime-known",
26685 });
26686 break :blk try sema.analyzeValueAsCallconv(block, cc_src, val);26620 break :blk try sema.analyzeValueAsCallconv(block, cc_src, val);
26687 } else if (extra.data.bits.has_cc_ref) blk: {26621 } else if (extra.data.bits.has_cc_ref) blk: {
26688 const cc_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);26622 const cc_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);
...@@ -26690,9 +26624,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -26690,9 +26624,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
26690 const cc_ty = try sema.getBuiltinType("CallingConvention");26624 const cc_ty = try sema.getBuiltinType("CallingConvention");
26691 const uncoerced_cc = try sema.resolveInst(cc_ref);26625 const uncoerced_cc = try sema.resolveInst(cc_ref);
26692 const coerced_cc = try sema.coerce(block, cc_ty, uncoerced_cc, cc_src);26626 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, .{26627 const cc_val = try sema.resolveConstDefinedValue(block, cc_src, coerced_cc, .{ .simple = .@"callconv" });
26694 .needed_comptime_reason = "calling convention must be comptime-known",
26695 });
26696 break :blk try sema.analyzeValueAsCallconv(block, cc_src, cc_val);26628 break :blk try sema.analyzeValueAsCallconv(block, cc_src, cc_val);
26697 } else cc: {26629 } else cc: {
26698 if (has_body) {26630 if (has_body) {
...@@ -26730,9 +26662,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -26730,9 +26662,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
26730 const body = sema.code.bodySlice(extra_index, body_len);26662 const body = sema.code.bodySlice(extra_index, body_len);
26731 extra_index += body.len;26663 extra_index += body.len;
2673226664
26733 const val = try sema.resolveGenericBody(block, ret_src, body, inst, Type.type, .{26665 const val = try sema.resolveGenericBody(block, ret_src, body, inst, Type.type, .{ .simple = .function_ret_ty });
26734 .needed_comptime_reason = "return type must be comptime-known",
26735 });
26736 const ty = val.toType();26666 const ty = val.toType();
26737 break :blk ty;26667 break :blk ty;
26738 } else if (extra.data.bits.has_ret_ty_ref) blk: {26668 } else if (extra.data.bits.has_ret_ty_ref) blk: {
...@@ -26742,9 +26672,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -26742,9 +26672,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
26742 error.GenericPoison => break :blk Type.generic_poison,26672 error.GenericPoison => break :blk Type.generic_poison,
26743 else => |e| return e,26673 else => |e| return e,
26744 };26674 };
26745 const ret_ty_val = sema.resolveConstDefinedValue(block, ret_src, ret_ty_air_ref, .{26675 const ret_ty_val = sema.resolveConstDefinedValue(block, ret_src, ret_ty_air_ref, .{ .simple = .function_ret_ty }) catch |err| switch (err) {
26746 .needed_comptime_reason = "return type must be comptime-known",
26747 }) catch |err| switch (err) {
26748 error.GenericPoison => break :blk Type.generic_poison,26676 error.GenericPoison => break :blk Type.generic_poison,
26749 else => |e| return e,26677 else => |e| return e,
26750 };26678 };
...@@ -26790,9 +26718,7 @@ fn zirCUndef(...@@ -26790,9 +26718,7 @@ fn zirCUndef(
26790 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;26718 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
26791 const src = block.builtinCallArgSrc(extra.node, 0);26719 const src = block.builtinCallArgSrc(extra.node, 0);
2679226720
26793 const name = try sema.resolveConstString(block, src, extra.operand, .{26721 const name = try sema.resolveConstString(block, src, extra.operand, .{ .simple = .operand_cUndef_macro_name });
26794 .needed_comptime_reason = "name of macro being undefined must be comptime-known",
26795 });
26796 try block.c_import_buf.?.writer().print("#undef {s}\n", .{name});26722 try block.c_import_buf.?.writer().print("#undef {s}\n", .{name});
26797 return .void_value;26723 return .void_value;
26798}26724}
...@@ -26805,9 +26731,7 @@ fn zirCInclude(...@@ -26805,9 +26731,7 @@ fn zirCInclude(
26805 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;26731 const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
26806 const src = block.builtinCallArgSrc(extra.node, 0);26732 const src = block.builtinCallArgSrc(extra.node, 0);
2680726733
26808 const name = try sema.resolveConstString(block, src, extra.operand, .{26734 const name = try sema.resolveConstString(block, src, extra.operand, .{ .simple = .operand_cInclude_file_name });
26809 .needed_comptime_reason = "path being included must be comptime-known",
26810 });
26811 try block.c_import_buf.?.writer().print("#include <{s}>\n", .{name});26735 try block.c_import_buf.?.writer().print("#include <{s}>\n", .{name});
26812 return .void_value;26736 return .void_value;
26813}26737}
...@@ -26823,14 +26747,10 @@ fn zirCDefine(...@@ -26823,14 +26747,10 @@ fn zirCDefine(
26823 const name_src = block.builtinCallArgSrc(extra.node, 0);26747 const name_src = block.builtinCallArgSrc(extra.node, 0);
26824 const val_src = block.builtinCallArgSrc(extra.node, 1);26748 const val_src = block.builtinCallArgSrc(extra.node, 1);
2682526749
26826 const name = try sema.resolveConstString(block, name_src, extra.lhs, .{26750 const name = try sema.resolveConstString(block, name_src, extra.lhs, .{ .simple = .operand_cDefine_macro_name });
26827 .needed_comptime_reason = "name of macro being undefined must be comptime-known",
26828 });
26829 const rhs = try sema.resolveInst(extra.rhs);26751 const rhs = try sema.resolveInst(extra.rhs);
26830 if (sema.typeOf(rhs).zigTypeTag(zcu) != .void) {26752 if (sema.typeOf(rhs).zigTypeTag(zcu) != .void) {
26831 const value = try sema.resolveConstString(block, val_src, extra.rhs, .{26753 const value = try sema.resolveConstString(block, val_src, extra.rhs, .{ .simple = .operand_cDefine_macro_value });
26832 .needed_comptime_reason = "value of macro being undefined must be comptime-known",
26833 });
26834 try block.c_import_buf.?.writer().print("#define {s} {s}\n", .{ name, value });26754 try block.c_import_buf.?.writer().print("#define {s} {s}\n", .{ name, value });
26835 } else {26755 } else {
26836 try block.c_import_buf.?.writer().print("#define {s}\n", .{name});26756 try block.c_import_buf.?.writer().print("#define {s}\n", .{name});
...@@ -26851,9 +26771,7 @@ fn zirWasmMemorySize(...@@ -26851,9 +26771,7 @@ fn zirWasmMemorySize(
26851 return sema.fail(block, builtin_src, "builtin @wasmMemorySize is available when targeting WebAssembly; targeted CPU architecture is {s}", .{@tagName(target.cpu.arch)});26771 return sema.fail(block, builtin_src, "builtin @wasmMemorySize is available when targeting WebAssembly; targeted CPU architecture is {s}", .{@tagName(target.cpu.arch)});
26852 }26772 }
2685326773
26854 const index: u32 = @intCast(try sema.resolveInt(block, index_src, extra.operand, Type.u32, .{26774 const index: u32 = @intCast(try sema.resolveInt(block, index_src, extra.operand, Type.u32, .{ .simple = .wasm_memory_index }));
26855 .needed_comptime_reason = "wasm memory size index must be comptime-known",
26856 }));
26857 try sema.requireRuntimeBlock(block, builtin_src, null);26775 try sema.requireRuntimeBlock(block, builtin_src, null);
26858 return block.addInst(.{26776 return block.addInst(.{
26859 .tag = .wasm_memory_size,26777 .tag = .wasm_memory_size,
...@@ -26878,9 +26796,7 @@ fn zirWasmMemoryGrow(...@@ -26878,9 +26796,7 @@ fn zirWasmMemoryGrow(
26878 return sema.fail(block, builtin_src, "builtin @wasmMemoryGrow is available when targeting WebAssembly; targeted CPU architecture is {s}", .{@tagName(target.cpu.arch)});26796 return sema.fail(block, builtin_src, "builtin @wasmMemoryGrow is available when targeting WebAssembly; targeted CPU architecture is {s}", .{@tagName(target.cpu.arch)});
26879 }26797 }
2688026798
26881 const index: u32 = @intCast(try sema.resolveInt(block, index_src, extra.lhs, Type.u32, .{26799 const index: u32 = @intCast(try sema.resolveInt(block, index_src, extra.lhs, Type.u32, .{ .simple = .wasm_memory_index }));
26882 .needed_comptime_reason = "wasm memory size index must be comptime-known",
26883 }));
26884 const delta = try sema.coerce(block, Type.usize, try sema.resolveInst(extra.rhs), delta_src);26800 const delta = try sema.coerce(block, Type.usize, try sema.resolveInst(extra.rhs), delta_src);
2688526801
26886 try sema.requireRuntimeBlock(block, builtin_src, null);26802 try sema.requireRuntimeBlock(block, builtin_src, null);
...@@ -26911,19 +26827,13 @@ fn resolvePrefetchOptions(...@@ -26911,19 +26827,13 @@ fn resolvePrefetchOptions(
26911 const cache_src = block.src(.{ .init_field_cache = src.offset.node_offset_builtin_call_arg.builtin_call_node });26827 const cache_src = block.src(.{ .init_field_cache = src.offset.node_offset_builtin_call_arg.builtin_call_node });
2691226828
26913 const rw = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "rw", .no_embedded_nulls), rw_src);26829 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, .{26830 const rw_val = try sema.resolveConstDefinedValue(block, rw_src, rw, .{ .simple = .prefetch_options });
26915 .needed_comptime_reason = "prefetch read/write must be comptime-known",
26916 });
2691726831
26918 const locality = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "locality", .no_embedded_nulls), locality_src);26832 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, .{26833 const locality_val = try sema.resolveConstDefinedValue(block, locality_src, locality, .{ .simple = .prefetch_options });
26920 .needed_comptime_reason = "prefetch locality must be comptime-known",
26921 });
2692226834
26923 const cache = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "cache", .no_embedded_nulls), cache_src);26835 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, .{26836 const cache_val = try sema.resolveConstDefinedValue(block, cache_src, cache, .{ .simple = .prefetch_options });
26925 .needed_comptime_reason = "prefetch cache must be comptime-known",
26926 });
2692726837
26928 return std.builtin.PrefetchOptions{26838 return std.builtin.PrefetchOptions{
26929 .rw = zcu.toEnum(std.builtin.PrefetchOptions.Rw, rw_val),26839 .rw = zcu.toEnum(std.builtin.PrefetchOptions.Rw, rw_val),
...@@ -26945,7 +26855,7 @@ fn zirPrefetch(...@@ -26945,7 +26855,7 @@ fn zirPrefetch(
2694526855
26946 const options = try sema.resolvePrefetchOptions(block, opts_src, extra.rhs);26856 const options = try sema.resolvePrefetchOptions(block, opts_src, extra.rhs);
2694726857
26948 if (!block.is_comptime) {26858 if (!block.isComptime()) {
26949 _ = try block.addInst(.{26859 _ = try block.addInst(.{
26950 .tag = .prefetch,26860 .tag = .prefetch,
26951 .data = .{ .prefetch = .{26861 .data = .{ .prefetch = .{
...@@ -26987,30 +26897,20 @@ fn resolveExternOptions(...@@ -26987,30 +26897,20 @@ fn resolveExternOptions(
26987 const dll_import_src = block.src(.{ .init_field_dll_import = src.offset.node_offset_builtin_call_arg.builtin_call_node });26897 const dll_import_src = block.src(.{ .init_field_dll_import = src.offset.node_offset_builtin_call_arg.builtin_call_node });
2698826898
26989 const name_ref = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "name", .no_embedded_nulls), name_src);26899 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, .{26900 const name = try sema.toConstString(block, name_src, name_ref, .{ .simple = .extern_options });
26991 .needed_comptime_reason = "name of the extern symbol must be comptime-known",
26992 });
2699326901
26994 const library_name_inst = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "library_name", .no_embedded_nulls), library_src);26902 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, .{26903 const library_name_val = try sema.resolveConstDefinedValue(block, library_src, library_name_inst, .{ .simple = .extern_options });
26996 .needed_comptime_reason = "library in which extern symbol is must be comptime-known",
26997 });
2699826904
26999 const linkage_ref = try sema.fieldVal(block, src, options, try ip.getOrPutString(gpa, pt.tid, "linkage", .no_embedded_nulls), linkage_src);26905 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, .{26906 const linkage_val = try sema.resolveConstDefinedValue(block, linkage_src, linkage_ref, .{ .simple = .extern_options });
27001 .needed_comptime_reason = "linkage of the extern symbol must be comptime-known",
27002 });
27003 const linkage = zcu.toEnum(std.builtin.GlobalLinkage, linkage_val);26907 const linkage = zcu.toEnum(std.builtin.GlobalLinkage, linkage_val);
2700426908
27005 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);26909 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, .{26910 const is_thread_local_val = try sema.resolveConstDefinedValue(block, thread_local_src, is_thread_local, .{ .simple = .extern_options });
27007 .needed_comptime_reason = "threadlocality of the extern symbol must be comptime-known",
27008 });
2700926911
27010 const library_name = if (library_name_val.optionalValue(zcu)) |library_name_payload| library_name: {26912 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()), .{26913 const library_name = try sema.toConstString(block, library_src, Air.internedToRef(library_name_payload.toIntern()), .{ .simple = .extern_options });
27012 .needed_comptime_reason = "library in which extern symbol is must be comptime-known",
27013 });
27014 if (library_name.len == 0) {26914 if (library_name.len == 0) {
27015 return sema.fail(block, library_src, "library name cannot be empty", .{});26915 return sema.fail(block, library_src, "library name cannot be empty", .{});
27016 }26916 }
...@@ -27019,9 +26919,7 @@ fn resolveExternOptions(...@@ -27019,9 +26919,7 @@ fn resolveExternOptions(
27019 } else null;26919 } else null;
2702026920
27021 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);26921 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, .{26922 const is_dll_import_val = try sema.resolveConstDefinedValue(block, dll_import_src, is_dll_import_ref, .{ .simple = .extern_options });
27023 .needed_comptime_reason = "it must be comptime-known if the symbol is imported from a dll",
27024 });
2702526923
27026 if (name.len == 0) {26924 if (name.len == 0) {
27027 return sema.fail(block, name_src, "extern symbol name cannot be empty", .{});26925 return sema.fail(block, name_src, "extern symbol name cannot be empty", .{});
...@@ -27134,9 +27032,7 @@ fn zirWorkItem(...@@ -27134,9 +27032,7 @@ fn zirWorkItem(
27134 },27032 },
27135 }27033 }
2713627034
27137 const dimension: u32 = @intCast(try sema.resolveInt(block, dimension_src, extra.operand, Type.u32, .{27035 const dimension: u32 = @intCast(try sema.resolveInt(block, dimension_src, extra.operand, Type.u32, .{ .simple = .work_group_dim_index }));
27138 .needed_comptime_reason = "dimension must be comptime-known",
27139 }));
27140 try sema.requireRuntimeBlock(block, builtin_src, null);27036 try sema.requireRuntimeBlock(block, builtin_src, null);
2714127037
27142 return block.addInst(.{27038 return block.addInst(.{
...@@ -27158,7 +27054,7 @@ fn zirInComptime(...@@ -27158,7 +27054,7 @@ fn zirInComptime(
27158 block: *Block,27054 block: *Block,
27159) CompileError!Air.Inst.Ref {27055) CompileError!Air.Inst.Ref {
27160 _ = sema;27056 _ = sema;
27161 return if (block.is_comptime) .bool_true else .bool_false;27057 return if (block.isComptime()) .bool_true else .bool_false;
27162}27058}
2716327059
27164fn zirBuiltinValue(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {27060fn zirBuiltinValue(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
...@@ -27249,9 +27145,7 @@ fn zirBranchHint(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat...@@ -27249,9 +27145,7 @@ fn zirBranchHint(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
2724927145
27250 const hint_ty = try sema.getBuiltinType("BranchHint");27146 const hint_ty = try sema.getBuiltinType("BranchHint");
27251 const coerced_hint = try sema.coerce(block, hint_ty, uncoerced_hint, operand_src);27147 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, .{27148 const hint_val = try sema.resolveConstDefinedValue(block, operand_src, coerced_hint, .{ .simple = .operand_branchHint });
27253 .needed_comptime_reason = "operand to '@branchHint' must be comptime-known",
27254 });
2725527149
27256 // We only apply the first hint in a branch.27150 // We only apply the first hint in a branch.
27257 // This allows user-provided hints to override implicit cold hints.27151 // This allows user-provided hints to override implicit cold hints.
...@@ -27261,20 +27155,20 @@ fn zirBranchHint(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat...@@ -27261,20 +27155,20 @@ fn zirBranchHint(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
27261}27155}
2726227156
27263fn requireRuntimeBlock(sema: *Sema, block: *Block, src: LazySrcLoc, runtime_src: ?LazySrcLoc) !void {27157fn requireRuntimeBlock(sema: *Sema, block: *Block, src: LazySrcLoc, runtime_src: ?LazySrcLoc) !void {
27264 if (block.is_comptime) {27158 if (block.isComptime()) {
27265 const msg = msg: {27159 const msg, const fail_block = msg: {
27266 const msg = try sema.errMsg(src, "unable to evaluate comptime expression", .{});27160 const msg = try sema.errMsg(src, "unable to evaluate comptime expression", .{});
27267 errdefer msg.destroy(sema.gpa);27161 errdefer msg.destroy(sema.gpa);
2726827162
27269 if (runtime_src) |some| {27163 if (runtime_src) |some| {
27270 try sema.errNote(some, msg, "operation is runtime due to this operand", .{});27164 try sema.errNote(some, msg, "operation is runtime due to this operand", .{});
27271 }27165 }
27272 if (block.comptime_reason) |some| {27166
27273 try some.explain(sema, msg);27167 const fail_block = try block.explainWhyBlockIsComptime(msg);
27274 }27168
27275 break :msg msg;27169 break :msg .{ msg, fail_block };
27276 };27170 };
27277 return sema.failWithOwnedErrorMsg(block, msg);27171 return sema.failWithOwnedErrorMsg(fail_block, msg);
27278 }27172 }
27279}27173}
2728027174
...@@ -27759,7 +27653,7 @@ fn addSafetyCheck(...@@ -27759,7 +27653,7 @@ fn addSafetyCheck(
27759 panic_id: Zcu.PanicId,27653 panic_id: Zcu.PanicId,
27760) !void {27654) !void {
27761 const gpa = sema.gpa;27655 const gpa = sema.gpa;
27762 assert(!parent_block.is_comptime);27656 assert(!parent_block.isComptime());
2776327657
27764 var fail_block: Block = .{27658 var fail_block: Block = .{
27765 .parent = parent_block,27659 .parent = parent_block,
...@@ -27767,7 +27661,7 @@ fn addSafetyCheck(...@@ -27767,7 +27661,7 @@ fn addSafetyCheck(
27767 .namespace = parent_block.namespace,27661 .namespace = parent_block.namespace,
27768 .instructions = .{},27662 .instructions = .{},
27769 .inlining = parent_block.inlining,27663 .inlining = parent_block.inlining,
27770 .is_comptime = false,27664 .comptime_reason = null,
27771 .src_base_inst = parent_block.src_base_inst,27665 .src_base_inst = parent_block.src_base_inst,
27772 .type_name_ctx = parent_block.type_name_ctx,27666 .type_name_ctx = parent_block.type_name_ctx,
27773 };27667 };
...@@ -27874,7 +27768,7 @@ fn addSafetyCheckUnwrapError(...@@ -27874,7 +27768,7 @@ fn addSafetyCheckUnwrapError(
27874 unwrap_err_tag: Air.Inst.Tag,27768 unwrap_err_tag: Air.Inst.Tag,
27875 is_non_err_tag: Air.Inst.Tag,27769 is_non_err_tag: Air.Inst.Tag,
27876) !void {27770) !void {
27877 assert(!parent_block.is_comptime);27771 assert(!parent_block.isComptime());
27878 const ok = try parent_block.addUnOp(is_non_err_tag, operand);27772 const ok = try parent_block.addUnOp(is_non_err_tag, operand);
27879 const gpa = sema.gpa;27773 const gpa = sema.gpa;
2788027774
...@@ -27884,7 +27778,7 @@ fn addSafetyCheckUnwrapError(...@@ -27884,7 +27778,7 @@ fn addSafetyCheckUnwrapError(
27884 .namespace = parent_block.namespace,27778 .namespace = parent_block.namespace,
27885 .instructions = .{},27779 .instructions = .{},
27886 .inlining = parent_block.inlining,27780 .inlining = parent_block.inlining,
27887 .is_comptime = false,27781 .comptime_reason = null,
27888 .src_base_inst = parent_block.src_base_inst,27782 .src_base_inst = parent_block.src_base_inst,
27889 .type_name_ctx = parent_block.type_name_ctx,27783 .type_name_ctx = parent_block.type_name_ctx,
27890 };27784 };
...@@ -27918,7 +27812,7 @@ fn addSafetyCheckIndexOob(...@@ -27918,7 +27812,7 @@ fn addSafetyCheckIndexOob(
27918 len: Air.Inst.Ref,27812 len: Air.Inst.Ref,
27919 cmp_op: Air.Inst.Tag,27813 cmp_op: Air.Inst.Tag,
27920) !void {27814) !void {
27921 assert(!parent_block.is_comptime);27815 assert(!parent_block.isComptime());
27922 const ok = try parent_block.addBinOp(cmp_op, index, len);27816 const ok = try parent_block.addBinOp(cmp_op, index, len);
27923 return addSafetyCheckCall(sema, parent_block, src, ok, "outOfBounds", &.{ index, len });27817 return addSafetyCheckCall(sema, parent_block, src, ok, "outOfBounds", &.{ index, len });
27924}27818}
...@@ -27930,7 +27824,7 @@ fn addSafetyCheckInactiveUnionField(...@@ -27930,7 +27824,7 @@ fn addSafetyCheckInactiveUnionField(
27930 active_tag: Air.Inst.Ref,27824 active_tag: Air.Inst.Ref,
27931 wanted_tag: Air.Inst.Ref,27825 wanted_tag: Air.Inst.Ref,
27932) !void {27826) !void {
27933 assert(!parent_block.is_comptime);27827 assert(!parent_block.isComptime());
27934 const ok = try parent_block.addBinOp(.cmp_eq, active_tag, wanted_tag);27828 const ok = try parent_block.addBinOp(.cmp_eq, active_tag, wanted_tag);
27935 return addSafetyCheckCall(sema, parent_block, src, ok, "inactiveUnionField", &.{ active_tag, wanted_tag });27829 return addSafetyCheckCall(sema, parent_block, src, ok, "inactiveUnionField", &.{ active_tag, wanted_tag });
27936}27830}
...@@ -27944,7 +27838,7 @@ fn addSafetyCheckSentinelMismatch(...@@ -27944,7 +27838,7 @@ fn addSafetyCheckSentinelMismatch(
27944 ptr: Air.Inst.Ref,27838 ptr: Air.Inst.Ref,
27945 sentinel_index: Air.Inst.Ref,27839 sentinel_index: Air.Inst.Ref,
27946) !void {27840) !void {
27947 assert(!parent_block.is_comptime);27841 assert(!parent_block.isComptime());
27948 const pt = sema.pt;27842 const pt = sema.pt;
27949 const zcu = pt.zcu;27843 const zcu = pt.zcu;
27950 const expected_sentinel_val = maybe_sentinel orelse return;27844 const expected_sentinel_val = maybe_sentinel orelse return;
...@@ -27986,7 +27880,7 @@ fn addSafetyCheckCall(...@@ -27986,7 +27880,7 @@ fn addSafetyCheckCall(
27986 func_name: []const u8,27880 func_name: []const u8,
27987 args: []const Air.Inst.Ref,27881 args: []const Air.Inst.Ref,
27988) !void {27882) !void {
27989 assert(!parent_block.is_comptime);27883 assert(!parent_block.isComptime());
27990 const gpa = sema.gpa;27884 const gpa = sema.gpa;
27991 const pt = sema.pt;27885 const pt = sema.pt;
27992 const zcu = pt.zcu;27886 const zcu = pt.zcu;
...@@ -27997,7 +27891,7 @@ fn addSafetyCheckCall(...@@ -27997,7 +27891,7 @@ fn addSafetyCheckCall(
27997 .namespace = parent_block.namespace,27891 .namespace = parent_block.namespace,
27998 .instructions = .{},27892 .instructions = .{},
27999 .inlining = parent_block.inlining,27893 .inlining = parent_block.inlining,
28000 .is_comptime = false,27894 .comptime_reason = null,
28001 .src_base_inst = parent_block.src_base_inst,27895 .src_base_inst = parent_block.src_base_inst,
28002 .type_name_ctx = parent_block.type_name_ctx,27896 .type_name_ctx = parent_block.type_name_ctx,
28003 };27897 };
...@@ -29168,9 +29062,7 @@ fn elemPtr(...@@ -29168,9 +29062,7 @@ fn elemPtr(
29168 .array, .vector => try sema.elemPtrArray(block, src, indexable_ptr_src, indexable_ptr, elem_index_src, elem_index, init, oob_safety),29062 .array, .vector => try sema.elemPtrArray(block, src, indexable_ptr_src, indexable_ptr, elem_index_src, elem_index, init, oob_safety),
29169 .@"struct" => blk: {29063 .@"struct" => blk: {
29170 // Tuple field access.29064 // Tuple field access.
29171 const index_val = try sema.resolveConstDefinedValue(block, elem_index_src, elem_index, .{29065 const index_val = try sema.resolveConstDefinedValue(block, elem_index_src, elem_index, .{ .simple = .tuple_field_index });
29172 .needed_comptime_reason = "tuple field access index must be comptime-known",
29173 });
29174 const index: u32 = @intCast(try index_val.toUnsignedIntSema(pt));29066 const index: u32 = @intCast(try index_val.toUnsignedIntSema(pt));
29175 break :blk try sema.tupleFieldPtr(block, src, indexable_ptr, elem_index_src, index, init);29067 break :blk try sema.tupleFieldPtr(block, src, indexable_ptr, elem_index_src, index, init);
29176 },29068 },
...@@ -29225,9 +29117,7 @@ fn elemPtrOneLayerOnly(...@@ -29225,9 +29117,7 @@ fn elemPtrOneLayerOnly(
29225 .array, .vector => try sema.elemPtrArray(block, src, indexable_src, indexable, elem_index_src, elem_index, init, oob_safety),29117 .array, .vector => try sema.elemPtrArray(block, src, indexable_src, indexable, elem_index_src, elem_index, init, oob_safety),
29226 .@"struct" => blk: {29118 .@"struct" => blk: {
29227 assert(child_ty.isTuple(zcu));29119 assert(child_ty.isTuple(zcu));
29228 const index_val = try sema.resolveConstDefinedValue(block, elem_index_src, elem_index, .{29120 const index_val = try sema.resolveConstDefinedValue(block, elem_index_src, elem_index, .{ .simple = .tuple_field_index });
29229 .needed_comptime_reason = "tuple field access index must be comptime-known",
29230 });
29231 const index: u32 = @intCast(try index_val.toUnsignedIntSema(pt));29121 const index: u32 = @intCast(try index_val.toUnsignedIntSema(pt));
29232 break :blk try sema.tupleFieldPtr(block, indexable_src, indexable, elem_index_src, index, false);29122 break :blk try sema.tupleFieldPtr(block, indexable_src, indexable, elem_index_src, index, false);
29233 },29123 },
...@@ -29305,9 +29195,7 @@ fn elemVal(...@@ -29305,9 +29195,7 @@ fn elemVal(
29305 },29195 },
29306 .@"struct" => {29196 .@"struct" => {
29307 // Tuple field access.29197 // Tuple field access.
29308 const index_val = try sema.resolveConstDefinedValue(block, elem_index_src, elem_index, .{29198 const index_val = try sema.resolveConstDefinedValue(block, elem_index_src, elem_index, .{ .simple = .tuple_field_index });
29309 .needed_comptime_reason = "tuple field access index must be comptime-known",
29310 });
29311 const index: u32 = @intCast(try index_val.toUnsignedIntSema(pt));29199 const index: u32 = @intCast(try index_val.toUnsignedIntSema(pt));
29312 return sema.tupleField(block, indexable_src, indexable, elem_index_src, index);29200 return sema.tupleField(block, indexable_src, indexable, elem_index_src, index);
29313 },29201 },
...@@ -30093,9 +29981,7 @@ fn coerceExtra(...@@ -30093,9 +29981,7 @@ fn coerceExtra(
30093 const val = maybe_inst_val orelse {29981 const val = maybe_inst_val orelse {
30094 if (dest_ty.zigTypeTag(zcu) == .comptime_int) {29982 if (dest_ty.zigTypeTag(zcu) == .comptime_int) {
30095 if (!opts.report_err) return error.NotCoercible;29983 if (!opts.report_err) return error.NotCoercible;
30096 return sema.failWithNeededComptime(block, inst_src, .{29984 return sema.failWithNeededComptime(block, inst_src, .{ .simple = .casted_to_comptime_int });
30097 .needed_comptime_reason = "value being casted to 'comptime_int' must be comptime-known",
30098 });
30099 }29985 }
30100 break :float;29986 break :float;
30101 };29987 };
...@@ -30120,9 +30006,7 @@ fn coerceExtra(...@@ -30120,9 +30006,7 @@ fn coerceExtra(
30120 if (dest_ty.zigTypeTag(zcu) == .comptime_int) {30006 if (dest_ty.zigTypeTag(zcu) == .comptime_int) {
30121 if (!opts.report_err) return error.NotCoercible;30007 if (!opts.report_err) return error.NotCoercible;
30122 if (opts.no_cast_to_comptime_int) return inst;30008 if (opts.no_cast_to_comptime_int) return inst;
30123 return sema.failWithNeededComptime(block, inst_src, .{30009 return sema.failWithNeededComptime(block, inst_src, .{ .simple = .casted_to_comptime_int });
30124 .needed_comptime_reason = "value being casted to 'comptime_int' must be comptime-known",
30125 });
30126 }30010 }
3012730011
30128 // integer widening30012 // integer widening
...@@ -30158,9 +30042,7 @@ fn coerceExtra(...@@ -30158,9 +30042,7 @@ fn coerceExtra(
30158 return Air.internedToRef(result_val.toIntern());30042 return Air.internedToRef(result_val.toIntern());
30159 } else if (dest_ty.zigTypeTag(zcu) == .comptime_float) {30043 } else if (dest_ty.zigTypeTag(zcu) == .comptime_float) {
30160 if (!opts.report_err) return error.NotCoercible;30044 if (!opts.report_err) return error.NotCoercible;
30161 return sema.failWithNeededComptime(block, inst_src, .{30045 return sema.failWithNeededComptime(block, inst_src, .{ .simple = .casted_to_comptime_float });
30162 .needed_comptime_reason = "value being casted to 'comptime_float' must be comptime-known",
30163 });
30164 }30046 }
3016530047
30166 // float widening30048 // float widening
...@@ -30175,9 +30057,7 @@ fn coerceExtra(...@@ -30175,9 +30057,7 @@ fn coerceExtra(
30175 const val = maybe_inst_val orelse {30057 const val = maybe_inst_val orelse {
30176 if (dest_ty.zigTypeTag(zcu) == .comptime_float) {30058 if (dest_ty.zigTypeTag(zcu) == .comptime_float) {
30177 if (!opts.report_err) return error.NotCoercible;30059 if (!opts.report_err) return error.NotCoercible;
30178 return sema.failWithNeededComptime(block, inst_src, .{30060 return sema.failWithNeededComptime(block, inst_src, .{ .simple = .casted_to_comptime_float });
30179 .needed_comptime_reason = "value being casted to 'comptime_float' must be comptime-known",
30180 });
30181 }30061 }
30182 break :int;30062 break :int;
30183 };30063 };
...@@ -32435,9 +32315,7 @@ fn coerceTupleToStruct(...@@ -32435,9 +32315,7 @@ fn coerceTupleToStruct(
32435 field_refs[struct_field_index] = coerced;32315 field_refs[struct_field_index] = coerced;
32436 if (struct_type.fieldIsComptime(ip, struct_field_index)) {32316 if (struct_type.fieldIsComptime(ip, struct_field_index)) {
32437 const init_val = try sema.resolveValue(coerced) orelse {32317 const init_val = try sema.resolveValue(coerced) orelse {
32438 return sema.failWithNeededComptime(block, field_src, .{32318 return sema.failWithNeededComptime(block, field_src, .{ .simple = .stored_to_comptime_field });
32439 .needed_comptime_reason = "value stored in comptime field must be comptime-known",
32440 });
32441 };32319 };
3244232320
32443 const field_init = Value.fromInterned(struct_type.field_inits.get(ip)[struct_field_index]);32321 const field_init = Value.fromInterned(struct_type.field_inits.get(ip)[struct_field_index]);
...@@ -32550,9 +32428,7 @@ fn coerceTupleToTuple(...@@ -32550,9 +32428,7 @@ fn coerceTupleToTuple(
32550 field_refs[field_index] = coerced;32428 field_refs[field_index] = coerced;
32551 if (default_val != .none) {32429 if (default_val != .none) {
32552 const init_val = (try sema.resolveValue(coerced)) orelse {32430 const init_val = (try sema.resolveValue(coerced)) orelse {
32553 return sema.failWithNeededComptime(block, field_src, .{32431 return sema.failWithNeededComptime(block, field_src, .{ .simple = .stored_to_comptime_field });
32554 .needed_comptime_reason = "value stored in comptime field must be comptime-known",
32555 });
32556 };32432 };
3255732433
32558 if (!init_val.eql(Value.fromInterned(default_val), Type.fromInterned(field_ty), pt.zcu)) {32434 if (!init_val.eql(Value.fromInterned(default_val), Type.fromInterned(field_ty), pt.zcu)) {
...@@ -32816,12 +32692,12 @@ fn analyzeRef(...@@ -32816,12 +32692,12 @@ fn analyzeRef(
32816 // In a comptime context, the store would fail, since the operand is runtime-known. But that's32692 // In a comptime context, the store would fail, since the operand is runtime-known. But that's
32817 // okay; we don't actually need this store to succeed, since we're creating a runtime value in a32693 // okay; we don't actually need this store to succeed, since we're creating a runtime value in a
32818 // comptime scope, so the value can never be used aside from to get its type.32694 // comptime scope, so the value can never be used aside from to get its type.
32819 if (!block.is_comptime) {32695 if (!block.isComptime()) {
32820 try sema.storePtr(block, src, alloc, operand);32696 try sema.storePtr(block, src, alloc, operand);
32821 }32697 }
3282232698
32823 // Cast to the constant pointer type. We do this directly rather than going via `coerce` to32699 // Cast to the constant pointer type. We do this directly rather than going via `coerce` to
32824 // avoid errors in the `block.is_comptime` case.32700 // avoid errors in the `block.isComptime()` case.
32825 return block.addBitCast(ptr_type, alloc);32701 return block.addBitCast(ptr_type, alloc);
32826}32702}
3282732703
...@@ -33184,24 +33060,24 @@ fn analyzeSlice(...@@ -33184,24 +33060,24 @@ fn analyzeSlice(
33184 array_ty = double_child_ty;33060 array_ty = double_child_ty;
33185 elem_ty = double_child_ty.childType(zcu);33061 elem_ty = double_child_ty.childType(zcu);
33186 } else {33062 } else {
33187 const bounds_error_message = "slice of single-item pointer must have comptime-known bounds [0..0], [0..1], or [1..1]";
33188 if (uncasted_end_opt == .none) {33063 if (uncasted_end_opt == .none) {
33189 return sema.fail(block, src, bounds_error_message, .{});33064 return sema.fail(block, src, "slice of single-item pointer must be bounded", .{});
33190 }33065 }
33191 const start_value = try sema.resolveConstDefinedValue(33066 const start_value = try sema.resolveConstDefinedValue(
33192 block,33067 block,
33193 start_src,33068 start_src,
33194 uncasted_start,33069 uncasted_start,
33195 .{ .needed_comptime_reason = bounds_error_message },33070 .{ .simple = .slice_single_item_ptr_bounds },
33196 );33071 );
3319733072
33198 const end_value = try sema.resolveConstDefinedValue(33073 const end_value = try sema.resolveConstDefinedValue(
33199 block,33074 block,
33200 end_src,33075 end_src,
33201 uncasted_end_opt,33076 uncasted_end_opt,
33202 .{ .needed_comptime_reason = bounds_error_message },33077 .{ .simple = .slice_single_item_ptr_bounds },
33203 );33078 );
3320433079
33080 const bounds_error_message = "slice of single-item pointer must have bounds [0..0], [0..1], or [1..1]";
33205 if (try sema.compareScalar(start_value, .neq, end_value, Type.comptime_int)) {33081 if (try sema.compareScalar(start_value, .neq, end_value, Type.comptime_int)) {
33206 if (try sema.compareScalar(start_value, .neq, Value.zero_comptime_int, Type.comptime_int)) {33082 if (try sema.compareScalar(start_value, .neq, Value.zero_comptime_int, Type.comptime_int)) {
33207 const msg = msg: {33083 const msg = msg: {
...@@ -33416,9 +33292,7 @@ fn analyzeSlice(...@@ -33416,9 +33292,7 @@ fn analyzeSlice(
33416 if (sentinel_opt != .none) {33292 if (sentinel_opt != .none) {
33417 const casted = try sema.coerce(block, elem_ty, sentinel_opt, sentinel_src);33293 const casted = try sema.coerce(block, elem_ty, sentinel_opt, sentinel_src);
33418 try checkSentinelType(sema, block, sentinel_src, elem_ty);33294 try checkSentinelType(sema, block, sentinel_src, elem_ty);
33419 break :s try sema.resolveConstDefinedValue(block, sentinel_src, casted, .{33295 break :s try sema.resolveConstDefinedValue(block, sentinel_src, casted, .{ .simple = .slice_sentinel });
33420 .needed_comptime_reason = "slice sentinel must be comptime-known",
33421 });
33422 }33296 }
33423 // If we are slicing to the end of something that is sentinel-terminated33297 // If we are slicing to the end of something that is sentinel-terminated
33424 // then the resulting slice type is also sentinel-terminated.33298 // then the resulting slice type is also sentinel-terminated.
...@@ -33499,9 +33373,9 @@ fn analyzeSlice(...@@ -33499,9 +33373,9 @@ fn analyzeSlice(
33499 runtime_src = end_src;33373 runtime_src = end_src;
33500 }33374 }
3350133375
33502 if (!checked_start_lte_end and block.wantSafety() and !block.is_comptime) {33376 if (!checked_start_lte_end and block.wantSafety() and !block.isComptime()) {
33503 // requirement: start <= end33377 // requirement: start <= end
33504 assert(!block.is_comptime);33378 assert(!block.isComptime());
33505 try sema.requireRuntimeBlock(block, src, runtime_src.?);33379 try sema.requireRuntimeBlock(block, src, runtime_src.?);
33506 const ok = try block.addBinOp(.cmp_lte, start, end);33380 const ok = try block.addBinOp(.cmp_lte, start, end);
33507 try sema.addSafetyCheckCall(block, src, ok, "startGreaterThanEnd", &.{ start, end });33381 try sema.addSafetyCheckCall(block, src, ok, "startGreaterThanEnd", &.{ start, end });
...@@ -35859,7 +35733,7 @@ fn backingIntType(...@@ -35859,7 +35733,7 @@ fn backingIntType(
35859 .namespace = struct_type.namespace,35733 .namespace = struct_type.namespace,
35860 .instructions = .{},35734 .instructions = .{},
35861 .inlining = null,35735 .inlining = null,
35862 .is_comptime = true,35736 .comptime_reason = null, // set below if needed
35863 .src_base_inst = struct_type.zir_index,35737 .src_base_inst = struct_type.zir_index,
35864 .type_name_ctx = struct_type.name,35738 .type_name_ctx = struct_type.name,
35865 };35739 };
...@@ -35899,6 +35773,10 @@ fn backingIntType(...@@ -35899,6 +35773,10 @@ fn backingIntType(
35899 .base_node_inst = struct_type.zir_index,35773 .base_node_inst = struct_type.zir_index,
35900 .offset = .{ .node_offset_container_tag = 0 },35774 .offset = .{ .node_offset_container_tag = 0 },
35901 };35775 };
35776 block.comptime_reason = .{ .reason = .{
35777 .src = backing_int_src,
35778 .r = .{ .simple = .type },
35779 } };
35902 const backing_int_ty = blk: {35780 const backing_int_ty = blk: {
35903 if (backing_int_body_len == 0) {35781 if (backing_int_body_len == 0) {
35904 const backing_int_ref: Zir.Inst.Ref = @enumFromInt(zir.extra[extra_index]);35782 const backing_int_ref: Zir.Inst.Ref = @enumFromInt(zir.extra[extra_index]);
...@@ -36512,7 +36390,13 @@ fn structFields(...@@ -36512,7 +36390,13 @@ fn structFields(
36512 .namespace = namespace_index,36390 .namespace = namespace_index,
36513 .instructions = .{},36391 .instructions = .{},
36514 .inlining = null,36392 .inlining = null,
36515 .is_comptime = true,36393 .comptime_reason = .{ .reason = .{
36394 .src = .{
36395 .base_node_inst = struct_type.zir_index,
36396 .offset = .nodeOffset(0),
36397 },
36398 .r = .{ .simple = .struct_fields },
36399 } },
36516 .src_base_inst = struct_type.zir_index,36400 .src_base_inst = struct_type.zir_index,
36517 .type_name_ctx = struct_type.name,36401 .type_name_ctx = struct_type.name,
36518 };36402 };
...@@ -36698,7 +36582,7 @@ fn structFieldInits(...@@ -36698,7 +36582,7 @@ fn structFieldInits(
36698 .namespace = namespace_index,36582 .namespace = namespace_index,
36699 .instructions = .{},36583 .instructions = .{},
36700 .inlining = null,36584 .inlining = null,
36701 .is_comptime = true,36585 .comptime_reason = undefined, // set when `block_scope` is used
36702 .src_base_inst = struct_type.zir_index,36586 .src_base_inst = struct_type.zir_index,
36703 .type_name_ctx = struct_type.name,36587 .type_name_ctx = struct_type.name,
36704 };36588 };
...@@ -36776,13 +36660,13 @@ fn structFieldInits(...@@ -36776,13 +36660,13 @@ fn structFieldInits(
36776 .offset = .{ .container_field_value = @intCast(field_i) },36660 .offset = .{ .container_field_value = @intCast(field_i) },
36777 };36661 };
3677836662
36663 block_scope.comptime_reason = .{ .reason = .{
36664 .src = init_src,
36665 .r = .{ .simple = .struct_field_default_value },
36666 } };
36779 const init = try sema.resolveInlineBody(&block_scope, body, zir_index);36667 const init = try sema.resolveInlineBody(&block_scope, body, zir_index);
36780 const coerced = try sema.coerce(&block_scope, field_ty, init, init_src);36668 const coerced = try sema.coerce(&block_scope, field_ty, init, init_src);
36781 const default_val = try sema.resolveValue(coerced) orelse {36669 const default_val = try sema.resolveConstValue(&block_scope, init_src, coerced, null);
36782 return sema.failWithNeededComptime(&block_scope, init_src, .{
36783 .needed_comptime_reason = "struct field default value must be comptime-known",
36784 });
36785 };
3678636670
36787 if (default_val.canMutateComptimeVarState(zcu)) {36671 if (default_val.canMutateComptimeVarState(zcu)) {
36788 return sema.fail(&block_scope, init_src, "field default value contains reference to comptime-mutable memory", .{});36672 return sema.fail(&block_scope, init_src, "field default value contains reference to comptime-mutable memory", .{});
...@@ -36850,20 +36734,26 @@ fn unionFields(...@@ -36850,20 +36734,26 @@ fn unionFields(
36850 const body = zir.bodySlice(extra_index, body_len);36734 const body = zir.bodySlice(extra_index, body_len);
36851 extra_index += body.len;36735 extra_index += body.len;
3685236736
36737 const src: LazySrcLoc = .{
36738 .base_node_inst = union_type.zir_index,
36739 .offset = .nodeOffset(0),
36740 };
36741
36853 var block_scope: Block = .{36742 var block_scope: Block = .{
36854 .parent = null,36743 .parent = null,
36855 .sema = sema,36744 .sema = sema,
36856 .namespace = union_type.namespace,36745 .namespace = union_type.namespace,
36857 .instructions = .{},36746 .instructions = .{},
36858 .inlining = null,36747 .inlining = null,
36859 .is_comptime = true,36748 .comptime_reason = .{ .reason = .{
36749 .src = src,
36750 .r = .{ .simple = .union_fields },
36751 } },
36860 .src_base_inst = union_type.zir_index,36752 .src_base_inst = union_type.zir_index,
36861 .type_name_ctx = union_type.name,36753 .type_name_ctx = union_type.name,
36862 };36754 };
36863 defer assert(block_scope.instructions.items.len == 0);36755 defer assert(block_scope.instructions.items.len == 0);
3686436756
36865 const src = block_scope.nodeOffset(0);
36866
36867 if (body.len != 0) {36757 if (body.len != 0) {
36868 _ = try sema.analyzeInlineBody(&block_scope, body, zir_index);36758 _ = try sema.analyzeInlineBody(&block_scope, body, zir_index);
36869 }36759 }
...@@ -36993,9 +36883,7 @@ fn unionFields(...@@ -36993,9 +36883,7 @@ fn unionFields(
36993 if (enum_field_vals.capacity() > 0) {36883 if (enum_field_vals.capacity() > 0) {
36994 const enum_tag_val = if (tag_ref != .none) blk: {36884 const enum_tag_val = if (tag_ref != .none) blk: {
36995 const coerced = try sema.coerce(&block_scope, int_tag_ty, tag_ref, value_src);36885 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, .{36886 const val = try sema.resolveConstDefinedValue(&block_scope, value_src, coerced, .{ .simple = .enum_field_tag_value });
36997 .needed_comptime_reason = "enum tag value must be comptime-known",
36998 });
36999 last_tag_val = val;36887 last_tag_val = val;
3700036888
37001 break :blk val;36889 break :blk val;
...@@ -37669,9 +37557,7 @@ pub fn analyzeAsAddressSpace(...@@ -37669,9 +37557,7 @@ pub fn analyzeAsAddressSpace(
37669 const zcu = pt.zcu;37557 const zcu = pt.zcu;
37670 const addrspace_ty = try sema.getBuiltinType("AddressSpace");37558 const addrspace_ty = try sema.getBuiltinType("AddressSpace");
37671 const coerced = try sema.coerce(block, addrspace_ty, air_ref, src);37559 const coerced = try sema.coerce(block, addrspace_ty, air_ref, src);
37672 const addrspace_val = try sema.resolveConstDefinedValue(block, src, coerced, .{37560 const addrspace_val = try sema.resolveConstDefinedValue(block, src, coerced, .{ .simple = .@"addrspace" });
37673 .needed_comptime_reason = "address space must be comptime-known",
37674 });
37675 const address_space = zcu.toEnum(std.builtin.AddressSpace, addrspace_val);37561 const address_space = zcu.toEnum(std.builtin.AddressSpace, addrspace_val);
37676 const target = pt.zcu.getTarget();37562 const target = pt.zcu.getTarget();
37677 const arch = target.cpu.arch;37563 const arch = target.cpu.arch;
...@@ -38560,7 +38446,7 @@ fn sliceToIpString(...@@ -38560,7 +38446,7 @@ fn sliceToIpString(
38560 block: *Block,38446 block: *Block,
38561 src: LazySrcLoc,38447 src: LazySrcLoc,
38562 slice_val: Value,38448 slice_val: Value,
38563 reason: NeededComptimeReason,38449 reason: ComptimeReason,
38564) CompileError!InternPool.NullTerminatedString {38450) CompileError!InternPool.NullTerminatedString {
38565 const pt = sema.pt;38451 const pt = sema.pt;
38566 const zcu = pt.zcu;38452 const zcu = pt.zcu;
...@@ -38580,7 +38466,7 @@ fn derefSliceAsArray(...@@ -38580,7 +38466,7 @@ fn derefSliceAsArray(
38580 block: *Block,38466 block: *Block,
38581 src: LazySrcLoc,38467 src: LazySrcLoc,
38582 slice_val: Value,38468 slice_val: Value,
38583 reason: NeededComptimeReason,38469 reason: ComptimeReason,
38584) CompileError!Value {38470) CompileError!Value {
38585 return try sema.maybeDerefSliceAsArray(block, src, slice_val) orelse {38471 return try sema.maybeDerefSliceAsArray(block, src, slice_val) orelse {
38586 return sema.failWithNeededComptime(block, src, reason);38472 return sema.failWithNeededComptime(block, src, reason);
...@@ -38734,7 +38620,10 @@ pub fn resolveDeclaredEnum(...@@ -38734,7 +38620,10 @@ pub fn resolveDeclaredEnum(
38734 .namespace = namespace,38620 .namespace = namespace,
38735 .instructions = .{},38621 .instructions = .{},
38736 .inlining = null,38622 .inlining = null,
38737 .is_comptime = true,38623 .comptime_reason = .{ .reason = .{
38624 .src = src,
38625 .r = .{ .simple = .enum_fields },
38626 } },
38738 .src_base_inst = tracked_inst,38627 .src_base_inst = tracked_inst,
38739 .type_name_ctx = type_name,38628 .type_name_ctx = type_name,
38740 };38629 };
...@@ -38798,9 +38687,7 @@ pub fn resolveDeclaredEnum(...@@ -38798,9 +38687,7 @@ pub fn resolveDeclaredEnum(
38798 last_tag_val = try sema.resolveConstDefinedValue(&block, .{38687 last_tag_val = try sema.resolveConstDefinedValue(&block, .{
38799 .base_node_inst = tracked_inst,38688 .base_node_inst = tracked_inst,
38800 .offset = .{ .container_field_name = field_i },38689 .offset = .{ .container_field_name = field_i },
38801 }, tag_inst, .{38690 }, tag_inst, .{ .simple = .enum_field_tag_value });
38802 .needed_comptime_reason = "enum tag value must be comptime-known",
38803 });
38804 if (!(try sema.intFitsInType(last_tag_val.?, int_tag_ty, null))) break :overflow true;38691 if (!(try sema.intFitsInType(last_tag_val.?, int_tag_ty, null))) break :overflow true;
38805 last_tag_val = try pt.getCoerced(last_tag_val.?, int_tag_ty);38692 last_tag_val = try pt.getCoerced(last_tag_val.?, int_tag_ty);
38806 if (wip_ty.nextField(ip, field_name, last_tag_val.?.toIntern())) |conflict| {38693 if (wip_ty.nextField(ip, field_name, last_tag_val.?.toIntern())) |conflict| {
...@@ -38879,9 +38766,7 @@ fn getPanicInnerFn(...@@ -38879,9 +38766,7 @@ fn getPanicInnerFn(
38879 const inner_name_ip = try ip.getOrPutString(gpa, pt.tid, inner_name, .no_embedded_nulls);38766 const inner_name_ip = try ip.getOrPutString(gpa, pt.tid, inner_name, .no_embedded_nulls);
38880 const opt_fn_ref = try namespaceLookupVal(sema, block, src, outer_ty.getNamespaceIndex(zcu), inner_name_ip);38767 const opt_fn_ref = try namespaceLookupVal(sema, block, src, outer_ty.getNamespaceIndex(zcu), inner_name_ip);
38881 const fn_ref = opt_fn_ref orelse return sema.fail(block, src, "std.builtin.Panic missing {s}", .{inner_name});38768 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, .{38769 const fn_val = try sema.resolveConstValue(block, src, fn_ref, .{ .simple = .panic_handler });
38883 .needed_comptime_reason = "panic handler must be comptime-known",
38884 });
38885 if (fn_val.typeOf(zcu).zigTypeTag(zcu) != .@"fn") {38770 if (fn_val.typeOf(zcu).zigTypeTag(zcu) != .@"fn") {
38886 return sema.fail(block, src, "std.builtin.Panic.{s} is not a function", .{inner_name});38771 return sema.fail(block, src, "std.builtin.Panic.{s} is not a function", .{inner_name});
38887 }38772 }
...@@ -38963,9 +38848,7 @@ pub fn resolveNavPtrModifiers(...@@ -38963,9 +38848,7 @@ pub fn resolveNavPtrModifiers(
38963 const @"linksection": InternPool.OptionalNullTerminatedString = ls: {38848 const @"linksection": InternPool.OptionalNullTerminatedString = ls: {
38964 const linksection_body = zir_decl.linksection_body orelse break :ls .none;38849 const linksection_body = zir_decl.linksection_body orelse break :ls .none;
38965 const linksection_ref = try sema.resolveInlineBody(block, linksection_body, decl_inst);38850 const linksection_ref = try sema.resolveInlineBody(block, linksection_body, decl_inst);
38966 const bytes = try sema.toConstString(block, section_src, linksection_ref, .{38851 const bytes = try sema.toConstString(block, section_src, linksection_ref, .{ .simple = .@"linksection" });
38967 .needed_comptime_reason = "linksection must be comptime-known",
38968 });
38969 if (std.mem.indexOfScalar(u8, bytes, 0) != null) {38852 if (std.mem.indexOfScalar(u8, bytes, 0) != null) {
38970 return sema.fail(block, section_src, "linksection cannot contain null bytes", .{});38853 return sema.fail(block, section_src, "linksection cannot contain null bytes", .{});
38971 } else if (bytes.len == 0) {38854 } 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...@@ -682,7 +682,13 @@ fn analyzeComptimeUnit(pt: Zcu.PerThread, cu_id: InternPool.ComptimeUnit.Id) Zcu
682 .namespace = comptime_unit.namespace,682 .namespace = comptime_unit.namespace,
683 .instructions = .{},683 .instructions = .{},
684 .inlining = null,684 .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 } },
686 .src_base_inst = comptime_unit.zir_index,692 .src_base_inst = comptime_unit.zir_index,
687 .type_name_ctx = try ip.getOrPutStringFmt(gpa, pt.tid, "{}.comptime", .{693 .type_name_ctx = try ip.getOrPutStringFmt(gpa, pt.tid, "{}.comptime", .{
688 Type.fromInterned(zcu.namespacePtr(comptime_unit.namespace).owner_type).containerTypeName(ip).fmt(ip),694 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...@@ -878,7 +884,7 @@ fn analyzeNavVal(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zcu.CompileErr
878 .namespace = old_nav.analysis.?.namespace,884 .namespace = old_nav.analysis.?.namespace,
879 .instructions = .{},885 .instructions = .{},
880 .inlining = null,886 .inlining = null,
881 .is_comptime = true,887 .comptime_reason = undefined, // set below
882 .src_base_inst = old_nav.analysis.?.zir_index,888 .src_base_inst = old_nav.analysis.?.zir_index,
883 .type_name_ctx = old_nav.fqn,889 .type_name_ctx = old_nav.fqn,
884 };890 };
...@@ -893,6 +899,11 @@ fn analyzeNavVal(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zcu.CompileErr...@@ -893,6 +899,11 @@ fn analyzeNavVal(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zcu.CompileErr
893 const section_src = block.src(.{ .node_offset_var_decl_section = 0 });899 const section_src = block.src(.{ .node_offset_var_decl_section = 0 });
894 const addrspace_src = block.src(.{ .node_offset_var_decl_addrspace = 0 });900 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
896 const maybe_ty: ?Type = if (zir_decl.type_body != null) ty: {907 const maybe_ty: ?Type = if (zir_decl.type_body != null) ty: {
897 // Since we have a type body, the type is resolved separately!908 // Since we have a type body, the type is resolved separately!
898 // Of course, we need to make sure we depend on it properly.909 // 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...@@ -1253,7 +1264,7 @@ fn analyzeNavType(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zcu.CompileEr
1253 .namespace = old_nav.analysis.?.namespace,1264 .namespace = old_nav.analysis.?.namespace,
1254 .instructions = .{},1265 .instructions = .{},
1255 .inlining = null,1266 .inlining = null,
1256 .is_comptime = true,1267 .comptime_reason = undefined, // set below
1257 .src_base_inst = old_nav.analysis.?.zir_index,1268 .src_base_inst = old_nav.analysis.?.zir_index,
1258 .type_name_ctx = old_nav.fqn,1269 .type_name_ctx = old_nav.fqn,
1259 };1270 };
...@@ -1262,6 +1273,13 @@ fn analyzeNavType(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zcu.CompileEr...@@ -1262,6 +1273,13 @@ fn analyzeNavType(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zcu.CompileEr
1262 const zir_decl = zir.getDeclaration(inst_resolved.inst);1273 const zir_decl = zir.getDeclaration(inst_resolved.inst);
1263 assert(old_nav.is_usingnamespace == (zir_decl.kind == .@"usingnamespace"));1274 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
1265 const type_body = zir_decl.type_body orelse {1283 const type_body = zir_decl.type_body orelse {
1266 // The type of this `Nav` is inferred from the value.1284 // The type of this `Nav` is inferred from the value.
1267 // In other words, this `nav_ty` depends on the corresponding `nav_val`.1285 // 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...@@ -1279,8 +1297,6 @@ fn analyzeNavType(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zcu.CompileEr
1279 return .{ .type_changed = true };1297 return .{ .type_changed = true };
1280 };1298 };
12811299
1282 const ty_src = block.src(.{ .node_offset_var_decl_ty = 0 });
1283
1284 const resolved_ty: Type = ty: {1300 const resolved_ty: Type = ty: {
1285 const uncoerced_type_ref = try sema.resolveInlineBody(&block, type_body, inst_resolved.inst);1301 const uncoerced_type_ref = try sema.resolveInlineBody(&block, type_body, inst_resolved.inst);
1286 const type_ref = try sema.coerce(&block, .type, uncoerced_type_ref, ty_src);1302 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...@@ -2442,7 +2458,7 @@ fn analyzeFnBodyInner(pt: Zcu.PerThread, func_index: InternPool.Index) Zcu.SemaE
2442 .namespace = decl_nav.analysis.?.namespace,2458 .namespace = decl_nav.analysis.?.namespace,
2443 .instructions = .{},2459 .instructions = .{},
2444 .inlining = null,2460 .inlining = null,
2445 .is_comptime = false,2461 .comptime_reason = null,
2446 .src_base_inst = decl_nav.analysis.?.zir_index,2462 .src_base_inst = decl_nav.analysis.?.zir_index,
2447 .type_name_ctx = func_nav.fqn,2463 .type_name_ctx = func_nav.fqn,
2448 };2464 };
src/print_zir.zig+10-4
...@@ -437,7 +437,6 @@ const Writer = struct {...@@ -437,7 +437,6 @@ const Writer = struct {
437 .field_call => try self.writeCall(stream, inst, .field),437 .field_call => try self.writeCall(stream, inst, .field),
438438
439 .block,439 .block,
440 .block_comptime,
441 .block_inline,440 .block_inline,
442 .suspend_block,441 .suspend_block,
443 .loop,442 .loop,
...@@ -445,6 +444,8 @@ const Writer = struct {...@@ -445,6 +444,8 @@ const Writer = struct {
445 .typeof_builtin,444 .typeof_builtin,
446 => try self.writeBlock(stream, inst),445 => try self.writeBlock(stream, inst),
447446
447 .block_comptime => try self.writeBlockComptime(stream, inst),
448
448 .condbr,449 .condbr,
449 .condbr_inline,450 .condbr_inline,
450 => try self.writeCondBr(stream, inst),451 => try self.writeCondBr(stream, inst),
...@@ -1343,16 +1344,21 @@ const Writer = struct {...@@ -1343,16 +1344,21 @@ const Writer = struct {
13431344
1344 fn writeBlock(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {1345 fn writeBlock(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
1345 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;1346 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1346 try self.writePlNodeBlockWithoutSrc(stream, inst);1347 const extra = self.code.extraData(Zir.Inst.Block, inst_data.payload_index);
1348 const body = self.code.bodySlice(extra.end, extra.data.body_len);
1349 try self.writeBracedBody(stream, body);
1350 try stream.writeAll(") ");
1347 try self.writeSrcNode(stream, inst_data.src_node);1351 try self.writeSrcNode(stream, inst_data.src_node);
1348 }1352 }
13491353
1350 fn writePlNodeBlockWithoutSrc(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {1354 fn writeBlockComptime(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
1351 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;1355 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1352 const extra = self.code.extraData(Zir.Inst.Block, inst_data.payload_index);1356 const extra = self.code.extraData(Zir.Inst.BlockComptime, inst_data.payload_index);
1353 const body = self.code.bodySlice(extra.end, extra.data.body_len);1357 const body = self.code.bodySlice(extra.end, extra.data.body_len);
1358 try stream.print("reason={s}, ", .{@tagName(extra.data.reason)});
1354 try self.writeBracedBody(stream, body);1359 try self.writeBracedBody(stream, body);
1355 try stream.writeAll(") ");1360 try stream.writeAll(") ");
1361 try self.writeSrcNode(stream, inst_data.src_node);
1356 }1362 }
13571363
1358 fn writeCondBr(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {1364 fn writeCondBr(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {