Skip to content

Commit 772243f

Browse files
committed
[silgen] Fix deallocating destructor emission to begin a borrow scope around the class's call to its destroying destructor.
1 parent 6e44e54 commit 772243f

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

lib/SILGen/SILGenDestructor.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "SILGenFunction.h"
1414
#include "RValue.h"
15+
#include "Scope.h"
1516
#include "swift/AST/AST.h"
1617
#include "swift/SIL/TypeLowering.h"
1718

@@ -96,9 +97,13 @@ void SILGenFunction::emitDeallocatingDestructor(DestructorDecl *dd) {
9697
= emitSiblingMethodRef(loc, selfValue, dtorConstant, subs);
9798

9899
// Call the destroying destructor.
99-
SILType objectPtrTy = SILType::getNativeObjectType(F.getASTContext());
100-
selfValue = B.createApply(loc, dtorValue.forward(*this),
101-
dtorTy, objectPtrTy, subs, selfValue);
100+
{
101+
FullExpr CleanupScope(Cleanups, CleanupLocation::get(loc));
102+
ManagedValue borrowedSelf = emitManagedBeginBorrow(loc, selfValue);
103+
SILType objectPtrTy = SILType::getNativeObjectType(F.getASTContext());
104+
selfValue = B.createApply(loc, dtorValue.forward(*this),
105+
dtorTy, objectPtrTy, subs, borrowedSelf.getUnmanagedValue());
106+
}
102107

103108
// Deallocate the object.
104109
selfValue = B.createUncheckedRefCast(loc, selfValue, classTy);

test/SILGen/lifetime.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,9 @@ class Foo<T> {
527527
// CHECK-LABEL: sil hidden @_T08lifetime3FooCfD : $@convention(method) <T> (@owned Foo<T>) -> ()
528528
// CHECK: bb0([[SELF:%[0-9]+]] : $Foo<T>):
529529
// CHECK: [[DESTROYING_REF:%[0-9]+]] = function_ref @_T08lifetime3FooCfd : $@convention(method) <τ_0_0> (@guaranteed Foo<τ_0_0>) -> @owned Builtin.NativeObject
530-
// CHECK-NEXT: [[RESULT_SELF:%[0-9]+]] = apply [[DESTROYING_REF]]<T>([[SELF]]) : $@convention(method) <τ_0_0> (@guaranteed Foo<τ_0_0>) -> @owned Builtin.NativeObject
530+
// CHECK-NEXT: [[BORROWED_SELF:%.*]] = begin_borrow [[SELF]]
531+
// CHECK-NEXT: [[RESULT_SELF:%[0-9]+]] = apply [[DESTROYING_REF]]<T>([[BORROWED_SELF]]) : $@convention(method) <τ_0_0> (@guaranteed Foo<τ_0_0>) -> @owned Builtin.NativeObject
532+
// CHECK-NEXT: end_borrow [[BORROWED_SELF]] from [[SELF]]
531533
// CHECK-NEXT: [[SELF:%[0-9]+]] = unchecked_ref_cast [[RESULT_SELF]] : $Builtin.NativeObject to $Foo<T>
532534
// CHECK-NEXT: dealloc_ref [[SELF]] : $Foo<T>
533535
// CHECK-NEXT: [[RESULT:%[0-9]+]] = tuple ()

0 commit comments

Comments
 (0)