-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[cxx-interop] Teach importer to interpret lifetimebound annotations #76311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// RUN: rm -rf %t | ||
// RUN: split-file %s %t | ||
// 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 | ||
// 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 | ||
|
||
//--- Inputs/module.modulemap | ||
module Test { | ||
header "nonescapable.h" | ||
requires cplusplus | ||
} | ||
|
||
//--- Inputs/nonescapable.h | ||
#include "swift/bridging" | ||
|
||
struct SWIFT_NONESCAPABLE View { | ||
View() : member(nullptr) {} | ||
View(const int *p [[clang::lifetimebound]]) : member(p) {} | ||
View(const View&) = default; | ||
private: | ||
const int *member; | ||
}; | ||
|
||
struct Owner { | ||
int data; | ||
|
||
View handOutView() const [[clang::lifetimebound]] { | ||
return View(&data); | ||
} | ||
}; | ||
|
||
Owner makeOwner() { | ||
return Owner{42}; | ||
} | ||
|
||
View getView(const Owner& owner [[clang::lifetimebound]]) { | ||
return View(&owner.data); | ||
} | ||
|
||
View getViewFromFirst(const Owner& owner [[clang::lifetimebound]], const Owner& owner2) { | ||
return View(&owner.data); | ||
} | ||
|
||
bool coinFlip; | ||
|
||
View getViewFromEither(const Owner& owner [[clang::lifetimebound]], const Owner& owner2 [[clang::lifetimebound]]) { | ||
if (coinFlip) | ||
return View(&owner.data); | ||
else | ||
return View(&owner2.data); | ||
} | ||
|
||
View getViewFromEither(View view1 [[clang::lifetimebound]], View view2 [[clang::lifetimebound]]) { | ||
if (coinFlip) | ||
return view1; | ||
else | ||
return view2; | ||
} | ||
|
||
struct SWIFT_NONESCAPABLE TestAnnotationTranslation { | ||
TestAnnotationTranslation() : member(nullptr) {} | ||
TestAnnotationTranslation(const int *p [[clang::lifetimebound]]) : member(p) {} | ||
TestAnnotationTranslation(const TestAnnotationTranslation& [[clang::lifetimebound]]) = default; | ||
private: | ||
const int *member; | ||
}; | ||
|
||
// CHECK: sil [clang makeOwner] {{.*}}: $@convention(c) () -> Owner | ||
// CHECK: sil [clang getView] {{.*}} : $@convention(c) (@in_guaranteed Owner) -> _scope(0) @autoreleased View | ||
// CHECK: sil [clang getViewFromFirst] {{.*}} : $@convention(c) (@in_guaranteed Owner, @in_guaranteed Owner) -> _scope(0) @autoreleased View | ||
// CHECK: sil [clang getViewFromEither] {{.*}} : $@convention(c) (@in_guaranteed Owner, @in_guaranteed Owner) -> _scope(0, 1) @autoreleased View | ||
// CHECK: sil [clang Owner.handOutView] {{.*}} : $@convention(cxx_method) (@in_guaranteed Owner) -> _scope(0) @autoreleased View | ||
// CHECK: sil [clang getViewFromEither] {{.*}} : $@convention(c) (@guaranteed View, @guaranteed View) -> _inherit(0, 1) @autoreleased View | ||
// CHECK: sil [clang View.init] {{.*}} : $@convention(c) () -> @out View | ||
|
||
//--- test.swift | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please add a module interface test that would check that the Swift lifetime attributes are printed in the generated interface for a C++ module? See There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, the Swift lifetimes are not supported by the module printer yet as there is no official/final syntax for it. That being said, I added checks like that because it is still useful to see what parameters are imported as borrowing. Let me know if that looks good. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @meg-gupta is planning to add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can write an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A slightly unrelated comment: I might be missing something but There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated the test to check the SIL. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. I'll fix |
||
import Test | ||
|
||
public func test() { | ||
let o = makeOwner() | ||
let o2 = makeOwner() | ||
let v1 = getView(o) | ||
let v2 = getViewFromFirst(o, o2) | ||
let _ = getViewFromEither(o, o2) | ||
let _ = o.handOutView() | ||
let _ = getViewFromEither(v1, v2) | ||
let defaultView = View() | ||
} |
Uh oh!
There was an error while loading. Please reload this page.