diff --git a/src/analyze.cpp b/src/analyze.cpp index 23e798a3efbd3d01c7e38fa9072cb12534f4d40a..d6485d0312a6c613cc78947c1c6736660bd73b57 100644 --- 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 index f4c9971c455a5f12d2236e43e2c20f0cf9d57b3e..43be4c4f099d9da3761c8651b86eff60a33346fb 100644 --- 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 index 8d7183990b900f4c9fe4d3c64fce1df1684770a2..86beb173b0fbd3e646251293c81463b0edf9a3b1 100644 --- 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; +}