-
Notifications
You must be signed in to change notification settings - Fork 237
[cxx-interop] Add documentation about inferring SWIFT_SHARED_REFERENCE in c++ inheritance #1078
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
base: main
Are you sure you want to change the base?
Conversation
…E in c++ inheritance
documentation/cxx-interop/index.md
Outdated
@@ -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. |
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.
What do you mean by Swift reference types or Swift class types? I think we should just pick one terminology here.
Maybe we want to mention that this inference only happens if exactly one base type is annotated as a shared reference?
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.
What do you mean by Swift reference types or Swift class types? I think we should just pick one terminology here.
Let's stick with Swift reference types, since that term is already used earlier in the doc (e.g., “Now that SharedObject is imported as a reference type in Swift,…”).
Maybe we want to mention that this inference only happens if exactly one base type is annotated as a shared reference?
That’s not entirely accurate. In cases where a type inherits from multiple bases (e.g., due to diamond inheritance or transitive inheritance), Swift will still infer SWIFT_SHARED_REFERENCE
as long as all annotated base types use the same retain
and release
functions.
A diagnostic is only emitted when there are conflicting retain
and release
function pairs among the base types.
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.
CC: @j-hui
cc @egorzhdan |
CC: @j-hui |
### 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 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 | ||
|
||
Similar to value types, casting an instance of a derived reference type to a |
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.
@egorzhdan, In these lines, are we referring to casting on the C++ side or the Swift side?
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.
I think Swift, i.e.,
... is not yet supported by Swift.
CC: @ravikandhadai |
documentation/cxx-interop/index.md
Outdated
@@ -1255,6 +1255,10 @@ object.doSomething() | |||
// `object` will be released here. | |||
``` | |||
|
|||
### Inference for Derived Types |
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.
Does this even need a new heading? I don't think "Inference for Derived Types" is very clear or precise, but I'm not sure how much it actually adds to the single paragraph it summarizes. I think the couple of sentences you added are fine as a closing remark for the "Shared Reference Types" section.
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.
@ravikandhadai suggested adding a new heading for this feature. One reason why a new heading might be useful is that the inheritance feature doesn't apply to SWIFT_IMMORTAL_REFERENCE
annotations.
I agree that this is a bad title. Maybe we can write Inference of Shared Reference behaviour in Derived Types
### 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 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 | ||
|
||
Similar to value types, casting an instance of a derived reference type to a |
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.
I think Swift, i.e.,
... is not yet supported by Swift.
documentation/cxx-interop/index.md
Outdated
@@ -1255,6 +1255,10 @@ 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 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. |
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.
A couple of things:
- Use single backticks instead of triple backticks for inline code markup
- This doesn't say anything about multiple inheritance, which inference does not handle
I rephrased things a little to encompass those changes:
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++ class inherits from exactly one `SWIFT_SHARED_REFERENCE` base class, the Swift compiler automatically infers the derived class to be a `SWIFT_SHARED_REFERENCE` as well. | |
The derived type also gets imported as a reference type, and uses the same `retain` and `release` functions as its base class. | |
C++ classes that inherit from multiple `SWIFT_SHARED_REFERENCE` base classes must still be explicitly annotated with `retain` and `release` operations that manage the reference counts of all base classes. |
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.
This doesn't say anything about multiple inheritance, which inference does not handle
When a C++ class inherits from exactly one
SWIFT_SHARED_REFERENCE
base class, the Swift compiler automatically infers the derived class to be aSWIFT_SHARED_REFERENCE
as well.
This is not entirely correct. It is fine to have multiple base SWIFT_SHARED_REFERENCE
types. The only restriction is that all of them must have the same retain
/release
functions.
This patch needs 2 important things to be included:
|
|
||
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. | ||
|
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.
@j-hui I changed some text based on your suggestions but added my flavor as well. How does it look now?
Adding a sentence in the documentation thatthe foreign-reference annotations are now propagated to inherited C++ types