Skip to content

Commit 46ffc2a

Browse files
authored
Merge pull request #82879 from egorzhdan/egorzhdan/always-addressable
[cxx-interop] Unconditionally apply `@_addressable` for C++ functions
2 parents ca50986 + cd58e65 commit 46ffc2a

File tree

5 files changed

+18
-12
lines changed

5 files changed

+18
-12
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2724,8 +2724,12 @@ static ParamDecl *getParameterInfo(ClangImporter::Implementation *impl,
27242724
// C++ types taking a reference might return a reference/pointer to a
27252725
// subobject of the referenced storage. In those cases we need to prevent the
27262726
// Swift compiler to pass in a temporary copy to prevent dangling.
2727-
if (ASTContext.LangOpts.hasFeature(Feature::AddressableParameters) &&
2728-
!param->getType().isNull() && param->getType()->isReferenceType()) {
2727+
auto paramContext = param->getDeclContext();
2728+
if (!param->getType().isNull() && param->getType()->isLValueReferenceType() &&
2729+
!swiftParamTy->isForeignReferenceType() &&
2730+
!(isa<clang::FunctionDecl>(paramContext) &&
2731+
cast<clang::FunctionDecl>(paramContext)->isOverloadedOperator()) &&
2732+
!isa<clang::ObjCMethodDecl, clang::ObjCContainerDecl>(paramContext)) {
27292733
paramInfo->setAddressable();
27302734
}
27312735

lib/SILGen/SILGenType.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "swift/SIL/SILVTableVisitor.h"
3939
#include "swift/SIL/SILWitnessVisitor.h"
4040
#include "swift/SIL/TypeLowering.h"
41+
#include "clang/AST/Decl.h"
4142

4243
using namespace swift;
4344
using namespace Lowering;
@@ -454,8 +455,11 @@ template<typename T> class SILGenWitnessTable : public SILWitnessVisitor<T> {
454455
// () -> NSString
455456
// But the first is correct, so make sure we don't mark this witness
456457
// as foreign.
457-
if (dyn_cast_or_null<clang::CXXMethodDecl>(
458-
witness.getDecl()->getClangDecl()))
458+
const auto *clangDecl = witness.getDecl()->getClangDecl();
459+
if (clangDecl &&
460+
(dyn_cast<clang::CXXMethodDecl>(clangDecl) ||
461+
(isa<clang::FunctionDecl>(clangDecl) &&
462+
cast<clang::FunctionDecl>(clangDecl)->isOverloadedOperator())))
459463
newDecl = newDecl.asForeign();
460464
return addMethodImplementation(
461465
requirementRef, getWitnessRef(newDecl, witness), witness);

test/Interop/Cxx/class/nonescapable-lifetimebound.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,20 @@ private:
130130
MoveOnly moveOnlyId(const MoveOnly& p [[clang::lifetimebound]]);
131131

132132
// CHECK: sil [clang makeOwner] {{.*}}: $@convention(c) () -> Owner
133-
// CHECK: sil [clang getView] {{.*}} : $@convention(c) (@in_guaranteed Owner) -> @lifetime(borrow address_for_deps 0) @owned View
134-
// CHECK: sil [clang getViewFromFirst] {{.*}} : $@convention(c) (@in_guaranteed Owner, @in_guaranteed Owner) -> @lifetime(borrow address_for_deps 0) @owned View
135-
// CHECK: sil [clang getViewFromEither] {{.*}} : $@convention(c) (@in_guaranteed Owner, @in_guaranteed Owner) -> @lifetime(borrow address_for_deps 0, borrow address_for_deps 1) @owned View
133+
// CHECK: sil [clang getView] {{.*}} : $@convention(c) (@in_guaranteed Owner) -> @lifetime(borrow address 0) @owned View
134+
// CHECK: sil [clang getViewFromFirst] {{.*}} : $@convention(c) (@in_guaranteed Owner, @in_guaranteed Owner) -> @lifetime(borrow address 0) @owned View
135+
// CHECK: sil [clang getViewFromEither] {{.*}} : $@convention(c) (@in_guaranteed Owner, @in_guaranteed Owner) -> @lifetime(borrow address 0, borrow address 1) @owned View
136136
// CHECK: sil [clang Owner.handOutView] {{.*}} : $@convention(cxx_method) (@in_guaranteed Owner) -> @lifetime(borrow 0) @owned View
137137
// CHECK: sil [clang Owner.handOutView2] {{.*}} : $@convention(cxx_method) (View, @in_guaranteed Owner) -> @lifetime(borrow 1) @owned View
138138
// CHECK: sil [clang getViewFromEither] {{.*}} : $@convention(c) (View, View) -> @lifetime(copy 0, copy 1) @owned View
139139
// CHECK: sil [clang View.init] {{.*}} : $@convention(c) () -> @lifetime(immortal) @out View
140140
// CHECK: sil [clang OtherView.init] {{.*}} : $@convention(c) (View) -> @lifetime(copy 0) @out OtherView
141141
// CHECK: sil [clang returnsImmortal] {{.*}} : $@convention(c) () -> @lifetime(immortal) @owned View
142142
// CHECK: sil [clang copyView] {{.*}} : $@convention(c) (View, @lifetime(copy 0) @inout View) -> ()
143-
// CHECK: sil [clang getCaptureView] {{.*}} : $@convention(c) (@in_guaranteed Owner) -> @lifetime(borrow address_for_deps 0) @owned CaptureView
143+
// CHECK: sil [clang getCaptureView] {{.*}} : $@convention(c) (@in_guaranteed Owner) -> @lifetime(borrow address 0) @owned CaptureView
144144
// CHECK: sil [clang CaptureView.captureView] {{.*}} : $@convention(cxx_method) (View, @lifetime(copy 0) @inout CaptureView) -> ()
145145
// CHECK: sil [clang CaptureView.handOut] {{.*}} : $@convention(cxx_method) (@lifetime(copy 1) @inout View, @in_guaranteed CaptureView) -> ()
146-
// CHECK: sil [clang NS.getView] {{.*}} : $@convention(c) (@in_guaranteed Owner) -> @lifetime(borrow address_for_deps 0) @owned View
146+
// CHECK: sil [clang NS.getView] {{.*}} : $@convention(c) (@in_guaranteed Owner) -> @lifetime(borrow address 0) @owned View
147147
// CHECK: sil [clang moveOnlyId] {{.*}} : $@convention(c) (@in_guaranteed MoveOnly) -> @lifetime(borrow {{.*}}0) @out MoveOnly
148148

149149
//--- test.swift

test/Interop/Cxx/stdlib/use-std-function.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
// libstdc++11 declares a templated constructor of std::function with an rvalue-reference parameter,
1111
// which aren't yet supported in Swift. Therefore initializing a std::function from Swift closures
1212
// will not work on the platforms that are shipped with this version of libstdc++ (rdar://125816354).
13-
// XFAIL: LinuxDistribution=ubuntu-24.04
14-
// XFAIL: LinuxDistribution=ubuntu-22.04
1513
// XFAIL: LinuxDistribution=rhel-9.3
1614
// XFAIL: LinuxDistribution=rhel-9.4
1715
// XFAIL: LinuxDistribution=rhel-9.5

test/SILGen/opaque_values_cxx.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Cxx
1717
// CHECK-LABEL: } // end sil function '$sSo3stdO3__1O0065vectorCUnsignedIntstd__1allocatorCUnsignedInt_dDGIrdqahddCJdFaAjaV3Cxx0B8SequenceSCAgHP13__beginUnsafe11RawIteratorQzyFTW'
1818
// CHECK-LABEL: sil {{.*}}[ossa] @$sSo3stdO{{(3__1O)?}}0047___wrap_iterUnsafePointerCUnsignedInt_heCInnaEgaVSQSCSQ2eeoiySbx_xtFZTW : {{.*}} {
1919
// CHECK: bb0([[LHS:%[^,]+]] : $std.__1.__wrap_iter<UnsafePointer<CUnsignedInt>>, [[RHS:%[^,]+]] :
20-
// CHECK: [[CALLEE:%[^,]+]] = function_ref @$sSo2eeoiySbSo3stdO{{(3__1O)?}}0047___wrap_iterUnsafePointerCUnsignedInt_heCInnaEgaV_AGtFTO
20+
// CHECK: [[CALLEE:%[^,]+]] = function_ref @_ZNSt3__1eqB8ne{{.*}}IPKjEEbRKNS_11__wrap_iterIT_EES7_
2121
// CHECK: [[EQUAL:%[^,]+]] = apply [[CALLEE]]([[LHS]], [[RHS]])
2222
// CHECK: return [[EQUAL]]
2323
// CHECK-LABEL: } // end sil function '$sSo3stdO{{(3__1O)?}}0047___wrap_iterUnsafePointerCUnsignedInt_heCInnaEgaVSQSCSQ2eeoiySbx_xtFZTW'

0 commit comments

Comments
 (0)