File tree Expand file tree Collapse file tree 4 files changed +33
-0
lines changed
test/Interop/Cxx/reference Expand file tree Collapse file tree 4 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -1863,6 +1863,17 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
18631863 dyn_cast<clang::TemplateTypeParmType>(clangDecl->getReturnType ())) {
18641864 importedType = {findGenericTypeInGenericDecls (templateType, genericParams),
18651865 false };
1866+ } else if ((isa<clang::PointerType>(clangDecl->getReturnType ()) ||
1867+ isa<clang::ReferenceType>(clangDecl->getReturnType ())) &&
1868+ isa<clang::TemplateTypeParmType>(clangDecl->getReturnType ()->getPointeeType ())) {
1869+ auto pointeeType = clangDecl->getReturnType ()->getPointeeType ();
1870+ auto templateParamType = cast<clang::TemplateTypeParmType>(pointeeType);
1871+ PointerTypeKind pointerKind = pointeeType.getQualifiers ().hasConst ()
1872+ ? PTK_UnsafePointer
1873+ : PTK_UnsafeMutablePointer;
1874+ auto genericType =
1875+ findGenericTypeInGenericDecls (templateParamType, genericParams);
1876+ importedType = {genericType->wrapInPointer (pointerKind), false };
18661877 } else if (!(isa<clang::RecordType>(clangDecl->getReturnType ()) ||
18671878 isa<clang::TemplateSpecializationType>(clangDecl->getReturnType ())) ||
18681879 // TODO: we currently don't lazily load operator return types, but
Original file line number Diff line number Diff line change @@ -28,4 +28,10 @@ void dontImportAtomicRef(_Atomic(int)&) { }
2828
2929void takeConstRef (const int &);
3030
31+ template <class T >
32+ T &refToTemplate (T &t) { return t; }
33+
34+ template <class T >
35+ const T &constRefToTemplate (const T &t) { return t; }
36+
3137#endif // TEST_INTEROP_CXX_REFERENCE_INPUTS_REFERENCE_H
Original file line number Diff line number Diff line change 1212// CHECK: func setConstStaticIntRvalueRef(_: Int32)
1313// CHECK: func getFuncRef() -> @convention(c) () -> Int32
1414// CHECK: func getFuncRvalueRef() -> @convention(c) () -> Int32
15+ // CHECK: func refToTemplate<T>(_ t: inout T) -> UnsafeMutablePointer<T>
16+ // CHECK: func constRefToTemplate<T>(_ t: T) -> UnsafePointer<T>
1517
1618// CHECK-NOT: refToDependent
1719// CHECK-NOT: dontImportAtomicRef
Original file line number Diff line number Diff line change @@ -87,4 +87,18 @@ ReferenceTestSuite.test("pod-struct-const-lvalue-reference") {
8787 expectEqual ( getStaticInt ( ) , 78 )
8888}
8989
90+ ReferenceTestSuite . test ( " reference to template " ) {
91+ var val : CInt = 53
92+ let ref = refToTemplate ( & val)
93+ expectEqual ( 53 , ref. pointee)
94+ ref. pointee = 42
95+ expectEqual ( 42 , val)
96+ }
97+
98+ ReferenceTestSuite . test ( " const reference to template " ) {
99+ var val : CInt = 53
100+ let ref = constRefToTemplate ( val)
101+ expectEqual ( 53 , ref. pointee)
102+ }
103+
90104runAllTests ( )
You can’t perform that action at this time.
0 commit comments