Skip to content

Commit b419b4f

Browse files
authored
Merge pull request #10007 from bitjammer/rdar-32433206-migrator-single-expr-closure-add-return
[Migrator] Add `return` keyword when adding temp bindings in single-e…
2 parents e2d8824 + 0a86e66 commit b419b4f

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

lib/Migrator/TupleSplatMigratorPass.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,15 @@ struct TupleSplatMigratorPass : public ASTMigratorPass,
254254
}
255255
}
256256

257-
Editor.replaceToken(closureE->getInLoc(), "= $0;");
257+
// If the original closure was a single expression without the need
258+
// for a `return` statement, it needs one now, because we've added a new
259+
// assignment statement just above.
260+
if (closureE->hasSingleExpressionBody()) {
261+
Editor.replaceToken(closureE->getInLoc(), "= $0; return");
262+
} else {
263+
Editor.replaceToken(closureE->getInLoc(), "= $0;");
264+
}
265+
258266
return true;
259267
}
260268

@@ -293,7 +301,11 @@ struct TupleSplatMigratorPass : public ASTMigratorPass,
293301
auto param = paramList->get(i);
294302
OS << param->getNameStr();
295303
}
296-
OS << ") = __val; ";
304+
OS << ") = __val;";
305+
306+
if (closureE->hasSingleExpressionBody()) {
307+
OS << " return";
308+
}
297309
}
298310

299311
Editor.replace(paramList->getSourceRange(), paramListText);

test/Migrator/tuple-arguments.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ test6({ (_, _) in })
2929
test6({ (x:Int, y:Int) in })
3030
test6({ (_, _) ->() in })
3131

32+
func test8(_: ((Int, Int)) -> Int) {}
33+
test8 { (_, _) -> Int in 2 }
34+
test8 { (x, y) in x }
35+
36+
func isEven(_ x: Int) -> Bool { return x % 2 == 0 }
37+
let items = Array(zip(0..<10, 0..<10))
38+
_ = items.filter { (_, x) in isEven(x) }
39+
_ = items.filter { _ in true }
40+
3241
func toString(indexes: Int?...) -> String {
3342
let _ = indexes.enumerated().flatMap({ (i: Int, index: Int?) -> String? in
3443
let _: Int = i

test/Migrator/tuple-arguments.swift.expected

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,20 @@ func test7(_: ((Int, Int, Int)) -> ()) {}
2626
test7({ let (x,y,z) = $0; })
2727
test6({ let (x, y) = $0; })
2828
test6({ let (_, _) = $0; })
29-
test6({ (__val:(Int, Int)) in let (x,y) = __val; })
30-
test6({ (__val:(Int, Int)) ->() in let (_,_) = __val; })
29+
test6({ (__val:(Int, Int)) in let (x,y) = __val; })
30+
test6({ (__val:(Int, Int)) ->() in let (_,_) = __val; })
31+
32+
func test8(_: ((Int, Int)) -> Int) {}
33+
test8 { (__val:(Int, Int)) -> Int in let (_,_) = __val; return 2 }
34+
test8 { let (x, y) = $0; return x }
35+
36+
func isEven(_ x: Int) -> Bool { return x % 2 == 0 }
37+
let items = Array(zip(0..<10, 0..<10))
38+
_ = items.filter { let (_, x) = $0; return isEven(x) }
39+
_ = items.filter { _ in true }
3140

3241
func toString(indexes: Int?...) -> String {
33-
let _ = indexes.enumerated().flatMap({ (__val:(Int, Int?)) -> String? in let (i,index) = __val;
42+
let _ = indexes.enumerated().flatMap({ (__val:(Int, Int?)) -> String? in let (i,index) = __val;
3443
let _: Int = i
3544
if index != nil {}
3645
return ""

0 commit comments

Comments
 (0)