From 6db9be8900bf43632c8a98d91c6a92f30b33500a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 9 Mar 2018 14:20:44 -0500 Subject: don't memoize comptime functions if they can mutate state via parameters closes #639 --- src/ir.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index 67caa84a93..36a6defd9d 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -11830,12 +11830,15 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal return_type = specified_return_type; } - IrInstruction *result; + bool cacheable = fn_eval_cacheable(exec_scope); + IrInstruction *result = nullptr; + if (cacheable) { + auto entry = ira->codegen->memoized_fn_eval_table.maybe_get(exec_scope); + if (entry) + result = entry->value; + } - auto entry = ira->codegen->memoized_fn_eval_table.maybe_get(exec_scope); - if (entry) { - result = entry->value; - } else { + if (result == nullptr) { // Analyze the fn body block like any other constant expression. AstNode *body_node = fn_entry->body_node; result = ir_eval_const_value(ira->codegen, exec_scope, body_node, return_type, @@ -11859,7 +11862,9 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal } } - ira->codegen->memoized_fn_eval_table.put(exec_scope, result); + if (cacheable) { + ira->codegen->memoized_fn_eval_table.put(exec_scope, result); + } if (type_is_invalid(result->value.type)) return ira->codegen->builtin_types.entry_invalid; -- cgit v1.2.3