Skip to content

Commit 2373449

Browse files
authored
Merge pull request #12945 from rjmccall/block-to-func-formal-bridging-4.1
Bridge the formal block type in a block-to-func thunk
2 parents 1c17a90 + c41f3a5 commit 2373449

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

lib/SILGen/SILGenBridging.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,12 @@ expandTupleTypes(AnyFunctionType::CanParamArrayRef params) {
356356
return results;
357357
}
358358

359+
static CanAnyFunctionType getBridgedBlockType(SILGenModule &SGM,
360+
CanAnyFunctionType blockType) {
361+
return SGM.Types.getBridgedFunctionType(AbstractionPattern(blockType),
362+
blockType, blockType->getExtInfo());
363+
}
364+
359365
static void buildFuncToBlockInvokeBody(SILGenFunction &SGF,
360366
SILLocation loc,
361367
CanAnyFunctionType formalFuncType,
@@ -369,10 +375,7 @@ static void buildFuncToBlockInvokeBody(SILGenFunction &SGF,
369375
SILFunctionConventions funcConv(funcTy, SGF.SGM.M);
370376

371377
// Make sure we lower the component types of the formal block type.
372-
formalBlockType =
373-
SGF.SGM.Types.getBridgedFunctionType(AbstractionPattern(formalBlockType),
374-
formalBlockType,
375-
formalBlockType->getExtInfo());
378+
formalBlockType = getBridgedBlockType(SGF.SGM, formalBlockType);
376379

377380
// Set up the indirect result.
378381
SILType blockResultTy = blockTy->getAllResultsType();
@@ -762,6 +765,9 @@ static void buildBlockToFuncThunkBody(SILGenFunction &SGF,
762765
// Collect the native arguments, which should all be +1.
763766
Scope scope(SGF.Cleanups, CleanupLocation::get(loc));
764767

768+
// Make sure we lower the component types of the formal block type.
769+
formalBlockTy = getBridgedBlockType(SGF.SGM, formalBlockTy);
770+
765771
assert(blockTy->getNumParameters() == funcTy->getNumParameters()
766772
&& "block and function types don't match");
767773

test/SILGen/Inputs/usr/include/BridgeTestFoundation.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,5 @@ void escapeBlockAlias(dispatch_block_t block);
9393
@interface ObjectWithSplitProperty : NSObject
9494
@property (nonatomic, setter=private_setFlagForSomething:) BOOL flagForSomething;
9595
@end
96+
97+
extern NSString * __nonnull (^ const __nonnull GlobalBlock)(NSString * __nonnull);

test/SILGen/objc_blocks_bridging.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,11 @@ struct GenericStruct<T> {
188188
// CHECK-LABEL: sil shared [transparent] [serializable] [reabstraction_thunk] @_T0Ix_Ixx_IyB_IyBy_TR : $@convention(c) (@inout_aliasable @block_storage @noescape @callee_owned (@owned @noescape @callee_owned () -> ()) -> (), @convention(block) @noescape () -> ()) -> ()
189189
// CHECK-LABEL: sil shared [transparent] [serializable] [reabstraction_thunk] @_T0IyB_Ix_TR : $@convention(thin) (@owned @convention(block) @noescape () -> ()) -> ()
190190

191+
// rdar://35402696
192+
func takeOptStringFunction(fn: (String) -> String?) {}
193+
func testGlobalBlock() {
194+
takeOptStringFunction(fn: GlobalBlock)
195+
}
196+
// CHECK-LABEL: sil hidden @_T020objc_blocks_bridging15testGlobalBlockyyF
197+
// CHECK: global_addr @GlobalBlock : $*@convention(block) (NSString) -> @autoreleased Optional<NSString>
198+
// CHECK: function_ref @_T0So8NSStringCABSgIeyBya_S2SIexxo_TR : $@convention(thin) (@owned String, @owned @convention(block) (NSString) -> @autoreleased Optional<NSString>) -> @owned String

0 commit comments

Comments
 (0)