Skip to content

Conversation

@Xazax-hun
Copy link
Contributor

@Xazax-hun Xazax-hun commented Jul 2, 2025

Swift cannot guarantee exclusivity of a class. On the other hand, lifetimebound annotations on the C++ side do not guarantee exclusivity. To resolve this issue, we ignore lifetime dependency annotations that introduce exclusive dependence on classes and import functions with ignored annotations as unsafe. Currently, Swift has no language feature to support these scenarios but it might get new features in the future to help people work around this problem.

rdar://153747746

@fahadnayyar
Copy link
Contributor

Swift cannot guarantee exclusivity of a class. On the other hand, lifetimebound annotations on the C++ side do not guarantee exclusivity.

I'm a bit confused about this statement. Could you please explain it a little more?

@Xazax-hun Xazax-hun force-pushed the dependence-on-sharedobj-is-unsafe branch from f524b6d to fe123c3 Compare July 2, 2025 18:12
@Xazax-hun
Copy link
Contributor Author

Xazax-hun commented Jul 2, 2025

Could you please explain it a little more?

Swift guarantees exclusivity for value types like structs and enums. On the other hand, people are free to share class types. As a result, the compiler or the runtime has no idea how many aliases are there for a class. But some Swift types like MutableSpan has exclusivity guarantees. Imagine something like:

@lifetime(borrow self)
public func getBufferContents() -> MutableSpan<UInt8> { ... }

where self is a class. Since there might be multiple aliases of self, one could call getBufferContents on these aliases violating the exclusivity guarantees of MutableSpan. Since Swift cannot verify the correctness of this code it rejects this construct. Unfortunately, we could create something like this when we are importing code from C++ as lifetimebound only describes some lifetime constraints for some values but tells us nothing about the exclusivity. This PR makes sure we do not create "safe" overloads that are in fact unsafe and also marks constructs like this unsafe when we import them from C++.

@Xazax-hun Xazax-hun force-pushed the dependence-on-sharedobj-is-unsafe branch 4 times, most recently from b009445 to 4ac0826 Compare July 10, 2025 10:21
@Xazax-hun Xazax-hun force-pushed the dependence-on-sharedobj-is-unsafe branch from 4ac0826 to 86ed399 Compare October 8, 2025 15:31
Swift cannot guarantee exclusivity of a class. On the other hand,
lifetimebound annotations on the C++ side do not guarantee exclusivity.
To resolve this issue, we ignore lifetime dependency annotations that
introduce exclusive dependence on classes and import functions with ignored
annotations as unsafe. Currently, Swift has no language feature to
support these scenarios but it might get new features in the future to
help people work around this problem.

rdar://153747746
@Xazax-hun Xazax-hun force-pushed the dependence-on-sharedobj-is-unsafe branch from 86ed399 to f08868f Compare October 9, 2025 10:55
@Xazax-hun
Copy link
Contributor Author

@swift-ci please smoke test

@Xazax-hun
Copy link
Contributor Author

@swift-ci please smoke test macos

@Xazax-hun Xazax-hun merged commit 90a5e49 into swiftlang:main Oct 14, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ interop Feature: Interoperability with C++

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants