diff --git a/src/analyze.cpp b/src/analyze.cpp index 2b8119ed48..8d66127865 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -3281,9 +3281,13 @@ static TypeTableEntry *analyze_array_type(CodeGen *g, ImportTableEntry *import, return resolve_expr_const_val_as_type(g, node, get_array_type(g, child_type, const_val->data.x_bignum.data.x_uint)); } - } else { + } else if (context->fn_entry) { return resolve_expr_const_val_as_type(g, node, get_slice_type(g, child_type, node->data.array_type.is_const)); + } else { + add_node_error(g, first_executing_node(size_node), + buf_sprintf("unable to evaluate constant expression")); + return g->builtin_types.entry_invalid; } } else { return resolve_expr_const_val_as_type(g, node, diff --git a/test/run_tests.cpp b/test/run_tests.cpp index 318ba36f94..588cd2b591 100644 --- a/test/run_tests.cpp +++ b/test/run_tests.cpp @@ -2027,6 +2027,13 @@ fn a(x: i32) { const y = @const_eval(x); } )SOURCE", 1, ".tmp_source.zig:3:27: error: unable to evaluate constant expression"); + + add_compile_fail_case("non constant expression in array size outside function", R"SOURCE( +struct Foo { + y: [get()]u8, +} +fn get() -> isize { 1 } + )SOURCE", 1, ".tmp_source.zig:3:9: error: unable to evaluate constant expression"); } //////////////////////////////////////////////////////////////////////////////