Skip to content

Destructor/Metatype fixups #7856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/SILGen/SILGenConvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -726,8 +726,7 @@ ManagedValue SILGenFunction::emitClassMetatypeToObject(SILLocation loc,

// Convert to an object reference.
value = B.createObjCMetatypeToObject(loc, value, resultTy);

return ManagedValue::forUnmanaged(value);
return emitManagedRValueWithCleanup(value);
}

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

return ManagedValue::forUnmanaged(value);
return emitManagedRValueWithCleanup(value);
}

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

SILGenFunction::OpaqueValueState
Expand Down
11 changes: 8 additions & 3 deletions lib/SILGen/SILGenDestructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "SILGenFunction.h"
#include "RValue.h"
#include "Scope.h"
#include "swift/AST/AST.h"
#include "swift/SIL/TypeLowering.h"

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

// Call the destroying destructor.
SILType objectPtrTy = SILType::getNativeObjectType(F.getASTContext());
selfValue = B.createApply(loc, dtorValue.forward(*this),
dtorTy, objectPtrTy, subs, selfValue);
{
FullExpr CleanupScope(Cleanups, CleanupLocation::get(loc));
ManagedValue borrowedSelf = emitManagedBeginBorrow(loc, selfValue);
SILType objectPtrTy = SILType::getNativeObjectType(F.getASTContext());
selfValue = B.createApply(loc, dtorValue.forward(*this),
dtorTy, objectPtrTy, subs, borrowedSelf.getUnmanagedValue());
}

// Deallocate the object.
selfValue = B.createUncheckedRefCast(loc, selfValue, classTy);
Expand Down
4 changes: 3 additions & 1 deletion test/SILGen/lifetime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,9 @@ class Foo<T> {
// CHECK-LABEL: sil hidden @_T08lifetime3FooCfD : $@convention(method) <T> (@owned Foo<T>) -> ()
// CHECK: bb0([[SELF:%[0-9]+]] : $Foo<T>):
// CHECK: [[DESTROYING_REF:%[0-9]+]] = function_ref @_T08lifetime3FooCfd : $@convention(method) <τ_0_0> (@guaranteed Foo<τ_0_0>) -> @owned Builtin.NativeObject
// CHECK-NEXT: [[RESULT_SELF:%[0-9]+]] = apply [[DESTROYING_REF]]<T>([[SELF]]) : $@convention(method) <τ_0_0> (@guaranteed Foo<τ_0_0>) -> @owned Builtin.NativeObject
// CHECK-NEXT: [[BORROWED_SELF:%.*]] = begin_borrow [[SELF]]
// CHECK-NEXT: [[RESULT_SELF:%[0-9]+]] = apply [[DESTROYING_REF]]<T>([[BORROWED_SELF]]) : $@convention(method) <τ_0_0> (@guaranteed Foo<τ_0_0>) -> @owned Builtin.NativeObject
// CHECK-NEXT: end_borrow [[BORROWED_SELF]] from [[SELF]]
// CHECK-NEXT: [[SELF:%[0-9]+]] = unchecked_ref_cast [[RESULT_SELF]] : $Builtin.NativeObject to $Foo<T>
// CHECK-NEXT: dealloc_ref [[SELF]] : $Foo<T>
// CHECK-NEXT: [[RESULT:%[0-9]+]] = tuple ()
Expand Down