authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-07 12:38:51-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-07 12:38:51-07:00
log94ed9f622ad4c53a8a28b0e60697edfd409b314f
tree93eb3c827455a7a60cc402cc77a88b22b282624d
parent694cfff23f0fe534fa794a083c1484f14ccd0ab2

blocks with one statement pass constant expression eval


3 files changed, 34 insertions(+), 0 deletions(-)

src/analyze.cpp+12
......@@ -4720,6 +4720,18 @@ static TypeTableEntry *analyze_block_expr(CodeGen *g, ImportTableEntry *import,
47204720 }
47214721 }
47224722 node->data.block.nested_block = child_context;
4723
4724 ConstExprValue *const_val = &node->data.block.resolved_expr.const_val;
4725 if (node->data.block.statements.length == 0) {
4726 const_val->ok = true;
4727 } else if (node->data.block.statements.length == 1) {
4728 AstNode *only_node = node->data.block.statements.at(0);
4729 ConstExprValue *other_const_val = &get_resolved_expr(only_node)->const_val;
4730 if (other_const_val->ok) {
4731 *const_val = *other_const_val;
4732 }
4733 }
4734
47234735 return return_type;
47244736}
47254737
std/rand.zig+4
......@@ -58,6 +58,10 @@ pub struct Rand {
5858 return f32(r.range_u64(0, precision)) / precision;
5959 }
6060
61 pub fn boolean(r: &Rand) -> bool {
62 return (r.get_u32() & 0x1) == 1;
63 }
64
6165 fn generate_numbers(r: &Rand) {
6266 for (r.array) |item, i| {
6367 const y : u32 = (item & 0x80000000) + (r.array[(i + 1) % ARRAY_SIZE] & 0x7fffffff);
test/self_hosted.zig+18
......@@ -215,3 +215,21 @@ fn explicit_cast_maybe_pointers() {
215215 const a: ?&i32 = undefined;
216216 const b: ?&f32 = (?&f32)(a);
217217}
218
219
220#attribute("test")
221fn const_expr_eval_on_single_expr_blocks() {
222 if (const_expr_eval_on_single_expr_blocks_fn(1, true) != 3) unreachable{}
223}
224
225fn const_expr_eval_on_single_expr_blocks_fn(x: i32, b: bool) -> i32 {
226 const literal = 3;
227
228 const result = if (b) {
229 literal
230 } else {
231 x
232 };
233
234 return result;
235}