Skip to content

Commit 217b450

Browse files
authored
Merge pull request #77294 from swiftlang/gaborh/import-return-independent-as-immortal
[cxx-interop] SWIFT_RETURNS_INDEPENDENT_VALUE implies immortal lifetime
2 parents 09c89fb + 17ef2f6 commit 217b450

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3857,6 +3857,18 @@ namespace {
38573857
if (decl->getTemplatedKind() == clang::FunctionDecl::TK_FunctionTemplate)
38583858
return;
38593859

3860+
SmallVector<LifetimeDependenceInfo, 1> lifetimeDependencies;
3861+
LifetimeDependenceInfo immortalLifetime(nullptr, nullptr, 0,
3862+
/*isImmortal*/ true);
3863+
if (const auto *funDecl = dyn_cast<FuncDecl>(result))
3864+
if (hasUnsafeAPIAttr(decl) && !funDecl->getResultInterfaceType()->isEscapable()) {
3865+
Impl.SwiftContext.evaluator.cacheOutput(
3866+
LifetimeDependenceInfoRequest{result},
3867+
Impl.SwiftContext.AllocateCopy(lifetimeDependencies));
3868+
lifetimeDependencies.push_back(immortalLifetime);
3869+
return;
3870+
}
3871+
38603872
auto retType = decl->getReturnType();
38613873
auto warnForEscapableReturnType = [&] {
38623874
if (isEscapableAnnotatedType(retType.getTypePtr())) {
@@ -3869,8 +3881,8 @@ namespace {
38693881
};
38703882

38713883
auto swiftParams = result->getParameters();
3872-
bool hasSelf = result->hasImplicitSelfDecl() && !isa<ConstructorDecl>(result);
3873-
SmallVector<LifetimeDependenceInfo, 1> lifetimeDependencies;
3884+
bool hasSelf =
3885+
result->hasImplicitSelfDecl() && !isa<ConstructorDecl>(result);
38743886
SmallBitVector inheritLifetimeParamIndicesForReturn(swiftParams->size() +
38753887
hasSelf);
38763888
SmallBitVector scopedLifetimeParamIndicesForReturn(swiftParams->size() +
@@ -3910,8 +3922,7 @@ namespace {
39103922
// Assume default constructed view types have no dependencies.
39113923
if (ctordecl->isDefaultConstructor() &&
39123924
importer::hasNonEscapableAttr(ctordecl->getParent()))
3913-
lifetimeDependencies.push_back(
3914-
LifetimeDependenceInfo(nullptr, nullptr, 0, /*isImmortal*/ true));
3925+
lifetimeDependencies.push_back(immortalLifetime);
39153926
}
39163927
if (lifetimeDependencies.empty()) {
39173928
if (isNonEscapableAnnotatedType(retType.getTypePtr())) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// RUN: rm -rf %t
22
// RUN: split-file %s %t
3-
// RUN: %target-swift-frontend -typecheck -I %swift_src_root/lib/ClangImporter/SwiftBridging -I %t/Inputs %t/test.swift -enable-experimental-feature NonescapableTypes -cxx-interoperability-mode=default -diagnostic-style llvm 2>&1
43
// RUN: %target-swift-frontend -I %swift_src_root/lib/ClangImporter/SwiftBridging -I %t/Inputs -emit-sil %t/test.swift -enable-experimental-feature NonescapableTypes -cxx-interoperability-mode=default -diagnostic-style llvm 2>&1 | %FileCheck %s
54

65
//--- Inputs/module.modulemap
@@ -77,6 +76,10 @@ private:
7776
const int *member;
7877
};
7978

79+
View returnsImmortal() SWIFT_RETURNS_INDEPENDENT_VALUE {
80+
return View();
81+
}
82+
8083
// CHECK: sil [clang makeOwner] {{.*}}: $@convention(c) () -> Owner
8184
// CHECK: sil [clang getView] {{.*}} : $@convention(c) (@in_guaranteed Owner) -> @lifetime(borrow 0) @autoreleased View
8285
// CHECK: sil [clang getViewFromFirst] {{.*}} : $@convention(c) (@in_guaranteed Owner, @in_guaranteed Owner) -> @lifetime(borrow 0) @autoreleased View
@@ -86,6 +89,7 @@ private:
8689
// CHECK: sil [clang getViewFromEither] {{.*}} : $@convention(c) (@guaranteed View, @guaranteed View) -> @lifetime(copy 0, copy 1) @autoreleased View
8790
// CHECK: sil [clang View.init] {{.*}} : $@convention(c) () -> @lifetime(immortal) @out View
8891
// CHECK: sil [clang OtherView.init] {{.*}} : $@convention(c) (@guaranteed View) -> @lifetime(copy 0) @out OtherView
92+
// CHECK: sil [clang returnsImmortal] {{.*}} : $@convention(c) () -> @lifetime(immortal) @autoreleased View
8993

9094
//--- test.swift
9195

@@ -102,4 +106,5 @@ public func test() {
102106
let _ = getViewFromEither(v1, v2)
103107
let defaultView = View()
104108
let _ = OtherView(defaultView)
109+
let _ = returnsImmortal()
105110
}

0 commit comments

Comments
 (0)