Skip to content

Commit 3e45eb7

Browse files
authored
Merge pull request #12763 from graydon/rdar-35198678-add-implicit-dtor-outside-typecheck
2 parents 90453ac + 7e363ff commit 3e45eb7

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3380,6 +3380,10 @@ class ClassDecl final : public NominalTypeDecl {
33803380
/// Retrieve the destructor for this class.
33813381
DestructorDecl *getDestructor();
33823382

3383+
/// Synthesize implicit, trivial destructor, add it to this ClassDecl
3384+
/// and return it.
3385+
DestructorDecl *addImplicitDestructor();
3386+
33833387
/// Determine whether this class inherits the convenience initializers
33843388
/// from its superclass.
33853389
///

lib/AST/Decl.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2680,6 +2680,25 @@ DestructorDecl *ClassDecl::getDestructor() {
26802680
return cast<DestructorDecl>(results.front());
26812681
}
26822682

2683+
DestructorDecl *ClassDecl::addImplicitDestructor() {
2684+
if (hasDestructor() || isInvalid())
2685+
return nullptr;
2686+
2687+
auto *selfDecl = ParamDecl::createSelf(getLoc(), this);
2688+
2689+
auto &ctx = getASTContext();
2690+
auto *DD = new (ctx) DestructorDecl(getLoc(), selfDecl, this);
2691+
2692+
DD->setImplicit();
2693+
2694+
// Create an empty body for the destructor.
2695+
DD->setBody(BraceStmt::create(ctx, getLoc(), { }, getLoc(), true));
2696+
addMember(DD);
2697+
setHasDestructor();
2698+
return DD;
2699+
}
2700+
2701+
26832702
bool ClassDecl::hasMissingDesignatedInitializers() const {
26842703
auto *mutableThis = const_cast<ClassDecl *>(this);
26852704
(void)mutableThis->lookupDirect(getASTContext().Id_init,

lib/ClangImporter/ImportDecl.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4274,6 +4274,7 @@ namespace {
42744274
auto result = createRootClass(Impl.SwiftContext.Id_Protocol,
42754275
nsObjectDecl->getDeclContext());
42764276
result->setForeignClassKind(ClassDecl::ForeignKind::RuntimeOnly);
4277+
result->addImplicitDestructor();
42774278
return result;
42784279
}
42794280

@@ -4356,8 +4357,10 @@ namespace {
43564357

43574358
if (declaredNative)
43584359
markMissingSwiftDecl(result);
4359-
if (decl->getAttr<clang::ObjCRuntimeVisibleAttr>())
4360+
if (decl->getAttr<clang::ObjCRuntimeVisibleAttr>()) {
43604361
result->setForeignClassKind(ClassDecl::ForeignKind::RuntimeOnly);
4362+
result->addImplicitDestructor();
4363+
}
43614364

43624365
// If this Objective-C class has a supertype, import it.
43634366
SmallVector<TypeLoc, 4> inheritedTypes;
@@ -4389,6 +4392,7 @@ namespace {
43894392
if (decl->getName() == "OS_object" ||
43904393
decl->getName() == "OS_os_log") {
43914394
result->setForeignClassKind(ClassDecl::ForeignKind::RuntimeOnly);
4395+
result->addImplicitDestructor();
43924396
}
43934397

43944398
// If the superclass is runtime-only, our class is also. This only

lib/Sema/CodeSynthesis.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,20 +2207,7 @@ swift::createDesignatedInitOverride(TypeChecker &tc,
22072207
}
22082208

22092209
void TypeChecker::addImplicitDestructor(ClassDecl *CD) {
2210-
if (CD->hasDestructor() || CD->isInvalid())
2211-
return;
2212-
2213-
auto *selfDecl = ParamDecl::createSelf(CD->getLoc(), CD);
2214-
2215-
auto *DD = new (Context) DestructorDecl(CD->getLoc(), selfDecl, CD);
2216-
2217-
DD->setImplicit();
2218-
2219-
// Type-check the destructor declaration.
2220-
typeCheckDecl(DD, /*isFirstPass=*/true);
2221-
2222-
// Create an empty body for the destructor.
2223-
DD->setBody(BraceStmt::create(Context, CD->getLoc(), { }, CD->getLoc(), true));
2224-
CD->addMember(DD);
2225-
CD->setHasDestructor();
2210+
auto *DD = CD->addImplicitDestructor();
2211+
if (DD)
2212+
typeCheckDecl(DD, /*isFirstPass=*/true);
22262213
}

0 commit comments

Comments
 (0)