|
1 |
| -// RUN: %target-typecheck-verify-swift |
| 1 | +// Run in compatibility modes that disable and enable optional flattening |
| 2 | +// in 'try?' to verify that initializer delegation diagnostics that are related |
| 3 | +// to 'try?' are stable. |
| 4 | +// RUN: %target-typecheck-verify-swift -swift-version 4 |
| 5 | +// RUN: %target-typecheck-verify-swift -swift-version 5 |
2 | 6 |
|
3 | 7 | // REQUIRES: objc_interop
|
4 | 8 | import Foundation
|
@@ -60,7 +64,7 @@ class Sub : Super {
|
60 | 64 | }
|
61 | 65 |
|
62 | 66 | convenience init(forceNonfail: Int) {
|
63 |
| - self.init(nonfail: forceNonfail)! // expected-error{{cannot force unwrap value of non-optional type 'Sub'}} {{37-38=}} |
| 67 | + self.init(nonfail: forceNonfail)! // expected-error{{cannot force unwrap value of non-optional type}} {{37-38=}} |
64 | 68 | }
|
65 | 69 |
|
66 | 70 | init(nonfail2: Int) { // okay, traps on nil
|
@@ -143,16 +147,115 @@ extension Super {
|
143 | 147 | }
|
144 | 148 |
|
145 | 149 | struct SomeStruct {
|
146 |
| - init(nonFail: Int) { // expected-note{{propagate the failure with 'init?'}}{{8-8=?}} |
147 |
| - self.init(fail: nonFail) // expected-error{{a non-failable initializer cannot delegate to failable initializer 'init(fail:)' written with 'init?'}} |
148 |
| - // expected-note@-1{{force potentially-failing result with '!'}}{{29-29=!}} |
| 150 | + init?(failable: Void) {} |
| 151 | + init!(failableIUO: Void) {} |
| 152 | + init(throws: Void) throws {} |
| 153 | + init?(failableAndThrows: Void) throws {} |
| 154 | + init!(failableIUOAndThrows: Void) throws {} |
| 155 | + |
| 156 | + init(delegationOk1: Void) { |
| 157 | + self.init(failable: ())! |
| 158 | + } |
| 159 | + |
| 160 | + init(delegationOk2: Void) { |
| 161 | + try! self.init(throws: ()) |
| 162 | + } |
| 163 | + |
| 164 | + init(delegationOk3: Void) { |
| 165 | + try! self.init(failableAndThrows: ())! |
| 166 | + } |
| 167 | + |
| 168 | + init(delegationOk4: Void) { |
| 169 | + try! self.init(failableIUOAndThrows: ()) |
| 170 | + } |
| 171 | + |
| 172 | + init(nonFailable: Void) { // expected-note{{propagate the failure with 'init?'}}{{7-7=?}} |
| 173 | + self.init(failable: ()) // expected-error{{a non-failable initializer cannot delegate to failable initializer 'init(failable:)' written with 'init?'}} |
| 174 | + // expected-note@-1{{force potentially-failing result with '!'}}{{28-28=!}} |
| 175 | + } |
| 176 | + |
| 177 | + init(delegationBad1: Void) { // expected-note {{propagate the failure with 'init?'}}{{7-7=?}} |
| 178 | + try? self.init(nonFailable: ()) |
| 179 | + // expected-warning@-1 {{no calls to throwing functions occur within 'try' expression}} |
| 180 | + // expected-error@-2 {{a non-failable initializer cannot use 'try?' to delegate to another initializer}} |
| 181 | + // expected-note@-3 {{force potentially-failing result with 'try!'}}{{5-9=try!}} |
| 182 | + } |
| 183 | + |
| 184 | + init(delegationBad2: Void) { // expected-note {{propagate the failure with 'init?'}}{{7-7=?}} |
| 185 | + try? self.init(failableIUO: ()) |
| 186 | + // expected-warning@-1 {{no calls to throwing functions occur within 'try' expression}} |
| 187 | + // expected-error@-2 {{a non-failable initializer cannot use 'try?' to delegate to another initializer}} |
| 188 | + // expected-note@-3 {{force potentially-failing result with 'try!'}}{{5-9=try!}} |
| 189 | + } |
| 190 | + |
| 191 | + init(delegationBad3: Void) { // expected-note {{propagate the failure with 'init?'}}{{7-7=?}} |
| 192 | + try? self.init(throws: ()) |
| 193 | + // expected-error@-1 {{a non-failable initializer cannot use 'try?' to delegate to another initializer}} |
| 194 | + // expected-note@-2 {{force potentially-failing result with 'try!'}}{{5-9=try!}} |
| 195 | + } |
| 196 | + |
| 197 | + init(delegationBad4: Void) { // expected-note {{propagate the failure with 'init?'}}{{7-7=?}} |
| 198 | + try! try? self.init(throws: ()) |
| 199 | + // expected-warning@-1 {{no calls to throwing functions occur within 'try' expression}} |
| 200 | + // expected-error@-2 {{a non-failable initializer cannot use 'try?' to delegate to another initializer}} |
| 201 | + // expected-note@-3 {{force potentially-failing result with 'try!'}}{{10-14=try!}} |
| 202 | + } |
| 203 | + |
| 204 | + init(delegationBad5: Void) { // expected-note {{propagate the failure with 'init?'}}{{7-7=?}} |
| 205 | + try try? self.init(throws: ()) |
| 206 | + // expected-warning@-1 {{no calls to throwing functions occur within 'try' expression}} |
| 207 | + // expected-error@-2 {{a non-failable initializer cannot use 'try?' to delegate to another initializer}} |
| 208 | + // expected-note@-3 {{force potentially-failing result with 'try!'}}{{9-13=try!}} |
| 209 | + } |
| 210 | + |
| 211 | + init(delegationBad6: Void) { // expected-note {{propagate the failure with 'init?'}}{{7-7=?}} |
| 212 | + try? self.init(failableAndThrows: ())! |
| 213 | + // expected-error@-1 {{a non-failable initializer cannot use 'try?' to delegate to another initializer}} |
| 214 | + // expected-note@-2 {{force potentially-failing result with 'try!'}}{{5-9=try!}} |
| 215 | + } |
| 216 | + |
| 217 | + init(delegationBad7: Void) { // expected-note {{propagate the failure with 'init?'}}{{7-7=?}} |
| 218 | + try! self.init(failableAndThrows: ()) |
| 219 | + // expected-error@-1 {{a non-failable initializer cannot delegate to failable initializer 'init(failableAndThrows:)' written with 'init?'}} |
| 220 | + // expected-note@-2 {{force potentially-failing result with '!'}}{{42-42=!}} |
| 221 | + } |
| 222 | + |
| 223 | + init(delegationBad8: Void) { // expected-note {{propagate the failure with 'init?'}}{{7-7=?}} |
| 224 | + try? self.init(failableIUOAndThrows: ()) |
| 225 | + // expected-error@-1 {{a non-failable initializer cannot use 'try?' to delegate to another initializer}} |
| 226 | + // expected-note@-2 {{force potentially-failing result with 'try!'}}{{5-9=try!}} |
| 227 | + } |
| 228 | + |
| 229 | + init(delegationBad9: Void) { // expected-note 2 {{propagate the failure with 'init?'}}{{7-7=?}} |
| 230 | + try? self.init(failableAndThrows: ()) |
| 231 | + // expected-error@-1 {{a non-failable initializer cannot use 'try?' to delegate to another initializer}} |
| 232 | + // expected-error@-2 {{a non-failable initializer cannot delegate to failable initializer 'init(failableAndThrows:)' written with 'init?'}} |
| 233 | + // expected-note@-3 {{force potentially-failing result with '!'}}{{42-42=!}} |
| 234 | + // expected-note@-4 {{force potentially-failing result with 'try!'}}{{5-9=try!}} |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +extension Optional { |
| 239 | + init(delegationOk1: Void) { |
| 240 | + self.init(nonFailable: ()) |
| 241 | + } |
| 242 | + |
| 243 | + init?(delegationOk2: Void) { |
| 244 | + self.init(nonFailable: ()) |
| 245 | + } |
| 246 | + |
| 247 | + init?(delegationOk3: Void) { |
| 248 | + self.init(failable: ()) |
149 | 249 | }
|
150 | 250 |
|
151 |
| - init(nonFail2: Int) { |
152 |
| - self.init(fail: nonFail2)! |
| 251 | + init(delegationBad1: Void) { // expected-note {{propagate the failure with 'init?'}}{{7-7=?}} |
| 252 | + self.init(failable: ()) |
| 253 | + // expected-error@-1 {{a non-failable initializer cannot delegate to failable initializer 'init(failable:)' written with 'init?'}} |
| 254 | + // expected-note@-2 {{force potentially-failing result with '!'}}{{28-28=!}} |
153 | 255 | }
|
154 | 256 |
|
155 |
| - init?(fail: Int) {} |
| 257 | + init(nonFailable: Void) {} |
| 258 | + init?(failable: Void) {} |
156 | 259 | }
|
157 | 260 |
|
158 | 261 | // ----------------------------------------------------------------------------
|
|
0 commit comments