Skip to content

Commit 4d63543

Browse files
authored
Merge pull request #73620 from atrick/6.0-fix-init-objc-block
[6.0] Fix initialization of imported ObjC block types.
2 parents 934fce9 + 6a07658 commit 4d63543

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
@@ -637,9 +637,22 @@ UNOWNED_OR_NONE_DEPENDING_ON_RESULT(AtomicLoad)
637637
UNOWNED_OR_NONE_DEPENDING_ON_RESULT(ExtractElement)
638638
UNOWNED_OR_NONE_DEPENDING_ON_RESULT(InsertElement)
639639
UNOWNED_OR_NONE_DEPENDING_ON_RESULT(ShuffleVector)
640-
UNOWNED_OR_NONE_DEPENDING_ON_RESULT(ZeroInitializer)
641640
#undef UNOWNED_OR_NONE_DEPENDING_ON_RESULT
642641

642+
#define OWNED_OR_NONE_DEPENDING_ON_RESULT(ID) \
643+
ValueOwnershipKind ValueOwnershipKindBuiltinVisitor::visit##ID( \
644+
BuiltinInst *BI, StringRef Attr) { \
645+
if (BI->getType().isTrivial(*BI->getFunction())) { \
646+
return OwnershipKind::None; \
647+
} \
648+
return OwnershipKind::Owned; \
649+
}
650+
// A zeroInitializer may initialize an imported struct with __unsafe_unretained
651+
// fields. The initialized value is immediately consumed by an assignment, so it
652+
// must be owned.
653+
OWNED_OR_NONE_DEPENDING_ON_RESULT(ZeroInitializer)
654+
#undef OWNED_OR_NONE_DEPENDING_ON_RESULT
655+
643656
#define BUILTIN(X,Y,Z)
644657
#define BUILTIN_SIL_OPERATION(ID, NAME, CATEGORY) \
645658
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)