Skip to content

Commit e4d625c

Browse files
authored
Merge pull request #76696 from swiftlang/gaborh/lifetimebound-on-ctors
[cxx-interop] Add test for lifetimebound annotation in ctor
2 parents b58a908 + 9551e9d commit e4d625c

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3858,7 +3858,7 @@ namespace {
38583858
return;
38593859

38603860
auto swiftParams = result->getParameters();
3861-
bool hasSelf = result->hasImplicitSelfDecl();
3861+
bool hasSelf = result->hasImplicitSelfDecl() && !isa<ConstructorDecl>(result);
38623862
SmallVector<LifetimeDependenceInfo, 1> lifetimeDependencies;
38633863
SmallBitVector inheritLifetimeParamIndicesForReturn(swiftParams->size() +
38643864
hasSelf);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ struct SWIFT_NONESCAPABLE View {
1616
View() : member(nullptr) {}
1717
View(const int *p [[clang::lifetimebound]]) : member(p) {}
1818
View(const View&) = default;
19+
private:
20+
const int *member;
21+
friend struct OtherView;
22+
};
23+
24+
struct SWIFT_NONESCAPABLE OtherView {
25+
OtherView() : member(nullptr) {}
26+
OtherView(View v [[clang::lifetimebound]]) : member(v.member) {}
27+
OtherView(const OtherView&) = default;
1928
private:
2029
const int *member;
2130
};
@@ -76,6 +85,7 @@ private:
7685
// CHECK: sil [clang Owner.handOutView2] {{.*}} : $@convention(cxx_method) (View, @in_guaranteed Owner) -> @lifetime(borrow 1) @autoreleased View
7786
// CHECK: sil [clang getViewFromEither] {{.*}} : $@convention(c) (@guaranteed View, @guaranteed View) -> @lifetime(copy 0, copy 1) @autoreleased View
7887
// CHECK: sil [clang View.init] {{.*}} : $@convention(c) () -> @lifetime(immortal) @out View
88+
// CHECK: sil [clang OtherView.init] {{.*}} : $@convention(c) (@guaranteed View) -> @lifetime(copy 0) @out OtherView
7989

8090
//--- test.swift
8191

@@ -91,4 +101,5 @@ public func test() {
91101
let _ = o.handOutView2(v1)
92102
let _ = getViewFromEither(v1, v2)
93103
let defaultView = View()
104+
let _ = OtherView(defaultView)
94105
}

0 commit comments

Comments
 (0)