zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 94ed9f622ad4c53a8a28b0e60697edfd409b314f (tree)
parent 694cfff23f0fe534fa794a083c1484f14ccd0ab2
Author: Andrew Kelley <superjoe30@gmail.com>
Date:   Sun,  7 Feb 2016 12:38:51 -0700

blocks with one statement pass constant expression eval

Diffstat:
Msrc/analyze.cpp | 12++++++++++++
Mstd/rand.zig | 4++++
Mtest/self_hosted.zig | 18++++++++++++++++++
3 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/src/analyze.cpp b/src/analyze.cpp @@ -4720,6 +4720,18 @@ static TypeTableEntry *analyze_block_expr(CodeGen *g, ImportTableEntry *import, } } node->data.block.nested_block = child_context; + + ConstExprValue *const_val = &node->data.block.resolved_expr.const_val; + if (node->data.block.statements.length == 0) { + const_val->ok = true; + } else if (node->data.block.statements.length == 1) { + AstNode *only_node = node->data.block.statements.at(0); + ConstExprValue *other_const_val = &get_resolved_expr(only_node)->const_val; + if (other_const_val->ok) { + *const_val = *other_const_val; + } + } + return return_type; } diff --git a/std/rand.zig b/std/rand.zig @@ -58,6 +58,10 @@ pub struct Rand { return f32(r.range_u64(0, precision)) / precision; } + pub fn boolean(r: &Rand) -> bool { + return (r.get_u32() & 0x1) == 1; + } + fn generate_numbers(r: &Rand) { for (r.array) |item, i| { const y : u32 = (item & 0x80000000) + (r.array[(i + 1) % ARRAY_SIZE] & 0x7fffffff); diff --git a/test/self_hosted.zig b/test/self_hosted.zig @@ -215,3 +215,21 @@ fn explicit_cast_maybe_pointers() { const a: ?&i32 = undefined; const b: ?&f32 = (?&f32)(a); } + + +#attribute("test") +fn const_expr_eval_on_single_expr_blocks() { + if (const_expr_eval_on_single_expr_blocks_fn(1, true) != 3) unreachable{} +} + +fn const_expr_eval_on_single_expr_blocks_fn(x: i32, b: bool) -> i32 { + const literal = 3; + + const result = if (b) { + literal + } else { + x + }; + + return result; +}