Skip to content

Commit fa22946

Browse files
author
Gabor Horvath
committed
[cxx-interop] Do not create copies of optionals of nullable pointers
In reverse interop, we create copies of values that will be consumed by the Swift function. This is not necessary for pointers that are passed as swift::Optional to Swift. These are layout compatible, and consuming a pointer should not require us to do anything extra, hopefully ARC would take care of all the details. rdar://146855233
1 parent 1bd0cd7 commit fa22946

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

lib/PrintAsClang/PrintClangFunction.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,9 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
13221322
signature.visitParameterList(
13231323
[&](const LoweredFunctionSignature::IndirectResultValue &) {},
13241324
[&](const LoweredFunctionSignature::DirectParameter &param) {
1325-
if (isConsumedParameterInCaller(param.getConvention()))
1325+
if (isConsumedParameterInCaller(param.getConvention()) &&
1326+
!hasKnownOptionalNullableCxxMapping(
1327+
param.getParamDecl().getInterfaceType()))
13261328
emitParamCopyForConsume(param.getParamDecl());
13271329
++paramIndex;
13281330
},
@@ -1394,7 +1396,9 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
13941396
},
13951397
[&](const LoweredFunctionSignature::DirectParameter &param) {
13961398
printParamUse(param.getParamDecl(), /*isIndirect=*/false,
1397-
isConsumedParameterInCaller(param.getConvention()),
1399+
isConsumedParameterInCaller(param.getConvention()) &&
1400+
!hasKnownOptionalNullableCxxMapping(
1401+
param.getParamDecl().getInterfaceType()),
13981402
encodeTypeInfo(param, moduleContext, typeMapping));
13991403
},
14001404
[&](const LoweredFunctionSignature::IndirectParameter &param) {
@@ -1756,11 +1760,13 @@ void DeclAndTypeClangFunctionPrinter::printCxxSubscriptAccessorMethod(
17561760
bool DeclAndTypeClangFunctionPrinter::hasKnownOptionalNullableCxxMapping(
17571761
Type type) {
17581762
if (auto optionalObjectType = type->getOptionalObjectType()) {
1759-
if (optionalObjectType->getNominalOrBoundGenericNominal()) {
1763+
if (const auto *nominal =
1764+
optionalObjectType->getNominalOrBoundGenericNominal()) {
17601765
if (auto typeInfo = typeMapping.getKnownCxxTypeInfo(
17611766
optionalObjectType->getNominalOrBoundGenericNominal())) {
17621767
return typeInfo->canBeNullable;
17631768
}
1769+
return isa_and_nonnull<clang::ObjCInterfaceDecl>(nominal->getClangDecl());
17641770
}
17651771
}
17661772
return false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend %s -module-name UseCoreFoundation -enable-experimental-cxx-interop -clang-header-expose-decls=all-public -typecheck -verify -emit-clang-header-path %t/UseCoreFoundation.h
4+
// RUN: %target-interop-build-clangxx -std=gnu++20 -fobjc-arc -c -x objective-c++-header %t/UseCoreFoundation.h -o %t/o.o
5+
6+
// REQUIRES: objc_interop
7+
8+
import Foundation
9+
10+
public class TestFoundationType {
11+
private let _bundle: Bundle?
12+
13+
public init(resourcesBundle bundle: Bundle? = nil) {
14+
_bundle = bundle
15+
}
16+
}

0 commit comments

Comments
 (0)