authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-01-11 18:06:21-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-01-11 18:06:21-05:00
logfc53708dc0e0395e0360a4d62ed75ce66ad3c58e
treee7a08171d2907959cf178ce7b127994a257ada30
parent7493af59538fc59f6180c83a188e1b28bdf33eb4

better error message for unable to eval const expr


2 files changed, 28 insertions(+), 25 deletions(-)

src/ir.cpp+18-19
......@@ -5325,15 +5325,19 @@ static void add_call_stack_errors(CodeGen *codegen, IrExecutable *exec, ErrorMsg
53255325 add_call_stack_errors(codegen, exec->parent_exec, err_msg, limit - 1);
53265326}
53275327
5328static ErrorMsg *ir_add_error_node(IrAnalyze *ira, AstNode *source_node, Buf *msg) {
5329 ira->new_irb.exec->invalid = true;
5330 ErrorMsg *err_msg = add_node_error(ira->codegen, source_node, msg);
5331 if (ira->new_irb.exec->parent_exec) {
5332 add_call_stack_errors(ira->codegen, ira->new_irb.exec, err_msg, 10);
5328static ErrorMsg *exec_add_error_node(CodeGen *codegen, IrExecutable *exec, AstNode *source_node, Buf *msg) {
5329 exec->invalid = true;
5330 ErrorMsg *err_msg = add_node_error(codegen, source_node, msg);
5331 if (exec->parent_exec) {
5332 add_call_stack_errors(codegen, exec, err_msg, 10);
53335333 }
53345334 return err_msg;
53355335}
53365336
5337static ErrorMsg *ir_add_error_node(IrAnalyze *ira, AstNode *source_node, Buf *msg) {
5338 return exec_add_error_node(ira->codegen, ira->new_irb.exec, source_node, msg);
5339}
5340
53375341static ErrorMsg *ir_add_error(IrAnalyze *ira, IrInstruction *source_instruction, Buf *msg) {
53385342 return ir_add_error_node(ira, source_instruction->source_node, msg);
53395343}
......@@ -5345,10 +5349,7 @@ static void ir_add_typedef_err_note(IrAnalyze *ira, ErrorMsg *msg, TypeTableEntr
53455349 }
53465350}
53475351
5348static IrInstruction *ir_exec_const_result(IrExecutable *exec) {
5349 if (exec->basic_block_list.length != 1)
5350 return nullptr;
5351
5352static IrInstruction *ir_exec_const_result(CodeGen *codegen, IrExecutable *exec) {
53525353 IrBasicBlock *bb = exec->basic_block_list.at(0);
53535354 for (size_t i = 0; i < bb->instruction_list.length; i += 1) {
53545355 IrInstruction *instruction = bb->instruction_list.at(i);
......@@ -5356,14 +5357,18 @@ static IrInstruction *ir_exec_const_result(IrExecutable *exec) {
53565357 IrInstructionReturn *ret_inst = (IrInstructionReturn *)instruction;
53575358 IrInstruction *value = ret_inst->value;
53585359 if (value->value.special == ConstValSpecialRuntime) {
5359 return nullptr;
5360 exec_add_error_node(codegen, exec, value->source_node,
5361 buf_sprintf("unable to evaluate constant expression"));
5362 return codegen->invalid_instruction;
53605363 }
53615364 return value;
53625365 } else if (ir_has_side_effects(instruction)) {
5363 return nullptr;
5366 exec_add_error_node(codegen, exec, instruction->source_node,
5367 buf_sprintf("unable to evaluate constant expression"));
5368 return codegen->invalid_instruction;
53645369 }
53655370 }
5366 return nullptr;
5371 zig_unreachable();
53675372}
53685373
53695374static 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
59685973 fprintf(stderr, "}\n");
59695974 }
59705975
5971 IrInstruction *result = ir_exec_const_result(&analyzed_executable);
5972 if (!result) {
5973 add_node_error(codegen, source_node, buf_sprintf("unable to evaluate constant expression"));
5974 return codegen->invalid_instruction;
5975 }
5976
5977 return result;
5976 return ir_exec_const_result(codegen, &analyzed_executable);
59785977}
59795978
59805979static TypeTableEntry *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {
test/run_tests.cpp+10-6
......@@ -1128,7 +1128,10 @@ const Foo = struct {
11281128};
11291129var global_var: usize = 1;
11301130fn get() -> usize { global_var }
1131 )SOURCE", 1, ".tmp_source.zig:3:12: error: unable to evaluate constant expression");
1131 )SOURCE", 3,
1132 ".tmp_source.zig:6:21: error: unable to evaluate constant expression",
1133 ".tmp_source.zig:3:12: note: called from here",
1134 ".tmp_source.zig:3:8: note: called from here");
11321135
11331136
11341137 add_compile_fail_case("unnecessary if statement", R"SOURCE(
......@@ -1274,7 +1277,9 @@ fn get_it() -> Foo {
12741277}
12751278var global_side_effect = false;
12761279
1277 )SOURCE", 1, ".tmp_source.zig:5:17: error: unable to evaluate constant expression");
1280 )SOURCE", 2,
1281 ".tmp_source.zig:7:24: error: unable to evaluate constant expression",
1282 ".tmp_source.zig:5:17: note: called from here");
12781283
12791284 add_compile_fail_case("undeclared identifier error should mark fn as impure", R"SOURCE(
12801285fn foo() {
......@@ -1442,10 +1447,9 @@ fn function_with_return_type_type() {
14421447 list.length = 10;
14431448}
14441449
1445 )SOURCE", 3,
1446 ".tmp_source.zig:3:5: error: failed to evaluate function at compile time",
1447 ".tmp_source.zig:4:5: note: unable to evaluate this expression at compile time",
1448 ".tmp_source.zig:3:32: note: required to be compile-time function because of return type 'type'");
1450 )SOURCE", 2,
1451 ".tmp_source.zig:4:7: error: unable to evaluate constant expression",
1452 ".tmp_source.zig:17:19: note: called from here");
14491453
14501454 add_compile_fail_case("bogus method call on slice", R"SOURCE(
14511455fn f(m: []const u8) {