Skip to content

Commit 9551e9d

Browse files
author
Gabor Horvath
committed
[cxx-interop] Add test for lifetimebound annotation in ctor
There was also an off by one error in this case.
1 parent a9d5903 commit 9551e9d

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
@@ -3857,7 +3857,7 @@ namespace {
38573857
return;
38583858

38593859
auto swiftParams = result->getParameters();
3860-
bool hasSelf = result->hasImplicitSelfDecl();
3860+
bool hasSelf = result->hasImplicitSelfDecl() && !isa<ConstructorDecl>(result);
38613861
SmallVector<LifetimeDependenceInfo, 1> lifetimeDependencies;
38623862
SmallBitVector inheritLifetimeParamIndicesForReturn(swiftParams->size() +
38633863
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)