authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-04 15:28:13-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-04 15:28:35-04:00
log68db9d5074cece234efa3a5352fe6cd36d210455
treeb79ead870777d9d90ab809b70a687c77af20df0d
parent36828a2e6aa6879641bf6980379dd806b6f479a1
signaturelock-open Commit is signed but in an unrecognized format.

add compile error for comptime control flow inside runtime block

closes #834

7 files changed, 241 insertions(+), 29 deletions(-)

src/all_types.hpp+18
......@@ -1836,6 +1836,7 @@ enum ScopeId {
18361836 ScopeIdFnDef,
18371837 ScopeIdCompTime,
18381838 ScopeIdCoroPrelude,
1839 ScopeIdRuntime,
18391840};
18401841
18411842struct Scope {
......@@ -1928,6 +1929,15 @@ struct ScopeLoop {
19281929 ZigList<IrBasicBlock *> *incoming_blocks;
19291930};
19301931
1932// This scope blocks certain things from working such as comptime continue
1933// inside a runtime if expression.
1934// NodeTypeIfBoolExpr, NodeTypeWhileExpr, NodeTypeForExpr
1935struct ScopeRuntime {
1936 Scope base;
1937
1938 IrInstruction *is_comptime;
1939};
1940
19311941// This scope is created for a suspend block in order to have labeled
19321942// suspend for breaking out of a suspend and for detecting if a suspend
19331943// block is inside a suspend block.
......@@ -2147,6 +2157,7 @@ enum IrInstructionId {
21472157 IrInstructionIdErrSetCast,
21482158 IrInstructionIdToBytes,
21492159 IrInstructionIdFromBytes,
2160 IrInstructionIdCheckRuntimeScope,
21502161};
21512162
21522163struct IrInstruction {
......@@ -3236,6 +3247,13 @@ struct IrInstructionSqrt {
32363247 IrInstruction *op;
32373248};
32383249
3250struct IrInstructionCheckRuntimeScope {
3251 IrInstruction base;
3252
3253 IrInstruction *scope_is_comptime;
3254 IrInstruction *is_comptime;
3255};
3256
32393257static const size_t slice_ptr_index = 0;
32403258static const size_t slice_len_index = 1;
32413259
src/analyze.cpp+8
......@@ -157,6 +157,13 @@ ScopeLoop *create_loop_scope(AstNode *node, Scope *parent) {
157157 return scope;
158158}
159159
160Scope *create_runtime_scope(AstNode *node, Scope *parent, IrInstruction *is_comptime) {
161 ScopeRuntime *scope = allocate<ScopeRuntime>(1);
162 scope->is_comptime = is_comptime;
163 init_scope(&scope->base, ScopeIdRuntime, node, parent);
164 return &scope->base;
165}
166
160167ScopeSuspend *create_suspend_scope(AstNode *node, Scope *parent) {
161168 assert(node->type == NodeTypeSuspend);
162169 ScopeSuspend *scope = allocate<ScopeSuspend>(1);
......@@ -3770,6 +3777,7 @@ FnTableEntry *scope_get_fn_if_root(Scope *scope) {
37703777 case ScopeIdSuspend:
37713778 case ScopeIdCompTime:
37723779 case ScopeIdCoroPrelude:
3780 case ScopeIdRuntime:
37733781 scope = scope->parent;
37743782 continue;
37753783 case ScopeIdFnDef:
src/analyze.hpp+1
......@@ -111,6 +111,7 @@ ScopeFnDef *create_fndef_scope(AstNode *node, Scope *parent, FnTableEntry *fn_en
111111ScopeDecls *create_decls_scope(AstNode *node, Scope *parent, TypeTableEntry *container_type, ImportTableEntry *import);
112112Scope *create_comptime_scope(AstNode *node, Scope *parent);
113113Scope *create_coro_prelude_scope(AstNode *node, Scope *parent);
114Scope *create_runtime_scope(AstNode *node, Scope *parent, IrInstruction *is_comptime);
114115
115116void init_const_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str);
116117ConstExprValue *create_const_str_lit(CodeGen *g, Buf *str);
src/codegen.cpp+2
......@@ -678,6 +678,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
678678 case ScopeIdSuspend:
679679 case ScopeIdCompTime:
680680 case ScopeIdCoroPrelude:
681 case ScopeIdRuntime:
681682 return get_di_scope(g, scope->parent);
682683 }
683684 zig_unreachable();
......@@ -4869,6 +4870,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
48694870 case IrInstructionIdFromBytes:
48704871 case IrInstructionIdToBytes:
48714872 case IrInstructionIdEnumToInt:
4873 case IrInstructionIdCheckRuntimeScope:
48724874 zig_unreachable();
48734875
48744876 case IrInstructionIdReturn:
src/ir.cpp+91-29
......@@ -832,6 +832,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionSqrt *) {
832832 return IrInstructionIdSqrt;
833833}
834834
835static constexpr IrInstructionId ir_instruction_id(IrInstructionCheckRuntimeScope *) {
836 return IrInstructionIdCheckRuntimeScope;
837}
838
835839template<typename T>
836840static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node) {
837841 T *special_instruction = allocate<T>(1);
......@@ -2974,6 +2978,17 @@ static IrInstruction *ir_build_sqrt(IrBuilder *irb, Scope *scope, AstNode *sourc
29742978 return &instruction->base;
29752979}
29762980
2981static IrInstruction *ir_build_check_runtime_scope(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *scope_is_comptime, IrInstruction *is_comptime) {
2982 IrInstructionCheckRuntimeScope *instruction = ir_build_instruction<IrInstructionCheckRuntimeScope>(irb, scope, source_node);
2983 instruction->scope_is_comptime = scope_is_comptime;
2984 instruction->is_comptime = is_comptime;
2985
2986 ir_ref_instruction(scope_is_comptime, irb->current_basic_block);
2987 ir_ref_instruction(is_comptime, irb->current_basic_block);
2988
2989 return &instruction->base;
2990}
2991
29772992static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) {
29782993 results[ReturnKindUnconditional] = 0;
29792994 results[ReturnKindError] = 0;
......@@ -2999,6 +3014,7 @@ static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_sco
29993014 case ScopeIdLoop:
30003015 case ScopeIdSuspend:
30013016 case ScopeIdCompTime:
3017 case ScopeIdRuntime:
30023018 scope = scope->parent;
30033019 continue;
30043020 case ScopeIdDeferExpr:
......@@ -3051,6 +3067,7 @@ static bool ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *o
30513067 case ScopeIdLoop:
30523068 case ScopeIdSuspend:
30533069 case ScopeIdCompTime:
3070 case ScopeIdRuntime:
30543071 scope = scope->parent;
30553072 continue;
30563073 case ScopeIdDeferExpr:
......@@ -4980,7 +4997,8 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode
49804997
49814998 ir_set_cursor_at_end_and_append_block(irb, then_block);
49824999
4983 IrInstruction *then_expr_result = ir_gen_node(irb, then_node, scope);
5000 Scope *subexpr_scope = create_runtime_scope(node, scope, is_comptime);
5001 IrInstruction *then_expr_result = ir_gen_node(irb, then_node, subexpr_scope);
49845002 if (then_expr_result == irb->codegen->invalid_instruction)
49855003 return then_expr_result;
49865004 IrBasicBlock *after_then_block = irb->current_basic_block;
......@@ -4990,7 +5008,7 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode
49905008 ir_set_cursor_at_end_and_append_block(irb, else_block);
49915009 IrInstruction *else_expr_result;
49925010 if (else_node) {
4993 else_expr_result = ir_gen_node(irb, else_node, scope);
5011 else_expr_result = ir_gen_node(irb, else_node, subexpr_scope);
49945012 if (else_expr_result == irb->codegen->invalid_instruction)
49955013 return else_expr_result;
49965014 } else {
......@@ -5271,6 +5289,7 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
52715289 ir_should_inline(irb->exec, scope) || node->data.while_expr.is_inline);
52725290 ir_build_br(irb, scope, node, cond_block, is_comptime);
52735291
5292 Scope *subexpr_scope = create_runtime_scope(node, scope, is_comptime);
52745293 Buf *var_symbol = node->data.while_expr.var_symbol;
52755294 Buf *err_symbol = node->data.while_expr.err_symbol;
52765295 if (err_symbol != nullptr) {
......@@ -5281,13 +5300,13 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
52815300 VariableTableEntry *payload_var;
52825301 if (var_symbol) {
52835302 // TODO make it an error to write to payload variable
5284 payload_var = ir_create_var(irb, symbol_node, scope, var_symbol,
5303 payload_var = ir_create_var(irb, symbol_node, subexpr_scope, var_symbol,
52855304 true, false, false, is_comptime);
52865305 payload_scope = payload_var->child_scope;
52875306 } else {
5288 payload_scope = scope;
5307 payload_scope = subexpr_scope;
52895308 }
5290 IrInstruction *err_val_ptr = ir_gen_node_extra(irb, node->data.while_expr.condition, scope, LValPtr);
5309 IrInstruction *err_val_ptr = ir_gen_node_extra(irb, node->data.while_expr.condition, subexpr_scope, LValPtr);
52915310 if (err_val_ptr == irb->codegen->invalid_instruction)
52925311 return err_val_ptr;
52935312 IrInstruction *err_val = ir_build_load_ptr(irb, scope, node->data.while_expr.condition, err_val_ptr);
......@@ -5367,12 +5386,14 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
53675386 return ir_build_phi(irb, scope, node, incoming_blocks.length, incoming_blocks.items, incoming_values.items);
53685387 } else if (var_symbol != nullptr) {
53695388 ir_set_cursor_at_end_and_append_block(irb, cond_block);
5389 Scope *subexpr_scope = create_runtime_scope(node, scope, is_comptime);
53705390 // TODO make it an error to write to payload variable
53715391 AstNode *symbol_node = node; // TODO make more accurate
5372 VariableTableEntry *payload_var = ir_create_var(irb, symbol_node, scope, var_symbol,
5392
5393 VariableTableEntry *payload_var = ir_create_var(irb, symbol_node, subexpr_scope, var_symbol,
53735394 true, false, false, is_comptime);
53745395 Scope *child_scope = payload_var->child_scope;
5375 IrInstruction *maybe_val_ptr = ir_gen_node_extra(irb, node->data.while_expr.condition, scope, LValPtr);
5396 IrInstruction *maybe_val_ptr = ir_gen_node_extra(irb, node->data.while_expr.condition, subexpr_scope, LValPtr);
53765397 if (maybe_val_ptr == irb->codegen->invalid_instruction)
53775398 return maybe_val_ptr;
53785399 IrInstruction *maybe_val = ir_build_load_ptr(irb, scope, node->data.while_expr.condition, maybe_val_ptr);
......@@ -5456,7 +5477,9 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
54565477 ZigList<IrInstruction *> incoming_values = {0};
54575478 ZigList<IrBasicBlock *> incoming_blocks = {0};
54585479
5459 ScopeLoop *loop_scope = create_loop_scope(node, scope);
5480 Scope *subexpr_scope = create_runtime_scope(node, scope, is_comptime);
5481
5482 ScopeLoop *loop_scope = create_loop_scope(node, subexpr_scope);
54605483 loop_scope->break_block = end_block;
54615484 loop_scope->continue_block = continue_block;
54625485 loop_scope->is_comptime = is_comptime;
......@@ -5474,7 +5497,7 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
54745497
54755498 if (continue_expr_node) {
54765499 ir_set_cursor_at_end_and_append_block(irb, continue_block);
5477 IrInstruction *expr_result = ir_gen_node(irb, continue_expr_node, scope);
5500 IrInstruction *expr_result = ir_gen_node(irb, continue_expr_node, subexpr_scope);
54785501 if (expr_result == irb->codegen->invalid_instruction)
54795502 return expr_result;
54805503 if (!instr_is_unreachable(expr_result))
......@@ -5485,7 +5508,7 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
54855508 if (else_node) {
54865509 ir_set_cursor_at_end_and_append_block(irb, else_block);
54875510
5488 else_result = ir_gen_node(irb, else_node, scope);
5511 else_result = ir_gen_node(irb, else_node, subexpr_scope);
54895512 if (else_result == irb->codegen->invalid_instruction)
54905513 return else_result;
54915514 if (!instr_is_unreachable(else_result))
......@@ -5828,20 +5851,21 @@ static IrInstruction *ir_gen_test_expr(IrBuilder *irb, Scope *scope, AstNode *no
58285851
58295852 ir_set_cursor_at_end_and_append_block(irb, then_block);
58305853
5854 Scope *subexpr_scope = create_runtime_scope(node, scope, is_comptime);
58315855 Scope *var_scope;
58325856 if (var_symbol) {
58335857 IrInstruction *var_type = nullptr;
58345858 bool is_shadowable = false;
58355859 bool is_const = true;
5836 VariableTableEntry *var = ir_create_var(irb, node, scope,
5860 VariableTableEntry *var = ir_create_var(irb, node, subexpr_scope,
58375861 var_symbol, is_const, is_const, is_shadowable, is_comptime);
58385862
5839 IrInstruction *var_ptr_value = ir_build_unwrap_maybe(irb, scope, node, maybe_val_ptr, false);
5840 IrInstruction *var_value = var_is_ptr ? var_ptr_value : ir_build_load_ptr(irb, scope, node, var_ptr_value);
5841 ir_build_var_decl(irb, scope, node, var, var_type, nullptr, var_value);
5863 IrInstruction *var_ptr_value = ir_build_unwrap_maybe(irb, subexpr_scope, node, maybe_val_ptr, false);
5864 IrInstruction *var_value = var_is_ptr ? var_ptr_value : ir_build_load_ptr(irb, subexpr_scope, node, var_ptr_value);
5865 ir_build_var_decl(irb, subexpr_scope, node, var, var_type, nullptr, var_value);
58425866 var_scope = var->child_scope;
58435867 } else {
5844 var_scope = scope;
5868 var_scope = subexpr_scope;
58455869 }
58465870 IrInstruction *then_expr_result = ir_gen_node(irb, then_node, var_scope);
58475871 if (then_expr_result == irb->codegen->invalid_instruction)
......@@ -5853,7 +5877,7 @@ static IrInstruction *ir_gen_test_expr(IrBuilder *irb, Scope *scope, AstNode *no
58535877 ir_set_cursor_at_end_and_append_block(irb, else_block);
58545878 IrInstruction *else_expr_result;
58555879 if (else_node) {
5856 else_expr_result = ir_gen_node(irb, else_node, scope);
5880 else_expr_result = ir_gen_node(irb, else_node, subexpr_scope);
58575881 if (else_expr_result == irb->codegen->invalid_instruction)
58585882 return else_expr_result;
58595883 } else {
......@@ -5902,20 +5926,21 @@ static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *
59025926
59035927 ir_set_cursor_at_end_and_append_block(irb, ok_block);
59045928
5929 Scope *subexpr_scope = create_runtime_scope(node, scope, is_comptime);
59055930 Scope *var_scope;
59065931 if (var_symbol) {
59075932 IrInstruction *var_type = nullptr;
59085933 bool is_shadowable = false;
5909 IrInstruction *var_is_comptime = force_comptime ? ir_build_const_bool(irb, scope, node, true) : ir_build_test_comptime(irb, scope, node, err_val);
5910 VariableTableEntry *var = ir_create_var(irb, node, scope,
5934 IrInstruction *var_is_comptime = force_comptime ? ir_build_const_bool(irb, subexpr_scope, node, true) : ir_build_test_comptime(irb, subexpr_scope, node, err_val);
5935 VariableTableEntry *var = ir_create_var(irb, node, subexpr_scope,
59115936 var_symbol, var_is_const, var_is_const, is_shadowable, var_is_comptime);
59125937
5913 IrInstruction *var_ptr_value = ir_build_unwrap_err_payload(irb, scope, node, err_val_ptr, false);
5914 IrInstruction *var_value = var_is_ptr ? var_ptr_value : ir_build_load_ptr(irb, scope, node, var_ptr_value);
5915 ir_build_var_decl(irb, scope, node, var, var_type, nullptr, var_value);
5938 IrInstruction *var_ptr_value = ir_build_unwrap_err_payload(irb, subexpr_scope, node, err_val_ptr, false);
5939 IrInstruction *var_value = var_is_ptr ? var_ptr_value : ir_build_load_ptr(irb, subexpr_scope, node, var_ptr_value);
5940 ir_build_var_decl(irb, subexpr_scope, node, var, var_type, nullptr, var_value);
59165941 var_scope = var->child_scope;
59175942 } else {
5918 var_scope = scope;
5943 var_scope = subexpr_scope;
59195944 }
59205945 IrInstruction *then_expr_result = ir_gen_node(irb, then_node, var_scope);
59215946 if (then_expr_result == irb->codegen->invalid_instruction)
......@@ -5933,14 +5958,14 @@ static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *
59335958 IrInstruction *var_type = nullptr;
59345959 bool is_shadowable = false;
59355960 bool is_const = true;
5936 VariableTableEntry *var = ir_create_var(irb, node, scope,
5961 VariableTableEntry *var = ir_create_var(irb, node, subexpr_scope,
59375962 err_symbol, is_const, is_const, is_shadowable, is_comptime);
59385963
5939 IrInstruction *var_value = ir_build_unwrap_err_code(irb, scope, node, err_val_ptr);
5940 ir_build_var_decl(irb, scope, node, var, var_type, nullptr, var_value);
5964 IrInstruction *var_value = ir_build_unwrap_err_code(irb, subexpr_scope, node, err_val_ptr);
5965 ir_build_var_decl(irb, subexpr_scope, node, var, var_type, nullptr, var_value);
59415966 err_var_scope = var->child_scope;
59425967 } else {
5943 err_var_scope = scope;
5968 err_var_scope = subexpr_scope;
59445969 }
59455970 else_expr_result = ir_gen_node(irb, else_node, err_var_scope);
59465971 if (else_expr_result == irb->codegen->invalid_instruction)
......@@ -6037,6 +6062,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
60376062 ZigList<IrInstructionCheckSwitchProngsRange> check_ranges = {0};
60386063
60396064 // First do the else and the ranges
6065 Scope *subexpr_scope = create_runtime_scope(node, scope, is_comptime);
60406066 Scope *comptime_scope = create_comptime_scope(node, scope);
60416067 AstNode *else_prong = nullptr;
60426068 for (size_t prong_i = 0; prong_i < prong_count; prong_i += 1) {
......@@ -6054,7 +6080,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
60546080
60556081 IrBasicBlock *prev_block = irb->current_basic_block;
60566082 ir_set_cursor_at_end_and_append_block(irb, else_block);
6057 if (!ir_gen_switch_prong_expr(irb, scope, node, prong_node, end_block,
6083 if (!ir_gen_switch_prong_expr(irb, subexpr_scope, node, prong_node, end_block,
60586084 is_comptime, var_is_comptime, target_value_ptr, nullptr, &incoming_blocks, &incoming_values))
60596085 {
60606086 return irb->codegen->invalid_instruction;
......@@ -6121,7 +6147,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
61216147 range_block_no, is_comptime));
61226148
61236149 ir_set_cursor_at_end_and_append_block(irb, range_block_yes);
6124 if (!ir_gen_switch_prong_expr(irb, scope, node, prong_node, end_block,
6150 if (!ir_gen_switch_prong_expr(irb, subexpr_scope, node, prong_node, end_block,
61256151 is_comptime, var_is_comptime, target_value_ptr, nullptr, &incoming_blocks, &incoming_values))
61266152 {
61276153 return irb->codegen->invalid_instruction;
......@@ -6165,7 +6191,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
61656191
61666192 IrBasicBlock *prev_block = irb->current_basic_block;
61676193 ir_set_cursor_at_end_and_append_block(irb, prong_block);
6168 if (!ir_gen_switch_prong_expr(irb, scope, node, prong_node, end_block,
6194 if (!ir_gen_switch_prong_expr(irb, subexpr_scope, node, prong_node, end_block,
61696195 is_comptime, var_is_comptime, target_value_ptr, only_item_value, &incoming_blocks, &incoming_values))
61706196 {
61716197 return irb->codegen->invalid_instruction;
......@@ -6308,6 +6334,8 @@ static IrInstruction *ir_gen_continue(IrBuilder *irb, Scope *continue_scope, Ast
63086334 // * defer expression scope => error, cannot break out of defer expression
63096335 // * loop scope => OK
63106336
6337 ZigList<ScopeRuntime *> runtime_scopes = {};
6338
63116339 Scope *search_scope = continue_scope;
63126340 ScopeLoop *loop_scope;
63136341 for (;;) {
......@@ -6330,6 +6358,9 @@ static IrInstruction *ir_gen_continue(IrBuilder *irb, Scope *continue_scope, Ast
63306358 loop_scope = this_loop_scope;
63316359 break;
63326360 }
6361 } else if (search_scope->id == ScopeIdRuntime) {
6362 ScopeRuntime *scope_runtime = (ScopeRuntime *)search_scope;
6363 runtime_scopes.append(scope_runtime);
63336364 }
63346365 search_scope = search_scope->parent;
63356366 }
......@@ -6341,6 +6372,11 @@ static IrInstruction *ir_gen_continue(IrBuilder *irb, Scope *continue_scope, Ast
63416372 is_comptime = loop_scope->is_comptime;
63426373 }
63436374
6375 for (size_t i = 0; i < runtime_scopes.length; i += 1) {
6376 ScopeRuntime *scope_runtime = runtime_scopes.at(i);
6377 ir_mark_gen(ir_build_check_runtime_scope(irb, continue_scope, node, scope_runtime->is_comptime, is_comptime));
6378 }
6379
63446380 IrBasicBlock *dest_block = loop_scope->continue_block;
63456381 ir_gen_defers_for_block(irb, continue_scope, dest_block->scope, false);
63466382 return ir_mark_gen(ir_build_br(irb, continue_scope, node, dest_block, is_comptime));
......@@ -20910,6 +20946,29 @@ static TypeTableEntry *ir_analyze_instruction_int_to_enum(IrAnalyze *ira, IrInst
2091020946 return result->value.type;
2091120947}
2091220948
20949static TypeTableEntry *ir_analyze_instruction_check_runtime_scope(IrAnalyze *ira, IrInstructionCheckRuntimeScope *instruction) {
20950 IrInstruction *block_comptime_inst = instruction->scope_is_comptime->other;
20951 bool scope_is_comptime;
20952 if (!ir_resolve_bool(ira, block_comptime_inst, &scope_is_comptime))
20953 return ira->codegen->builtin_types.entry_invalid;
20954
20955 IrInstruction *is_comptime_inst = instruction->is_comptime->other;
20956 bool is_comptime;
20957 if (!ir_resolve_bool(ira, is_comptime_inst, &is_comptime))
20958 return ira->codegen->builtin_types.entry_invalid;
20959
20960 if (!scope_is_comptime && is_comptime) {
20961 ErrorMsg *msg = ir_add_error(ira, &instruction->base,
20962 buf_sprintf("comptime control flow inside runtime block"));
20963 add_error_note(ira->codegen, msg, block_comptime_inst->source_node,
20964 buf_sprintf("runtime block created here"));
20965 return ira->codegen->builtin_types.entry_invalid;
20966 }
20967
20968 ir_build_const_from(ira, &instruction->base);
20969 return ira->codegen->builtin_types.entry_void;
20970}
20971
2091320972static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) {
2091420973 switch (instruction->id) {
2091520974 case IrInstructionIdInvalid:
......@@ -21186,6 +21245,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
2118621245 return ir_analyze_instruction_int_to_enum(ira, (IrInstructionIntToEnum *)instruction);
2118721246 case IrInstructionIdEnumToInt:
2118821247 return ir_analyze_instruction_enum_to_int(ira, (IrInstructionEnumToInt *)instruction);
21248 case IrInstructionIdCheckRuntimeScope:
21249 return ir_analyze_instruction_check_runtime_scope(ira, (IrInstructionCheckRuntimeScope *)instruction);
2118921250 }
2119021251 zig_unreachable();
2119121252}
......@@ -21301,6 +21362,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2130121362 case IrInstructionIdOverflowOp: // TODO when we support multiple returns this can be side effect free
2130221363 case IrInstructionIdCheckSwitchProngs:
2130321364 case IrInstructionIdCheckStatementIsVoid:
21365 case IrInstructionIdCheckRuntimeScope:
2130421366 case IrInstructionIdPanic:
2130521367 case IrInstructionIdSetEvalBranchQuota:
2130621368 case IrInstructionIdPtrType:
src/ir_print.cpp+11
......@@ -957,6 +957,14 @@ static void ir_print_enum_to_int(IrPrint *irp, IrInstructionEnumToInt *instructi
957957 fprintf(irp->f, ")");
958958}
959959
960static void ir_print_check_runtime_scope(IrPrint *irp, IrInstructionCheckRuntimeScope *instruction) {
961 fprintf(irp->f, "@checkRuntimeScope(");
962 ir_print_other_instruction(irp, instruction->scope_is_comptime);
963 fprintf(irp->f, ",");
964 ir_print_other_instruction(irp, instruction->is_comptime);
965 fprintf(irp->f, ")");
966}
967
960968static void ir_print_int_to_err(IrPrint *irp, IrInstructionIntToErr *instruction) {
961969 fprintf(irp->f, "inttoerr ");
962970 ir_print_other_instruction(irp, instruction->target);
......@@ -1749,6 +1757,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
17491757 case IrInstructionIdEnumToInt:
17501758 ir_print_enum_to_int(irp, (IrInstructionEnumToInt *)instruction);
17511759 break;
1760 case IrInstructionIdCheckRuntimeScope:
1761 ir_print_check_runtime_scope(irp, (IrInstructionCheckRuntimeScope *)instruction);
1762 break;
17521763 }
17531764 fprintf(irp->f, "\n");
17541765}
test/compile_errors.zig+110
......@@ -1,6 +1,116 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: *tests.CompileErrorContext) void {
4 cases.add(
5 "comptime continue inside runtime switch",
6 \\export fn entry() void {
7 \\ var p: i32 = undefined;
8 \\ comptime var q = true;
9 \\ inline while (q) {
10 \\ switch (p) {
11 \\ 11 => continue,
12 \\ else => {},
13 \\ }
14 \\ q = false;
15 \\ }
16 \\}
17 ,
18 ".tmp_source.zig:6:19: error: comptime control flow inside runtime block",
19 ".tmp_source.zig:5:9: note: runtime block created here",
20 );
21
22 cases.add(
23 "comptime continue inside runtime while error",
24 \\export fn entry() void {
25 \\ var p: error!usize = undefined;
26 \\ comptime var q = true;
27 \\ outer: inline while (q) {
28 \\ while (p) |_| {
29 \\ continue :outer;
30 \\ } else |_| {}
31 \\ q = false;
32 \\ }
33 \\}
34 ,
35 ".tmp_source.zig:6:13: error: comptime control flow inside runtime block",
36 ".tmp_source.zig:5:9: note: runtime block created here",
37 );
38
39 cases.add(
40 "comptime continue inside runtime while optional",
41 \\export fn entry() void {
42 \\ var p: ?usize = undefined;
43 \\ comptime var q = true;
44 \\ outer: inline while (q) {
45 \\ while (p) |_| continue :outer;
46 \\ q = false;
47 \\ }
48 \\}
49 ,
50 ".tmp_source.zig:5:23: error: comptime control flow inside runtime block",
51 ".tmp_source.zig:5:9: note: runtime block created here",
52 );
53
54 cases.add(
55 "comptime continue inside runtime while bool",
56 \\export fn entry() void {
57 \\ var p: usize = undefined;
58 \\ comptime var q = true;
59 \\ outer: inline while (q) {
60 \\ while (p == 11) continue :outer;
61 \\ q = false;
62 \\ }
63 \\}
64 ,
65 ".tmp_source.zig:5:25: error: comptime control flow inside runtime block",
66 ".tmp_source.zig:5:9: note: runtime block created here",
67 );
68
69 cases.add(
70 "comptime continue inside runtime if error",
71 \\export fn entry() void {
72 \\ var p: error!i32 = undefined;
73 \\ comptime var q = true;
74 \\ inline while (q) {
75 \\ if (p) |_| continue else |_| {}
76 \\ q = false;
77 \\ }
78 \\}
79 ,
80 ".tmp_source.zig:5:20: error: comptime control flow inside runtime block",
81 ".tmp_source.zig:5:9: note: runtime block created here",
82 );
83
84 cases.add(
85 "comptime continue inside runtime if optional",
86 \\export fn entry() void {
87 \\ var p: ?i32 = undefined;
88 \\ comptime var q = true;
89 \\ inline while (q) {
90 \\ if (p) |_| continue;
91 \\ q = false;
92 \\ }
93 \\}
94 ,
95 ".tmp_source.zig:5:20: error: comptime control flow inside runtime block",
96 ".tmp_source.zig:5:9: note: runtime block created here",
97 );
98
99 cases.add(
100 "comptime continue inside runtime if bool",
101 \\export fn entry() void {
102 \\ var p: usize = undefined;
103 \\ comptime var q = true;
104 \\ inline while (q) {
105 \\ if (p == 11) continue;
106 \\ q = false;
107 \\ }
108 \\}
109 ,
110 ".tmp_source.zig:5:22: error: comptime control flow inside runtime block",
111 ".tmp_source.zig:5:9: note: runtime block created here",
112 );
113
4114 cases.add(
5115 "switch with invalid expression parameter",
6116 \\export fn entry() void {