Skip to content

🍒[cxx-interop] Ban ObjCBool from being substituted into C++ templates #74967

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 5, 2024

Conversation

egorzhdan
Copy link
Contributor

Explanation: This fixes a compiler crash when calling a templated C++ function with a parameter of type ObjCBool from Swift. The compiler now emits an error for this.
Scope: Changes the logic that transforms Swift types to C++ types for the purpose of C++ template instantiation.
Risk: Low, only affects C++ templates.
Testing: Added a compile test.
Issue: rdar://130424969
Reviewer: @beccadax

Original PR: #74790

This fixes a compiler crash when calling a templated C++ function with a parameter of type `ObjCBool` from Swift. The compiler now emits an error for this.

Previously the following would happen:
1. Swift starts to emit SILGen for a call expression `takeTAsConstRef(ObjCBool(true))`.
2. `takeTAsConstRef` is a templated C++ function, so Swift asks Clang to instantiate a `takeTAsConstRef` with a built-in Obj-C boolean type.
3. Swift gets an instantiated function template back that looks like this: `void takeTAsConstRef(_Bool t) { ... }`.
4. Swift's ClangImporter begins to import that instantiated function.
5. Since the parameter type is not spelled as `BOOL`, the parameter is not imported as `ObjCBool`. Instead, it's imported as regular Swift `Bool`.
6. Swift realizes that the types don't match (`ObjCBool` vs `Bool`), tries to apply any of the known implicit conversions, fails to do so, crashes.

rdar://130424969
(cherry picked from commit fe5b009)
@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Jul 4, 2024
@egorzhdan egorzhdan requested a review from a team as a code owner July 4, 2024 13:34
@egorzhdan
Copy link
Contributor Author

@swift-ci please test

Copy link
Member

@DougGregor DougGregor left a comment

Choose a reason for hiding this comment

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

Feels like a band-aid, but it's better than crashing

@egorzhdan egorzhdan merged commit 0ef18ab into release/6.0 Jul 5, 2024
5 checks passed
@egorzhdan egorzhdan deleted the egorzhdan/6.0-objcbool-template-param branch July 5, 2024 15:20
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