authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-21 17:49:54-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-21 17:49:54-04:00
logff6d563b0455aea51775e6906f5e7f0dd67b7127
tree46eb0b20c729ee623bbc4f6f8b965c09c9c8a1b3
parent5441f7767237709e8f3c05c2dc75070e835b4024
signaturelock-open Commit is signed but in an unrecognized format.

fix implicit cast to optional to error union to return result loc


3 files changed, 24 insertions(+), 5 deletions(-)

src/analyze.cpp+2
...@@ -4512,6 +4512,8 @@ bool fn_eval_cacheable(Scope *scope, ZigType *return_type) {...@@ -4512,6 +4512,8 @@ bool fn_eval_cacheable(Scope *scope, ZigType *return_type) {
4512 ScopeVarDecl *var_scope = (ScopeVarDecl *)scope;4512 ScopeVarDecl *var_scope = (ScopeVarDecl *)scope;
4513 if (type_is_invalid(var_scope->var->var_type))4513 if (type_is_invalid(var_scope->var->var_type))
4514 return false;4514 return false;
4515 if (var_scope->var->const_value->special == ConstValSpecialUndef)
4516 return false;
4515 if (can_mutate_comptime_var_state(var_scope->var->const_value))4517 if (can_mutate_comptime_var_state(var_scope->var->const_value))
4516 return false;4518 return false;
4517 } else if (scope->id == ScopeIdFnDef) {4519 } else if (scope->id == ScopeIdFnDef) {
src/ir.cpp+4-5
...@@ -15092,7 +15092,9 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe...@@ -15092,7 +15092,9 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
15092 ZigType *ptr_return_type = get_pointer_to_type(ira->codegen, ira->explicit_return_type, false);15092 ZigType *ptr_return_type = get_pointer_to_type(ira->codegen, ira->explicit_return_type, false);
15093 result_loc->written = true;15093 result_loc->written = true;
15094 result_loc->resolved_loc = ir_build_return_ptr(ira, result_loc->source_instruction, ptr_return_type);15094 result_loc->resolved_loc = ir_build_return_ptr(ira, result_loc->source_instruction, ptr_return_type);
15095 set_up_result_loc_for_inferred_comptime(result_loc->resolved_loc);15095 if (ir_should_inline(ira->old_irb.exec, result_loc->source_instruction->scope)) {
15096 set_up_result_loc_for_inferred_comptime(result_loc->resolved_loc);
15097 }
15096 return result_loc->resolved_loc;15098 return result_loc->resolved_loc;
15097 }15099 }
15098 case ResultLocIdPeer: {15100 case ResultLocIdPeer: {
...@@ -15207,9 +15209,6 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe...@@ -15207,9 +15209,6 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
15207 parent_ptr_type->data.pointer.is_const, parent_ptr_type->data.pointer.is_volatile, PtrLenSingle,15209 parent_ptr_type->data.pointer.is_const, parent_ptr_type->data.pointer.is_volatile, PtrLenSingle,
15208 parent_ptr_align, 0, 0, parent_ptr_type->data.pointer.allow_zero);15210 parent_ptr_align, 0, 0, parent_ptr_type->data.pointer.allow_zero);
1520915211
15210 if (value->value.special == ConstValSpecialRuntime) {
15211 parent_result_loc->value.special = ConstValSpecialRuntime;
15212 }
15213 result_loc->written = true;15212 result_loc->written = true;
15214 result_loc->resolved_loc = ir_analyze_ptr_cast(ira, suspend_source_instr, parent_result_loc,15213 result_loc->resolved_loc = ir_analyze_ptr_cast(ira, suspend_source_instr, parent_result_loc,
15215 ptr_type, result_bit_cast->base.source_instruction, false);15214 ptr_type, result_bit_cast->base.source_instruction, false);
...@@ -15388,7 +15387,7 @@ static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node...@@ -15388,7 +15387,7 @@ static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node
15388 casted_arg = arg;15387 casted_arg = arg;
15389 }15388 }
1539015389
15391 ConstExprValue *arg_val = ir_resolve_const(ira, casted_arg, UndefBad);15390 ConstExprValue *arg_val = ir_resolve_const(ira, casted_arg, UndefOk);
15392 if (!arg_val)15391 if (!arg_val)
15393 return false;15392 return false;
1539415393
test/stage1/behavior/error.zig+18
...@@ -357,3 +357,21 @@ test "nested catch" {...@@ -357,3 +357,21 @@ test "nested catch" {
357 S.entry();357 S.entry();
358 comptime S.entry();358 comptime S.entry();
359}359}
360
361test "implicit cast to optional to error union to return result loc" {
362 const S = struct {
363 fn entry() void {
364 if (func(undefined)) |opt| {
365 expect(opt != null);
366 } else |_| @panic("expected non error");
367 }
368 fn func(f: *Foo) anyerror!?*Foo {
369 return f;
370 }
371 const Foo = struct {
372 field: i32,
373 };
374 };
375 S.entry();
376 //comptime S.entry(); TODO
377}