Skip to content

[🍒][cxx-interop] Only mark projections of self-contained types as unsafe. #67413

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

Conversation

zoecarver
Copy link
Contributor

Description: Projections of trivial types and view types aren't unsafe. This matches what was described in the vision document. A view type like StringRef is already referencing memory it does not own, so if it projects its data again (via the data method for example), there's no additional unsafety. The really unsafe thing is when a self contained (owned) type projects its owned memory because there are subtle difference in C++ and Swift lifetime models (along with different safety expectations). I have discussed with the interop code owners and we all agree that this is the correct change to make. This has been corroborated by feedback from adopters. Finally, this is the behavior that was agreed upon by the workgroup and the broader community and is what is outlined in the forward vision document.
Scope: C++ interop
Risk: Medium. This change will break source. It changes an important part of importing and affects safety. It also has a high risk of breaking parts of the overlay. This change will be hard to walk back if we determine that it is unsafe (for example). It has not been testing with any adopters and this new behavior has not been tested in the wild.
Testing: Unit testing (CI)
Original PR: #67296
Radar: rdar://112220411

@zoecarver zoecarver requested a review from a team as a code owner July 19, 2023 21:31
@zoecarver zoecarver force-pushed the pick-only-methods-on-self-contained-types-are-unsafe branch from ee37bf3 to 65dc0d3 Compare July 19, 2023 21:32
@zoecarver
Copy link
Contributor Author

@swift-ci please test

@zoecarver
Copy link
Contributor Author

@swift-ci Please Build Toolchain macOS Platform

count: count)
#else
let buffer = UnsafeBufferPointer<UInt8>(start: _bridged.bytes_begin(),
count: count)
Copy link
Contributor

@ravikandhadai ravikandhadai Jul 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zoecarver both #if and #else branches seems to have the same code? Did you mean to say _bridged.__bytes_beginUnsafe() in the #else case?


// Mark this as safe to help our diganostics down the road.
if (!cxxRecordReturnType->getDefinition()) {
return true;
}

if (!hasCustomCopyOrMoveConstructor(cxxRecordReturnType) &&
// A projection of a view type (such as a string_view) from a self
// contained parent is a proejction (unsafe).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor typo: "proejction", we can combine the fix with later fixes.

@zoecarver
Copy link
Contributor Author

@swift-ci please test

@zoecarver
Copy link
Contributor Author

@swift-ci Please Build Toolchain macOS Platform

@zoecarver zoecarver force-pushed the pick-only-methods-on-self-contained-types-are-unsafe branch from 7ac2e7e to 30c929f Compare July 21, 2023 05:44
@zoecarver
Copy link
Contributor Author

@swift-ci Please Build Toolchain macOS Platform

@zoecarver
Copy link
Contributor Author

@swift-ci please test

@zoecarver
Copy link
Contributor Author

@swift-ci please test macOS

@zoecarver
Copy link
Contributor Author

@swift-ci please test windows

@ravikandhadai ravikandhadai self-requested a review July 21, 2023 18:39
Copy link
Contributor

@ravikandhadai ravikandhadai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Zoe! LGTM.

@zoecarver zoecarver merged commit f3cb88a into swiftlang:release/5.9 Jul 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants