Skip to content

C++ Interop: import const methods as non-mutating #38618

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 28, 2021

Conversation

egorzhdan
Copy link
Contributor

This change makes ClangImporter import some C++ member functions as non-mutating, given that they satisfy two requirements:

  • the function itself is declared as const
  • the parent struct doesn't contain any mutable members

get accessors of subscript operators are now also imported as non-mutating if the C++ operator[] satisfies the requirements above.

In the future we should allow annotating a C++ method as mutating explicitly, to make it possible to preserve the correct behavior for methods that are declared const, but mutate the object anyway, for example via const_cast.

Fixes SR-12795.

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Jul 24, 2021
@egorzhdan egorzhdan requested a review from zoecarver July 24, 2021 16:32
This change makes ClangImporter import some C++ member functions as non-mutating, given that they satisfy two requirements:
* the function itself is marked as `const`
* the parent struct doesn't contain any `mutable` members

`get` accessors of subscript operators are now also imported as non-mutating if the C++ `operator[]` satisfies the requirements above.

Fixes SR-12795.
@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.

It would be cool to see some performance improvements here. Unfortunately, we don't have enough C++ interop performance tests yet.

Anyway, thanks for the patch, as always!

@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test macOS

@egorzhdan
Copy link
Contributor Author

Yeah, looks like the C++ interop performance tests currently verify object instantiation, but not method calls.

@egorzhdan egorzhdan merged commit e2985b6 into swiftlang:main Jul 28, 2021
@egorzhdan egorzhdan deleted the cxx-mutating branch July 28, 2021 19:20
@LucianoPAlmeida
Copy link
Contributor

Hey @egorzhdan :)
It seems like this caused a failure on tests https://ci.swift.org/job/swift-PR-macos/28502/testReport/junit/Swift(watchsimulator-i386)/ClangImporter/cxx_interop_ir_swift/ when running targeting i386 watch simulator, noted when running tests for #38464. By the logs of why is failing, my guess would be since i386 is a 32-bit this alloca %TSo7MethodsV, align 8 expects 4 bytes align 4. Is that right?

@shahmishal
Copy link
Member

To unblock CI, I created a PR to disable the test and filed a issue:
#38690
https://bugs.swift.org/browse/SR-14986

@egorzhdan
Copy link
Contributor Author

Sorry for breaking the test!
@LucianoPAlmeida yes, you're right, the test should accept align 4 on a 32-bit arch.
I submitted a patch with a fix: #38692.

@LucianoPAlmeida
Copy link
Contributor

Sorry for breaking the test!
@LucianoPAlmeida yes, you're right, the test should accept align 4 on a 32-bit arch.
I submitted a patch with a fix: #38692.

Thank you for the fix :)

@varungandhi-apple
Copy link
Contributor

varungandhi-apple commented Jul 29, 2021

Also, for the future, if you're changing code in the ClangImporter, IRGen or the runtime, it is better to err on the side of caution and run full testing instead of smoke testing as there are often platform differences which aren't caught by smoke testing.

@egorzhdan
Copy link
Contributor Author

Also, for the future, if you're changing code in the ClangImporter, IRGen or the runtime, it is better to err on the side of caution and run full testing instead of smoke testing as there are often platform differences which aren't caught by smoke testing.

Yes, that's reasonable, thanks!

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.

5 participants