Commit 94ed9f622a
Changed files (3)
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;
}
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);
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;
+}