From 0548af8c3d82f4cb13c2cd200078fc1d9aa39340 Mon Sep 17 00:00:00 2001 From: Fahad Nayyar Date: Tue, 10 Jun 2025 14:59:35 -0700 Subject: [PATCH 1/4] [cxx-interop] Add documentation about inferring SWIFT_SHARED_REFERENCE in c++ inheritance --- documentation/cxx-interop/index.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/documentation/cxx-interop/index.md b/documentation/cxx-interop/index.md index a1f06a9e8..a5ed45d3b 100644 --- a/documentation/cxx-interop/index.md +++ b/documentation/cxx-interop/index.md @@ -1255,6 +1255,9 @@ object.doSomething() // `object` will be released here. ``` +### Inference for Derived Types +Swift compiler automatically infers the ```SWIFT_SHARED_REFERENCE``` annotation for C++ types that inherit from a base type annotated with ```SWIFT_SHARED_REFERENCE```. These derived types are imported as Swift reference types or Swift class types that use the same ```retain``` and ```release``` functions as the base type. No additional annotation is required on the derived types. + ### Inheritance and Virtual Member Functions Similar to value types, casting an instance of a derived reference type to a From 5115e6864c2d2842eabe2c910c0a86656158f85e Mon Sep 17 00:00:00 2001 From: Fahad Nayyar Date: Tue, 1 Jul 2025 14:51:40 -0700 Subject: [PATCH 2/4] Removed Swift class types from the text --- documentation/cxx-interop/index.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/documentation/cxx-interop/index.md b/documentation/cxx-interop/index.md index a5ed45d3b..629cbf2a3 100644 --- a/documentation/cxx-interop/index.md +++ b/documentation/cxx-interop/index.md @@ -1256,7 +1256,8 @@ object.doSomething() ``` ### Inference for Derived Types -Swift compiler automatically infers the ```SWIFT_SHARED_REFERENCE``` annotation for C++ types that inherit from a base type annotated with ```SWIFT_SHARED_REFERENCE```. These derived types are imported as Swift reference types or Swift class types that use the same ```retain``` and ```release``` functions as the base type. No additional annotation is required on the derived types. + +Swift compiler automatically infers the ```SWIFT_SHARED_REFERENCE``` annotation for C++ types that inherit from a base type annotated with ```SWIFT_SHARED_REFERENCE```. These derived types are also imported as a reference type in Swift that use the same ```retain``` and ```release``` functions as the base type. No additional annotation is required on the derived types. ### Inheritance and Virtual Member Functions From dd4d78078d22be173e155baf6b48462cb8530410 Mon Sep 17 00:00:00 2001 From: Fahad Nayyar Date: Thu, 3 Jul 2025 17:31:19 -0700 Subject: [PATCH 3/4] edits --- documentation/cxx-interop/index.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/documentation/cxx-interop/index.md b/documentation/cxx-interop/index.md index 629cbf2a3..5cbc6f584 100644 --- a/documentation/cxx-interop/index.md +++ b/documentation/cxx-interop/index.md @@ -1255,9 +1255,10 @@ object.doSomething() // `object` will be released here. ``` -### Inference for Derived Types +### Inference of Shared Reference behaviour in Derived Types -Swift compiler automatically infers the ```SWIFT_SHARED_REFERENCE``` annotation for C++ types that inherit from a base type annotated with ```SWIFT_SHARED_REFERENCE```. These derived types are also imported as a reference type in Swift that use the same ```retain``` and ```release``` functions as the base type. No additional annotation is required on the derived types. +When a C++ type inherits from a `SWIFT_SHARED_REFERENCE` base type, the Swift compiler automatically infers `SWIFT_SHARED_REFERENCE` annotation for the derived type. +The derived type also gets imported as a reference type, and uses the same `retain` and `release` functions as its base class. ### Inheritance and Virtual Member Functions From 66da0bfd454886e38c4cf9a0dbe9324c2f88c4fa Mon Sep 17 00:00:00 2001 From: Fahad Nayyar Date: Thu, 3 Jul 2025 18:23:42 -0700 Subject: [PATCH 4/4] added some more info --- documentation/cxx-interop/index.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/documentation/cxx-interop/index.md b/documentation/cxx-interop/index.md index 5cbc6f584..660c76183 100644 --- a/documentation/cxx-interop/index.md +++ b/documentation/cxx-interop/index.md @@ -1259,6 +1259,12 @@ object.doSomething() When a C++ type inherits from a `SWIFT_SHARED_REFERENCE` base type, the Swift compiler automatically infers `SWIFT_SHARED_REFERENCE` annotation for the derived type. The derived type also gets imported as a reference type, and uses the same `retain` and `release` functions as its base class. +This inference works as long as all the annotated base types in the inheritance chain (including multiple or indirect inheritance) have the same `retain` and `release` functions. +If multiple base types have conflicting `retain` or `release` functions, the derived type is imported as a Swift value type, and the compiler emits a warning. + + +Note that this inference currently applies only to `SWIFT_SHARED_REFERENCE`. +It does not apply to types annotated with `SWIFT_IMMORTAL_REFERENCE` or `SWIFT_UNSAFE_REFERENCE`. ### Inheritance and Virtual Member Functions