Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit be0ca0a

Browse files
authored
Merge pull request swiftlang#34515 from DougGregor/objc-async-throws-non-optional
[Concurrency] Implicitly strip optionals for return type of translated "async throws".
2 parents 1203074 + 4bcccec commit be0ca0a

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2065,7 +2065,13 @@ static Type decomposeCompletionHandlerType(
20652065
paramIdx == *info.completionHandlerErrorParamIndex())
20662066
continue;
20672067

2068-
resultTypeElts.push_back(param.getPlainType());
2068+
// If there is an error parameter, remove nullability.
2069+
Type paramType = param.getPlainType();
2070+
// TODO: Clang should gain a nullability form that overrides this.
2071+
if (info.completionHandlerErrorParamIndex())
2072+
paramType = paramType->lookThroughAllOptionalTypes();
2073+
2074+
resultTypeElts.push_back(paramType);
20692075
}
20702076

20712077
switch (resultTypeElts.size()) {

lib/Sema/TypeCheckDeclObjC.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,8 +1946,10 @@ bool swift::fixDeclarationName(InFlightDiagnostic &diag, const ValueDecl *decl,
19461946
}
19471947

19481948
// Fix the argument names that need fixing.
1949-
assert(name.getArgumentNames().size()
1950-
== targetName.getArgumentNames().size());
1949+
if (name.getArgumentNames().size()
1950+
!= targetName.getArgumentNames().size())
1951+
return false;
1952+
19511953
auto params = func->getParameters();
19521954
for (unsigned i = 0, n = name.getArgumentNames().size(); i != n; ++i) {
19531955
auto origArg = name.getArgumentNames()[i];

test/ClangImporter/objc_async.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import ObjCConcurrency
88
func testSlowServer(slowServer: SlowServer) async throws {
99
let _: Int = await slowServer.doSomethingSlow("mail")
1010
let _: Bool = await slowServer.checkAvailability()
11-
let _: String = try await slowServer.findAnswer() ?? "nope"
12-
let _: String = await try slowServer.findAnswerFailingly() ?? "nope"
11+
let _: String = try await slowServer.findAnswer()
12+
let _: String = await try slowServer.findAnswerFailingly()
1313
let _: Void = await slowServer.doSomethingFun("jump")
1414
let _: (Int) -> Void = slowServer.completionHandler
1515

test/IDE/print_clang_objc_async.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
// CHECK-DAG: func doSomethingSlow(_ operation: String, completionHandler handler: @escaping (Int) -> Void)
1111
// CHECK-DAG: func doSomethingSlow(_ operation: String) async -> Int
1212
// CHECK-DAG: func doSomethingDangerous(_ operation: String, completionHandler handler: ((String?, Error?) -> Void)? = nil)
13-
// CHECK-DAG: func doSomethingDangerous(_ operation: String) async throws -> String?
13+
// CHECK-DAG: func doSomethingDangerous(_ operation: String) async throws -> String
1414
// CHECK-DAG: func checkAvailability(completionHandler: @escaping (Bool) -> Void)
1515
// CHECK-DAG: func checkAvailability() async -> Bool
1616
// CHECK-DAG: func findAnswer(completionHandler handler: @escaping (String?, Error?) -> Void)
17-
// CHECK-DAG: func findAnswer() async throws -> String?
17+
// CHECK-DAG: func findAnswer() async throws -> String
1818
// CHECK-DAG: func findAnswerFailingly(completionHandler handler: @escaping (String?, Error?) -> Void) throws
19-
// CHECK-DAG: func findAnswerFailingly() async throws -> String?
19+
// CHECK-DAG: func findAnswerFailingly() async throws -> String
2020
// CHECK-DAG: func doSomethingFun(_ operation: String) async
2121
// CHECK: {{^[}]$}}
2222

test/decl/protocol/conforms/objc_async.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import ObjCConcurrency
77

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

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

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

0 commit comments

Comments
 (0)