Skip to content

Commit 4272552

Browse files
authored
Merge pull request #73577 from atrick/fix-init-objc-block
Fix initialization of imported ObjC block types.
2 parents 856be9f + c112e03 commit 4272552

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

lib/SIL/IR/ValueOwnership.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,22 @@ UNOWNED_OR_NONE_DEPENDING_ON_RESULT(AtomicLoad)
652652
UNOWNED_OR_NONE_DEPENDING_ON_RESULT(ExtractElement)
653653
UNOWNED_OR_NONE_DEPENDING_ON_RESULT(InsertElement)
654654
UNOWNED_OR_NONE_DEPENDING_ON_RESULT(ShuffleVector)
655-
UNOWNED_OR_NONE_DEPENDING_ON_RESULT(ZeroInitializer)
656655
#undef UNOWNED_OR_NONE_DEPENDING_ON_RESULT
657656

657+
#define OWNED_OR_NONE_DEPENDING_ON_RESULT(ID) \
658+
ValueOwnershipKind ValueOwnershipKindBuiltinVisitor::visit##ID( \
659+
BuiltinInst *BI, StringRef Attr) { \
660+
if (BI->getType().isTrivial(*BI->getFunction())) { \
661+
return OwnershipKind::None; \
662+
} \
663+
return OwnershipKind::Owned; \
664+
}
665+
// A zeroInitializer may initialize an imported struct with __unsafe_unretained
666+
// fields. The initialized value is immediately consumed by an assignment, so it
667+
// must be owned.
668+
OWNED_OR_NONE_DEPENDING_ON_RESULT(ZeroInitializer)
669+
#undef OWNED_OR_NONE_DEPENDING_ON_RESULT
670+
658671
#define BUILTIN(X,Y,Z)
659672
#define BUILTIN_SIL_OPERATION(ID, NAME, CATEGORY) \
660673
ValueOwnershipKind ValueOwnershipKindBuiltinVisitor::visit##ID( \
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#import <CoreFoundation/CoreFoundation.h>
2+
3+
typedef bool (^boolBlock)(void);
4+
5+
struct objc_bool_block {
6+
__unsafe_unretained boolBlock block;
7+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-swift-frontend -import-objc-header %S/Inputs/objc_init_blocks.h %s -emit-sil -sil-verify-all
2+
3+
// REQUIRES: objc_interop
4+
5+
// rdar://126142109: import an __unsafe_unretained block as zero-initialized.
6+
//
7+
// Make sure that the SIL ownership verifier passes.
8+
// UnsafeUnretainedBlockClass.init()
9+
// CHECK-LABEL: sil hidden @$s16objc_init_blocks26UnsafeUnretainedBlockClassCACycfc : $@convention(method) (@owned UnsafeUnretainedBlockClass) -> @owned UnsafeUnretainedBlockClass {
10+
// CHECK: [[ZI:%.*]] = builtin "zeroInitializer"<objc_bool_block>() : $objc_bool_block
11+
// CHECK: store [[ZI]] to %{{.*}} : $*objc_bool_block
12+
// CHECK-LABEL: } // end sil function '$s16objc_init_blocks26UnsafeUnretainedBlockClassCACycfc'
13+
open class UnsafeUnretainedBlockClass {
14+
public internal(set) var sc: objc_bool_block
15+
init() {
16+
self.sc = objc_bool_block()
17+
}
18+
}

0 commit comments

Comments
 (0)