Skip to content

[verifier] [cxx-interop] Remove empty return result from constructors. #32656

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
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
8 changes: 8 additions & 0 deletions lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,14 @@ class Verifier : public ASTWalker {
}

if (S->hasResult()) {
if (isa<ConstructorDecl>(func)) {
Out << "Expected ReturnStmt not to have a result. A constructor "
"should not return a result. Returned expression: ";
S->getResult()->dump(Out);
Out << "\n";
abort();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to double-check, return nil in failable constructors does not trigger this new error, right? It should be covered by existing tests I think, but worth checking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A failable constructor will return an Optional<Class> where the element is .none. So, this error is not triggered (also, for good measure, I ran a test program and it didn't trigger this error).

}

auto result = S->getResult();
auto returnType = result->getType();
// Make sure that the return has the same type as the function.
Expand Down
14 changes: 3 additions & 11 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,7 @@ synthesizeEnumRawValueConstructorBody(AbstractFunctionDecl *afd,
/*implicit*/ true);
assign->setType(TupleType::getEmpty(ctx));

auto result = TupleExpr::createEmpty(ctx, SourceLoc(), SourceLoc(),
/*Implicit=*/true);
auto ret = new (ctx) ReturnStmt(SourceLoc(), result, /*Implicit=*/true);
auto ret = new (ctx) ReturnStmt(SourceLoc(), nullptr, /*Implicit=*/true);

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

auto result = TupleExpr::createEmpty(ctx, SourceLoc(), SourceLoc(),
/*Implicit=*/true);
result->setType(TupleType::getEmpty(ctx));

auto ret = new (ctx) ReturnStmt(SourceLoc(), result, /*Implicit=*/true);
auto ret = new (ctx) ReturnStmt(SourceLoc(), nullptr, /*Implicit=*/true);
stmts.push_back(ret);

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

auto result = TupleExpr::createEmpty(ctx, SourceLoc(), SourceLoc(),
/*Implicit=*/true);
auto ret = new (ctx) ReturnStmt(SourceLoc(), result, /*Implicit=*/true);
auto ret = new (ctx) ReturnStmt(SourceLoc(), nullptr, /*Implicit=*/true);

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