Skip to content

Commit 2f8c32c

Browse files
committed
[cxx-interop] Import complex inferred dependent return types
1 parent 6ac87ba commit 2f8c32c

File tree

5 files changed

+51
-17
lines changed

5 files changed

+51
-17
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ namespace {
228228

229229
// TODO: Add support for dependent types (SR-13809).
230230
#define DEPENDENT_TYPE(Class, Base) \
231-
ImportResult Visit##Class##Type(const clang::Class##Type *) { return Type(); }
231+
ImportResult Visit##Class##Type(const clang::Class##Type *) { return Impl.SwiftContext.TheAnyType; }
232232
#define TYPE(Class, Base)
233233
#include "clang/AST/TypeNodes.inc"
234234

test/Interop/Cxx/templates/Inputs/dependent-types.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ template<class T, class U>
2626
M<U> differentDependentArgAndRet(M<T> a) { return {a.value}; }
2727

2828
template<class T>
29-
M<T> dependantReturnTypeInffered(T a) { return {a}; }
29+
M<T> dependantReturnTypeInferred(T a) { return {a}; }
3030

3131
template<class T>
3232
M<T> dependantReturnTypeSameAsArg(M<T> a) { return {a.value}; }
3333

3434
// TODO: still not supported yet (rdar://89034704)
3535
template<class T, class U>
36-
typename M<U>::U complexDifferentDependentArgAndRet(typename M<T>::U a) { return a.value; }
36+
typename M<U>::U complexDifferentDependentArgAndRet(typename M<T>::U a) { return a; }
3737

3838
template<class T>
39-
typename M<T>::U complexDependantReturnTypeInffered(T a) { return a; }
39+
typename M<T>::U complexDependantReturnTypeInferred(T a) { return a; }
4040

4141
template<class T>
42-
typename M<T>::U complexDependantReturnTypeSameAsArg(typename M<T>::U a) { return a.value; }
42+
typename M<T>::U complexDependantReturnTypeSameAsArg(typename M<T>::U a) { return a; }
4343

4444
template<class T>
4545
M<T> multipleArgs(M<T> a, T b, int c) { return {a.value + b}; }
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// RUN: %target-swift-ide-test -print-module -module-to-print=DependentTypes -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s
22

33
// CHECK: func differentDependentArgAndRet<T, U>(_ a: Any, T: T.Type, U: U.Type) -> Any
4-
// CHECK: func dependantReturnTypeInffered<T>(_ a: T) -> Any
4+
// CHECK: func dependantReturnTypeInferred<T>(_ a: T) -> Any
55
// CHECK: func dependantReturnTypeSameAsArg<T>(_ a: Any, T: T.Type) -> Any
6+
// CHECK: func complexDependantReturnTypeInferred<T>(_ a: T) -> Any
67
// CHECK: func multipleArgs<T>(_ a: Any, _ b: T, _ c: Int32) -> Any
78
// CHECK: func multipleDependentArgsInferred<T, U>(_ a: Any, _ b: Any, _ c: T, _ d: U) -> Any
89
// CHECK: func multipleDependentArgs<T, U>(_ a: Any, _ b: Any, T: T.Type, U: U.Type) -> Any
910
// CHECK: func refToDependent<T>(_ a: inout T) -> Any
10-
// We don't support references to dependent types (rdar://89034440).
11+
// TODO: Currently not imported (rdar://89034440).
1112
// CHECK-NOT: dependentRef
1213
// CHECK-NOT: dependentRefAndRefInferred

test/Interop/Cxx/templates/dependent-types-silgen.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import DependentTypes
66
// CHECK-LABEL: sil [ossa] @$s4main4tests5Int64VyF : $@convention(thin) () -> Int64
77
// CHECK: [[ANY_OUT:%.*]] = alloc_stack $Any
88

9-
// CHECK: [[THUNK_REF:%.*]] = function_ref @$sSC27dependantReturnTypeInfferedyyps5Int64VF : $@convention(thin) (Int64) -> @out Any
9+
// CHECK: [[THUNK_REF:%.*]] = function_ref @$sSC27dependantReturnTypeInferredyyps5Int64VF : $@convention(thin) (Int64) -> @out Any
1010
// CHECK: apply [[THUNK_REF]]([[ANY_OUT]], %{{[0-9]+}}) : $@convention(thin) (Int64) -> @out Any
1111

1212
// CHECK: [[SPEC_OUT:%.*]] = alloc_stack $__CxxTemplateInst1MIxE
@@ -24,21 +24,21 @@ import DependentTypes
2424

2525

2626
// Check the synthesized thunk:
27-
// CHECK-LABEL: sil [transparent] [serialized] [ossa] @$sSC27dependantReturnTypeInfferedyyps5Int64VF : $@convention(thin) (Int64) -> @out Any
27+
// CHECK-LABEL: sil [transparent] [serialized] [ossa] @$sSC27dependantReturnTypeInferredyyps5Int64VF : $@convention(thin) (Int64) -> @out Any
2828
// CHECK: bb0(%0 : $*Any, %1 : $Int64):
2929
// CHECK: [[SPEC_OUT:%.*]] = alloc_stack $__CxxTemplateInst1MIxE
3030

31-
// CHECK: [[FN:%.*]] = function_ref @{{_Z27dependantReturnTypeInfferedIxE1MIT_ES1_|\?\?\$dependantReturnTypeInffered@_J@@YA\?AU\?\$M@_J@@_J@Z}} : $@convention(c) (Int64) -> __CxxTemplateInst1MIxE
31+
// CHECK: [[FN:%.*]] = function_ref @{{_Z27dependantReturnTypeInferredIxE1MIT_ES1_|\?\?\$dependantReturnTypeInferred@_J@@YA\?AU\?\$M@_J@@_J@Z}} : $@convention(c) (Int64) -> __CxxTemplateInst1MIxE
3232
// CHECK: [[OUT:%.*]] = apply [[FN]](%1) : $@convention(c) (Int64) -> __CxxTemplateInst1MIxE
3333

3434
// CHECK: store [[OUT]] to [trivial] [[SPEC_OUT]] : $*__CxxTemplateInst1MIxE
3535
// CHECK: unconditional_checked_cast_addr __CxxTemplateInst1MIxE in [[SPEC_OUT]] : $*__CxxTemplateInst1MIxE to Any in %0 : $*Any
36-
// CHECK-LABEL: end sil function '$sSC27dependantReturnTypeInfferedyyps5Int64VF'
36+
// CHECK-LABEL: end sil function '$sSC27dependantReturnTypeInferredyyps5Int64VF'
3737

3838
public func test() -> Int64 {
39-
let m = dependantReturnTypeInffered(Int64(42)) as! M<Int64>
39+
let m = dependantReturnTypeInferred(Int64(42)) as! M<Int64>
4040
return m.getValue()
4141
}
4242

4343
// CHECK-LABEL: sil [clang __CxxTemplateInst1MIxE.getValue] @{{_ZNK1MIxE8getValueEv|\?getValue@\?\$M@_J@@QEBA_JXZ}} : $@convention(cxx_method) (@in_guaranteed __CxxTemplateInst1MIxE) -> Int64
44-
// CHECK-LABEL: sil [serialized] [clang dependantReturnTypeInffered] @{{_Z27dependantReturnTypeInfferedIxE1MIT_ES1_|\?\?\$dependantReturnTypeInffered@_J@@YA\?AU\?\$M@_J@@_J@Z}} : $@convention(c) (Int64) -> __CxxTemplateInst1MIxE
44+
// CHECK-LABEL: sil [serialized] [clang dependantReturnTypeInferred] @{{_Z27dependantReturnTypeInferredIxE1MIT_ES1_|\?\?\$dependantReturnTypeInferred@_J@@YA\?AU\?\$M@_J@@_J@Z}} : $@convention(c) (Int64) -> __CxxTemplateInst1MIxE

test/Interop/Cxx/templates/dependent-types.swift

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ DependentTypesTestSuite.test("Different dependent arg and return type.") {
1919
}
2020

2121
DependentTypesTestSuite.test("Different dependent inferred by arg.") {
22-
let m = dependantReturnTypeInffered(42) as! M<Int>
22+
let m = dependantReturnTypeInferred(42) as! M<Int>
2323
expectEqual(m.getValue(), 42)
2424
}
2525

2626
DependentTypesTestSuite.test("Instantiate the same function twice") {
2727
// Intentionally test the same thing twice.
28-
let m = dependantReturnTypeInffered(42) as! M<Int>
28+
let m = dependantReturnTypeInferred(42) as! M<Int>
2929
expectEqual(m.getValue(), 42)
3030

31-
let m2 = dependantReturnTypeInffered(42) as! M<Int>
31+
let m2 = dependantReturnTypeInferred(42) as! M<Int>
3232
expectEqual(m2.getValue(), 42)
3333
}
3434

@@ -58,7 +58,6 @@ DependentTypesTestSuite.test("Takes const ref and returns dependent type.") {
5858
expectEqual(m.getValue(), 42)
5959
}
6060

61-
6261
// We still have some problems calling methods on Windows: SR-13129 and rdar://88391102
6362
#if !os(Windows)
6463
DependentTypesTestSuite.test("Function template methods") {
@@ -83,6 +82,40 @@ DependentTypesTestSuite.test("Function template methods (static)") {
8382
let m2 = M<Int>.memberDependentReturnTypeStatic(CInt(32)) as! M<CInt>
8483
expectEqual(m2.getValue(), 32)
8584
}
85+
86+
DependentTypesTestSuite.test("Complex different dependent return type inferrd.") {
87+
let m = complexDependantReturnTypeInferred(M<Int>(value: 42)) as! M<Int>
88+
expectEqual(m.getValue(), 42)
89+
}
90+
91+
// TODO: Currently still failing: Could not cast value of type '__C.__CxxTemplateInst1MIlE' to 'Swift.Int'
92+
DependentTypesTestSuite.test("Complex different dependent argument and return type") {
93+
let m = complexDifferentDependentArgAndRet(42, T: Int.self, U: Int.self) as! Int
94+
expectEqual(m, 42)
95+
96+
let m2 = complexDependantReturnTypeSameAsArg(42, T: Int.self) as! Int
97+
expectEqual(m2, 42)
98+
}
99+
86100
#endif // Windows
87101

102+
//TODO: Import issue: rdar://89028943
103+
// DependentTypesTestSuite.test("Dependent to Reference") {
104+
// var x = 42
105+
// let m = dependentToRef(x) as! M<Int>
106+
// expectEqual(m.getValue(), 42)
107+
// }
108+
109+
//TODO: Not imported: rdar://89034440
110+
// DependentTypesTestSuite.test("Dependent Reference.") {
111+
// let m = dependentRef()
112+
// expectEqual(m.getValue(), 42)
113+
// }
114+
115+
//TODO: Not imported: rdar://89034440
116+
// DependentTypesTestSuite.test("Dependent reference and reference inferred") {
117+
// let m = dependentRefAndRefInferred(M<Int>(value: 40), 2) as! M<Int>
118+
// expectEqual(m.getValue(), 42)
119+
// }
120+
88121
runAllTests()

0 commit comments

Comments
 (0)