diff --git a/src/analyze.cpp b/src/analyze.cpp index be2a181713..3bad09a4d9 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -421,9 +421,17 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import, TypeTableEntry *explicit_type = variable_declaration->type != nullptr ? resolve_type(g, variable_declaration->type) : nullptr; + if (explicit_type == g->builtin_types.entry_unreachable) { + add_node_error(g, variable_declaration->type, + buf_sprintf("variable of type 'unreachable' is not allowed.")); + } TypeTableEntry *implicit_type = variable_declaration->expr != nullptr ? analyze_expression(g, import, context, explicit_type, variable_declaration->expr) : nullptr; + if (implicit_type == g->builtin_types.entry_unreachable) { + add_node_error(g, node, + buf_sprintf("variable initialization is unreachable.")); + } if (implicit_type == nullptr) { 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, assert(param_decl->type->type == NodeTypeType); TypeTableEntry *type = param_decl->type->codegen_node->data.type_node.entry; + if (type == g->builtin_types.entry_unreachable) { + add_node_error(g, param_decl->type, + buf_sprintf("parameter of type 'unreachable' is not allowed.")); + } + LocalVariableTableEntry *variable_entry = allocate(1); buf_init_from_buf(&variable_entry->name, ¶m_decl->name); variable_entry->type = type; diff --git a/test/run_tests.cpp b/test/run_tests.cpp index 7afc761a3f..875e2b94ff 100644 --- a/test/run_tests.cpp +++ b/test/run_tests.cpp @@ -404,6 +404,22 @@ fn f() { } )SOURCE", 1, ".tmp_source.zig:3:9: error: type mismatch. expected bool. got i32"); + add_compile_fail_case("assign unreachable", R"SOURCE( +fn f() { + let a = return; +} + )SOURCE", 1, ".tmp_source.zig:3:5: error: variable initialization is unreachable."); + + add_compile_fail_case("unreachable variable", R"SOURCE( +fn f() { + let a : unreachable = return; +} + )SOURCE", 1, ".tmp_source.zig:3:13: error: variable of type 'unreachable' is not allowed."); + + add_compile_fail_case("unreachable parameter", R"SOURCE( +fn f(a : unreachable) {} + )SOURCE", 1, ".tmp_source.zig:2:10: error: parameter of type 'unreachable' is not allowed."); + } static void print_compiler_invocation(TestCase *test_case, Buf *zig_stderr) {