Skip to content

Commit

Permalink
compiler: ensure result of block_comptime is comptime-known
Browse files Browse the repository at this point in the history
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: ziglang#22296
  • Loading branch information
mlugg committed Dec 30, 2024
1 parent ec60156 commit a985d30
Show file tree
Hide file tree
Showing 7 changed files with 815 additions and 657 deletions.
159 changes: 159 additions & 0 deletions lib/std/zig.zig
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,165 @@ pub const EnvVar = enum {
}
};

pub const SimpleComptimeReason = enum(u32) {
// Evaluating at comptime because a builtin operand must be comptime-known.
// These messages all mention a specific builtin.
operand_Type,
operand_setEvalBranchQuota,
operand_setFloatMode,
operand_branchHint,
operand_setRuntimeSafety,
operand_embedFile,
operand_cImport,
operand_cDefine_macro_name,
operand_cDefine_macro_value,
operand_cInclude_file_name,
operand_cUndef_macro_name,
operand_shuffle_mask,
operand_atomicRmw_operation,
operand_reduce_operation,

// Evaluating at comptime because an operand must be comptime-known.
// These messages do not mention a specific builtin (and may not be about a builtin at all).
export_target,
export_options,
extern_options,
prefetch_options,
call_modifier,
compile_error_string,
inline_assembly_code,
atomic_order,
array_mul_factor,
slice_cat_operand,
comptime_call_target,
wasm_memory_index,
work_group_dim_index,

// Evaluating at comptime because types must be comptime-known.
// Reasons other than `.type` are just more specific messages.
type,
array_sentinel,
pointer_sentinel,
slice_sentinel,
array_length,
vector_length,
error_set_contents,
struct_fields,
enum_fields,
union_fields,
function_ret_ty,
function_parameters,

// Evaluating at comptime because decl/field name must be comptime-known.
decl_name,
field_name,
struct_field_name,
enum_field_name,
union_field_name,
tuple_field_name,
tuple_field_index,

// Evaluating at comptime because it is an attribute of a global declaration.
container_var_init,
@"callconv",
@"align",
@"addrspace",
@"linksection",

// Miscellaneous reasons.
comptime_keyword,
comptime_call_modifier,
switch_item,
tuple_field_default_value,
struct_field_default_value,
enum_field_tag_value,
slice_single_item_ptr_bounds,
comptime_param_arg,
stored_to_comptime_field,
stored_to_comptime_var,
casted_to_comptime_enum,
casted_to_comptime_int,
casted_to_comptime_float,
panic_handler,

pub fn message(r: SimpleComptimeReason) []const u8 {
return switch (r) {
// zig fmt: off
.operand_Type => "operand to '@Type' must be comptime-known",
.operand_setEvalBranchQuota => "operand to '@setEvalBranchQuota' must be comptime-known",
.operand_setFloatMode => "operand to '@setFloatMode' must be comptime-known",
.operand_branchHint => "operand to '@branchHint' must be comptime-known",
.operand_setRuntimeSafety => "operand to '@setRuntimeSafety' must be comptime-known",
.operand_embedFile => "operand to '@embedFile' must be comptime-known",
.operand_cImport => "operand to '@cImport' is evaluated at comptime",
.operand_cDefine_macro_name => "'@cDefine' macro name must be comptime-known",
.operand_cDefine_macro_value => "'@cDefine' macro value must be comptime-known",
.operand_cInclude_file_name => "'@cInclude' file name must be comptime-known",
.operand_cUndef_macro_name => "'@cUndef' macro name must be comptime-known",
.operand_shuffle_mask => "'@shuffle' mask must be comptime-known",
.operand_atomicRmw_operation => "'@atomicRmw' operation must be comptime-known",
.operand_reduce_operation => "'@reduce' operation must be comptime-known",

.export_target => "export target must be comptime-known",
.export_options => "export options must be comptime-known",
.extern_options => "extern options must be comptime-known",
.prefetch_options => "prefetch options must be comptime-known",
.call_modifier => "call modifier must be comptime-known",
.compile_error_string => "compile error string must be comptime-known",
.inline_assembly_code => "inline assembly code must be comptime-known",
.atomic_order => "atomic order must be comptime-known",
.array_mul_factor => "array multiplication factor must be comptime-known",
.slice_cat_operand => "slice being concatenated must be comptime-known",
.comptime_call_target => "function being called at comptime must be comptime-known",
.wasm_memory_index => "wasm memory index must be comptime-known",
.work_group_dim_index => "work group dimension index must be comptime-known",

.type => "types must be comptime-known",
.array_sentinel => "array sentinel value must be comptime-known",
.pointer_sentinel => "pointer sentinel value must be comptime-known",
.slice_sentinel => "slice sentinel value must be comptime-known",
.array_length => "array length must be comptime-known",
.vector_length => "vector length must be comptime-known",
.error_set_contents => "error set contents must be comptime-known",
.struct_fields => "struct fields must be comptime-known",
.enum_fields => "enum fields must be comptime-known",
.union_fields => "union fields must be comptime-known",
.function_ret_ty => "function return type must be comptime-known",
.function_parameters => "function parameters must be comptime-known",

.decl_name => "declaration name must be comptime-known",
.field_name => "field name must be comptime-known",
.struct_field_name => "struct field name must be comptime-known",
.enum_field_name => "enum field name must be comptime-known",
.union_field_name => "union field name must be comptime-known",
.tuple_field_name => "tuple field name must be comptime-known",
.tuple_field_index => "tuple field index must be comptime-known",

.container_var_init => "initializer of container-level variable must be comptime-known",
.@"callconv" => "calling convention must be comptime-known",
.@"align" => "alignment must be comptime-known",
.@"addrspace" => "address space must be comptime-known",
.@"linksection" => "linksection must be comptime-known",

.comptime_keyword => "'comptime' keyword forces comptime evaluation",
.comptime_call_modifier => "'.compile_time' call modifier forces comptime evaluation",
.switch_item => "switch prong values must be comptime-known",
.tuple_field_default_value => "tuple field default value must be comptime-known",
.struct_field_default_value => "struct field default value must be comptime-known",
.enum_field_tag_value => "enum field tag value must be comptime-known",
.slice_single_item_ptr_bounds => "slice of single-item pointer must have comptime-known bounds",
.comptime_param_arg => "argument to comptime parameter must be comptime-known",
.stored_to_comptime_field => "value stored to a comptime field must be comptime-known",
.stored_to_comptime_var => "value stored to a comptime variable must be comptime-known",
.casted_to_comptime_enum => "value casted to enum with 'comptime_int' tag type must be comptime-known",
.casted_to_comptime_int => "value casted to 'comptime_int' must be comptime-known",
.casted_to_comptime_float => "value casted to 'comptime_float' must be comptime-known",
.panic_handler => "panic handler must be comptime-known",
// zig fmt: on
};
}
};

test {
_ = Ast;
_ = AstRlAnnotate;
Expand Down
Loading

0 comments on commit a985d30

Please sign in to comment.