can't declare unreachable variables

master
Josh Wolfe 2015-12-03 12:38:28 -07:00
parent 90565a5109
commit 09a78d6235
2 changed files with 29 additions and 0 deletions

View File

@ -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<LocalVariableTableEntry>(1);
buf_init_from_buf(&variable_entry->name, &param_decl->name);
variable_entry->type = type;

View File

@ -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) {