Skip to content

Commit 6026a5f

Browse files
committed
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
1 parent 6d67658 commit 6026a5f

File tree

7 files changed

+821
-658
lines changed

7 files changed

+821
-658
lines changed

lib/std/zig.zig

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,165 @@ pub const EnvVar = enum {
718718
}
719719
};
720720

721+
pub 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+
721880
test {
722881
_ = Ast;
723882
_ = AstRlAnnotate;

0 commit comments

Comments
 (0)