Skip to content

C++ Interop: support mutating attribute for C++ methods #39999

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
Nov 30, 2021

Conversation

egorzhdan
Copy link
Contributor

This change teaches ClangImporter to import C++ methods marked with __attribute__((__swift_attr__("mutating"))) as mutating in Swift. This is useful, for example, when a method mutates this despite being const in C++ (e.g. via const_cast).

This is a follow-up after #38618.

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Nov 1, 2021
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

struct HasConstMethodAnnotatedAsMutating {
int a;

int annotatedMutating() const __attribute__((__swift_attr__("mutating"))) {
Copy link
Contributor

@zoecarver zoecarver Nov 1, 2021

Choose a reason for hiding this comment

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

Maybe a test where there are multiple swift_attr attributes. Also, you can spell it like this: struct __attribute__((swift_attr("mutating"))) X { };.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe a test where there are multiple swift_attr attributes

Good idea, I added a test.

Also, you can spell it like this: struct __attribute__((swift_attr("mutating"))) X { };.

Hmm, that currently produces mutating struct X which doesn't look right, but it also worked this way before this change :)
Do you think mutating on a struct should apply it to all members methods?

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think mutating on a struct should apply it to all members methods?

Hmm, I don't know. But the test looks good how it is now. I think that's the correct way to write it.

Copy link
Contributor

@zoecarver zoecarver left a comment

Choose a reason for hiding this comment

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

This is a great improvement. As always, thanks for all your work on interop, Egor!

Feel free to merge this whenever you feel it's ready.

@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

1 similar comment
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

Copy link
Contributor

@zoecarver zoecarver left a comment

Choose a reason for hiding this comment

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

Still looks good. 🚢 it

This change teaches ClangImporter to import C++ methods marked with `__attribute__((__swift_attr__("mutating")))` as mutating in Swift. This is useful, for example, when a method mutates `this` despite being `const` in C++ (e.g. via `const_cast`).
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan egorzhdan merged commit 9010d89 into swiftlang:main Nov 30, 2021
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