From 4d9318cee075fbc662cb4e20a74c7f887c3a3da4 Mon Sep 17 00:00:00 2001 From: Vexu <15308111+Vexu@users.noreply.github.com> Date: Tue, 19 Nov 2019 17:29:43 +0200 Subject: [PATCH] fix missing implicit cast in return instruction --- src/ir.cpp | 20 ++++++++++---------- test/compile_errors.zig | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 7bca551852..9f0fc40ded 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -13535,16 +13535,6 @@ static IrInstruction *ir_analyze_instruction_return(IrAnalyze *ira, IrInstructio if (type_is_invalid(operand->value.type)) return ir_unreach_error(ira); - if (!instr_is_comptime(operand) && ira->explicit_return_type != nullptr && - handle_is_ptr(ira->explicit_return_type)) - { - // result location mechanism took care of it. - IrInstruction *result = ir_build_return(&ira->new_irb, instruction->base.scope, - instruction->base.source_node, nullptr); - result->value.type = ira->codegen->builtin_types.entry_unreachable; - return ir_finish_anal(ira, result); - } - IrInstruction *casted_operand = ir_implicit_cast(ira, operand, ira->explicit_return_type); if (type_is_invalid(casted_operand->value.type)) { AstNode *source_node = ira->explicit_return_type_source_node; @@ -13556,6 +13546,16 @@ static IrInstruction *ir_analyze_instruction_return(IrAnalyze *ira, IrInstructio return ir_unreach_error(ira); } + if (!instr_is_comptime(operand) && ira->explicit_return_type != nullptr && + handle_is_ptr(ira->explicit_return_type)) + { + // result location mechanism took care of it. + IrInstruction *result = ir_build_return(&ira->new_irb, instruction->base.scope, + instruction->base.source_node, nullptr); + result->value.type = ira->codegen->builtin_types.entry_unreachable; + return ir_finish_anal(ira, result); + } + if (casted_operand->value.special == ConstValSpecialRuntime && casted_operand->value.type->id == ZigTypeIdPointer && casted_operand->value.data.rh_ptr == RuntimeHintPtrStack) diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 04355261f9..9e47d2a458 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -12,6 +12,27 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:3:5: error: switch must handle all possibilities", ); + cases.add( + "incorrect return type", + \\ pub export fn entry() void{ + \\ _ = foo(); + \\ } + \\ const A = struct { + \\ a: u32, + \\ }; + \\ fn foo() A { + \\ return bar(); + \\ } + \\ const B = struct { + \\ a: u32, + \\ }; + \\ fn bar() B { + \\ unreachable; + \\ } + , + "tmp.zig:8:16: error: expected type 'A', found 'B'", + ); + cases.add( "regression test #2980: base type u32 is not type checked properly when assigning a value within a struct", \\const Foo = struct {