| author | |
| committer | |
| log | 94ed9f622ad4c53a8a28b0e60697edfd409b314f |
| tree | 93eb3c827455a7a60cc402cc77a88b22b282624d |
| parent | 694cfff23f0fe534fa794a083c1484f14ccd0ab2 |
3 files changed, 34 insertions(+), 0 deletions(-)
src/analyze.cpp+12| ... | @@ -4720,6 +4720,18 @@ static TypeTableEntry *analyze_block_expr(CodeGen *g, ImportTableEntry *import, | ... | @@ -4720,6 +4720,18 @@ static TypeTableEntry *analyze_block_expr(CodeGen *g, ImportTableEntry *import, |
| 4720 | } | 4720 | } |
| 4721 | } | 4721 | } |
| 4722 | node->data.block.nested_block = child_context; | 4722 | 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 | |||
| 4723 | return return_type; | 4735 | return return_type; |
| 4724 | } | 4736 | } |
| 4725 | 4737 |
std/rand.zig+4| ... | @@ -58,6 +58,10 @@ pub struct Rand { | ... | @@ -58,6 +58,10 @@ pub struct Rand { |
| 58 | return f32(r.range_u64(0, precision)) / precision; | 58 | return f32(r.range_u64(0, precision)) / precision; |
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | pub fn boolean(r: &Rand) -> bool { | ||
| 62 | return (r.get_u32() & 0x1) == 1; | ||
| 63 | } | ||
| 64 | |||
| 61 | fn generate_numbers(r: &Rand) { | 65 | fn generate_numbers(r: &Rand) { |
| 62 | for (r.array) |item, i| { | 66 | for (r.array) |item, i| { |
| 63 | const y : u32 = (item & 0x80000000) + (r.array[(i + 1) % ARRAY_SIZE] & 0x7fffffff); | 67 | 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() { | ... | @@ -215,3 +215,21 @@ fn explicit_cast_maybe_pointers() { |
| 215 | const a: ?&i32 = undefined; | 215 | const a: ?&i32 = undefined; |
| 216 | const b: ?&f32 = (?&f32)(a); | 216 | const b: ?&f32 = (?&f32)(a); |
| 217 | } | 217 | } |
| 218 | |||
| 219 | |||
| 220 | #attribute("test") | ||
| 221 | fn const_expr_eval_on_single_expr_blocks() { | ||
| 222 | if (const_expr_eval_on_single_expr_blocks_fn(1, true) != 3) unreachable{} | ||
| 223 | } | ||
| 224 | |||
| 225 | fn 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 | } |