diff --git a/src/analyze.cpp b/src/analyze.cpp index 23e798a3ef..d6485d0312 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 f4c9971c45..43be4c4f09 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 8d7183990b..86beb173b0 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; +}