Skip to content

Commit a5f1da2

Browse files
roopDougGregor
authored andcommitted
[Property wrappers] New tests for @autoclosure handling
1 parent 87443fa commit a5f1da2

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

test/SILGen/property_wrappers.swift

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,46 @@ func triggerUseLazy() {
243243
_ = UseLazy<Int>(wibble: [1, 2, 3])
244244
}
245245

246+
func computeInt() -> Int {
247+
return 42
248+
}
249+
250+
func triggerUseLazyTestAutoclosure() {
251+
_ = UseLazy(bar: computeInt())
252+
253+
// triggerUseLazyTestAutoclosure()
254+
// CHECK-LABEL: sil hidden [ossa] @$s17property_wrappers29triggerUseLazyTestAutoclosureyyF
255+
256+
// computeInt() must not get called here
257+
// CHECK-NOT: // function_ref computeInt()
258+
// CHECK-NOT: function_ref @$s17property_wrappers10computeIntSiyF : $@convention(thin) () -> Int
259+
260+
// Rather, an implicit closure is referenced
261+
// CHECK: // function_ref implicit closure #1 in triggerUseLazyTestAutoclosure()
262+
// CHECK: function_ref @$s17property_wrappers29triggerUseLazyTestAutoclosureyyFSiycfu_ : $@convention(thin) () -> Int
263+
264+
// And the implicit closure calls computeInt()
265+
// CHECK: sil private [transparent] [ossa] @$s17property_wrappers29triggerUseLazyTestAutoclosureyyFSiycfu_ : $@convention(thin) () -> Int
266+
// CHECK: // function_ref computeInt()
267+
// CHECK: function_ref @$s17property_wrappers10computeIntSiyF : $@convention(thin) () -> Int
268+
}
269+
270+
@propertyWrapper
271+
struct WrapperWithNonEscapingAutoclosure<V> {
272+
var wrappedValue: V
273+
init(wrappedValue: @autoclosure () -> V) {
274+
self.wrappedValue = wrappedValue()
275+
}
276+
}
277+
278+
struct UseWrapperWithNonEscapingAutoclosure {
279+
@WrapperWithNonEscapingAutoclosure var foo: Int
280+
281+
// Memberwise init should take an Int arg, not a closure
282+
// CHECK: // UseWrapperWithNonEscapingAutoclosure.init(foo:)
283+
// CHECK: sil hidden [ossa] @$s17property_wrappers36UseWrapperWithNonEscapingAutoclosureV3fooACSi_tcfC : $@convention(method) (Int, @thin UseWrapperWithNonEscapingAutoclosure.Type) -> UseWrapperWithNonEscapingAutoclosure
284+
}
285+
246286
struct UseStatic {
247287
// CHECK: sil hidden [ossa] @$s17property_wrappers9UseStaticV12staticWibbleSaySiGvgZ
248288
// CHECK: sil private [global_init] [ossa] @$s17property_wrappers9UseStaticV13_staticWibble33_{{.*}}4LazyOySaySiGGvau
@@ -334,6 +374,24 @@ func testComposition() {
334374
_ = CompositionMembers(p1: nil)
335375
}
336376

377+
@propertyWrapper
378+
struct WrapperWithAutoclosure<V> {
379+
var wrappedValue: V
380+
init(wrappedValue: @autoclosure @escaping () -> V) {
381+
self.wrappedValue = wrappedValue()
382+
}
383+
}
384+
385+
struct CompositionWithAutoclosure {
386+
@WrapperA @WrapperB @WrapperWithAutoclosure var p1: Int
387+
@WrapperA @WrapperWithAutoclosure @WrapperB var p2: Int
388+
@WrapperWithAutoclosure @WrapperA @WrapperB var p3: Int
389+
390+
// In the memberwise init, only p1 should be a closure - p2 and p3 should be just Int
391+
// CompositionWithAutoclosure.init(p1:p2:p3:)
392+
// CHECK-LABEL: sil hidden [ossa] @$s17property_wrappers26CompositionWithAutoclosureV2p12p22p3ACSiyXA_S2itcfC : $@convention(method) (@owned @callee_guaranteed () -> Int, Int, Int, @thin CompositionWithAutoclosure.Type) -> CompositionWithAutoclosure
393+
}
394+
337395
// Observers with non-default mutatingness.
338396
@propertyWrapper
339397
struct NonMutatingSet<T> {

test/SILOptimizer/di_property_wrappers_errors.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,41 @@ struct SR_11477_S {
6868
@SR_11477_W var foo: Int
6969
init() {} // expected-error {{return from initializer without initializing all stored properties}} expected-note {{'self.foo' not initialized}}
7070
}
71+
72+
@propertyWrapper
73+
struct WrapperWithAutoclosure<V> {
74+
var wrappedValue: V
75+
init(wrappedValue: @autoclosure @escaping () -> V) {
76+
self.wrappedValue = wrappedValue()
77+
}
78+
}
79+
80+
struct UseWrapperWithAutoclosure {
81+
@WrapperWithAutoclosure var wrapped: Int
82+
83+
init() {
84+
wrapped = 42 // expected-error{{'self' used before all stored properties are initialized}}
85+
// expected-note@-1{{'self.wrapped' not initialized}}
86+
} // expected-error{{return from initializer without initializing all stored properties}}
87+
// expected-note@-1{{'self.wrapped' not initialized}}
88+
89+
init(conditional b: Bool) {
90+
if b {
91+
self._wrapped = WrapperWithAutoclosure(wrappedValue: 32)
92+
} else {
93+
wrapped = 42 // expected-error{{'self' used before all stored properties are initialized}}
94+
// expected-note@-1{{'self.wrapped' not initialized}}
95+
}
96+
} // expected-error{{return from initializer without initializing all stored properties}}
97+
// expected-note@-1{{'self.wrapped' not initialized}}
98+
99+
init(dynamic b: Bool) {
100+
if b {
101+
wrapped = 42 // expected-error{{'self' used before all stored properties are initialized}}
102+
// expected-note@-1{{'self.wrapped' not initialized}}
103+
}
104+
wrapped = 27 // expected-error{{'self' used before all stored properties are initialized}}
105+
// expected-note@-1{{'self.wrapped' not initialized}}
106+
} // expected-error{{return from initializer without initializing all stored properties}}
107+
// expected-note@-1{{'self.wrapped' not initialized}}
108+
}

0 commit comments

Comments
 (0)