Skip to content

Commit 8a81052

Browse files
committed
SILGen: Unwrap optional async callback arguments if imported return is unbridged and not optional
The bridging code handles optional wrapping and unwrapping, but in cases where a nullable completion callback argument did not need bridging, it would get short circuited out of the bridging code, and did not get unwrapped. Fixes rdar://73798726
1 parent cc45a70 commit 8a81052

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

lib/SILGen/SILGenThunk.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,14 @@ SILGenModule::getOrCreateForeignAsyncCompletionHandlerImplFunction(
315315
// FIXME: pass down formal type
316316
destBuf->getType().getASTType(),
317317
destBuf->getType().getObjectType());
318+
// Force-unwrap an argument that comes to us as Optional if it's
319+
// formally non-optional in the return.
320+
if (bridgedArg.getType().getOptionalObjectType()
321+
&& !destBuf->getType().getOptionalObjectType()) {
322+
bridgedArg = SGF.emitPreconditionOptionalHasValue(loc,
323+
bridgedArg,
324+
/*implicit*/ true);
325+
}
318326
bridgedArg.forwardInto(SGF, loc, destBuf);
319327
};
320328

test/Inputs/clang-importer-sdk/usr/include/ObjCConcurrency.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ typedef void (^CompletionHandler)(NSString * _Nullable, NSString * _Nullable_res
5151
// rdar://72604599
5252
- (void)stopRecordingWithHandler:(nullable void (^)(NSObject *_Nullable_result x, NSError *_Nullable error))handler __attribute__((swift_async_name("stopRecording()"))) __attribute__((swift_async(not_swift_private, 1)));
5353

54+
// rdar://73798726
55+
- (void)getSomeObjectWithCompletionHandler:(nullable void (^)(NSObject *_Nullable x, NSError *_Nullable error))handler;
5456
@end
5557

5658
@protocol RefrigeratorDelegate<NSObject>

test/SILGen/objc_async.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func testSlowServer(slowServer: SlowServer) async throws {
6060
let _: String = try await slowServer.doSomethingDangerousNullably("foo")
6161

6262
let _: NSObject? = try await slowServer.stopRecording()
63+
let _: NSObject = try await slowServer.someObject()
6364
}
6465

6566
// CHECK: sil{{.*}}@[[INT_COMPLETION_BLOCK]]

0 commit comments

Comments
 (0)