authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-12-03 12:38:28-07:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-12-03 12:38:28-07:00
log09a78d6235957001e6b92ea9a83899b18a394ac1
tree2bb859e10b8892d2e3b24db38e3995f42e39fe96
parent90565a5109116f0c689215d28ea14d3f4d046c81

can't declare unreachable variables


2 files changed, 29 insertions(+), 0 deletions(-)

src/analyze.cpp+13
...@@ -421,9 +421,17 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,...@@ -421,9 +421,17 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
421421
422 TypeTableEntry *explicit_type = variable_declaration->type != nullptr ?422 TypeTableEntry *explicit_type = variable_declaration->type != nullptr ?
423 resolve_type(g, variable_declaration->type) : nullptr;423 resolve_type(g, variable_declaration->type) : nullptr;
424 if (explicit_type == g->builtin_types.entry_unreachable) {
425 add_node_error(g, variable_declaration->type,
426 buf_sprintf("variable of type 'unreachable' is not allowed."));
427 }
424428
425 TypeTableEntry *implicit_type = variable_declaration->expr != nullptr ?429 TypeTableEntry *implicit_type = variable_declaration->expr != nullptr ?
426 analyze_expression(g, import, context, explicit_type, variable_declaration->expr) : nullptr;430 analyze_expression(g, import, context, explicit_type, variable_declaration->expr) : nullptr;
431 if (implicit_type == g->builtin_types.entry_unreachable) {
432 add_node_error(g, node,
433 buf_sprintf("variable initialization is unreachable."));
434 }
427435
428 if (implicit_type == nullptr) {436 if (implicit_type == nullptr) {
429 add_node_error(g, node, buf_sprintf("initial values are required for variable declaration."));437 add_node_error(g, node, buf_sprintf("initial values are required for variable declaration."));
...@@ -715,6 +723,11 @@ static void analyze_top_level_declaration(CodeGen *g, ImportTableEntry *import,...@@ -715,6 +723,11 @@ static void analyze_top_level_declaration(CodeGen *g, ImportTableEntry *import,
715 assert(param_decl->type->type == NodeTypeType);723 assert(param_decl->type->type == NodeTypeType);
716 TypeTableEntry *type = param_decl->type->codegen_node->data.type_node.entry;724 TypeTableEntry *type = param_decl->type->codegen_node->data.type_node.entry;
717725
726 if (type == g->builtin_types.entry_unreachable) {
727 add_node_error(g, param_decl->type,
728 buf_sprintf("parameter of type 'unreachable' is not allowed."));
729 }
730
718 LocalVariableTableEntry *variable_entry = allocate<LocalVariableTableEntry>(1);731 LocalVariableTableEntry *variable_entry = allocate<LocalVariableTableEntry>(1);
719 buf_init_from_buf(&variable_entry->name, &param_decl->name);732 buf_init_from_buf(&variable_entry->name, &param_decl->name);
720 variable_entry->type = type;733 variable_entry->type = type;
test/run_tests.cpp+16
...@@ -404,6 +404,22 @@ fn f() {...@@ -404,6 +404,22 @@ fn f() {
404}404}
405 )SOURCE", 1, ".tmp_source.zig:3:9: error: type mismatch. expected bool. got i32");405 )SOURCE", 1, ".tmp_source.zig:3:9: error: type mismatch. expected bool. got i32");
406406
407 add_compile_fail_case("assign unreachable", R"SOURCE(
408fn f() {
409 let a = return;
410}
411 )SOURCE", 1, ".tmp_source.zig:3:5: error: variable initialization is unreachable.");
412
413 add_compile_fail_case("unreachable variable", R"SOURCE(
414fn f() {
415 let a : unreachable = return;
416}
417 )SOURCE", 1, ".tmp_source.zig:3:13: error: variable of type 'unreachable' is not allowed.");
418
419 add_compile_fail_case("unreachable parameter", R"SOURCE(
420fn f(a : unreachable) {}
421 )SOURCE", 1, ".tmp_source.zig:2:10: error: parameter of type 'unreachable' is not allowed.");
422
407}423}
408424
409static void print_compiler_invocation(TestCase *test_case, Buf *zig_stderr) {425static void print_compiler_invocation(TestCase *test_case, Buf *zig_stderr) {