Skip to content

Commit 46c5209

Browse files
authored
C++ Interop: Set location when creating a return pattern (#6185)
This requires changing `ReturnSlotPattern` and `OutParamPattern` definitions to use untyped node id, so they can have any associated node. Follow up of #5197. Part of #5064.
1 parent 9441c27 commit 46c5209

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

toolchain/check/cpp/import.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,17 +1492,29 @@ static auto GetReturnPattern(Context& context, SemIR::LocId loc_id,
14921492
return SemIR::InstId::None;
14931493
}
14941494
auto pattern_type_id = GetPatternType(context, type_id);
1495+
clang::SourceLocation return_type_loc =
1496+
clang_decl->getReturnTypeSourceRange().getBegin();
1497+
if (return_type_loc.isInvalid()) {
1498+
// TODO: While `getReturnTypeSourceRange()` should work, it seems broken for
1499+
// trailing return type. See
1500+
// https://github.com/llvm/llvm-project/issues/162649. Until this is fixed,
1501+
// we fallback to `getTypeSpecStartLoc()`.
1502+
return_type_loc = clang_decl->getTypeSpecStartLoc();
1503+
}
1504+
SemIR::ImportIRInstId return_type_import_ir_inst_id =
1505+
AddImportIRInst(context.sem_ir(), return_type_loc);
14951506
SemIR::InstId return_slot_pattern_id = AddPatternInst(
1496-
// TODO: Fill in a location for the return type once available.
1497-
context,
1498-
SemIR::LocIdAndInst::NoLoc(SemIR::ReturnSlotPattern(
1499-
{.type_id = pattern_type_id, .type_inst_id = type_inst_id})));
1507+
context, MakeImportedLocIdAndInst(
1508+
context, return_type_import_ir_inst_id,
1509+
SemIR::ReturnSlotPattern({.type_id = pattern_type_id,
1510+
.type_inst_id = type_inst_id})));
15001511
SemIR::InstId param_pattern_id = AddPatternInst(
1501-
// TODO: Fill in a location for the return type once available.
1502-
context, SemIR::LocIdAndInst::NoLoc(SemIR::OutParamPattern(
1503-
{.type_id = pattern_type_id,
1504-
.subpattern_id = return_slot_pattern_id,
1505-
.index = SemIR::CallParamIndex::None})));
1512+
context,
1513+
MakeImportedLocIdAndInst(
1514+
context, return_type_import_ir_inst_id,
1515+
SemIR::OutParamPattern({.type_id = pattern_type_id,
1516+
.subpattern_id = return_slot_pattern_id,
1517+
.index = SemIR::CallParamIndex::None})));
15061518
return param_pattern_id;
15071519
}
15081520

toolchain/check/testdata/interop/cpp/function/class.carbon

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,18 +440,21 @@ library "[[@TEST_NAME]]";
440440
import Cpp library "decl_value_return_type.h";
441441

442442
fn F() {
443-
// CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+13]]:3: note: in thunk for C++ function used here [InCppThunk]
443+
// CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+16]]:3: note: in thunk for C++ function used here [InCppThunk]
444444
// CHECK:STDERR: Cpp.foo();
445445
// CHECK:STDERR: ^~~~~~~~~
446446
// CHECK:STDERR:
447-
// CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+9]]:3: error: function returns incomplete type `Cpp.C` [IncompleteTypeInFunctionReturnType]
447+
// CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+12]]:3: error: function returns incomplete type `Cpp.C` [IncompleteTypeInFunctionReturnType]
448448
// CHECK:STDERR: Cpp.foo();
449449
// CHECK:STDERR: ^~~~~~~~~
450450
// CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE-10]]:10: in file included here [InCppInclude]
451451
// CHECK:STDERR: ./decl_value_return_type.h:2:7: note: class was forward declared here [ClassForwardDeclaredHere]
452452
// CHECK:STDERR: class C;
453453
// CHECK:STDERR: ^
454-
// CHECK:STDERR: fail_import_decl_value_return_type.carbon: note: return type declared here [IncompleteReturnTypeHere]
454+
// CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE-14]]:10: in file included here [InCppInclude]
455+
// CHECK:STDERR: ./decl_value_return_type.h:4:1: note: return type declared here [IncompleteReturnTypeHere]
456+
// CHECK:STDERR: auto foo() -> C;
457+
// CHECK:STDERR: ^
455458
// CHECK:STDERR:
456459
Cpp.foo();
457460
}

toolchain/check/testdata/interop/cpp/function/struct.carbon

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,18 +439,21 @@ library "[[@TEST_NAME]]";
439439
import Cpp library "decl_value_return_type.h";
440440

441441
fn F() {
442-
// CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+13]]:3: note: in thunk for C++ function used here [InCppThunk]
442+
// CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+16]]:3: note: in thunk for C++ function used here [InCppThunk]
443443
// CHECK:STDERR: Cpp.foo();
444444
// CHECK:STDERR: ^~~~~~~~~
445445
// CHECK:STDERR:
446-
// CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+9]]:3: error: function returns incomplete type `Cpp.S` [IncompleteTypeInFunctionReturnType]
446+
// CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+12]]:3: error: function returns incomplete type `Cpp.S` [IncompleteTypeInFunctionReturnType]
447447
// CHECK:STDERR: Cpp.foo();
448448
// CHECK:STDERR: ^~~~~~~~~
449449
// CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE-10]]:10: in file included here [InCppInclude]
450450
// CHECK:STDERR: ./decl_value_return_type.h:2:8: note: class was forward declared here [ClassForwardDeclaredHere]
451451
// CHECK:STDERR: struct S;
452452
// CHECK:STDERR: ^
453-
// CHECK:STDERR: fail_import_decl_value_return_type.carbon: note: return type declared here [IncompleteReturnTypeHere]
453+
// CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE-14]]:10: in file included here [InCppInclude]
454+
// CHECK:STDERR: ./decl_value_return_type.h:4:1: note: return type declared here [IncompleteReturnTypeHere]
455+
// CHECK:STDERR: auto foo() -> S;
456+
// CHECK:STDERR: ^
454457
// CHECK:STDERR:
455458
Cpp.foo();
456459
}

toolchain/check/testdata/interop/cpp/function/union.carbon

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,18 +401,21 @@ library "[[@TEST_NAME]]";
401401
import Cpp library "decl_value_return_type.h";
402402

403403
fn F() {
404-
// CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+13]]:3: note: in thunk for C++ function used here [InCppThunk]
404+
// CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+16]]:3: note: in thunk for C++ function used here [InCppThunk]
405405
// CHECK:STDERR: Cpp.foo();
406406
// CHECK:STDERR: ^~~~~~~~~
407407
// CHECK:STDERR:
408-
// CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+9]]:3: error: function returns incomplete type `Cpp.U` [IncompleteTypeInFunctionReturnType]
408+
// CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE+12]]:3: error: function returns incomplete type `Cpp.U` [IncompleteTypeInFunctionReturnType]
409409
// CHECK:STDERR: Cpp.foo();
410410
// CHECK:STDERR: ^~~~~~~~~
411411
// CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE-10]]:10: in file included here [InCppInclude]
412412
// CHECK:STDERR: ./decl_value_return_type.h:2:7: note: class was forward declared here [ClassForwardDeclaredHere]
413413
// CHECK:STDERR: union U;
414414
// CHECK:STDERR: ^
415-
// CHECK:STDERR: fail_import_decl_value_return_type.carbon: note: return type declared here [IncompleteReturnTypeHere]
415+
// CHECK:STDERR: fail_import_decl_value_return_type.carbon:[[@LINE-14]]:10: in file included here [InCppInclude]
416+
// CHECK:STDERR: ./decl_value_return_type.h:4:1: note: return type declared here [IncompleteReturnTypeHere]
417+
// CHECK:STDERR: auto foo() -> U;
418+
// CHECK:STDERR: ^
416419
// CHECK:STDERR:
417420
Cpp.foo();
418421
}

0 commit comments

Comments
 (0)