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...@@ -5325,15 +5325,19 @@ static void add_call_stack_errors(CodeGen *codegen, IrExecutable *exec, ErrorMsg
5325 add_call_stack_errors(codegen, exec->parent_exec, err_msg, limit - 1);5325 add_call_stack_errors(codegen, exec->parent_exec, err_msg, limit - 1);
5326}5326}
53275327
5328static ErrorMsg *ir_add_error_node(IrAnalyze *ira, AstNode *source_node, Buf *msg) {5328static ErrorMsg *exec_add_error_node(CodeGen *codegen, IrExecutable *exec, AstNode *source_node, Buf *msg) {
5329 ira->new_irb.exec->invalid = true;5329 exec->invalid = true;
5330 ErrorMsg *err_msg = add_node_error(ira->codegen, source_node, msg);5330 ErrorMsg *err_msg = add_node_error(codegen, source_node, msg);
5331 if (ira->new_irb.exec->parent_exec) {5331 if (exec->parent_exec) {
5332 add_call_stack_errors(ira->codegen, ira->new_irb.exec, err_msg, 10);5332 add_call_stack_errors(codegen, exec, err_msg, 10);
5333 }5333 }
5334 return err_msg;5334 return err_msg;
5335}5335}
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
5337static ErrorMsg *ir_add_error(IrAnalyze *ira, IrInstruction *source_instruction, Buf *msg) {5341static ErrorMsg *ir_add_error(IrAnalyze *ira, IrInstruction *source_instruction, Buf *msg) {
5338 return ir_add_error_node(ira, source_instruction->source_node, msg);5342 return ir_add_error_node(ira, source_instruction->source_node, msg);
5339}5343}
...@@ -5345,10 +5349,7 @@ static void ir_add_typedef_err_note(IrAnalyze *ira, ErrorMsg *msg, TypeTableEntr...@@ -5345,10 +5349,7 @@ static void ir_add_typedef_err_note(IrAnalyze *ira, ErrorMsg *msg, TypeTableEntr
5345 }5349 }
5346}5350}
53475351
5348static IrInstruction *ir_exec_const_result(IrExecutable *exec) {5352static IrInstruction *ir_exec_const_result(CodeGen *codegen, IrExecutable *exec) {
5349 if (exec->basic_block_list.length != 1)
5350 return nullptr;
5351
5352 IrBasicBlock *bb = exec->basic_block_list.at(0);5353 IrBasicBlock *bb = exec->basic_block_list.at(0);
5353 for (size_t i = 0; i < bb->instruction_list.length; i += 1) {5354 for (size_t i = 0; i < bb->instruction_list.length; i += 1) {
5354 IrInstruction *instruction = bb->instruction_list.at(i);5355 IrInstruction *instruction = bb->instruction_list.at(i);
...@@ -5356,14 +5357,18 @@ static IrInstruction *ir_exec_const_result(IrExecutable *exec) {...@@ -5356,14 +5357,18 @@ static IrInstruction *ir_exec_const_result(IrExecutable *exec) {
5356 IrInstructionReturn *ret_inst = (IrInstructionReturn *)instruction;5357 IrInstructionReturn *ret_inst = (IrInstructionReturn *)instruction;
5357 IrInstruction *value = ret_inst->value;5358 IrInstruction *value = ret_inst->value;
5358 if (value->value.special == ConstValSpecialRuntime) {5359 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;
5360 }5363 }
5361 return value;5364 return value;
5362 } else if (ir_has_side_effects(instruction)) {5365 } 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;
5364 }5369 }
5365 }5370 }
5366 return nullptr;5371 zig_unreachable();
5367}5372}
53685373
5369static bool ir_emit_global_runtime_side_effect(IrAnalyze *ira, IrInstruction *source_instruction) {5374static 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...@@ -5968,13 +5973,7 @@ IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node
5968 fprintf(stderr, "}\n");5973 fprintf(stderr, "}\n");
5969 }5974 }
59705975
5971 IrInstruction *result = ir_exec_const_result(&analyzed_executable);5976 return ir_exec_const_result(codegen, &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;
5978}5977}
59795978
5980static TypeTableEntry *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {5979static TypeTableEntry *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {
test/run_tests.cpp+10-6
...@@ -1128,7 +1128,10 @@ const Foo = struct {...@@ -1128,7 +1128,10 @@ const Foo = struct {
1128};1128};
1129var global_var: usize = 1;1129var global_var: usize = 1;
1130fn get() -> usize { global_var }1130fn 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
1134 add_compile_fail_case("unnecessary if statement", R"SOURCE(1137 add_compile_fail_case("unnecessary if statement", R"SOURCE(
...@@ -1274,7 +1277,9 @@ fn get_it() -> Foo {...@@ -1274,7 +1277,9 @@ fn get_it() -> Foo {
1274}1277}
1275var global_side_effect = false;1278var 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
1279 add_compile_fail_case("undeclared identifier error should mark fn as impure", R"SOURCE(1284 add_compile_fail_case("undeclared identifier error should mark fn as impure", R"SOURCE(
1280fn foo() {1285fn foo() {
...@@ -1442,10 +1447,9 @@ fn function_with_return_type_type() {...@@ -1442,10 +1447,9 @@ fn function_with_return_type_type() {
1442 list.length = 10;1447 list.length = 10;
1443}1448}
14441449
1445 )SOURCE", 3,1450 )SOURCE", 2,
1446 ".tmp_source.zig:3:5: error: failed to evaluate function at compile time",1451 ".tmp_source.zig:4:7: error: unable to evaluate constant expression",
1447 ".tmp_source.zig:4:5: note: unable to evaluate this expression at compile time",1452 ".tmp_source.zig:17:19: note: called from here");
1448 ".tmp_source.zig:3:32: note: required to be compile-time function because of return type 'type'");
14491453
1450 add_compile_fail_case("bogus method call on slice", R"SOURCE(1454 add_compile_fail_case("bogus method call on slice", R"SOURCE(
1451fn f(m: []const u8) {1455fn f(m: []const u8) {