Skip to content

Commit f0ff799

Browse files
committed
Tests: Refactor availability_accessors.swift.
1 parent 0c7621d commit f0ff799

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

test/Sema/availability_accessors.swift

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
// RUN: %target-typecheck-verify-swift -parse-as-library -module-name MyModule
22

3-
struct Value {
4-
static let defaultValue = Value(a: Nested(b: 1))
5-
6-
struct Nested {
7-
var b: Int
8-
9-
@discardableResult
10-
mutating func setToZero() -> Int {
11-
let prev = b
12-
b = 0
13-
return prev
14-
}
3+
struct Nested {
4+
var b: Int
5+
6+
@discardableResult
7+
mutating func setToZero() -> Int {
8+
let prev = b
9+
b = 0
10+
return prev
1511
}
12+
}
13+
14+
protocol ValueProto {
15+
static var defaultValue: Self { get }
16+
17+
var a: Nested { get set }
18+
19+
subscript(_ i: Int) -> Nested { get set }
20+
21+
@discardableResult mutating func setToZero() -> Int
22+
}
23+
24+
struct StructValue: ValueProto {
25+
static var defaultValue: Self { .init(a: Nested(b: 1)) }
1626

1727
var a: Nested
1828

@@ -29,22 +39,22 @@ struct Value {
2939
}
3040
}
3141

32-
struct BaseStruct {
33-
var available: Value = .defaultValue
42+
struct BaseStruct<T: ValueProto> {
43+
var available: T = .defaultValue
3444

35-
var unavailableGetter: Value {
45+
var unavailableGetter: T {
3646
@available(*, unavailable)
3747
get { fatalError() } // expected-note 28 {{getter for 'unavailableGetter' has been explicitly marked unavailable here}}
3848
set {}
3949
}
4050

41-
var unavailableSetter: Value {
51+
var unavailableSetter: T {
4252
get { .defaultValue }
4353
@available(*, unavailable)
4454
set { fatalError() } // expected-note 21 {{setter for 'unavailableSetter' has been explicitly marked unavailable here}}
4555
}
4656

47-
var unavailableGetterAndSetter: Value {
57+
var unavailableGetterAndSetter: T {
4858
@available(*, unavailable)
4959
get { fatalError() } // expected-note 28 {{getter for 'unavailableGetterAndSetter' has been explicitly marked unavailable here}}
5060
@available(*, unavailable)
@@ -56,10 +66,8 @@ func takesIntInOut(_ i: inout Int) -> Int {
5666
return 0
5767
}
5868

59-
let someValue = Value.defaultValue
60-
6169
func testRValueLoads() {
62-
var x = BaseStruct() // expected-warning {{variable 'x' was never mutated; consider changing to 'let' constant}}
70+
var x = BaseStruct<StructValue>() // expected-warning {{variable 'x' was never mutated; consider changing to 'let' constant}}
6371

6472
_ = x.available
6573
_ = x.available.a
@@ -82,8 +90,8 @@ func testRValueLoads() {
8290
_ = x.unavailableGetterAndSetter[0].b // expected-error {{getter for 'unavailableGetterAndSetter' is unavailable}}
8391
}
8492

85-
func testLValueAssignments() {
86-
var x = BaseStruct()
93+
func testLValueAssignments(_ someValue: StructValue) {
94+
var x = BaseStruct<StructValue>()
8795

8896
x.available = someValue
8997
x.available.a = someValue.a
@@ -108,7 +116,7 @@ func testLValueAssignments() {
108116

109117
func testKeyPathLoads() {
110118
let a = [0]
111-
var x = BaseStruct()
119+
var x = BaseStruct<StructValue>()
112120

113121
_ = x[keyPath: \.available]
114122
_ = x[keyPath: \.available.a]
@@ -139,9 +147,9 @@ func testKeyPathLoads() {
139147
_ = a[keyPath: \.[takesIntInOut(&x.unavailableGetterAndSetter[0].b)]] // expected-error {{getter for 'unavailableGetterAndSetter' is unavailable}} expected-error {{setter for 'unavailableGetterAndSetter' is unavailable}}
140148
}
141149

142-
func testKeyPathAssignments() {
150+
func testKeyPathAssignments(_ someValue: StructValue) {
143151
var a = [0]
144-
var x = BaseStruct()
152+
var x = BaseStruct<StructValue>()
145153

146154
x[keyPath: \.available] = someValue
147155
x[keyPath: \.available.a] = someValue.a
@@ -174,7 +182,7 @@ func testKeyPathAssignments() {
174182
}
175183

176184
func testMutatingMember() {
177-
var x = BaseStruct()
185+
var x = BaseStruct<StructValue>()
178186
let a = [0]
179187

180188
x.available.setToZero()
@@ -205,7 +213,7 @@ func testMutatingMember() {
205213
func testPassAsInOutParameter() {
206214
func takesInOut<T>(_ t: inout T) {}
207215

208-
var x = BaseStruct()
216+
var x = BaseStruct<StructValue>()
209217

210218
takesInOut(&x.available)
211219
takesInOut(&x.available.a)
@@ -228,7 +236,7 @@ func testPassAsInOutParameter() {
228236
takesInOut(&x.unavailableGetterAndSetter[0].b) // expected-error {{getter for 'unavailableGetterAndSetter' is unavailable}} expected-error {{setter for 'unavailableGetterAndSetter' is unavailable}}
229237
}
230238

231-
var global = BaseStruct()
239+
var global = BaseStruct<StructValue>()
232240

233241
struct TestPatternBindingInitExprs {
234242
var available = global.available

0 commit comments

Comments
 (0)