add compile error for non constant expr global

This commit is contained in:
Andrew Kelley 2016-01-22 16:40:15 -07:00
parent 72fa03bada
commit 7bd9c82386
2 changed files with 14 additions and 0 deletions

View File

@ -2343,6 +2343,13 @@ static VariableTableEntry *analyze_variable_declaration_raw(CodeGen *g, ImportTa
add_node_error(g, source_node, buf_sprintf("variable of type 'type' must be constant"));
implicit_type = g->builtin_types.entry_invalid;
}
if (implicit_type->id != TypeTableEntryIdInvalid && !context->fn_entry) {
ConstExprValue *const_val = &get_resolved_expr(variable_declaration->expr)->const_val;
if (!const_val->ok) {
add_node_error(g, first_executing_node(variable_declaration->expr),
buf_sprintf("global variable initializer requires constant expression"));
}
}
}
if (implicit_type == nullptr && is_const) {

View File

@ -1567,6 +1567,13 @@ fn f() => {
};
}
)SOURCE", 1, ".tmp_source.zig:6:9: error: multiple else prongs in switch expression");
add_compile_fail_case("global variable initializer must be constant expression", R"SOURCE(
extern {
fn foo() i32;
}
const x = foo();
)SOURCE", 1, ".tmp_source.zig:5:11: error: global variable initializer requires constant expression");
}
static void print_compiler_invocation(TestCase *test_case) {