Skip to content

Commit 810bc89

Browse files
Merge pull request #13613 from aschwaighofer/fix_unowned_refcount_insertion
When inserting retain/releases for sil_unowned types use the right in…
2 parents 7eebf86 + c82df2d commit 810bc89

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

lib/SILOptimizer/Utils/Local.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ swift::createIncrementBefore(SILValue Ptr, SILInstruction *InsertPt) {
5454

5555
// If Ptr is refcounted itself, create the strong_retain and
5656
// return.
57-
if (Ptr->getType().isReferenceCounted(B.getModule()))
58-
return B.createStrongRetain(Loc, Ptr, B.getDefaultAtomicity());
57+
if (Ptr->getType().isReferenceCounted(B.getModule())) {
58+
if (Ptr->getType().is<UnownedStorageType>())
59+
return B.createUnownedRetain(Loc, Ptr, B.getDefaultAtomicity());
60+
else
61+
return B.createStrongRetain(Loc, Ptr, B.getDefaultAtomicity());
62+
}
5963

6064
// Otherwise, create the retain_value.
6165
return B.createRetainValue(Loc, Ptr, B.getDefaultAtomicity());
@@ -74,8 +78,12 @@ swift::createDecrementBefore(SILValue Ptr, SILInstruction *InsertPt) {
7478
auto Loc = RegularLocation(SourceLoc());
7579

7680
// If Ptr has reference semantics itself, create a strong_release.
77-
if (Ptr->getType().isReferenceCounted(B.getModule()))
78-
return B.createStrongRelease(Loc, Ptr, B.getDefaultAtomicity());
81+
if (Ptr->getType().isReferenceCounted(B.getModule())) {
82+
if (Ptr->getType().is<UnownedStorageType>())
83+
return B.createUnownedRelease(Loc, Ptr, B.getDefaultAtomicity());
84+
else
85+
return B.createStrongRelease(Loc, Ptr, B.getDefaultAtomicity());
86+
}
7987

8088
// Otherwise create a release value.
8189
return B.createReleaseValue(Loc, Ptr, B.getDefaultAtomicity());

test/SILOptimizer/mandatory_inlining.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,17 @@ func invoke(_ c: C) {
160160
_ = class_constrained_generic(c)
161161
// CHECK: return
162162
}
163+
164+
// Make sure we don't crash.
165+
@_transparent
166+
public func mydo(_ what: @autoclosure () -> ()) {
167+
what()
168+
}
169+
public class A {
170+
public func bar() {}
171+
public func foo(_ act: (@escaping () ->()) -> ()) {
172+
act { [unowned self] in
173+
mydo( self.bar() )
174+
}
175+
}
176+
}

0 commit comments

Comments
 (0)