Przeglądaj źródła

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.
Boaz Brickner 6 miesięcy temu
rodzic
commit
46c5209f2f

+ 21 - 9
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;
 }
 

+ 6 - 3
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();
 }

+ 6 - 3
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();
 }

+ 6 - 3
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();
 }