mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
can't declare unreachable variables
This commit is contained in:
parent
90565a5109
commit
09a78d6235
2 changed files with 29 additions and 0 deletions
|
|
@ -421,9 +421,17 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
|
||||||
|
|
||||||
TypeTableEntry *explicit_type = variable_declaration->type != nullptr ?
|
TypeTableEntry *explicit_type = variable_declaration->type != nullptr ?
|
||||||
resolve_type(g, 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 ?
|
TypeTableEntry *implicit_type = variable_declaration->expr != nullptr ?
|
||||||
analyze_expression(g, import, context, explicit_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) {
|
if (implicit_type == nullptr) {
|
||||||
add_node_error(g, node, buf_sprintf("initial values are required for variable declaration."));
|
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);
|
assert(param_decl->type->type == NodeTypeType);
|
||||||
TypeTableEntry *type = param_decl->type->codegen_node->data.type_node.entry;
|
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);
|
LocalVariableTableEntry *variable_entry = allocate<LocalVariableTableEntry>(1);
|
||||||
buf_init_from_buf(&variable_entry->name, ¶m_decl->name);
|
buf_init_from_buf(&variable_entry->name, ¶m_decl->name);
|
||||||
variable_entry->type = type;
|
variable_entry->type = type;
|
||||||
|
|
|
||||||
|
|
@ -404,6 +404,22 @@ fn f() {
|
||||||
}
|
}
|
||||||
)SOURCE", 1, ".tmp_source.zig:3:9: error: type mismatch. expected bool. got i32");
|
)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) {
|
static void print_compiler_invocation(TestCase *test_case, Buf *zig_stderr) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue