Skip to content

Commit 753322c

Browse files
committed
[Type checker] Lazily synthesize body of default constructors.
1 parent d4bb649 commit 753322c

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

lib/AST/Decl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3777,7 +3777,7 @@ DestructorDecl *ClassDecl::getDestructor() {
37773777

37783778
/// Synthesizer callback for an empty implicit function body.
37793779
static std::pair<BraceStmt *, bool>
3780-
synthesizeEmplyFunctionBody(AbstractFunctionDecl *afd, void *context) {
3780+
synthesizeEmptyFunctionBody(AbstractFunctionDecl *afd, void *context) {
37813781
ASTContext &ctx = afd->getASTContext();
37823782
return { BraceStmt::create(ctx, afd->getLoc(), { }, afd->getLoc(), true),
37833783
/*isTypeChecked=*/true };
@@ -3794,7 +3794,7 @@ void ClassDecl::addImplicitDestructor() {
37943794
DD->setValidationToChecked();
37953795

37963796
// Synthesize an empty body for the destructor as needed.
3797-
DD->setBodySynthesizer(synthesizeEmplyFunctionBody);
3797+
DD->setBodySynthesizer(synthesizeEmptyFunctionBody);
37983798
addMember(DD);
37993799

38003800
// Propagate access control and versioned-ness.

lib/Sema/TypeCheckDecl.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5599,6 +5599,16 @@ void TypeChecker::synthesizeMemberForLookup(NominalTypeDecl *target,
55995599
}
56005600
}
56015601

5602+
/// Synthesizer callback for a function body consisting of "return".
5603+
static std::pair<BraceStmt *, bool>
5604+
synthesizeSingleReturnFunctionBody(AbstractFunctionDecl *afd, void *) {
5605+
ASTContext &ctx = afd->getASTContext();
5606+
SmallVector<ASTNode, 1> stmts;
5607+
stmts.push_back(new (ctx) ReturnStmt(afd->getLoc(), nullptr));
5608+
return { BraceStmt::create(ctx, afd->getLoc(), stmts, afd->getLoc(), true),
5609+
/*isTypeChecked=*/true };
5610+
}
5611+
56025612
void TypeChecker::defineDefaultConstructor(NominalTypeDecl *decl) {
56035613
FrontendStatsTracer StatsTracer(Context.Stats, "define-default-ctor", decl);
56045614
PrettyStackTraceDecl stackTrace("defining default constructor for",
@@ -5624,11 +5634,8 @@ void TypeChecker::defineDefaultConstructor(NominalTypeDecl *decl) {
56245634
// Add the constructor.
56255635
decl->addMember(ctor);
56265636

5627-
// Create an empty body for the default constructor.
5628-
SmallVector<ASTNode, 1> stmts;
5629-
stmts.push_back(new (Context) ReturnStmt(decl->getLoc(), nullptr));
5630-
ctor->setBody(BraceStmt::create(Context, SourceLoc(), stmts, SourceLoc()),
5631-
AbstractFunctionDecl::BodyKind::TypeChecked);
5637+
// Lazily synthesize an empty body for the default constructor.
5638+
ctor->setBodySynthesizer(synthesizeSingleReturnFunctionBody);
56325639
}
56335640

56345641
static void validateAttributes(TypeChecker &TC, Decl *D) {

0 commit comments

Comments
 (0)