Skip to content

Commit 04d4e9d

Browse files
committed
[cxx-interop] Correctly map T& -> inout T.
This is important because the specialized template will be imported as `inout int` (for example) and we must make sure to match the signature. Updates tests based on this and fixes a few tests that started failing after daceecf.
1 parent daceecf commit 04d4e9d

6 files changed

+29
-23
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1979,7 +1979,7 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
19791979
Type swiftParamTy;
19801980
bool isParamTypeImplicitlyUnwrapped = false;
19811981
bool isInOut = false;
1982-
if ((isa<clang::ReferenceType>(paramTy) || isa<clang::PointerType>(paramTy)) &&
1982+
if (isa<clang::PointerType>(paramTy) &&
19831983
isa<clang::TemplateTypeParmType>(paramTy->getPointeeType())) {
19841984
auto pointeeType = paramTy->getPointeeType();
19851985
auto templateParamType = cast<clang::TemplateTypeParmType>(pointeeType);
@@ -1991,6 +1991,12 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
19911991
swiftParamTy = genericType->wrapInPointer(pointerKind);
19921992
if (!swiftParamTy)
19931993
return nullptr;
1994+
} else if (isa<clang::ReferenceType>(paramTy) &&
1995+
isa<clang::TemplateTypeParmType>(paramTy->getPointeeType())) {
1996+
auto templateParamType = cast<clang::TemplateTypeParmType>(paramTy->getPointeeType());
1997+
swiftParamTy =
1998+
findGenericTypeInGenericDecls(templateParamType, genericParams);
1999+
isInOut = true;
19942000
} else if (auto *templateParamType =
19952001
dyn_cast<clang::TemplateTypeParmType>(paramTy)) {
19962002
swiftParamTy =

test/Interop/Cxx/templates/defaulted-template-type-parameter-module-interface.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// CHECK: func defaultedTemplateTypeParamAndUnrealtedParam(_: Int32)
1515
// CHECK: func overloadedDefaultedTemplate<T>(_: T)
1616
// CHECK: func overloadedDefaultedTemplate(_: Int32)
17-
// CHECK: func defaultedTemplateReferenceTypeParam<T>(_ t: UnsafeMutablePointer<T>)
17+
// CHECK: func defaultedTemplateReferenceTypeParam<T>(_ t: inout T)
1818
// The following types aren't imported correctly, but that does not have to do
1919
// with the fact that the template type paramaters are defaulted.
2020
// CHECK: func defaultedTemplatePointerTypeParam<T>(_ t: UnsafeMutablePointer<T>)

test/Interop/Cxx/templates/function-template-module-interface.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
// CHECK: mutating func test2(_: Int32, _ varargs: Any...)
1515
// CHECK: }
1616

17-
// CHECK: func lvalueReference<T>(_ ref: UnsafeMutablePointer<T>)
18-
// CHECK: func constLvalueReference<T>(_: UnsafePointer<T>)
19-
// CHECK: func forwardingReference<T>(_: UnsafeMutablePointer<T>)
17+
// CHECK: func lvalueReference<T>(_ ref: inout T)
18+
// CHECK: func constLvalueReference<T>(_: inout T)
19+
// CHECK: func forwardingReference<T>(_: inout T)
2020
// CHECK: func PointerTemplateParameter<T>(_: UnsafeMutablePointer<T>)
2121

2222
// CHECK: enum Orbiters {
2323
// CHECK: static func galileo<T>(_: T)
2424
// CHECK: static func cassini<T, U>(_: T, _: U)
25-
// CHECK: static func magellan<T>(_: UnsafeMutablePointer<T>)
25+
// CHECK: static func magellan<T>(_: inout T)
2626
// CHECK: }

test/Interop/Cxx/templates/member-templates-module-interface.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
// CHECK: mutating func passThroughConst<T>(_ val: T) -> T
99
// CHECK: func passThroughOnConst<T>(_ val: T) -> T
1010
// CHECK: func passThroughConstOnConst<T>(_ val: T) -> T
11-
// CHECK: mutating func doNothingConstRef<T>(_ val: UnsafePointer<T>)
12-
// CHECK: mutating func make42Ref<T>(_ val: UnsafeMutablePointer<T>)
11+
// CHECK: mutating func doNothingConstRef<T>(_ val: inout T)
12+
// CHECK: mutating func make42Ref<T>(_ val: inout T)
1313
// CHECK: }
1414

1515
// CHECK: struct __CxxTemplateInst32TemplateClassWithMemberTemplatesIiE {
@@ -24,5 +24,5 @@
2424
// CHECK: init()
2525
// CHECK: static func add<T>(_ a: T, _ b: T) -> T
2626
// CHECK: static func addTwoTemplates<T, U>(_ a: T, _ b: U) -> T
27-
// CHECK: static func removeReference<T>(_ a: UnsafeMutablePointer<T>) -> T
27+
// CHECK: static func removeReference<T>(_ a: inout T) -> T
2828
// CHECK: }

test/Interop/Cxx/templates/member-templates-silgen.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import MemberTemplates
1010
// CHECK: [[ADD:%.*]] = function_ref @_ZN18HasMemberTemplates17addSameTypeParamsIiEET_S1_S1_ : $@convention(cxx_method) (Int32, Int32, @inout HasMemberTemplates) -> Int32
1111
// CHECK: apply [[ADD]]({{.*}}) : $@convention(cxx_method) (Int32, Int32, @inout HasMemberTemplates) -> Int32
1212

13-
// CHECK: [[ADD_TWO_TEMPLATES:%.*]] = function_ref @_ZN18HasMemberTemplates18addMixedTypeParamsIiiEET_S1_T0_ : $@convention(cxx_method) (Int32, Int32, @inout HasMemberTemplates) -> Int32 // user: %26
13+
// CHECK: [[ADD_TWO_TEMPLATES:%.*]] = function_ref @_ZN18HasMemberTemplates18addMixedTypeParamsIiiEET_S1_T0_ : $@convention(cxx_method) (Int32, Int32, @inout HasMemberTemplates) -> Int32
1414
// CHECK: apply [[ADD_TWO_TEMPLATES]]({{.*}}) : $@convention(cxx_method) (Int32, Int32, @inout HasMemberTemplates) -> Int32
1515

16-
// CHECK: [[ADD_ALL:%.*]] = function_ref @_ZN18HasMemberTemplates6addAllIiiEEiiT_T0_ : $@convention(cxx_method) (Int32, Int32, Int32, @inout HasMemberTemplates) -> Int32 // user: %39
16+
// CHECK: [[ADD_ALL:%.*]] = function_ref @_ZN18HasMemberTemplates6addAllIiiEEiiT_T0_ : $@convention(cxx_method) (Int32, Int32, Int32, @inout HasMemberTemplates) -> Int32
1717
// CHECK: apply [[ADD_ALL]]({{.*}}) : $@convention(cxx_method) (Int32, Int32, Int32, @inout HasMemberTemplates) -> Int32
1818

19-
// CHECK: [[DO_NOTHING:%.*]] = function_ref @_ZN18HasMemberTemplates17doNothingConstRefIiEEvRKT_ : $@convention(cxx_method) (UnsafePointer<Int32>, @inout HasMemberTemplates) -> () // user: %48
20-
// CHECK: apply [[DO_NOTHING]]({{.*}}) : $@convention(cxx_method) (UnsafePointer<Int32>, @inout HasMemberTemplates) -> ()
19+
// CHECK: [[DO_NOTHING:%.*]] = function_ref @_ZN18HasMemberTemplates17doNothingConstRefIiEEvRKT_ : $@convention(cxx_method) (@inout Int32, @inout HasMemberTemplates) -> ()
20+
// CHECK: apply [[DO_NOTHING]]({{.*}}) : $@convention(cxx_method) (@inout Int32, @inout HasMemberTemplates) -> ()
2121

2222
// CHECK-LABEL: end sil function '$s4main9basicTestyyF'
2323
func basicTest() {
@@ -29,13 +29,13 @@ func basicTest() {
2929
obj.doNothingConstRef(&i)
3030
}
3131

32-
// CHECK-LABEL: sil hidden_external [clang HasMemberTemplates._ZN18HasMemberTemplates17addSameTypeParamsIiEET_S1_S1_] @_ZN18HasMemberTemplates17addSameTypeParamsIiEET_S1_S1_ : $@convention(cxx_method) (Int32, Int32, @inout HasMemberTemplates) -> Int32
32+
// CHECK-LABEL: sil [clang HasMemberTemplates.addSameTypeParams] @_ZN18HasMemberTemplates17addSameTypeParamsIiEET_S1_S1_ : $@convention(cxx_method) (Int32, Int32, @inout HasMemberTemplates) -> Int32
3333

34-
// CHECK-LABEL: sil hidden_external [clang HasMemberTemplates._ZN18HasMemberTemplates18addMixedTypeParamsIiiEET_S1_T0_] @_ZN18HasMemberTemplates18addMixedTypeParamsIiiEET_S1_T0_ : $@convention(cxx_method) (Int32, Int32, @inout HasMemberTemplates) -> Int32
34+
// CHECK-LABEL: sil [clang HasMemberTemplates.addMixedTypeParams] @_ZN18HasMemberTemplates18addMixedTypeParamsIiiEET_S1_T0_ : $@convention(cxx_method) (Int32, Int32, @inout HasMemberTemplates) -> Int32
3535

36-
// CHECK-LABEL: sil hidden_external [clang HasMemberTemplates._ZN18HasMemberTemplates6addAllIiiEEiiT_T0_] @_ZN18HasMemberTemplates6addAllIiiEEiiT_T0_ : $@convention(cxx_method) (Int32, Int32, Int32, @inout HasMemberTemplates) -> Int32
36+
// CHECK-LABEL: sil [clang HasMemberTemplates.addAll] @_ZN18HasMemberTemplates6addAllIiiEEiiT_T0_ : $@convention(cxx_method) (Int32, Int32, Int32, @inout HasMemberTemplates) -> Int32
3737

38-
// CHECK-LABEL: sil hidden_external [clang HasMemberTemplates._ZN18HasMemberTemplates17doNothingConstRefIiEEvRKT_] @_ZN18HasMemberTemplates17doNothingConstRefIiEEvRKT_ : $@convention(cxx_method) (UnsafePointer<Int32>, @inout HasMemberTemplates) -> ()
38+
// CHECK-LABEL: sil [clang HasMemberTemplates.doNothingConstRef] @_ZN18HasMemberTemplates17doNothingConstRefIiEEvRKT_ : $@convention(cxx_method) (@inout Int32, @inout HasMemberTemplates) -> ()
3939

4040
// CHECK-LABEL: sil hidden @$s4main12testSetValueyyF : $@convention(thin) () -> ()
4141

@@ -56,8 +56,8 @@ func testSetValue() {
5656
// CHECK: [[ADD_TWO_TEMPLATES_FN:%.*]] = function_ref @_ZN24HasStaticMemberTemplates15addTwoTemplatesIlcEET_S1_T0_ : $@convention(c) (Int, Int8) -> Int
5757
// CHECK: apply [[ADD_TWO_TEMPLATES_FN]]({{.*}}) : $@convention(c) (Int, Int8) -> Int
5858

59-
// CHECK: [[REMOVE_REFERENCE_FN:%.*]] = function_ref @_ZN24HasStaticMemberTemplates15removeReferenceIlEET_RS1_ : $@convention(c) (UnsafeMutablePointer<Int>) -> Int
60-
// CHECK: apply [[REMOVE_REFERENCE_FN]]({{.*}}) : $@convention(c) (UnsafeMutablePointer<Int>) -> Int
59+
// CHECK: [[REMOVE_REFERENCE_FN:%.*]] = function_ref @_ZN24HasStaticMemberTemplates15removeReferenceIlEET_RS1_ : $@convention(c) (@inout Int) -> Int
60+
// CHECK: apply [[REMOVE_REFERENCE_FN]]({{.*}}) : $@convention(c) (@inout Int) -> Int
6161

6262
// CHECK-LABEL: end sil function '$s4main17testStaticMembersyyF'
6363
func testStaticMembers() {
@@ -68,8 +68,8 @@ func testStaticMembers() {
6868
HasStaticMemberTemplates.removeReference(&x)
6969
}
7070

71-
// CHECK: sil hidden_external [clang HasStaticMemberTemplates._ZN24HasStaticMemberTemplates3addIlEET_S1_S1_] @_ZN24HasStaticMemberTemplates3addIlEET_S1_S1_ : $@convention(c) (Int, Int) -> Int
71+
// CHECK: sil [clang HasStaticMemberTemplates.add] @_ZN24HasStaticMemberTemplates3addIlEET_S1_S1_ : $@convention(c) (Int, Int) -> Int
7272

73-
// CHECK: sil hidden_external [clang HasStaticMemberTemplates._ZN24HasStaticMemberTemplates15addTwoTemplatesIlcEET_S1_T0_] @_ZN24HasStaticMemberTemplates15addTwoTemplatesIlcEET_S1_T0_ : $@convention(c) (Int, Int8) -> Int
73+
// CHECK: sil [clang HasStaticMemberTemplates.addTwoTemplates] @_ZN24HasStaticMemberTemplates15addTwoTemplatesIlcEET_S1_T0_ : $@convention(c) (Int, Int8) -> Int
7474

75-
// CHECK: sil hidden_external [clang HasStaticMemberTemplates._ZN24HasStaticMemberTemplates15removeReferenceIlEET_RS1_] @_ZN24HasStaticMemberTemplates15removeReferenceIlEET_RS1_ : $@convention(c) (UnsafeMutablePointer<Int>) -> Int
75+
// CHECK: sil [clang HasStaticMemberTemplates.removeReference] @_ZN24HasStaticMemberTemplates15removeReferenceIlEET_RS1_ : $@convention(c) (@inout Int) -> Int

test/Interop/Cxx/templates/template-type-parameter-not-in-signature-module-interface.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// CHECK: func multiTemplateTypeParamOneUsedInSignature<T, U>(_ u: U, T: T.Type) -> U
66
// CHECK: func multiTemplateTypeParamNotUsedInSignatureWithUnrelatedParams<T, U>(_ x: Int32, _ y: Int32, T: T.Type, U: U.Type)
77
// CHECK: func templateTypeParamUsedInReturnType<T>(_ x: Int32) -> T
8-
// CHECK: func templateTypeParamUsedInReferenceParam<T>(_ t: UnsafeMutablePointer<T>) -> T
8+
// CHECK: func templateTypeParamUsedInReferenceParam<T>(_ t: inout T) -> T
99
// CHECK: @available(*, unavailable, message: "Variadic function is unavailable")
1010
// CHECK: func templateTypeParamNotUsedInSignatureWithVarargs<T, U>(T: T.Type, U: U.Type, _ varargs: Any...)
1111
// CHECK: @available(*, unavailable, message: "Variadic function is unavailable")

0 commit comments

Comments
 (0)