Skip to content

[SILGen] Fix leak in thunk for async throwing swift conformance to ObjC requirement with optional completion. #70729

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
Jan 5, 2024
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
4 changes: 2 additions & 2 deletions lib/SILGen/SILGenBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1773,10 +1773,10 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
SILParameterInfo param) {
auto bridgedTy = param.getInterfaceType();
auto bridgedArg = emitNativeToBridgedValue(loc,
arg, nativeFormalTy,
arg.borrow(*this, loc), nativeFormalTy,
bridgedTy,
SILType::getPrimitiveObjectType(bridgedTy));
completionHandlerArgs.push_back(bridgedArg.borrow(*this, loc).getValue());
completionHandlerArgs.push_back(bridgedArg.getValue());
};

Scope completionArgDestructureScope(*this, loc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ typedef void (^NonsendableCompletionHandler)(NSString * _Nullable, NSString * _N
-(void)doSomethingSlowNullably:(NSString *)operation completionHandler:(void (^ _Nullable)(NSInteger))handler;
-(void)findAnswerNullably:(NSString *)operation completionHandler:(void (^ _Nullable)(NSString *))handler;
-(void)doSomethingDangerousNullably:(NSString *)operation completionHandler:(void (^ _Nullable)(NSString *_Nullable, NSError *_Nullable))handler;
-(void)doSomethingUnspecifiedNullablyWithCompletionHandler:(void (^ _Nullable)(NSString *_Nullable, NSError *_Nullable))handler;

// rdar://72604599
- (void)stopRecordingWithHandler:(nullable void (^)(NSObject *_Nullable_result x, NSError *_Nullable error))handler __attribute__((swift_async_name("stopRecording()"))) __attribute__((swift_async(not_swift_private, 1)));
Expand Down
7 changes: 7 additions & 0 deletions test/SILGen/objc_async_from_swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ class SlowServerlet: SlowServer {
return x
}

// CHECK-LABEL: sil{{.*}}13SlowServerlet{{.*}}30doSomethingUnspecifiedNullably{{.*}} :
// CHECK: [[GENERIC_EXECUTOR:%.*]] = enum $Optional<Builtin.Executor>, #Optional.none
// CHECK: hop_to_executor [[GENERIC_EXECUTOR]] :
override func doSomethingUnspecifiedNullably() async throws -> String {
fatalError()
}

// CHECK-LABEL: sil{{.*}}13SlowServerlet{{.*}}17doSomethingFlaggy{{.*}} :
// CHECK: [[GENERIC_EXECUTOR:%.*]] = enum $Optional<Builtin.Executor>, #Optional.none
// CHECK: hop_to_executor [[GENERIC_EXECUTOR]] :
Expand Down
67 changes: 67 additions & 0 deletions validation-test/SILGen/rdar119732084.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// RUN: %target-swift-emit-silgen -module-name Conformance -I %t %t/Conformance.swift -disable-availability-checking | %FileCheck %s

// REQUIRES: concurrency
// REQUIRES: objc_interop

//--- module.modulemap
module ObjCProto {
header "objc_proto.h"
export *
}

//--- objc_proto.h
#import <Foundation/Foundation.h>

@protocol Doable <NSObject>

- (void)doitWithCompletion:(void (^ __nullable)(NSObject * __nullable result, NSError * __nullable error))completion;

@end

//--- Conformance.swift
import ObjCProto

public final class ConformsToProto: NSObject {}

extension ConformsToProto: Doable {
// CHECK-LABEL: sil shared [thunk] [ossa] @$s11Conformance15ConformsToProtoC4doitSo8NSObjectCyYaKFyyYacfU_To : {{.*}} {
// CHECK: {{bb[0-9]+}}([[UNOWNED_MAYBE_COMPLETION:%[^,]+]] :
// CHECK: [[MAYBE_COMPLETION:%[^,]+]] = copy_block [[UNOWNED_MAYBE_COMPLETION]]
// CHECK: try_apply {{.*}} normal [[SUCCESS:bb[0-9]+]], error [[FAILURE:bb[0-9]+]]
// CHECK: [[SUCCESS]]([[RESULT:%[^,]+]] :
// CHECK: [[GUARANTEED_MAYBE_COMPLETION_SUCCESS:%[^,]+]] = begin_borrow [[MAYBE_COMPLETION]]
// CHECK: switch_enum [[GUARANTEED_MAYBE_COMPLETION_SUCCESS]]
// CHECK-SAME: case #Optional.some!enumelt: [[SUCCESS_AND_COMPLETION:bb[0-9]+]]
// CHECK-SAME: case #Optional.none!enumelt: [[SUCCESS_NO_COMPLETION:bb[0-9]+]]
// CHECK: [[SUCCESS_NO_COMPLETION]]:
// CHECK: br [[SUCCESS_JOIN:bb[0-9]+]]
// CHECK: [[SUCCESS_AND_COMPLETION]]
// CHECK: br [[SUCCESS_JOIN]]
// CHECK: [[SUCCESS_JOIN]]:
// CHECK: end_borrow [[GUARANTEED_MAYBE_COMPLETION_SUCCESS]]
// CHECK: destroy_value [[MAYBE_COMPLETION]]
// CHECK: destroy_value [[RESULT]]
// CHECK: br [[EXIT:bb[0-9]+]]
// CHECK: [[FAILURE]]([[ERROR:%[^,]+]] :
// CHECK: [[GUARANTEED_MAYBE_COMPLETION_FAILURE:%[^,]+]] = begin_borrow [[MAYBE_COMPLETION]]
// CHECK: switch_enum [[GUARANTEED_MAYBE_COMPLETION_FAILURE]]
// CHECK-SAME: case #Optional.some!enumelt: [[FAILURE_AND_COMPLETION:bb[0-9]+]]
// CHECK-SAME: case #Optional.none!enumelt: [[FAILURE_NO_COMPLETION:bb[0-9]+]]
// CHECK: [[FAILURE_NO_COMPLETION]]:
// CHECK: br [[FAILURE_JOIN:bb[0-9]+]]
// CHECK: [[FAILURE_AND_COMPLETION]]
// CHECK: br [[FAILURE_JOIN]]
// CHECK: [[FAILURE_JOIN]]:
// CHECK: end_borrow [[GUARANTEED_MAYBE_COMPLETION_FAILURE]]
// CHECK: destroy_value [[MAYBE_COMPLETION]]
// CHECK: destroy_value [[ERROR]]
// CHECK: br [[EXIT]]
// CHECK: [[EXIT]]:
// CHECK-LABEL: } // end sil function '$s11Conformance15ConformsToProtoC4doitSo8NSObjectCyYaKFyyYacfU_To'
public func doit() async throws -> NSObject {
fatalError()
}
}