Skip to content

[cxx-interop] Adding swift_name attributes to virtual methods overrides #83067

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
merged 1 commit into from
Jul 18, 2025

Conversation

susmonteiro
Copy link
Contributor

@susmonteiro susmonteiro commented Jul 15, 2025

The design for overriding virtual methods that may be renamed with a swift_name attribute is as follows:

  • only the base method can be renamed with a swift_name attribute
  • all the overriding methods will inherit their name from the base method, independently of whether they don't have a swift_name attribute or have a different one
  • a swift_name attribute on a virtual method override is going to be ignored. We emit a warning and the method will be marked unavailable
  • in the case of multiple inheritance:
    • if the deriving class overrides the method, we validate that all the overriding methods share the same name. If not, the method is marked unavailable and cannot be called with any name. The idea here is to preserve virtual dispatch.
    • if the deriving class does not override the method, then it will inherit all the method names from all its base classes and these will all be valid names. The reasoning is that in C++, this would result in ambiguous methods, as there would be no way to pick which method to call. In Swift, this allows the developer to explicitly choose with overriding method to call.

Follow up to #82485

rdar://155032235

@susmonteiro
Copy link
Contributor Author

@swift-ci please smoke test

@Xazax-hun
Copy link
Contributor

It could be a follow-up PR but I think we should emit a warning when:

  • An override has a swift_name different from the swift_name of the base
  • There is a swift_name attribute on an override that is ignored

the program will crash if we try to use that name to call the method

This sounds a bit harsh. Is it possible to do something else, e.g., making it unavailable?

@susmonteiro
Copy link
Contributor Author

@Xazax-hun you think we should emit a warning in those 2 situations even if the method is not being used? This is feasible, but I'm wondering if it would generate too many warnings

This sounds a bit harsh. Is it possible to do something else, e.g., making it unavailable?

Sorry, my description wasn't very explicit, but that's exactly what I'm doing: marking the method unavailable and, if the method is called, then the program won't compile because the method is unavailable

Impl.markUnavailable(result, unavailabilityMsg);

} else {
// If the swift_name attribute renames the function to the same as
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we want to do the opposite and only import the derived method in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! In the case where the override has the same swift_name, we synthesize a new thunk for the method.

However, when the base method has a swift_name but the override doesn't, we still skip the generation of a thunk and clone the one from the base method. I improved the comment explaining why this is correct.

@susmonteiro
Copy link
Contributor Author

@swift-ci please smoke test

Copy link
Contributor

@Xazax-hun Xazax-hun left a comment

Choose a reason for hiding this comment

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

Mostly looks good to me, I have some nits and questions inline.

@susmonteiro susmonteiro force-pushed the rename-virtual-methods branch 2 times, most recently from 8ebc8e1 to 5d87aec Compare July 17, 2025 15:22
@susmonteiro susmonteiro force-pushed the rename-virtual-methods branch from 5d87aec to 585ca5e Compare July 17, 2025 15:23
@susmonteiro
Copy link
Contributor Author

@swift-ci please smoke test

Copy link
Contributor

@Xazax-hun Xazax-hun left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@susmonteiro susmonteiro merged commit 0337f7e into swiftlang:main Jul 18, 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.

2 participants