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) {
45124512 ScopeVarDecl *var_scope = (ScopeVarDecl *)scope;
45134513 if (type_is_invalid(var_scope->var->var_type))
45144514 return false;
4515 if (var_scope->var->const_value->special == ConstValSpecialUndef)
4516 return false;
45154517 if (can_mutate_comptime_var_state(var_scope->var->const_value))
45164518 return false;
45174519 } else if (scope->id == ScopeIdFnDef) {
src/ir.cpp+4-5
......@@ -15092,7 +15092,9 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
1509215092 ZigType *ptr_return_type = get_pointer_to_type(ira->codegen, ira->explicit_return_type, false);
1509315093 result_loc->written = true;
1509415094 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 }
1509615098 return result_loc->resolved_loc;
1509715099 }
1509815100 case ResultLocIdPeer: {
......@@ -15207,9 +15209,6 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
1520715209 parent_ptr_type->data.pointer.is_const, parent_ptr_type->data.pointer.is_volatile, PtrLenSingle,
1520815210 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 }
1521315212 result_loc->written = true;
1521415213 result_loc->resolved_loc = ir_analyze_ptr_cast(ira, suspend_source_instr, parent_result_loc,
1521515214 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
1538815387 casted_arg = arg;
1538915388 }
1539015389
15391 ConstExprValue *arg_val = ir_resolve_const(ira, casted_arg, UndefBad);
15390 ConstExprValue *arg_val = ir_resolve_const(ira, casted_arg, UndefOk);
1539215391 if (!arg_val)
1539315392 return false;
1539415393
test/stage1/behavior/error.zig+18
......@@ -357,3 +357,21 @@ test "nested catch" {
357357 S.entry();
358358 comptime S.entry();
359359}
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}