Skip to content

Commit 0c0d131

Browse files
authored
Merge pull request #7856 from gottesmm/destructor_fixups
Destructor/Metatype fixups
2 parents a34697b + 772243f commit 0c0d131

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

lib/SILGen/SILGenConvert.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,7 @@ ManagedValue SILGenFunction::emitClassMetatypeToObject(SILLocation loc,
726726

727727
// Convert to an object reference.
728728
value = B.createObjCMetatypeToObject(loc, value, resultTy);
729-
730-
return ManagedValue::forUnmanaged(value);
729+
return emitManagedRValueWithCleanup(value);
731730
}
732731

733732
ManagedValue SILGenFunction::emitExistentialMetatypeToObject(SILLocation loc,
@@ -746,7 +745,7 @@ ManagedValue SILGenFunction::emitExistentialMetatypeToObject(SILLocation loc,
746745
// Convert to an object reference.
747746
value = B.createObjCExistentialMetatypeToObject(loc, value, resultTy);
748747

749-
return ManagedValue::forUnmanaged(value);
748+
return emitManagedRValueWithCleanup(value);
750749
}
751750

752751
ManagedValue SILGenFunction::emitProtocolMetatypeToObject(SILLocation loc,
@@ -763,7 +762,7 @@ ManagedValue SILGenFunction::emitProtocolMetatypeToObject(SILLocation loc,
763762
// deallocate itself. It doesn't matter if we ever actually clean up that
764763
// retain though.
765764
value = B.createCopyValue(loc, value);
766-
return ManagedValue::forUnmanaged(value);
765+
return emitManagedRValueWithCleanup(value);
767766
}
768767

769768
SILGenFunction::OpaqueValueState

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)