Skip to content

Commit 5ce5096

Browse files
committed
[Test] Add more tests for @_typeEraser
1 parent 3cdc30f commit 5ce5096

File tree

2 files changed

+96
-17
lines changed

2 files changed

+96
-17
lines changed

test/Sema/type_eraser.swift

Lines changed: 84 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,103 @@ class AnyP: P {
77
@_typeEraser(AnyP)
88
protocol P {}
99

10-
class ConcreteP: P {}
10+
struct ConcreteP: P, Hashable {}
1111

12-
dynamic func opaque() -> some P {
12+
// CHECK-LABEL: testBasic
13+
dynamic func testBasic() -> some P {
1314
// CHECK: underlying_to_opaque_expr{{.*}}'some P'
1415
// CHECK-NEXT: call_expr implicit type='AnyP'{{.*}}arg_labels=erasing:
1516
// CHECK: call_expr type='ConcreteP'
1617
ConcreteP()
1718
}
1819

19-
class AnyQ: Q {
20-
init<T: Q>(erasing: T) {}
20+
// CHECK-LABEL: testTypeAlias
21+
typealias AliasForP = P
22+
dynamic func testTypeAlias() -> some AliasForP {
23+
// CHECK: underlying_to_opaque_expr{{.*}}'some P'
24+
// CHECK-NEXT: call_expr implicit type='AnyP'{{.*}}arg_labels=erasing:
25+
// CHECK: call_expr type='ConcreteP'
26+
ConcreteP()
27+
}
28+
29+
// CHECK-LABEL: testNoDynamic
30+
func testNoDynamic() -> some P {
31+
// CHECK: underlying_to_opaque_expr{{.*}}'some P'
32+
// CHECK-NEXT: call_expr type='ConcreteP'
33+
ConcreteP()
2134
}
22-
@_typeEraser(AnyQ)
23-
protocol Q {}
2435

25-
struct ConcreteQ: Q {}
36+
// CHECK-LABEL: testNoOpaque
37+
dynamic func testNoOpaque() -> P {
38+
// CHECK: erasure_expr implicit type='P'
39+
// CHECK-NEXT: normal_conformance type=ConcreteP protocol=P
40+
// CHECK-NEXT: call_expr type='ConcreteP'
41+
ConcreteP()
42+
}
2643

44+
// CHECK-LABEL: testComposition
45+
typealias Composition = P & Hashable
46+
dynamic func testComposition() -> some Composition {
47+
// CHECK: underlying_to_opaque_expr{{.*}}'some Hashable & P'
48+
// CHECK-NEXT: call_expr type='ConcreteP'
49+
ConcreteP()
50+
}
51+
52+
// CHECK-LABEL: struct_decl{{.*}}Builder
2753
@_functionBuilder
2854
struct Builder {
29-
static func buildBlock(_ params: Q...) -> ConcreteQ {
30-
return ConcreteQ()
55+
static func buildBlock(_ params: P...) -> ConcreteP {
56+
return ConcreteP()
3157
}
3258
}
3359

34-
// CHECK-LABEL: transformFnBody
35-
@Builder
36-
dynamic var transformFnBody: some Q {
37-
// CHECK: return_stmt
38-
// CHECK-NEXT: underlying_to_opaque_expr implicit type='some Q'
39-
// CHECK-NEXT: call_expr implicit type='AnyQ'{{.*}}arg_labels=erasing:
40-
// CHECK: declref_expr implicit type='@lvalue ConcreteQ'
41-
ConcreteQ()
60+
// CHECK-LABEL: TestFunctionBuilder
61+
class TestFunctionBuilder {
62+
// CHECK-LABEL: testTransformFnBody
63+
@Builder dynamic var testTransformFnBody: some P {
64+
// CHECK: return_stmt
65+
// CHECK-NEXT: underlying_to_opaque_expr implicit type='some P'
66+
// CHECK-NEXT: call_expr implicit type='AnyP'{{.*}}arg_labels=erasing:
67+
// CHECK: declref_expr implicit type='@lvalue ConcreteP'
68+
ConcreteP()
69+
}
70+
71+
// CHECK-LABEL: func_decl{{.*}}takesBuilder
72+
func takesBuilder(@Builder closure: () -> ConcreteP) -> ConcreteP { closure() }
73+
74+
// CHECK-LABEL: testClosureBuilder
75+
dynamic var testClosureBuilder: some P {
76+
// CHECK: underlying_to_opaque_expr implicit type='some P'
77+
// CHECK-NEXT: call_expr implicit type='AnyP'{{.*}}arg_labels=erasing:
78+
// CHECK: closure_expr type='() -> ConcreteP'
79+
takesBuilder {
80+
// CHECK: return_stmt
81+
// CHECK-NEXT: load_expr implicit type='ConcreteP'
82+
ConcreteP()
83+
}
84+
}
85+
}
86+
87+
// CHECK-LABEL: class_decl{{.*}}DynamicReplacement
88+
class DynamicReplacement {
89+
dynamic func testDynamicReplaceable() -> some P {
90+
// CHECK: underlying_to_opaque_expr implicit type='some P'
91+
// CHECK-NEXT: call_expr implicit type='AnyP'{{.*}}arg_labels=erasing:
92+
// CHECK: call_expr type='ConcreteP'
93+
ConcreteP()
94+
}
95+
}
96+
97+
// CHECK-LABEL: extension_decl{{.*}}DynamicReplacement
98+
extension DynamicReplacement {
99+
// CHECK-LABEL: testDynamicReplacement
100+
@_dynamicReplacement(for: testDynamicReplaceable)
101+
func testDynamicReplacement() -> some P {
102+
print("not single expr return")
103+
// CHECK: return_stmt
104+
// CHECK-NEXT: underlying_to_opaque_expr implicit type='some P'
105+
// CHECK-NEXT: call_expr implicit type='AnyP'{{.*}}arg_labels=erasing:
106+
// CHECK: call_expr type='ConcreteP'
107+
return ConcreteP()
108+
}
42109
}

test/Serialization/serialize_attr.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,15 @@ public class CC<T : PP> {
8282
// CHECK-DAG: sil [serialized] [_specialize exported: false, kind: full, where T == Int, U == Float] [canonical] [ossa] @$s14serialize_attr14specializeThis_1uyx_q_tr0_lF : $@convention(thin) <T, U> (@in_guaranteed T, @in_guaranteed U) -> () {
8383

8484
// CHECK-DAG: sil [serialized] [noinline] [_specialize exported: false, kind: full, where T == RR, U == SS] [canonical] [ossa] @$s14serialize_attr2CCC3foo_1gqd___AA2GGVyxGtqd___AHtAA2QQRd__lF : $@convention(method) <T where T : PP><U where U : QQ> (@in_guaranteed U, GG<T>, @guaranteed CC<T>) -> (@out U, GG<T>) {
85+
86+
87+
// @_typeEraser
88+
// -----------------------------------------------------------------------------
89+
90+
public class AnyP: P {
91+
public init<T: P>(erasing: T) {}
92+
}
93+
94+
// CHECK-DAG: @_typeEraser(AnyP) protocol P
95+
@_typeEraser(AnyP)
96+
public protocol P {}

0 commit comments

Comments
 (0)