authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-03-12 00:08:52-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-03-12 00:08:52-04:00
log49c3922037ef0b913466e707d85a4e085f6e9716
tree58904795206468e87acd5ad3dc9c491111d076f6
parentc18059a3ddcaec1bef18180ec797fe41baeac7f3

fix incorrect setEvalBranchQuota compile error

closes #688

3 files changed, 24 insertions(+), 1 deletions(-)

src/all_types.hpp+1
......@@ -49,6 +49,7 @@ struct IrExecutable {
4949 size_t backward_branch_quota;
5050 bool invalid;
5151 bool is_inline;
52 bool is_generic_instantiation;
5253 FnTableEntry *fn_entry;
5354 Buf *c_import_buf;
5455 AstNode *source_node;
src/ir.cpp+2-1
......@@ -12127,6 +12127,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
1212712127 impl_fn->ir_executable.parent_exec = ira->new_irb.exec;
1212812128 impl_fn->analyzed_executable.source_node = call_instruction->base.source_node;
1212912129 impl_fn->analyzed_executable.parent_exec = ira->new_irb.exec;
12130 impl_fn->analyzed_executable.is_generic_instantiation = true;
1213012131
1213112132 ira->codegen->fn_defs.append(impl_fn);
1213212133 }
......@@ -15234,7 +15235,7 @@ static TypeTableEntry *ir_analyze_instruction_type_id(IrAnalyze *ira,
1523415235static TypeTableEntry *ir_analyze_instruction_set_eval_branch_quota(IrAnalyze *ira,
1523515236 IrInstructionSetEvalBranchQuota *instruction)
1523615237{
15237 if (ira->new_irb.exec->parent_exec != nullptr) {
15238 if (ira->new_irb.exec->parent_exec != nullptr && !ira->new_irb.exec->is_generic_instantiation) {
1523815239 ir_add_error(ira, &instruction->base,
1523915240 buf_sprintf("@setEvalBranchQuota must be called from the top of the comptime stack"));
1524015241 return ira->codegen->builtin_types.entry_invalid;
test/cases/eval.zig+21
......@@ -448,3 +448,24 @@ test "comptime function with mutable pointer is not memoized" {
448448fn increment(value: &i32) void {
449449 *value += 1;
450450}
451
452fn generateTable(comptime T: type) [1010]T {
453 var res : [1010]T = undefined;
454 var i : usize = 0;
455 while (i < 1010) : (i += 1) {
456 res[i] = T(i);
457 }
458 return res;
459}
460
461fn doesAlotT(comptime T: type, value: usize) T {
462 @setEvalBranchQuota(5000);
463 const table = comptime blk: {
464 break :blk generateTable(T);
465 };
466 return table[value];
467}
468
469test "@setEvalBranchQuota at same scope as generic function call" {
470 assert(doesAlotT(u32, 2) == 2);
471}