authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-05-14 23:30:31+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-19 15:53:32-04:00
log6672ee9eb39a1cd9a3d6874ac1fb6de347ee2d33
treec48c52b2697c55b383aca557bbc69cd7e352d16d
parentb660134a18a308a3a4378c98b78b84595da34e7e

Fix too eager comptime evaluation of error ptr


4 files changed, 56 insertions(+), 23 deletions(-)

src/ir.cpp+27-23
......@@ -21126,17 +21126,19 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira, IrI
2112621126 ConstExprValue *ptr_val = ir_resolve_const(ira, base_ptr, UndefBad);
2112721127 if (!ptr_val)
2112821128 return ira->codegen->invalid_instruction;
21129 ConstExprValue *err_union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, instruction->base.source_node);
21130 if (err_union_val == nullptr)
21131 return ira->codegen->invalid_instruction;
21132 if (err_union_val->special != ConstValSpecialRuntime) {
21133 ErrorTableEntry *err = err_union_val->data.x_err_union.error_set->data.x_err_set;
21134 assert(err);
21129 if (ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar) {
21130 ConstExprValue *err_union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, instruction->base.source_node);
21131 if (err_union_val == nullptr)
21132 return ira->codegen->invalid_instruction;
21133 if (err_union_val->special != ConstValSpecialRuntime) {
21134 ErrorTableEntry *err = err_union_val->data.x_err_union.error_set->data.x_err_set;
21135 assert(err);
2113521136
21136 IrInstruction *result = ir_const(ira, &instruction->base,
21137 type_entry->data.error_union.err_set_type);
21138 result->value.data.x_err_set = err;
21139 return result;
21137 IrInstruction *result = ir_const(ira, &instruction->base,
21138 type_entry->data.error_union.err_set_type);
21139 result->value.data.x_err_set = err;
21140 return result;
21141 }
2114021142 }
2114121143 }
2114221144
......@@ -21179,21 +21181,23 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
2117921181 ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);
2118021182 if (!ptr_val)
2118121183 return ira->codegen->invalid_instruction;
21182 ConstExprValue *err_union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, instruction->base.source_node);
21183 if (err_union_val == nullptr)
21184 return ira->codegen->invalid_instruction;
21185 if (err_union_val->special != ConstValSpecialRuntime) {
21186 ErrorTableEntry *err = err_union_val->data.x_err_union.error_set->data.x_err_set;
21187 if (err != nullptr) {
21188 ir_add_error(ira, &instruction->base,
21189 buf_sprintf("caught unexpected error '%s'", buf_ptr(&err->name)));
21184 if (ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar) {
21185 ConstExprValue *err_union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, instruction->base.source_node);
21186 if (err_union_val == nullptr)
2119021187 return ira->codegen->invalid_instruction;
21191 }
21188 if (err_union_val->special != ConstValSpecialRuntime) {
21189 ErrorTableEntry *err = err_union_val->data.x_err_union.error_set->data.x_err_set;
21190 if (err != nullptr) {
21191 ir_add_error(ira, &instruction->base,
21192 buf_sprintf("caught unexpected error '%s'", buf_ptr(&err->name)));
21193 return ira->codegen->invalid_instruction;
21194 }
2119221195
21193 IrInstruction *result = ir_const(ira, &instruction->base, result_type);
21194 result->value.data.x_ptr.special = ConstPtrSpecialRef;
21195 result->value.data.x_ptr.data.ref.pointee = err_union_val->data.x_err_union.payload;
21196 return result;
21196 IrInstruction *result = ir_const(ira, &instruction->base, result_type);
21197 result->value.data.x_ptr.special = ConstPtrSpecialRef;
21198 result->value.data.x_ptr.data.ref.pointee = err_union_val->data.x_err_union.payload;
21199 return result;
21200 }
2119721201 }
2119821202 }
2119921203
src/ir_print.cpp+11
......@@ -1941,3 +1941,14 @@ void ir_print(CodeGen *codegen, FILE *f, IrExecutable *executable, int indent_si
19411941 }
19421942 }
19431943}
1944
1945void ir_print_instruction(CodeGen *codegen, FILE *f, IrInstruction *instruction, int indent_size) {
1946 IrPrint ir_print = {};
1947 IrPrint *irp = &ir_print;
1948 irp->codegen = codegen;
1949 irp->f = f;
1950 irp->indent = indent_size;
1951 irp->indent_size = indent_size;
1952
1953 ir_print_instruction(irp, instruction);
1954}
src/ir_print.hpp+1
......@@ -13,5 +13,6 @@
1313#include <stdio.h>
1414
1515void ir_print(CodeGen *codegen, FILE *f, IrExecutable *executable, int indent_size);
16void ir_print_instruction(CodeGen *codegen, FILE *f, IrInstruction *instruction, int indent_size);
1617
1718#endif
test/stage1/behavior/if.zig+17
......@@ -35,3 +35,20 @@ fn elseIfExpressionF(c: u8) u8 {
3535 return u8(2);
3636 }
3737}
38
39// #2297
40var global_with_val: anyerror!u32 = 0;
41var global_with_err: anyerror!u32 = error.SomeError;
42
43test "unwrap mutable global var" {
44 if (global_with_val) |v| {
45 expect(v == 0);
46 } else |e| {
47 unreachable;
48 }
49 if (global_with_err) |_| {
50 unreachable;
51 } else |e| {
52 expect(e == error.SomeError);
53 }
54}