From ff6d563b0455aea51775e6906f5e7f0dd67b7127 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 21 Jun 2019 17:49:54 -0400 Subject: [PATCH] fix implicit cast to optional to error union to return result loc --- src/analyze.cpp | 2 ++ src/ir.cpp | 9 ++++----- test/stage1/behavior/error.zig | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index d048dd977..85e390f2e 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -4512,6 +4512,8 @@ bool fn_eval_cacheable(Scope *scope, ZigType *return_type) { ScopeVarDecl *var_scope = (ScopeVarDecl *)scope; if (type_is_invalid(var_scope->var->var_type)) return false; + if (var_scope->var->const_value->special == ConstValSpecialUndef) + return false; if (can_mutate_comptime_var_state(var_scope->var->const_value)) return false; } else if (scope->id == ScopeIdFnDef) { diff --git a/src/ir.cpp b/src/ir.cpp index 425c22530..a6ec836a4 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -15092,7 +15092,9 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe ZigType *ptr_return_type = get_pointer_to_type(ira->codegen, ira->explicit_return_type, false); result_loc->written = true; result_loc->resolved_loc = ir_build_return_ptr(ira, result_loc->source_instruction, ptr_return_type); - set_up_result_loc_for_inferred_comptime(result_loc->resolved_loc); + if (ir_should_inline(ira->old_irb.exec, result_loc->source_instruction->scope)) { + set_up_result_loc_for_inferred_comptime(result_loc->resolved_loc); + } return result_loc->resolved_loc; } case ResultLocIdPeer: { @@ -15207,9 +15209,6 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe parent_ptr_type->data.pointer.is_const, parent_ptr_type->data.pointer.is_volatile, PtrLenSingle, parent_ptr_align, 0, 0, parent_ptr_type->data.pointer.allow_zero); - if (value->value.special == ConstValSpecialRuntime) { - parent_result_loc->value.special = ConstValSpecialRuntime; - } result_loc->written = true; result_loc->resolved_loc = ir_analyze_ptr_cast(ira, suspend_source_instr, parent_result_loc, 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 casted_arg = arg; } - ConstExprValue *arg_val = ir_resolve_const(ira, casted_arg, UndefBad); + ConstExprValue *arg_val = ir_resolve_const(ira, casted_arg, UndefOk); if (!arg_val) return false; diff --git a/test/stage1/behavior/error.zig b/test/stage1/behavior/error.zig index 82ddc72b7..264f140c9 100644 --- a/test/stage1/behavior/error.zig +++ b/test/stage1/behavior/error.zig @@ -357,3 +357,21 @@ test "nested catch" { S.entry(); comptime S.entry(); } + +test "implicit cast to optional to error union to return result loc" { + const S = struct { + fn entry() void { + if (func(undefined)) |opt| { + expect(opt != null); + } else |_| @panic("expected non error"); + } + fn func(f: *Foo) anyerror!?*Foo { + return f; + } + const Foo = struct { + field: i32, + }; + }; + S.entry(); + //comptime S.entry(); TODO +}