diff --git a/toolchain/check/cpp/import.cpp b/toolchain/check/cpp/import.cpp index a6198607cdb6..073a4b80afc2 100644 --- a/toolchain/check/cpp/import.cpp +++ b/toolchain/check/cpp/import.cpp @@ -1492,17 +1492,29 @@ static auto GetReturnPattern(Context& context, SemIR::LocId loc_id, return SemIR::InstId::None; } auto pattern_type_id = GetPatternType(context, type_id); + clang::SourceLocation return_type_loc = + clang_decl->getReturnTypeSourceRange().getBegin(); + if (return_type_loc.isInvalid()) { + // TODO: While `getReturnTypeSourceRange()` should work, it seems broken for + // trailing return type. See + // https://github.com/llvm/llvm-project/issues/162649. Until this is fixed, + // we fallback to `getTypeSpecStartLoc()`. + return_type_loc = clang_decl->getTypeSpecStartLoc(); + } + SemIR::ImportIRInstId return_type_import_ir_inst_id = + AddImportIRInst(context.sem_ir(), return_type_loc); SemIR::InstId return_slot_pattern_id = AddPatternInst( - // TODO: Fill in a location for the return type once available. - context, - SemIR::LocIdAndInst::NoLoc(SemIR::ReturnSlotPattern( - {.type_id = pattern_type_id, .type_inst_id = type_inst_id}))); + context, MakeImportedLocIdAndInst( + context, return_type_import_ir_inst_id, + SemIR::ReturnSlotPattern({.type_id = pattern_type_id, + .type_inst_id = type_inst_id}))); SemIR::InstId param_pattern_id = AddPatternInst( - // TODO: Fill in a location for the return type once available. - context, SemIR::LocIdAndInst::NoLoc(SemIR::OutParamPattern( - {.type_id = pattern_type_id, - .subpattern_id = return_slot_pattern_id, - .index = SemIR::CallParamIndex::None}))); + context, + MakeImportedLocIdAndInst( + context, return_type_import_ir_inst_id, + SemIR::OutParamPattern({.type_id = pattern_type_id, + .subpattern_id = return_slot_pattern_id, + .index = SemIR::CallParamIndex::None}))); return param_pattern_id; } diff --git a/toolchain/check/testdata/interop/cpp/function/class.carbon b/toolchain/check/testdata/interop/cpp/function/class.carbon index 3c5f78ae5322..a2ad67a5f804 100644 --- a/toolchain/check/testdata/interop/cpp/function/class.carbon +++ b/toolchain/check/testdata/interop/cpp/function/class.carbon @@ -440,18 +440,21 @@ library "[[@TEST_NAME]]"; import Cpp library "decl_value_return_type.h"; fn F() { - // CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+13]]:3: note: in thunk for C++ function used here [InCppThunk] + // CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+16]]:3: note: in thunk for C++ function used here [InCppThunk] // CHECK:STDERR: Cpp.foo(); // CHECK:STDERR: ^~~~~~~~~ // CHECK:STDERR: - // CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+9]]:3: error: function returns incomplete type `Cpp.C` [IncompleteTypeInFunctionReturnType] + // CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+12]]:3: error: function returns incomplete type `Cpp.C` [IncompleteTypeInFunctionReturnType] // CHECK:STDERR: Cpp.foo(); // CHECK:STDERR: ^~~~~~~~~ // CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE-10]]:10: in file included here [InCppInclude] // CHECK:STDERR: ./decl_value_return_type.h:2:7: note: class was forward declared here [ClassForwardDeclaredHere] // CHECK:STDERR: class C; // CHECK:STDERR: ^ - // CHECK:STDERR: fail_import_decl_value_return_type.carbon: note: return type declared here [IncompleteReturnTypeHere] + // CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE-14]]:10: in file included here [InCppInclude] + // CHECK:STDERR: ./decl_value_return_type.h:4:1: note: return type declared here [IncompleteReturnTypeHere] + // CHECK:STDERR: auto foo() -> C; + // CHECK:STDERR: ^ // CHECK:STDERR: Cpp.foo(); } diff --git a/toolchain/check/testdata/interop/cpp/function/struct.carbon b/toolchain/check/testdata/interop/cpp/function/struct.carbon index 9b3ddb7b391d..0c05b7201c39 100644 --- a/toolchain/check/testdata/interop/cpp/function/struct.carbon +++ b/toolchain/check/testdata/interop/cpp/function/struct.carbon @@ -439,18 +439,21 @@ library "[[@TEST_NAME]]"; import Cpp library "decl_value_return_type.h"; fn F() { - // CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+13]]:3: note: in thunk for C++ function used here [InCppThunk] + // CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+16]]:3: note: in thunk for C++ function used here [InCppThunk] // CHECK:STDERR: Cpp.foo(); // CHECK:STDERR: ^~~~~~~~~ // CHECK:STDERR: - // CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+9]]:3: error: function returns incomplete type `Cpp.S` [IncompleteTypeInFunctionReturnType] + // CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+12]]:3: error: function returns incomplete type `Cpp.S` [IncompleteTypeInFunctionReturnType] // CHECK:STDERR: Cpp.foo(); // CHECK:STDERR: ^~~~~~~~~ // CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE-10]]:10: in file included here [InCppInclude] // CHECK:STDERR: ./decl_value_return_type.h:2:8: note: class was forward declared here [ClassForwardDeclaredHere] // CHECK:STDERR: struct S; // CHECK:STDERR: ^ - // CHECK:STDERR: fail_import_decl_value_return_type.carbon: note: return type declared here [IncompleteReturnTypeHere] + // CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE-14]]:10: in file included here [InCppInclude] + // CHECK:STDERR: ./decl_value_return_type.h:4:1: note: return type declared here [IncompleteReturnTypeHere] + // CHECK:STDERR: auto foo() -> S; + // CHECK:STDERR: ^ // CHECK:STDERR: Cpp.foo(); } diff --git a/toolchain/check/testdata/interop/cpp/function/union.carbon b/toolchain/check/testdata/interop/cpp/function/union.carbon index e35252e70a20..5efa27c515d2 100644 --- a/toolchain/check/testdata/interop/cpp/function/union.carbon +++ b/toolchain/check/testdata/interop/cpp/function/union.carbon @@ -401,18 +401,21 @@ library "[[@TEST_NAME]]"; import Cpp library "decl_value_return_type.h"; fn F() { - // CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+13]]:3: note: in thunk for C++ function used here [InCppThunk] + // CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+16]]:3: note: in thunk for C++ function used here [InCppThunk] // CHECK:STDERR: Cpp.foo(); // CHECK:STDERR: ^~~~~~~~~ // CHECK:STDERR: - // CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+9]]:3: error: function returns incomplete type `Cpp.U` [IncompleteTypeInFunctionReturnType] + // CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+12]]:3: error: function returns incomplete type `Cpp.U` [IncompleteTypeInFunctionReturnType] // CHECK:STDERR: Cpp.foo(); // CHECK:STDERR: ^~~~~~~~~ // CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE-10]]:10: in file included here [InCppInclude] // CHECK:STDERR: ./decl_value_return_type.h:2:7: note: class was forward declared here [ClassForwardDeclaredHere] // CHECK:STDERR: union U; // CHECK:STDERR: ^ - // CHECK:STDERR: fail_import_decl_value_return_type.carbon: note: return type declared here [IncompleteReturnTypeHere] + // CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE-14]]:10: in file included here [InCppInclude] + // CHECK:STDERR: ./decl_value_return_type.h:4:1: note: return type declared here [IncompleteReturnTypeHere] + // CHECK:STDERR: auto foo() -> U; + // CHECK:STDERR: ^ // CHECK:STDERR: Cpp.foo(); }