Fix too eager comptime evaluation of error ptr

This commit is contained in:
LemonBoy 2019-05-14 23:30:31 +02:00 committed by Andrew Kelley
parent b660134a18
commit 6672ee9eb3
4 changed files with 56 additions and 23 deletions

View File

@ -21126,6 +21126,7 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira, IrI
ConstExprValue *ptr_val = ir_resolve_const(ira, base_ptr, UndefBad);
if (!ptr_val)
return ira->codegen->invalid_instruction;
if (ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar) {
ConstExprValue *err_union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, instruction->base.source_node);
if (err_union_val == nullptr)
return ira->codegen->invalid_instruction;
@ -21139,6 +21140,7 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira, IrI
return result;
}
}
}
IrInstruction *result = ir_build_unwrap_err_code(&ira->new_irb,
instruction->base.scope, instruction->base.source_node, base_ptr);
@ -21179,6 +21181,7 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);
if (!ptr_val)
return ira->codegen->invalid_instruction;
if (ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar) {
ConstExprValue *err_union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, instruction->base.source_node);
if (err_union_val == nullptr)
return ira->codegen->invalid_instruction;
@ -21196,6 +21199,7 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
return result;
}
}
}
IrInstruction *result = ir_build_unwrap_err_payload(&ira->new_irb,
instruction->base.scope, instruction->base.source_node, value, instruction->safety_check_on);

View File

@ -1941,3 +1941,14 @@ void ir_print(CodeGen *codegen, FILE *f, IrExecutable *executable, int indent_si
}
}
}
void ir_print_instruction(CodeGen *codegen, FILE *f, IrInstruction *instruction, int indent_size) {
IrPrint ir_print = {};
IrPrint *irp = &ir_print;
irp->codegen = codegen;
irp->f = f;
irp->indent = indent_size;
irp->indent_size = indent_size;
ir_print_instruction(irp, instruction);
}

View File

@ -13,5 +13,6 @@
#include <stdio.h>
void ir_print(CodeGen *codegen, FILE *f, IrExecutable *executable, int indent_size);
void ir_print_instruction(CodeGen *codegen, FILE *f, IrInstruction *instruction, int indent_size);
#endif

View File

@ -35,3 +35,20 @@ fn elseIfExpressionF(c: u8) u8 {
return u8(2);
}
}
// #2297
var global_with_val: anyerror!u32 = 0;
var global_with_err: anyerror!u32 = error.SomeError;
test "unwrap mutable global var" {
if (global_with_val) |v| {
expect(v == 0);
} else |e| {
unreachable;
}
if (global_with_err) |_| {
unreachable;
} else |e| {
expect(e == error.SomeError);
}
}