Skip to content

Commit 634f719

Browse files
Merge pull request swiftlang#12362 from aschwaighofer/test_case_import_as_nested_objc_types
Add test case that uses metadata of 'import as nested types' Objective-C types
2 parents 6cc22fe + 1832bcd commit 634f719

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

test/IRGen/Inputs/usr/include/Gizmo.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,10 @@ struct StructOfNSStrings {
137137
};
138138

139139
struct StructOfNSStrings useStructOfNSStringsInObjC(struct StructOfNSStrings);
140+
141+
@interface OuterType : NSObject
142+
@end
143+
144+
__attribute__((swift_name("OuterType.InnerType")))
145+
@interface OuterTypeInnerType : NSObject
146+
@end

test/IRGen/objc_types_as_member.sil

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: rm -rf %t && mkdir -p %t
2+
// RUN: %build-irgen-test-overlays
3+
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/Inputs -I %t) %s -emit-ir | %FileCheck %s
4+
5+
// REQUIRES: objc_interop
6+
7+
import gizmo
8+
9+
sil @use_metatype : $@convention(thin) <T> (@thin T.Type) -> ()
10+
11+
// CHECK-LABEL: define swiftcc void @test(%TSo9InnerTypeC* swiftself, %swift.type* %Self, i8** %SelfWitnessTable)
12+
// CHECK: [[TMP:%.*]] = call %swift.type* @_T0So9OuterTypeCMa()
13+
// CHECK: call swiftcc void @use_metatype(%swift.type* [[TMP]])
14+
// CHECK: ret void
15+
16+
sil @test : $@convention(witness_method) (@guaranteed OuterType.InnerType) -> () {
17+
bb0(%0 : $OuterType.InnerType):
18+
%1 = function_ref @use_metatype : $@convention(thin) <τ_0_0> (@thin τ_0_0.Type) -> ()
19+
%2 = metatype $@thin OuterType.Type
20+
%3 = apply %1<OuterType>(%2) : $@convention(thin) <τ_0_0> (@thin τ_0_0.Type) -> ()
21+
return %3 : $()
22+
}
23+
24+
// CHECK-LABEL: define {{.*}}%swift.type* @_T0So9OuterTypeCMa()
25+
// CHECK: [[TMP:%.*]] = call %objc_class* @swift_rt_swift_getInitializedObjCClass(
26+
// CHECK: call %swift.type* @swift_getObjCClassMetadata(%objc_class* [[TMP]])
27+
// CHECK: ret

test/Interpreter/Inputs/ObjCClasses/ObjCClasses.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ NS_ASSUME_NONNULL_BEGIN
109109
- (void) shouldBeTrueCBool: (_Bool)value;
110110
@end
111111

112+
@interface OuterType : NSObject
113+
@end
114+
115+
__attribute__((swift_name("OuterType.InnerType")))
116+
@interface OuterTypeInnerType : NSObject
117+
@property NSArray<OuterType *> *things;
118+
@end
119+
112120
NS_ASSUME_NONNULL_END
113121

114122
#endif

test/Interpreter/Inputs/ObjCClasses/ObjCClasses.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,24 @@ - (void) shouldBeTrueCBool: (_Bool)value {
170170
}
171171

172172
@end
173+
174+
@implementation OuterType
175+
176+
- (id)init {
177+
if ((self = [super init]) != nil) {
178+
}
179+
return self;
180+
}
181+
182+
@end
183+
184+
@implementation OuterTypeInnerType
185+
186+
- (id)init {
187+
if ((self = [super init]) != nil) {
188+
self.things = [NSArray array];
189+
}
190+
return self;
191+
}
192+
193+
@end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
//
4+
// RUN: %target-clang -fobjc-arc %S/Inputs/ObjCClasses/ObjCClasses.m -c -o %t/ObjCClasses.o
5+
// RUN: %target-build-swift -O -I %S/Inputs/ObjCClasses/ -Xlinker %t/ObjCClasses.o %s -o %t/a.out
6+
// RUN: %target-run %t/a.out
7+
8+
// REQUIRES: executable_test
9+
// REQUIRES: objc_interop
10+
11+
import ObjCClasses
12+
import Foundation
13+
14+
protocol OuterProto:class {
15+
}
16+
17+
protocol InnerProto : class {
18+
var outerthings:[OuterProto] { get }
19+
}
20+
21+
extension OuterType : OuterProto {
22+
}
23+
24+
extension OuterType.InnerType: InnerProto {
25+
var outerthings:[OuterProto] {
26+
return self.things
27+
}
28+
}
29+
30+
var innerthing:InnerProto = OuterType.InnerType()
31+
32+
@inline(never)
33+
func getInnerThing() -> InnerProto {
34+
return innerthing
35+
}
36+
37+
@inline(never)
38+
func dontCrash() {
39+
let thing = getInnerThing()
40+
let devices = thing.outerthings
41+
print("Got devices: \(devices)")
42+
}
43+
44+
// CHECK: Got devices: []
45+
dontCrash()

0 commit comments

Comments
 (0)