authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-22 16:40:15-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-22 16:40:15-07:00
log7bd9c8238626998f8016086762483114e5c58f70
tree544bf96dece1861e7d99062a239e5a01927aedb3
parent72fa03badad2e5837f639359a63a75abaa99a479

add compile error for non constant expr global


2 files changed, 14 insertions(+), 0 deletions(-)

src/analyze.cpp+7
...@@ -2343,6 +2343,13 @@ static VariableTableEntry *analyze_variable_declaration_raw(CodeGen *g, ImportTa...@@ -2343,6 +2343,13 @@ static VariableTableEntry *analyze_variable_declaration_raw(CodeGen *g, ImportTa
2343 add_node_error(g, source_node, buf_sprintf("variable of type 'type' must be constant"));2343 add_node_error(g, source_node, buf_sprintf("variable of type 'type' must be constant"));
2344 implicit_type = g->builtin_types.entry_invalid;2344 implicit_type = g->builtin_types.entry_invalid;
2345 }2345 }
2346 if (implicit_type->id != TypeTableEntryIdInvalid && !context->fn_entry) {
2347 ConstExprValue *const_val = &get_resolved_expr(variable_declaration->expr)->const_val;
2348 if (!const_val->ok) {
2349 add_node_error(g, first_executing_node(variable_declaration->expr),
2350 buf_sprintf("global variable initializer requires constant expression"));
2351 }
2352 }
2346 }2353 }
23472354
2348 if (implicit_type == nullptr && is_const) {2355 if (implicit_type == nullptr && is_const) {
test/run_tests.cpp+7
...@@ -1567,6 +1567,13 @@ fn f() => {...@@ -1567,6 +1567,13 @@ fn f() => {
1567 };1567 };
1568}1568}
1569 )SOURCE", 1, ".tmp_source.zig:6:9: error: multiple else prongs in switch expression");1569 )SOURCE", 1, ".tmp_source.zig:6:9: error: multiple else prongs in switch expression");
1570
1571 add_compile_fail_case("global variable initializer must be constant expression", R"SOURCE(
1572extern {
1573 fn foo() i32;
1574}
1575const x = foo();
1576 )SOURCE", 1, ".tmp_source.zig:5:11: error: global variable initializer requires constant expression");
1570}1577}
15711578
1572static void print_compiler_invocation(TestCase *test_case) {1579static void print_compiler_invocation(TestCase *test_case) {