|
| 1 | +// RUN: %target-typecheck-verify-swift -swift-version 5 |
| 2 | + |
| 3 | +// Additional warnings produced in Swift 5+ mode. |
| 4 | + |
| 5 | +func takeAny(_ left: Any, _ right: Any) -> Int? { |
| 6 | + return left as? Int |
| 7 | +} |
| 8 | + |
| 9 | +func takesOptionalAny(_: Any?, _: Any?) {} |
| 10 | + |
| 11 | +class C { |
| 12 | + var a: Int! |
| 13 | + var b: Any?! |
| 14 | + func returningIUO() -> Int! { return a } |
| 15 | + func returningAny() -> Any { return a } // expected-warning {{expression implicitly coerced from 'Int?' to 'Any'}} |
| 16 | + // expected-note@-1 {{provide a default value to avoid this warning}}{{40-40= ?? <#default value#>}} |
| 17 | + // expected-note@-2 {{force-unwrap the value to avoid this warning}}{{40-40=!}} |
| 18 | + // expected-note@-3 {{explicitly cast to 'Any' with 'as Any' to silence this warning}}{{40-40= as Any}} |
| 19 | + |
| 20 | + subscript(i: Int) -> Int! { return 0 } |
| 21 | + subscript(i: Float) -> Any! { return 0 } |
| 22 | +} |
| 23 | + |
| 24 | +class D { |
| 25 | + init!() {} |
| 26 | +} |
| 27 | + |
| 28 | +func returningIUO() -> Int! { return 1 } |
| 29 | + |
| 30 | +func warnIUOToAnyCoercion(_ a: Int!, _ b: Any?!) { |
| 31 | + _ = takeAny(a, b) // expected-warning {{expression implicitly coerced from 'Int?' to 'Any'}} |
| 32 | + // expected-note@-1 {{provide a default value to avoid this warning}}{{16-16= ?? <#default value#>}} |
| 33 | + // expected-note@-2 {{force-unwrap the value to avoid this warning}}{{16-16=!}} |
| 34 | + // expected-note@-3 {{explicitly cast to 'Any' with 'as Any' to silence this warning}}{{16-16= as Any}} |
| 35 | + // expected-warning@-4 {{expression implicitly coerced from 'Any??' to 'Any'}} |
| 36 | + // expected-note@-5 {{force-unwrap the value to avoid this warning}}{{19-19=!!}} |
| 37 | + // expected-note@-6 {{explicitly cast to 'Any' with 'as Any' to silence this warning}}{{19-19= as Any}} |
| 38 | + _ = takeAny(returningIUO(), C().returningIUO()) // expected-warning {{expression implicitly coerced from 'Int?' to 'Any'}} |
| 39 | + // expected-note@-1 {{provide a default value to avoid this warning}}{{29-29= ?? <#default value#>}} |
| 40 | + // expected-note@-2 {{force-unwrap the value to avoid this warning}}{{29-29=!}} |
| 41 | + // expected-note@-3 {{explicitly cast to 'Any' with 'as Any' to silence this warning}}{{29-29= as Any}} |
| 42 | + // expected-warning@-4 {{expression implicitly coerced from 'Int?' to 'Any'}} |
| 43 | + // expected-note@-5 {{provide a default value to avoid this warning}}{{49-49= ?? <#default value#>}} |
| 44 | + // expected-note@-6 {{force-unwrap the value to avoid this warning}}{{49-49=!}} |
| 45 | + // expected-note@-7 {{explicitly cast to 'Any' with 'as Any' to silence this warning}}{{49-49= as Any}} |
| 46 | + _ = takeAny(C().a, C().b) // expected-warning {{expression implicitly coerced from 'Int?' to 'Any'}} |
| 47 | + // expected-note@-1 {{provide a default value to avoid this warning}}{{20-20= ?? <#default value#>}} |
| 48 | + // expected-note@-2 {{force-unwrap the value to avoid this warning}}{{20-20=!}} |
| 49 | + // expected-note@-3 {{explicitly cast to 'Any' with 'as Any' to silence this warning}}{{20-20= as Any}} |
| 50 | + // expected-warning@-4 {{expression implicitly coerced from 'Any??' to 'Any'}} |
| 51 | + // expected-note@-5 {{force-unwrap the value to avoid this warning}}{{27-27=!!}} |
| 52 | + // expected-note@-6 {{explicitly cast to 'Any' with 'as Any' to silence this warning}}{{27-27= as Any}} |
| 53 | + _ = takeAny(C()[0], C()[1.0]) // expected-warning {{expression implicitly coerced from 'Int?' to 'Any'}} |
| 54 | + // expected-note@-1 {{provide a default value to avoid this warning}}{{21-21= ?? <#default value#>}} |
| 55 | + // expected-note@-2 {{force-unwrap the value to avoid this warning}}{{21-21=!}} |
| 56 | + // expected-note@-3 {{explicitly cast to 'Any' with 'as Any' to silence this warning}}{{21-21= as Any}} |
| 57 | + // expected-warning@-4 {{expression implicitly coerced from 'Any?' to 'Any'}} |
| 58 | + // expected-note@-5 {{provide a default value to avoid this warning}}{{31-31= ?? <#default value#>}} |
| 59 | + // expected-note@-6 {{force-unwrap the value to avoid this warning}}{{31-31=!}} |
| 60 | + // expected-note@-7 {{explicitly cast to 'Any' with 'as Any' to silence this warning}}{{31-31= as Any}} |
| 61 | + _ = takeAny(D(), D()) // expected-warning {{expression implicitly coerced from 'D?' to 'Any'}} |
| 62 | + // expected-note@-1 {{provide a default value to avoid this warning}}{{18-18= ?? <#default value#>}} |
| 63 | + // expected-note@-2 {{force-unwrap the value to avoid this warning}}{{18-18=!}} |
| 64 | + // expected-note@-3 {{explicitly cast to 'Any' with 'as Any' to silence this warning}}{{18-18= as Any}} |
| 65 | + // expected-warning@-4 {{expression implicitly coerced from 'D?' to 'Any'}} |
| 66 | + // expected-note@-5 {{provide a default value to avoid this warning}}{{23-23= ?? <#default value#>}} |
| 67 | + // expected-note@-6 {{force-unwrap the value to avoid this warning}}{{23-23=!}} |
| 68 | + // expected-note@-7 {{explicitly cast to 'Any' with 'as Any' to silence this warning}}{{23-23= as Any}} |
| 69 | + |
| 70 | + _ = takeAny(a as Any, b as Any) |
| 71 | +} |
| 72 | + |
| 73 | +func warnIUOToOptionalAnyCoercion(_ a: Int!, _ b: Any?!, _ c: Int??!, _ d: Any???!) { |
| 74 | + takesOptionalAny(a, b) // expected-warning {{expression implicitly coerced from 'Any??' to 'Any?'}} |
| 75 | + // expected-note@-1 {{provide a default value to avoid this warning}}{{24-24= ?? <#default value#>}} |
| 76 | + // expected-note@-2 {{force-unwrap the value to avoid this warning}}{{24-24=!}} |
| 77 | + // expected-note@-3 {{explicitly cast to 'Any?' with 'as Any?' to silence this warning}}{{24-24= as Any?}} |
| 78 | + |
| 79 | + takesOptionalAny(a, b ?? "") |
| 80 | + takesOptionalAny(a, b!) |
| 81 | + takesOptionalAny(a, b as Any?) |
| 82 | + |
| 83 | + takesOptionalAny(c, d) // expected-warning {{expression implicitly coerced from 'Int???' to 'Any?'}} |
| 84 | + // expected-note@-1 {{force-unwrap the value to avoid this warning}}{{21-21=!!}} |
| 85 | + // expected-note@-2 {{explicitly cast to 'Any?' with 'as Any?' to silence this warning}}{{21-21= as Any?}} |
| 86 | + // expected-warning@-3 {{expression implicitly coerced from 'Any????' to 'Any?'}} |
| 87 | + // expected-note@-4 {{force-unwrap the value to avoid this warning}}{{24-24=!!!}} |
| 88 | + // expected-note@-5 {{explicitly cast to 'Any?' with 'as Any?' to silence this warning}}{{24-24= as Any?}} |
| 89 | + |
| 90 | + takesOptionalAny(c!!, d!!!) |
| 91 | + takesOptionalAny(c as Any?, d as Any?) |
| 92 | +} |
| 93 | + |
| 94 | +func takesCollectionOfAny(_ a: [Any], _ d: [String : Any]) {} |
| 95 | + |
| 96 | +func warnCollectionOfIUOToAnyCoercion(_ a: Int!) { |
| 97 | + takesCollectionOfAny([a], ["test" : a]) // expected-warning {{expression implicitly coerced from 'Int?' to 'Any'}} |
| 98 | + // expected-note@-1 {{provide a default value to avoid this warning}}{{26-26= ?? <#default value#>}} |
| 99 | + // expected-note@-2 {{force-unwrap the value to avoid this warning}}{{26-26=!}} |
| 100 | + // expected-note@-3 {{explicitly cast to 'Any' with 'as Any' to silence this warning}}{{26-26= as Any}} |
| 101 | + // expected-warning@-4 {{expression implicitly coerced from 'Int?' to 'Any'}} |
| 102 | + // expected-note@-5 {{provide a default value to avoid this warning}}{{40-40= ?? <#default value#>}} |
| 103 | + // expected-note@-6 {{force-unwrap the value to avoid this warning}}{{40-40=!}} |
| 104 | + // expected-note@-7 {{explicitly cast to 'Any' with 'as Any' to silence this warning}}{{40-40= as Any}} |
| 105 | +} |
0 commit comments