1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
// RUN: %target-typecheck-verify-swift -D CONDITION_1
postfix operator ++
postfix func ++ (_: Int) -> Int { 0 }
struct OneResult {}
struct TwoResult {}
protocol MyProto {
func optionalMethod() -> [Int]?
}
struct MyStruct {
var optionalMember: MyProto? { nil }
func methodOne() -> OneResult { OneResult() }
func methodTwo() -> TwoResult { TwoResult() }
}
func globalFunc<T>(_ arg: T) -> T { arg }
func testBasic(baseExpr: MyStruct) {
baseExpr
#if CONDITION_1
.methodOne() // expected-warning {{result of call to 'methodOne()' is unused}}
#else
.methodTwo()
#endif
}
MyStruct()
#if CONDITION_1
.methodOne() // expected-warning {{result of call to 'methodOne()' is unused}}
#else
.methodTwo()
#endif
func testInvalidContent(baseExpr: MyStruct, otherExpr: Int) {
baseExpr // expected-warning {{expression of type 'MyStruct' is unused}}
#if CONDITION_1
{ $0 + 1 } // expected-error {{closure expression is unused}}
#endif
baseExpr // expected-warning {{expression of type 'MyStruct' is unused}}
#if CONDITION_1
+ otherExpr // expected-error {{unary operator cannot be separated from its operand}}
// expected-warning@-1 {{result of operator '+' is unused}}
#endif
baseExpr
#if CONDITION_1
.methodOne() // expected-warning {{result of call to 'methodOne()' is unused}}
print("debug") // expected-error {{unexpected tokens in '#if' expression body}}
#endif
}
func testExprKind(baseExpr: MyStruct, idx: Int) {
baseExpr
#if CONDITION_1
.optionalMember?.optionalMethod()![idx]++ // expected-warning {{result of operator '++' is unused}}
#else
.otherMethod(arg) {
//...
}
#endif
baseExpr
#if CONDITION_1
.methodOne() + 12 // expected-error {{unexpected tokens in '#if' expression body}}
// expected-warning@-1 {{result of call to 'methodOne()' is unused}}
#endif
}
func emptyElse(baseExpr: MyStruct) {
baseExpr
#if CONDITION_1
.methodOne() // expected-warning {{result of call to 'methodOne()' is unused}}
#elseif CONDITION_2
// OK. Do nothing.
#endif
baseExpr
#if CONDITION_1
.methodOne() // expected-warning {{result of call to 'methodOne()' is unused}}
#elseif CONDITION_2
return // expected-error {{unexpected tokens in '#if' expression body}}
#endif
}
func consecutiveIfConfig(baseExpr: MyStruct) {
baseExpr
#if CONDITION_1
.methodOne()
#endif
#if CONDITION_2
.methodTwo()
#endif
.unknownMethod() // expected-error {{value of type 'OneResult' has no member 'unknownMethod'}}
}
func nestedIfConfig(baseExpr: MyStruct) {
baseExpr
#if CONDITION_1
#if CONDITION_2
.methodOne()
#endif
#if CONDITION_1
.methodTwo() // expected-warning {{result of call to 'methodTwo()' is unused}}
#endif
#else
.unknownMethod1()
#if CONDITION_2
.unknownMethod2()
#endif
#endif
}
func ifconfigExprInExpr(baseExpr: MyStruct) {
globalFunc( // expected-warning {{result of call to 'globalFunc' is unused}}
baseExpr
#if CONDITION_1
.methodOne()
#else
.methodTwo()
#endif
)
}
func canImportVersioned() {
#if canImport(A, _version: 2)
let a = 1
#endif
#if canImport(A, _version: 2.2)
let a = 1
#endif
#if canImport(A, _version: 2.2.2)
let a = 1
#endif
#if canImport(A, _version: 2.2.2.2)
let a = 1
#endif
#if canImport(A, _version: 2.2.2.2.2) // expected-warning {{trailing components of version '2.2.2.2' are ignored}}
let a = 1
#endif
#if canImport(A, _underlyingVersion: 4)
let a = 1
#endif
#if canImport(A, _underlyingVersion: 2.200)
let a = 1
#endif
#if canImport(A, _underlyingVersion: 2.200.1)
let a = 1
#endif
#if canImport(A, _underlyingVersion: 2.200.1.3)
let a = 1
#endif
#if canImport(A, unknown: 2.2) // expected-error {{2nd parameter of canImport should be labeled as _version or _underlyingVersion}}
let a = 1
#endif
#if canImport(A,) // expected-error {{unexpected ',' separator}}
let a = 1
#endif
#if canImport(A, 2.2) // expected-error {{2nd parameter of canImport should be labeled as _version or _underlyingVersion}}
let a = 1
#endif
#if canImport(A, 2.2, 1.1) // expected-error {{canImport can take only two parameters}}
let a = 1
#endif
#if canImport(A, _version:) // expected-error {{expected expression in list of expressions}}
let a = 1
#endif
#if canImport(A, _version: "") // expected-error {{_version argument cannot be empty}}
let a = 1
#endif
#if canImport(A, _version: >=2.2) // expected-error {{cannot parse module version '>=2.2'}}
let a = 1
#endif
#if canImport(A, _version: 20A301) // expected-error {{'A' is not a valid digit in integer literal}}
let a = 1
#endif
#if canImport(A, _version: "20A301") // expected-error {{cannot parse module version '20A301'}}
let a = 1
#endif
}
|