Skip to content

Commit f7aae22

Browse files
committed
[embedded] A few more test cases for existential casting
1 parent 3cff05d commit f7aae22

File tree

2 files changed

+191
-1
lines changed

2 files changed

+191
-1
lines changed

test/embedded/existential.swift

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,48 @@ func test7(_ p: any Any) {
216216
c.a()
217217
}
218218

219+
class BaseClass {
220+
func foo() { print("BaseClass.foo") }
221+
deinit {
222+
print("BaseClass.deinit")
223+
}
224+
}
225+
226+
class SubClass : BaseClass {
227+
override func foo() { print("SubClass.foo") }
228+
}
229+
230+
func test8(_ p: any Any) {
231+
print("test any as? SubClass")
232+
if let c = p as? SubClass {
233+
print("success")
234+
c.foo()
235+
} else {
236+
print("cast failed")
237+
}
238+
}
239+
240+
func test9(_ p: any Any) {
241+
print("test any as? BaseClass")
242+
if let c = p as? BaseClass {
243+
print("success")
244+
c.foo()
245+
} else {
246+
print("cast failed")
247+
}
248+
}
249+
250+
func test10(_ p: any Any) {
251+
print("test any as! BaseClass")
252+
let c = p as! BaseClass
253+
c.foo()
254+
}
255+
256+
func test11(_ p: any Any) {
257+
print("test any as! SubClass")
258+
let c = p as! SubClass
259+
c.foo()
260+
}
219261
@main
220262
struct Main {
221263
static func main() {
@@ -262,7 +304,7 @@ struct Main {
262304
// OUTPUT: deinit called
263305
// OUTPUT: cast failed
264306
// OUTPUT-NOT: deinit called
265-
test5(GenericStructWithClass<Int>())
307+
test5(GenericStructWithClass<Int>())
266308
// OUTPUT: test any as? MyStruct
267309
// OUTPUT: cast failed
268310
// OUTPUT: deinit called
@@ -289,5 +331,46 @@ struct Main {
289331
// OUTPUT: a LargeMyStruct 5
290332
// OUTPUT: deinit called
291333
// OUTPUT-NOT: deinit called
334+
test8(SubClass())
335+
// OUTPUT: success
336+
// OUTPUT: SubClass.foo
337+
// OUTPUT: BaseClass.deinit
338+
// OUTPUT-NOT: deinit
339+
test8(BaseClass())
340+
// OUTPUT: test any as? SubClass
341+
// OUTPUT: cast failed
342+
// OUTPUT: BaseClass.deinit
343+
// OUTPUT-NOT: deinit
344+
test9(SubClass())
345+
// OUTPUT: test any as? BaseClass
346+
// OUTPUT: success
347+
// OUTPUT: SubClass.foo
348+
// OUTPUT: BaseClass.deinit
349+
// OUTPUT-NOT: deinit
350+
test9(BaseClass())
351+
// OUTPUT: test any as? BaseClass
352+
// OUTPUT: success
353+
// OUTPUT: BaseClass.foo
354+
// OUTPUT: BaseClass.deinit
355+
// OUTPUT-NOT: deinit
356+
test9(C())
357+
// OUTPUT: test any as? BaseClass
358+
// OUTPUT: cast failed
359+
// OUTPUT-NOT: deinit
360+
test10(BaseClass())
361+
// OUTPUT: test any as! BaseClass
362+
// OUTPUT: BaseClass.foo
363+
// OUTPUT: BaseClass.deinit
364+
// OUTPUT-NOT: deinit
365+
test10(SubClass())
366+
// OUTPUT: test any as! BaseClass
367+
// OUTPUT: SubClass.foo
368+
// OUTPUT: BaseClass.deinit
369+
// OUTPUT-NOT: deinit
370+
test11(SubClass())
371+
// OUTPUT: test any as! SubClass
372+
// OUTPUT: SubClass.foo
373+
// OUTPUT: BaseClass.deinit
374+
// OUTPUT-NOT: deinit
292375
}
293376
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-clang -x c -c %S/Inputs/unbuffered-putchar.c -o %t/unbuffered-putchar.o
4+
5+
// RUN: %target-build-swift -DT1 -enable-experimental-feature Embedded \
6+
// RUN: -parse-as-library -Xlinker %t/unbuffered-putchar.o \
7+
// RUN: -enable-experimental-feature EmbeddedExistentials \
8+
// RUN: -wmo -runtime-compatibility-version none %s -o %t/t1.out
9+
// RUN: not --crash %t/t1.out 2>&1 | %FileCheck %s --check-prefix=CHECK-T1
10+
11+
// RUN: %target-build-swift -DT2 -enable-experimental-feature Embedded \
12+
// RUN: -parse-as-library -Xlinker %t/unbuffered-putchar.o \
13+
// RUN: -enable-experimental-feature EmbeddedExistentials \
14+
// RUN: -wmo -runtime-compatibility-version none %s -o %t/t2.out
15+
// RUN: not --crash %t/t2.out 2>&1 | %FileCheck %s --check-prefix=CHECK-T2
16+
17+
// RUN: %target-build-swift -DT3 -enable-experimental-feature Embedded \
18+
// RUN: -parse-as-library -Xlinker %t/unbuffered-putchar.o \
19+
// RUN: -enable-experimental-feature EmbeddedExistentials \
20+
// RUN: -wmo -runtime-compatibility-version none %s -o %t/t3.out
21+
// RUN: not --crash %t/t3.out 2>&1 | %FileCheck %s --check-prefix=CHECK-T3
22+
23+
// RUN: %target-build-swift -DT4 -enable-experimental-feature Embedded \
24+
// RUN: -parse-as-library -Xlinker %t/unbuffered-putchar.o \
25+
// RUN: -enable-experimental-feature EmbeddedExistentials \
26+
// RUN: -wmo -runtime-compatibility-version none %s -o %t/t4.out
27+
// RUN: not --crash %t/t4.out 2>&1 | %FileCheck %s --check-prefix=CHECK-T4
28+
29+
// REQUIRES: swift_in_compiler
30+
// REQUIRES: executable_test
31+
// REQUIRES: optimized_stdlib
32+
// REQUIRES: swift_feature_Embedded
33+
// REQUIRES: swift_feature_EmbeddedExistentials
34+
35+
class CP {
36+
func foo() { print("foo called") }
37+
deinit {
38+
print("deinit called")
39+
}
40+
}
41+
42+
class C : CP {
43+
override func foo() { print("C.foo called") }
44+
}
45+
46+
class CP2 {
47+
func foo() { print("CP2.foo called") }
48+
deinit {
49+
print("deinit called")
50+
}
51+
}
52+
53+
struct StructWithClass {
54+
var c = C()
55+
}
56+
57+
58+
struct LargeStructWithClass {
59+
var c = C()
60+
var t = (0, 1, 2, 3, 4, 5, 6, 7, 8)
61+
func foo() { c.foo() }
62+
}
63+
64+
struct LargetMyStruct {
65+
var l = LargeStructWithClass()
66+
}
67+
68+
func test1(_ p: any Any) {
69+
print("test any as! CP")
70+
let c = p as! CP
71+
c.foo()
72+
}
73+
74+
func test2(_ p: any Any) {
75+
print("test any as! LargeStructWithClass")
76+
let c = p as! LargeStructWithClass
77+
c.foo()
78+
}
79+
80+
@main
81+
struct Main {
82+
static func main() {
83+
#if T1
84+
test1(StructWithClass())
85+
// CHECK-T1: test any as! CP
86+
// CHECK-T1: failed cast
87+
#endif
88+
89+
#if T2
90+
test1(CP2())
91+
// CHECK-T2: test any as! CP
92+
// CHECK-T2: failed cast
93+
#endif
94+
95+
#if T3
96+
test2(StructWithClass())
97+
// CHECK-T3: test any as! LargeStructWithClass
98+
// CHECK-T3: failed cast
99+
#endif
100+
101+
#if T4
102+
test2(CP2())
103+
// CHECK-T4: test any as! LargeStructWithClass
104+
// CHECK-T4: failed cast
105+
#endif
106+
}
107+
}

0 commit comments

Comments
 (0)