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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
// RUN: %empty-directory(%t)
// REQUIRES: concurrency
func manyWithError() async throws -> (String, Int) { ("", 0) }
func manyWithError(_ completion: @escaping (String?, Int?, Error?) -> Void) { }
func mixed() async -> (String, Int) { ("", 0) }
func mixed(_ completion: @escaping (String?, Int) -> Void) { }
func mixedError() async throws -> (String, Int) { ("", 0) }
func mixedError(_ completion: @escaping (String?, Int, Error?) -> Void) { }
func testParamsMulti() async throws {
// RUN: %refactor-check-compiles -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=MANYBOUND %s
manyWithError { res1, res2, err in
print("before")
if let bad = err {
print("got error \(bad)")
return
}
if let str = res1, let i = res2 {
print("got result \(str)")
print("got result \(i)")
}
print("after")
}
// MANYBOUND: do {
// MANYBOUND-NEXT: let (str, i) = try await manyWithError()
// MANYBOUND-NEXT: print("before")
// MANYBOUND-NEXT: print("got result \(str)")
// MANYBOUND-NEXT: print("got result \(i)")
// MANYBOUND-NEXT: print("after")
// MANYBOUND-NEXT: } catch let bad {
// MANYBOUND-NEXT: print("got error \(bad)")
// MANYBOUND-NEXT: }
// FIXME: This case is a little tricky: Being in the else block of 'if let str = res1'
// should allow us to place 'if let i = res2' in the failure block. However, this
// is a success condition, so we still place it in the success block. Really what
// we need to do here is check to see if manyWithError has an existing async
// alternative that still returns optional success values, and allow success
// classification in that case. Otherwise, we'd probably be better off leaving
// the condition unhandled, as it's not clear what the user is doing.
// RUN: %refactor-check-compiles -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=MANYUNBOUND-ERR %s
manyWithError { res1, res2, err in
print("before")
if let str = res1 {
print("got result \(str)")
} else if let i = res2 {
print("got result \(i)")
} else {
print("got error \(err!)")
}
print("after")
}
// MANYUNBOUND-ERR: do {
// MANYUNBOUND-ERR-NEXT: let (str, i) = try await manyWithError()
// MANYUNBOUND-ERR-NEXT: print("before")
// MANYUNBOUND-ERR-NEXT: print("got result \(str)")
// MANYUNBOUND-ERR-NEXT: print("got result \(i)")
// MANYUNBOUND-ERR-NEXT: print("after")
// MANYUNBOUND-ERR-NEXT: } catch let err {
// MANYUNBOUND-ERR-NEXT: print("got error \(err)")
// MANYUNBOUND-ERR-NEXT: }
// RUN: %refactor-check-compiles -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=MANYBOUND %s
manyWithError { res1, res2, err in
print("before")
if let bad = err {
print("got error \(bad)")
return
}
if let str = res1 {
print("got result \(str)")
}
if let i = res2 {
print("got result \(i)")
}
print("after")
}
// RUN: %refactor-check-compiles -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=MIXED-COND %s
manyWithError { res1, res2, err in
print("before")
if res1 != nil && res2 == nil {
print("got result \(res1!)")
}
print("after")
}
// MIXED-COND: convert_params_multi.swift
// MIXED-COND-NEXT: let (res1, res2) = try await manyWithError()
// MIXED-COND-NEXT: print("before")
// MIXED-COND-NEXT: if <#res1#> != nil && <#res2#> == nil {
// MIXED-COND-NEXT: print("got result \(res1)")
// MIXED-COND-NEXT: }
// MIXED-COND-NEXT: print("after")
// RUN: %refactor-check-compiles -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=MIXED-CONDELSE %s
manyWithError { res1, res2, err in
print("before")
if res1 != nil && res2 == nil {
print("got result \(res1!)")
} else {
print("bad")
}
print("after")
}
// MIXED-CONDELSE: var res1: String? = nil
// MIXED-CONDELSE-NEXT: var res2: Int? = nil
// MIXED-CONDELSE-NEXT: var err: (any Error)? = nil
// MIXED-CONDELSE-NEXT: do {
// MIXED-CONDELSE-NEXT: (res1, res2) = try await manyWithError()
// MIXED-CONDELSE-NEXT: } catch {
// MIXED-CONDELSE-NEXT: err = error
// MIXED-CONDELSE-NEXT: }
// MIXED-CONDELSE-EMPTY:
// MIXED-CONDELSE-NEXT: print("before")
// MIXED-CONDELSE-NEXT: if res1 != nil && res2 == nil {
// MIXED-CONDELSE-NEXT: print("got result \(res1!)")
// MIXED-CONDELSE-NEXT: } else {
// MIXED-CONDELSE-NEXT: print("bad")
// MIXED-CONDELSE-NEXT: }
// MIXED-CONDELSE-NEXT: print("after")
// RUN: %refactor-check-compiles -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=MANYUNBOUND-ERR %s
manyWithError { res1, res2, err in
print("before")
guard let str = res1, let i = res2 else {
print("got error \(err!)")
return
}
print("got result \(str)")
print("got result \(i)")
print("after")
}
// RUN: %refactor-check-compiles -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=MANYUNBOUND %s
manyWithError { res1, res2, err in
print("before")
guard res1 != nil && res2 != nil && err == nil else {
print("got error \(err!)")
return
}
print("got result \(res1!)")
print("got result \(res2!)")
print("after")
}
// MANYUNBOUND: do {
// MANYUNBOUND-NEXT: let (res1, res2) = try await manyWithError()
// MANYUNBOUND-NEXT: print("before")
// MANYUNBOUND-NEXT: print("got result \(res1)")
// MANYUNBOUND-NEXT: print("got result \(res2)")
// MANYUNBOUND-NEXT: print("after")
// MANYUNBOUND-NEXT: } catch let err {
// MANYUNBOUND-NEXT: print("got error \(err)")
// MANYUNBOUND-NEXT: }
// RUN: %refactor-check-compiles -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=MANYUNBOUND %s
manyWithError { res1, res2, err in
print("before")
guard res1 != nil else {
print("got error \(err!)")
return
}
print("got result \(res1!)")
print("got result \(res2!)")
print("after")
}
// RUN: %refactor-check-compiles -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=MANYUNBOUND %s
manyWithError { res1, res2, err in
print("before")
guard err == nil else {
print("got error \(err!)")
return
}
print("got result \(res1!)")
print("got result \(res2!)")
print("after")
}
// Cannot use refactor-check-compiles, as cannot use non-optional 'str' in
// optional binding.
// RUN: %refactor -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=MIXED %s
mixed { str, num in
print("before")
if let res = str {
print("got result \(res)")
}
print("\(num)")
print("after")
}
// MIXED: convert_params_multi.swift
// MIXED-NEXT: let (str, num) = await mixed()
// MIXED-NEXT: print("before")
// MIXED-NEXT: if let res = str {
// MIXED-NEXT: print("got result \(res)")
// MIXED-NEXT: }
// MIXED-NEXT: print("\(num)")
// MIXED-NEXT: print("after")
// MIXED-NOT: }
// RUN: %refactor-check-compiles -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=MIXED-ERROR %s
mixedError { str, num, err in
print("before")
if let res = str {
print("got result \(res)")
} else {
print("got \(err!)")
}
print("\(num)")
print("after")
}
// MIXED-ERROR: convert_params_multi.swift
// MIXED-ERROR-NEXT: do {
// MIXED-ERROR-NEXT: let (res, num) = try await mixedError()
// MIXED-ERROR-NEXT: print("before")
// MIXED-ERROR-NEXT: print("got result \(res)")
// MIXED-ERROR-NEXT: print("\(num)")
// MIXED-ERROR-NEXT: print("after")
// MIXED-ERROR-NEXT: } catch let err {
// MIXED-ERROR-NEXT: print("got \(err)")
// MIXED-ERROR-NEXT: }
// MIXED-ERROR-NOT: }
}
|