[embedded] A few more test cases for existential casting

This commit is contained in:
Arnold Schwaighofer
2025-11-20 07:34:33 -08:00
parent 3cff05d540
commit f7aae22e43
2 changed files with 191 additions and 1 deletions

View File

@@ -216,6 +216,48 @@ func test7(_ p: any Any) {
c.a()
}
class BaseClass {
func foo() { print("BaseClass.foo") }
deinit {
print("BaseClass.deinit")
}
}
class SubClass : BaseClass {
override func foo() { print("SubClass.foo") }
}
func test8(_ p: any Any) {
print("test any as? SubClass")
if let c = p as? SubClass {
print("success")
c.foo()
} else {
print("cast failed")
}
}
func test9(_ p: any Any) {
print("test any as? BaseClass")
if let c = p as? BaseClass {
print("success")
c.foo()
} else {
print("cast failed")
}
}
func test10(_ p: any Any) {
print("test any as! BaseClass")
let c = p as! BaseClass
c.foo()
}
func test11(_ p: any Any) {
print("test any as! SubClass")
let c = p as! SubClass
c.foo()
}
@main
struct Main {
static func main() {
@@ -262,7 +304,7 @@ struct Main {
// OUTPUT: deinit called
// OUTPUT: cast failed
// OUTPUT-NOT: deinit called
test5(GenericStructWithClass<Int>())
test5(GenericStructWithClass<Int>())
// OUTPUT: test any as? MyStruct
// OUTPUT: cast failed
// OUTPUT: deinit called
@@ -289,5 +331,46 @@ struct Main {
// OUTPUT: a LargeMyStruct 5
// OUTPUT: deinit called
// OUTPUT-NOT: deinit called
test8(SubClass())
// OUTPUT: success
// OUTPUT: SubClass.foo
// OUTPUT: BaseClass.deinit
// OUTPUT-NOT: deinit
test8(BaseClass())
// OUTPUT: test any as? SubClass
// OUTPUT: cast failed
// OUTPUT: BaseClass.deinit
// OUTPUT-NOT: deinit
test9(SubClass())
// OUTPUT: test any as? BaseClass
// OUTPUT: success
// OUTPUT: SubClass.foo
// OUTPUT: BaseClass.deinit
// OUTPUT-NOT: deinit
test9(BaseClass())
// OUTPUT: test any as? BaseClass
// OUTPUT: success
// OUTPUT: BaseClass.foo
// OUTPUT: BaseClass.deinit
// OUTPUT-NOT: deinit
test9(C())
// OUTPUT: test any as? BaseClass
// OUTPUT: cast failed
// OUTPUT-NOT: deinit
test10(BaseClass())
// OUTPUT: test any as! BaseClass
// OUTPUT: BaseClass.foo
// OUTPUT: BaseClass.deinit
// OUTPUT-NOT: deinit
test10(SubClass())
// OUTPUT: test any as! BaseClass
// OUTPUT: SubClass.foo
// OUTPUT: BaseClass.deinit
// OUTPUT-NOT: deinit
test11(SubClass())
// OUTPUT: test any as! SubClass
// OUTPUT: SubClass.foo
// OUTPUT: BaseClass.deinit
// OUTPUT-NOT: deinit
}
}

View File

@@ -0,0 +1,107 @@
// RUN: %empty-directory(%t)
// RUN: %target-clang -x c -c %S/Inputs/unbuffered-putchar.c -o %t/unbuffered-putchar.o
// RUN: %target-build-swift -DT1 -enable-experimental-feature Embedded \
// RUN: -parse-as-library -Xlinker %t/unbuffered-putchar.o \
// RUN: -enable-experimental-feature EmbeddedExistentials \
// RUN: -wmo -runtime-compatibility-version none %s -o %t/t1.out
// RUN: not --crash %t/t1.out 2>&1 | %FileCheck %s --check-prefix=CHECK-T1
// RUN: %target-build-swift -DT2 -enable-experimental-feature Embedded \
// RUN: -parse-as-library -Xlinker %t/unbuffered-putchar.o \
// RUN: -enable-experimental-feature EmbeddedExistentials \
// RUN: -wmo -runtime-compatibility-version none %s -o %t/t2.out
// RUN: not --crash %t/t2.out 2>&1 | %FileCheck %s --check-prefix=CHECK-T2
// RUN: %target-build-swift -DT3 -enable-experimental-feature Embedded \
// RUN: -parse-as-library -Xlinker %t/unbuffered-putchar.o \
// RUN: -enable-experimental-feature EmbeddedExistentials \
// RUN: -wmo -runtime-compatibility-version none %s -o %t/t3.out
// RUN: not --crash %t/t3.out 2>&1 | %FileCheck %s --check-prefix=CHECK-T3
// RUN: %target-build-swift -DT4 -enable-experimental-feature Embedded \
// RUN: -parse-as-library -Xlinker %t/unbuffered-putchar.o \
// RUN: -enable-experimental-feature EmbeddedExistentials \
// RUN: -wmo -runtime-compatibility-version none %s -o %t/t4.out
// RUN: not --crash %t/t4.out 2>&1 | %FileCheck %s --check-prefix=CHECK-T4
// REQUIRES: swift_in_compiler
// REQUIRES: executable_test
// REQUIRES: optimized_stdlib
// REQUIRES: swift_feature_Embedded
// REQUIRES: swift_feature_EmbeddedExistentials
class CP {
func foo() { print("foo called") }
deinit {
print("deinit called")
}
}
class C : CP {
override func foo() { print("C.foo called") }
}
class CP2 {
func foo() { print("CP2.foo called") }
deinit {
print("deinit called")
}
}
struct StructWithClass {
var c = C()
}
struct LargeStructWithClass {
var c = C()
var t = (0, 1, 2, 3, 4, 5, 6, 7, 8)
func foo() { c.foo() }
}
struct LargetMyStruct {
var l = LargeStructWithClass()
}
func test1(_ p: any Any) {
print("test any as! CP")
let c = p as! CP
c.foo()
}
func test2(_ p: any Any) {
print("test any as! LargeStructWithClass")
let c = p as! LargeStructWithClass
c.foo()
}
@main
struct Main {
static func main() {
#if T1
test1(StructWithClass())
// CHECK-T1: test any as! CP
// CHECK-T1: failed cast
#endif
#if T2
test1(CP2())
// CHECK-T2: test any as! CP
// CHECK-T2: failed cast
#endif
#if T3
test2(StructWithClass())
// CHECK-T3: test any as! LargeStructWithClass
// CHECK-T3: failed cast
#endif
#if T4
test2(CP2())
// CHECK-T4: test any as! LargeStructWithClass
// CHECK-T4: failed cast
#endif
}
}