-
Notifications
You must be signed in to change notification settings - Fork 10.6k
🍒[cxx-interop] Allow retain/release operations to be methods #84462
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
Merged
egorzhdan
merged 1 commit into
swiftlang:release/6.2
from
egorzhdan:egorzhdan/6.2-retain-release-methods
Sep 25, 2025
+325
−16
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
test/Interop/Cxx/foreign-reference/Inputs/lifetime-operation-methods.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#include <swift/bridging> | ||
|
||
struct RefCountedBox { | ||
int value; | ||
int refCount = 1; | ||
|
||
RefCountedBox(int value) : value(value) {} | ||
|
||
void doRetain() { refCount++; } | ||
void doRelease() { refCount--; } | ||
} SWIFT_SHARED_REFERENCE(.doRetain, .doRelease); | ||
|
||
struct DerivedRefCountedBox : RefCountedBox { | ||
int secondValue = 1; | ||
DerivedRefCountedBox(int value, int secondValue) | ||
: RefCountedBox(value), secondValue(secondValue) {} | ||
}; | ||
|
||
// MARK: Retain in a base type, release in derived | ||
|
||
struct BaseHasRetain { | ||
mutable int refCount = 1; | ||
void doRetainInBase() const { refCount++; } | ||
}; | ||
|
||
struct DerivedHasRelease : BaseHasRetain { | ||
int value; | ||
DerivedHasRelease(int value) : value(value) {} | ||
|
||
void doRelease() const { refCount--; } | ||
} SWIFT_SHARED_REFERENCE(.doRetainInBase, .doRelease); | ||
|
||
// MARK: Retain in a base type, release in templated derived | ||
|
||
template <typename T> | ||
struct TemplatedDerivedHasRelease : BaseHasRetain { | ||
T value; | ||
TemplatedDerivedHasRelease(T value) : value(value) {} | ||
|
||
void doReleaseTemplated() const { refCount--; } | ||
} SWIFT_SHARED_REFERENCE(.doRetainInBase, .doReleaseTemplated); | ||
|
||
using TemplatedDerivedHasReleaseFloat = TemplatedDerivedHasRelease<float>; | ||
using TemplatedDerivedHasReleaseInt = TemplatedDerivedHasRelease<int>; | ||
|
||
// MARK: Retain/release in CRTP base type | ||
|
||
template <typename Derived> | ||
struct CRTPBase { | ||
mutable int refCount = 1; | ||
void crtpRetain() const { refCount++; } | ||
void crtpRelease() const { refCount--; } | ||
} SWIFT_SHARED_REFERENCE(.crtpRetain, .crtpRelease); | ||
|
||
struct CRTPDerived : CRTPBase<CRTPDerived> { | ||
int value; | ||
CRTPDerived(int value) : value(value) {} | ||
}; | ||
|
||
// MARK: Virtual retain and release | ||
|
||
struct VirtualRetainRelease { | ||
int value; | ||
mutable int refCount = 1; | ||
VirtualRetainRelease(int value) : value(value) {} | ||
|
||
virtual void doRetainVirtual() const { refCount++; } | ||
virtual void doReleaseVirtual() const { refCount--; } | ||
virtual ~VirtualRetainRelease() = default; | ||
} SWIFT_SHARED_REFERENCE(.doRetainVirtual, .doReleaseVirtual); | ||
|
||
struct DerivedVirtualRetainRelease : VirtualRetainRelease { | ||
DerivedVirtualRetainRelease(int value) : VirtualRetainRelease(value) {} | ||
|
||
mutable bool calledDerived = false; | ||
void doRetainVirtual() const override { refCount++; calledDerived = true; } | ||
void doReleaseVirtual() const override { refCount--; } | ||
}; | ||
|
||
// MARK: Pure virtual retain and release | ||
|
||
struct PureVirtualRetainRelease { | ||
int value; | ||
mutable int refCount = 1; | ||
PureVirtualRetainRelease(int value) : value(value) {} | ||
|
||
virtual void doRetainPure() const = 0; | ||
virtual void doReleasePure() const = 0; | ||
virtual ~PureVirtualRetainRelease() = default; | ||
} SWIFT_SHARED_REFERENCE(.doRetainPure, .doReleasePure); | ||
|
||
struct DerivedPureVirtualRetainRelease : PureVirtualRetainRelease { | ||
mutable int refCount = 1; | ||
|
||
DerivedPureVirtualRetainRelease(int value) : PureVirtualRetainRelease(value) {} | ||
void doRetainPure() const override { refCount++; } | ||
void doReleasePure() const override { refCount--; } | ||
}; | ||
|
||
// MARK: Static retain/release | ||
#ifdef INCORRECT | ||
struct StaticRetainRelease { | ||
// expected-error@-1 {{specified retain function '.staticRetain' is a static function; expected an instance function}} | ||
// expected-error@-2 {{specified release function '.staticRelease' is a static function; expected an instance function}} | ||
int value; | ||
int refCount = 1; | ||
|
||
StaticRetainRelease(int value) : value(value) {} | ||
|
||
static void staticRetain(StaticRetainRelease* o) { o->refCount++; } | ||
static void staticRelease(StaticRetainRelease* o) { o->refCount--; } | ||
} SWIFT_SHARED_REFERENCE(.staticRetain, .staticRelease); | ||
|
||
struct DerivedStaticRetainRelease : StaticRetainRelease { | ||
// expected-error@-1 {{cannot find retain function '.staticRetain' for reference type 'DerivedStaticRetainRelease'}} | ||
// expected-error@-2 {{cannot find release function '.staticRelease' for reference type 'DerivedStaticRetainRelease'}} | ||
int secondValue = 1; | ||
DerivedStaticRetainRelease(int value, int secondValue) | ||
: StaticRetainRelease(value), secondValue(secondValue) {} | ||
}; | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
test/Interop/Cxx/foreign-reference/lifetime-operation-methods-module-interface.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// RUN: %target-swift-ide-test -print-module -cxx-interoperability-mode=upcoming-swift -I %swift_src_root/lib/ClangImporter/SwiftBridging -module-to-print=LifetimeOperationMethods -I %S/Inputs -source-filename=x | %FileCheck %s | ||
|
||
// CHECK: class RefCountedBox { | ||
// CHECK: func doRetain() | ||
// CHECK: func doRelease() | ||
// CHECK: } | ||
// CHECK: class DerivedRefCountedBox { | ||
// CHECK: func doRetain() | ||
// CHECK: func doRelease() | ||
// CHECK: } | ||
|
||
// CHECK: class DerivedHasRelease { | ||
// CHECK: func doRelease() | ||
// CHECK: func doRetainInBase() | ||
// CHECK: } | ||
|
||
// CHECK: class TemplatedDerivedHasRelease<CFloat> { | ||
// CHECK: var value: Float | ||
// CHECK: func doReleaseTemplated() | ||
// CHECK: func doRetainInBase() | ||
// CHECK: } | ||
// CHECK: class TemplatedDerivedHasRelease<CInt> { | ||
// CHECK: var value: Int32 | ||
// CHECK: func doReleaseTemplated() | ||
// CHECK: func doRetainInBase() | ||
// CHECK: } | ||
|
||
// CHECK: class CRTPDerived { | ||
// CHECK: var value: Int32 | ||
// CHECK: } | ||
|
||
// CHECK: class VirtualRetainRelease { | ||
// CHECK: func doRetainVirtual() | ||
// CHECK: func doReleaseVirtual() | ||
// CHECK: } | ||
// CHECK: class DerivedVirtualRetainRelease { | ||
// CHECK: func doRetainVirtual() | ||
// CHECK: func doReleaseVirtual() | ||
// CHECK: } | ||
|
||
// CHECK: class PureVirtualRetainRelease { | ||
// CHECK: func doRetainPure() | ||
// CHECK: func doReleasePure() | ||
// CHECK: } | ||
// CHECK: class DerivedPureVirtualRetainRelease { | ||
// CHECK: func doRetainPure() | ||
// CHECK: func doReleasePure() | ||
// CHECK: var refCount: Int32 | ||
// CHECK: } |
6 changes: 6 additions & 0 deletions
6
test/Interop/Cxx/foreign-reference/lifetime-operation-methods-typechecker.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// RUN: %target-typecheck-verify-swift -Xcc -DINCORRECT -I %S%{fs-sep}Inputs -I %swift_src_root/lib/ClangImporter/SwiftBridging -verify-additional-file %S%{fs-sep}Inputs%{fs-sep}lifetime-operation-methods.h -cxx-interoperability-mode=upcoming-swift -disable-availability-checking | ||
|
||
import LifetimeOperationMethods | ||
|
||
let _ = StaticRetainRelease(123) | ||
let _ = DerivedStaticRetainRelease(123, 456) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we also need a spelling for static member functions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now, we are rejecting static member functions in https://github.com/swiftlang/swift/pull/84343/files#diff-d29df7eb941cda5dc4dc36df1f3220fe4468a9b58d4fa8ca251c76337f353589R2805. In future I think it would be reasonable to allow it, possibly with the same leading-dot spelling, once we figure out the correct behavior for static retain/release functions declared in a base type.