Skip to content

Commit 0605ef4

Browse files
committed
[verifier] [cxx-interop] Remove empty return result from constructors.
Set the synthesized constructor's return result to nullptr and add an ASTVerifier check that the constructor's return statement does not contain a result.
1 parent 9c20198 commit 0605ef4

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

lib/AST/ASTVerifier.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,14 @@ class Verifier : public ASTWalker {
988988
}
989989

990990
if (S->hasResult()) {
991+
if (isa<ConstructorDecl>(func)) {
992+
Out << "Expected ReturnStmt not to have a result. A constructor "
993+
"should not return a result. Returned expression: ";
994+
S->getResult()->dump(Out);
995+
Out << "\n";
996+
abort();
997+
}
998+
991999
auto result = S->getResult();
9921000
auto returnType = result->getType();
9931001
// Make sure that the return has the same type as the function.

lib/ClangImporter/ImportDecl.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,7 @@ synthesizeEnumRawValueConstructorBody(AbstractFunctionDecl *afd,
502502
/*implicit*/ true);
503503
assign->setType(TupleType::getEmpty(ctx));
504504

505-
auto result = TupleExpr::createEmpty(ctx, SourceLoc(), SourceLoc(),
506-
/*Implicit=*/true);
507-
auto ret = new (ctx) ReturnStmt(SourceLoc(), result, /*Implicit=*/true);
505+
auto ret = new (ctx) ReturnStmt(SourceLoc(), nullptr, /*Implicit=*/true);
508506

509507
auto body = BraceStmt::create(ctx, SourceLoc(), {assign, ret}, SourceLoc(),
510508
/*implicit*/ true);
@@ -1366,11 +1364,7 @@ synthesizeValueConstructorBody(AbstractFunctionDecl *afd, void *context) {
13661364
}
13671365
}
13681366

1369-
auto result = TupleExpr::createEmpty(ctx, SourceLoc(), SourceLoc(),
1370-
/*Implicit=*/true);
1371-
result->setType(TupleType::getEmpty(ctx));
1372-
1373-
auto ret = new (ctx) ReturnStmt(SourceLoc(), result, /*Implicit=*/true);
1367+
auto ret = new (ctx) ReturnStmt(SourceLoc(), nullptr, /*Implicit=*/true);
13741368
stmts.push_back(ret);
13751369

13761370
// Create the function body.
@@ -1567,9 +1561,7 @@ synthesizeRawValueBridgingConstructorBody(AbstractFunctionDecl *afd,
15671561
/*Implicit=*/true);
15681562
assign->setType(TupleType::getEmpty(ctx));
15691563

1570-
auto result = TupleExpr::createEmpty(ctx, SourceLoc(), SourceLoc(),
1571-
/*Implicit=*/true);
1572-
auto ret = new (ctx) ReturnStmt(SourceLoc(), result, /*Implicit=*/true);
1564+
auto ret = new (ctx) ReturnStmt(SourceLoc(), nullptr, /*Implicit=*/true);
15731565

15741566
auto body = BraceStmt::create(ctx, SourceLoc(), {assign, ret}, SourceLoc());
15751567
return { body, /*isTypeChecked=*/true };

0 commit comments

Comments
 (0)