error for non const expr in array size outside fn

This commit is contained in:
Andrew Kelley 2016-02-07 15:16:36 -07:00
parent 26ea20d88f
commit 36cf9f0c72
2 changed files with 12 additions and 1 deletions

View File

@ -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,

View File

@ -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");
}
//////////////////////////////////////////////////////////////////////////////