Skip to content

[Migrator] Add return keyword when adding temp bindings in single-e… #10007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/Migrator/TupleSplatMigratorPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,15 @@ struct TupleSplatMigratorPass : public ASTMigratorPass,
}
}

Editor.replaceToken(closureE->getInLoc(), "= $0;");
// If the original closure was a single expression without the need
// for a `return` statement, it needs one now, because we've added a new
// assignment statement just above.
if (closureE->hasSingleExpressionBody()) {
Editor.replaceToken(closureE->getInLoc(), "= $0; return");
} else {
Editor.replaceToken(closureE->getInLoc(), "= $0;");
}

return true;
}

Expand Down Expand Up @@ -293,7 +301,11 @@ struct TupleSplatMigratorPass : public ASTMigratorPass,
auto param = paramList->get(i);
OS << param->getNameStr();
}
OS << ") = __val; ";
OS << ") = __val;";

if (closureE->hasSingleExpressionBody()) {
OS << " return";
}
}

Editor.replace(paramList->getSourceRange(), paramListText);
Expand Down
9 changes: 9 additions & 0 deletions test/Migrator/tuple-arguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ test6({ (_, _) in })
test6({ (x:Int, y:Int) in })
test6({ (_, _) ->() in })

func test8(_: ((Int, Int)) -> Int) {}
test8 { (_, _) -> Int in 2 }
test8 { (x, y) in x }

func isEven(_ x: Int) -> Bool { return x % 2 == 0 }
let items = Array(zip(0..<10, 0..<10))
_ = items.filter { (_, x) in isEven(x) }
_ = items.filter { _ in true }

func toString(indexes: Int?...) -> String {
let _ = indexes.enumerated().flatMap({ (i: Int, index: Int?) -> String? in
let _: Int = i
Expand Down
15 changes: 12 additions & 3 deletions test/Migrator/tuple-arguments.swift.expected
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,20 @@ func test7(_: ((Int, Int, Int)) -> ()) {}
test7({ let (x,y,z) = $0; })
test6({ let (x, y) = $0; })
test6({ let (_, _) = $0; })
test6({ (__val:(Int, Int)) in let (x,y) = __val; })
test6({ (__val:(Int, Int)) ->() in let (_,_) = __val; })
test6({ (__val:(Int, Int)) in let (x,y) = __val; })
test6({ (__val:(Int, Int)) ->() in let (_,_) = __val; })

func test8(_: ((Int, Int)) -> Int) {}
test8 { (__val:(Int, Int)) -> Int in let (_,_) = __val; return 2 }
test8 { let (x, y) = $0; return x }

func isEven(_ x: Int) -> Bool { return x % 2 == 0 }
let items = Array(zip(0..<10, 0..<10))
_ = items.filter { let (_, x) = $0; return isEven(x) }
_ = items.filter { _ in true }

func toString(indexes: Int?...) -> String {
let _ = indexes.enumerated().flatMap({ (__val:(Int, Int?)) -> String? in let (i,index) = __val;
let _ = indexes.enumerated().flatMap({ (__val:(Int, Int?)) -> String? in let (i,index) = __val;
let _: Int = i
if index != nil {}
return ""
Expand Down