Commit fc53708dc0

Andrew Kelley <superjoe30@gmail.com>
2017-01-12 00:06:21
better error message for unable to eval const expr
1 parent 7493af5
Changed files (2)
src/ir.cpp
@@ -5325,15 +5325,19 @@ static void add_call_stack_errors(CodeGen *codegen, IrExecutable *exec, ErrorMsg
     add_call_stack_errors(codegen, exec->parent_exec, err_msg, limit - 1);
 }
 
-static ErrorMsg *ir_add_error_node(IrAnalyze *ira, AstNode *source_node, Buf *msg) {
-    ira->new_irb.exec->invalid = true;
-    ErrorMsg *err_msg = add_node_error(ira->codegen, source_node, msg);
-    if (ira->new_irb.exec->parent_exec) {
-        add_call_stack_errors(ira->codegen, ira->new_irb.exec, err_msg, 10);
+static ErrorMsg *exec_add_error_node(CodeGen *codegen, IrExecutable *exec, AstNode *source_node, Buf *msg) {
+    exec->invalid = true;
+    ErrorMsg *err_msg = add_node_error(codegen, source_node, msg);
+    if (exec->parent_exec) {
+        add_call_stack_errors(codegen, exec, err_msg, 10);
     }
     return err_msg;
 }
 
+static ErrorMsg *ir_add_error_node(IrAnalyze *ira, AstNode *source_node, Buf *msg) {
+    return exec_add_error_node(ira->codegen, ira->new_irb.exec, source_node, msg);
+}
+
 static ErrorMsg *ir_add_error(IrAnalyze *ira, IrInstruction *source_instruction, Buf *msg) {
     return ir_add_error_node(ira, source_instruction->source_node, msg);
 }
@@ -5345,10 +5349,7 @@ static void ir_add_typedef_err_note(IrAnalyze *ira, ErrorMsg *msg, TypeTableEntr
     }
 }
 
-static IrInstruction *ir_exec_const_result(IrExecutable *exec) {
-    if (exec->basic_block_list.length != 1)
-        return nullptr;
-
+static IrInstruction *ir_exec_const_result(CodeGen *codegen, IrExecutable *exec) {
     IrBasicBlock *bb = exec->basic_block_list.at(0);
     for (size_t i = 0; i < bb->instruction_list.length; i += 1) {
         IrInstruction *instruction = bb->instruction_list.at(i);
@@ -5356,14 +5357,18 @@ static IrInstruction *ir_exec_const_result(IrExecutable *exec) {
             IrInstructionReturn *ret_inst = (IrInstructionReturn *)instruction;
             IrInstruction *value = ret_inst->value;
             if (value->value.special == ConstValSpecialRuntime) {
-                return nullptr;
+                exec_add_error_node(codegen, exec, value->source_node,
+                        buf_sprintf("unable to evaluate constant expression"));
+                return codegen->invalid_instruction;
             }
             return value;
         } else if (ir_has_side_effects(instruction)) {
-            return nullptr;
+            exec_add_error_node(codegen, exec, instruction->source_node,
+                    buf_sprintf("unable to evaluate constant expression"));
+            return codegen->invalid_instruction;
         }
     }
-    return nullptr;
+    zig_unreachable();
 }
 
 static bool ir_emit_global_runtime_side_effect(IrAnalyze *ira, IrInstruction *source_instruction) {
@@ -5968,13 +5973,7 @@ IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node
         fprintf(stderr, "}\n");
     }
 
-    IrInstruction *result = ir_exec_const_result(&analyzed_executable);
-    if (!result) {
-        add_node_error(codegen, source_node, buf_sprintf("unable to evaluate constant expression"));
-        return codegen->invalid_instruction;
-    }
-
-    return result;
+    return ir_exec_const_result(codegen, &analyzed_executable);
 }
 
 static TypeTableEntry *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {
test/run_tests.cpp
@@ -1128,7 +1128,10 @@ const Foo = struct {
 };
 var global_var: usize = 1;
 fn get() -> usize { global_var }
-    )SOURCE", 1, ".tmp_source.zig:3:12: error: unable to evaluate constant expression");
+    )SOURCE", 3,
+            ".tmp_source.zig:6:21: error: unable to evaluate constant expression",
+            ".tmp_source.zig:3:12: note: called from here",
+            ".tmp_source.zig:3:8: note: called from here");
 
 
     add_compile_fail_case("unnecessary if statement", R"SOURCE(
@@ -1274,7 +1277,9 @@ fn get_it() -> Foo {
 }
 var global_side_effect = false;
 
-    )SOURCE", 1, ".tmp_source.zig:5:17: error: unable to evaluate constant expression");
+    )SOURCE", 2,
+            ".tmp_source.zig:7:24: error: unable to evaluate constant expression",
+            ".tmp_source.zig:5:17: note: called from here");
 
     add_compile_fail_case("undeclared identifier error should mark fn as impure", R"SOURCE(
 fn foo() {
@@ -1442,10 +1447,9 @@ fn function_with_return_type_type() {
     list.length = 10;
 }
 
-    )SOURCE", 3,
-            ".tmp_source.zig:3:5: error: failed to evaluate function at compile time",
-            ".tmp_source.zig:4:5: note: unable to evaluate this expression at compile time",
-            ".tmp_source.zig:3:32: note: required to be compile-time function because of return type 'type'");
+    )SOURCE", 2,
+            ".tmp_source.zig:4:7: error: unable to evaluate constant expression",
+            ".tmp_source.zig:17:19: note: called from here");
 
     add_compile_fail_case("bogus method call on slice", R"SOURCE(
 fn f(m: []const u8) {