Skip to content

[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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

fahadnayyar
Copy link

Adding a sentence in the documentation thatthe foreign-reference annotations are now propagated to inherited C++ types

@fahadnayyar fahadnayyar marked this pull request as ready for review June 10, 2025 22:01
@@ -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.
Copy link
Contributor

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?

Copy link
Author

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.

Copy link
Author

Choose a reason for hiding this comment

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

CC: @j-hui

@Xazax-hun
Copy link
Contributor

cc @egorzhdan

@fahadnayyar
Copy link
Author

CC: @j-hui
if you can also please take a look!

### 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
Copy link
Author

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?

Copy link
Contributor

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.

@fahadnayyar
Copy link
Author

CC: @ravikandhadai

@@ -1255,6 +1255,10 @@ object.doSomething()
// `object` will be released here.
```

### Inference for Derived Types
Copy link
Contributor

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.

Copy link
Author

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
Copy link
Contributor

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.

@@ -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.
Copy link
Contributor

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:

Suggested change
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.

Copy link
Author

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 a SWIFT_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.

@fahadnayyar
Copy link
Author

This patch needs 2 important things to be included:

  • behaviour in case of multiple inheritance.
  • Clarification about immortal references.


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.

Copy link
Author

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?

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.

4 participants