Commit 36cf9f0c72

Andrew Kelley <superjoe30@gmail.com>
2016-02-07 23:16:36
error for non const expr in array size outside fn
1 parent 26ea20d
Changed files (2)
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,
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");
 }
 
 //////////////////////////////////////////////////////////////////////////////