Skip to content

[Concurrency] Implicitly strip optionals for return type of translated "async throws". #34515

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
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
8 changes: 7 additions & 1 deletion lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2065,7 +2065,13 @@ static Type decomposeCompletionHandlerType(
paramIdx == *info.completionHandlerErrorParamIndex())
continue;

resultTypeElts.push_back(param.getPlainType());
// If there is an error parameter, remove nullability.
Type paramType = param.getPlainType();
// TODO: Clang should gain a nullability form that overrides this.
if (info.completionHandlerErrorParamIndex())
paramType = paramType->lookThroughAllOptionalTypes();

resultTypeElts.push_back(paramType);
}

switch (resultTypeElts.size()) {
Expand Down
6 changes: 4 additions & 2 deletions lib/Sema/TypeCheckDeclObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1946,8 +1946,10 @@ bool swift::fixDeclarationName(InFlightDiagnostic &diag, const ValueDecl *decl,
}

// Fix the argument names that need fixing.
assert(name.getArgumentNames().size()
== targetName.getArgumentNames().size());
if (name.getArgumentNames().size()
!= targetName.getArgumentNames().size())
return false;

auto params = func->getParameters();
for (unsigned i = 0, n = name.getArgumentNames().size(); i != n; ++i) {
auto origArg = name.getArgumentNames()[i];
Expand Down
4 changes: 2 additions & 2 deletions test/ClangImporter/objc_async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import ObjCConcurrency
func testSlowServer(slowServer: SlowServer) async throws {
let _: Int = await slowServer.doSomethingSlow("mail")
let _: Bool = await slowServer.checkAvailability()
let _: String = try await slowServer.findAnswer() ?? "nope"
let _: String = await try slowServer.findAnswerFailingly() ?? "nope"
let _: String = try await slowServer.findAnswer()
let _: String = await try slowServer.findAnswerFailingly()
let _: Void = await slowServer.doSomethingFun("jump")
let _: (Int) -> Void = slowServer.completionHandler

Expand Down
6 changes: 3 additions & 3 deletions test/IDE/print_clang_objc_async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
// CHECK-DAG: func doSomethingSlow(_ operation: String, completionHandler handler: @escaping (Int) -> Void)
// CHECK-DAG: func doSomethingSlow(_ operation: String) async -> Int
// CHECK-DAG: func doSomethingDangerous(_ operation: String, completionHandler handler: ((String?, Error?) -> Void)? = nil)
// CHECK-DAG: func doSomethingDangerous(_ operation: String) async throws -> String?
// CHECK-DAG: func doSomethingDangerous(_ operation: String) async throws -> String
// CHECK-DAG: func checkAvailability(completionHandler: @escaping (Bool) -> Void)
// CHECK-DAG: func checkAvailability() async -> Bool
// CHECK-DAG: func findAnswer(completionHandler handler: @escaping (String?, Error?) -> Void)
// CHECK-DAG: func findAnswer() async throws -> String?
// CHECK-DAG: func findAnswer() async throws -> String
// CHECK-DAG: func findAnswerFailingly(completionHandler handler: @escaping (String?, Error?) -> Void) throws
// CHECK-DAG: func findAnswerFailingly() async throws -> String?
// CHECK-DAG: func findAnswerFailingly() async throws -> String
// CHECK-DAG: func doSomethingFun(_ operation: String) async
// CHECK: {{^[}]$}}

Expand Down
4 changes: 2 additions & 2 deletions test/decl/protocol/conforms/objc_async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ObjCConcurrency

// Conform via async method
class C1: ConcurrentProtocol {
func askUser(toSolvePuzzle puzzle: String) async throws -> String? { nil }
func askUser(toSolvePuzzle puzzle: String) async throws -> String { "" }

func askUser(toJumpThroughHoop hoop: String) async -> String { "hello" }
}
Expand All @@ -26,7 +26,7 @@ class C2: ConcurrentProtocol {
// Conform via both; this is an error
class C3: ConcurrentProtocol {
// expected-note@+1{{method 'askUser(toSolvePuzzle:)' declared here}}
func askUser(toSolvePuzzle puzzle: String) async throws -> String? { nil }
func askUser(toSolvePuzzle puzzle: String) async throws -> String { "" }

// expected-error@+1{{'askUser(toSolvePuzzle:completionHandler:)' with Objective-C selector 'askUserToSolvePuzzle:completionHandler:' conflicts with method 'askUser(toSolvePuzzle:)' with the same Objective-C selector}}
func askUser(toSolvePuzzle puzzle: String, completionHandler: ((String?, Error?) -> Void)?) {
Expand Down