Commit 7bd9c82386
Changed files (2)
src
test
src/analyze.cpp
@@ -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) {
test/run_tests.cpp
@@ -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) {