From 096777eda7020e02593bf3daca53046e503aba10 Mon Sep 17 00:00:00 2001 From: zoecarver Date: Wed, 24 Jun 2020 14:02:02 -0700 Subject: [PATCH] [cxx-interop] Do not give the default C++ constructor's return statement a result. The default C++ object constructor assigns the newly created object out of the function so, it should not return a value. Returning a value will trigger SILGen assertions. --- lib/ClangImporter/ImportDecl.cpp | 6 +----- validation-test/SILGen/Inputs/cxx-types.h | 3 +++ validation-test/SILGen/Inputs/module.modulemap | 3 +++ .../SILGen/cxx-address-only-object-init.swift | 12 ++++++++++++ 4 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 validation-test/SILGen/Inputs/cxx-types.h create mode 100644 validation-test/SILGen/Inputs/module.modulemap create mode 100644 validation-test/SILGen/cxx-address-only-object-init.swift diff --git a/lib/ClangImporter/ImportDecl.cpp b/lib/ClangImporter/ImportDecl.cpp index 6ee66bc9003a5..08fecf5e59d77 100644 --- a/lib/ClangImporter/ImportDecl.cpp +++ b/lib/ClangImporter/ImportDecl.cpp @@ -1272,11 +1272,7 @@ synthesizeStructDefaultConstructorBody(AbstractFunctionDecl *afd, auto assign = new (ctx) AssignExpr(lhs, SourceLoc(), call, /*implicit*/ true); assign->setType(emptyTuple); - auto result = TupleExpr::createEmpty(ctx, SourceLoc(), SourceLoc(), - /*Implicit=*/true); - result->setType(emptyTuple); - - auto ret = new (ctx) ReturnStmt(SourceLoc(), result, /*Implicit=*/true); + auto ret = new (ctx) ReturnStmt(SourceLoc(), nullptr, /*Implicit=*/true); // Create the function body. auto body = BraceStmt::create(ctx, SourceLoc(), {assign, ret}, SourceLoc()); diff --git a/validation-test/SILGen/Inputs/cxx-types.h b/validation-test/SILGen/Inputs/cxx-types.h new file mode 100644 index 0000000000000..1a883e6f2e126 --- /dev/null +++ b/validation-test/SILGen/Inputs/cxx-types.h @@ -0,0 +1,3 @@ +struct HasCustomCopyConst { + HasCustomCopyConst(HasCustomCopyConst const&) { } +}; diff --git a/validation-test/SILGen/Inputs/module.modulemap b/validation-test/SILGen/Inputs/module.modulemap new file mode 100644 index 0000000000000..cb4d7c21edee3 --- /dev/null +++ b/validation-test/SILGen/Inputs/module.modulemap @@ -0,0 +1,3 @@ +module CXXTypes { + header "cxx-types.h" +} diff --git a/validation-test/SILGen/cxx-address-only-object-init.swift b/validation-test/SILGen/cxx-address-only-object-init.swift new file mode 100644 index 0000000000000..0d39b9e4a231e --- /dev/null +++ b/validation-test/SILGen/cxx-address-only-object-init.swift @@ -0,0 +1,12 @@ +// RUN: %target-swift-frontend -enable-cxx-interop -I %S/Inputs %s -emit-silgen | %FileCheck %s + +import CXXTypes + +// Just make sure we create the object and don't crash. +// CHECK-LABEL: @$s4main4testyyF +// CHECK: alloc_stack +// CHECK: apply +// CHECK: return %{{[0-9]+}} : $() +public func test() { + let c = HasCustomCopyConst() +}