Commit Graph

19 Commits

Author SHA1 Message Date
John Hui
3f8e9cd993 [cxx-interop] Allow C++ function templates to be instantiated with Swift closures
Swift started to explicitly forbid the instantiation of C++ function
templates with arbitrary types in #77430, because many types cause the
Swift compiler to crash. However, those checks prevented them from being
instantiated with Swift closures (which were previously fine), causing
a regression.

This patch relaxes the convertTemplateArgument() function to also allow
converting Swift function types, and adds some tests to make sure doing
so is fine.

This patch also does some cleanup of existing tests checking the
instantiation of various types, and adds testing for C function pointers
and Obj-C blocks.

rdar://148124104
(cherry picked from commit 284de98744)
2025-04-30 13:49:23 -07:00
John Hui
5a4ff294c8 [cxx-interop] Forbid C++ function template instantiation with Swift types (#77430)
Instantiating C++ function templates with Swift types is not currently supported, but the Swift compiler attempts to do so. Sometimes this leads to a compiler crash; other times it leads to a runtime crash. This patch turns those crashes into compiler errors.

At the call site of a C++ function template, the Swift compiler must convert Swift types deduced by the Swift type checker back into Clang types, which are then used to instantiate the C++ function template. Prior to this patch, this conversion was performed by ClangTypeConverter::convert(), which converts unsupported Swift types. This patch factors out some reusable parts of convert() and uses them in a new ClangTypeConverter::convertTemplateArgument() method that only converts supported template argument types. In the future, this method can be elaborated to support instantiating C++ templates with more types.

rdar://112692940
2025-01-16 10:05:20 -05:00
Alexander Cyon
db0b5db54e [test/Interop] Fix typos (#75032) 2024-08-28 09:41:09 -07:00
Egor Zhdan
fe5b0097bc [cxx-interop] Ban ObjCBool from being substituted into C++ templates
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
2024-06-27 19:48:57 +01:00
Egor Zhdan
5b5ffde79e [cxx-interop] Do not crash when passing Bool as const T& parameter
The type bridging logic assumed that if a value of type `Swift.Bool` is passed to a Clang function as an argument, then the type of the parameter must be a Clang built-in type (usually `_Bool`). This is not always correct. For instance, the type might be a templated const reference.

rdar://125508505
2024-05-09 15:28:33 +01:00
Anthony Latsis
a65f1a161e Gardening: Migrate test suite to GH issues: Interop 2022-08-31 05:20:25 +03:00
Josh Soref
e75e5b5a03 Spelling interop (#42549)
* spelling: different

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: disappear

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: executable

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: instantiate

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: instantiation

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: member

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: parameter

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: section

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: trivia

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: unrelated

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
2022-04-22 14:13:23 -07:00
Egor Zhdan
7bcd13b899 [cxx-interop] Avoid crashing when importing functions that take pointers to dependent types
Importing `type_traits` from libstdc++ currently causes a crash on Linux:
```
swift-ide-test: tools/clang/include/clang/AST/TypeNodes.inc:33: clang::TypeInfo clang::ASTContext::getTypeInfoImpl(const clang::Type *) const: Assertion `!T->isDependentType() && "should not see dependent types here"' failed.
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
Stack dump:
0.	Program arguments: /home/egorzh/Builds/swift/swift/bin/swift-ide-test -print-module -module-to-print=std -source-filename=x -enable-cxx-interop
1.	/usr/lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/type_traits:1110:10: importing 'std::__do_is_implicitly_default_constructible_impl'
2.	/usr/lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/type_traits:1116:22: importing 'std::__do_is_implicitly_default_constructible_impl::__test'
```

This change fixes the crash by bailing on such functions.
2022-03-21 20:33:26 +00:00
Omar Habra
7802b5f726 Adding Tests. 2021-11-20 14:53:22 -08:00
zoecarver
c4da4975ed [NFC] Cleanup function templates implementation. Address post-commmit review comments from #33053.
Addresses the post-commit review comments from #33053. Just re-naming,
commenting, and some other small cleanups, nothing functionally
different.
2021-01-19 12:02:15 -08:00
Marcel Hlopko
bde9c3b683 [cxx-interop] Fix header guards in test/Interop (#35039) 2020-12-15 09:20:19 +01:00
Zoe Carver
2b0ff64fdb Merge pull request #34870 from zoecarver/cxx/fix/no-params
[cxx-interop] Bail on functions that use unimportable types.
2020-12-01 10:12:20 -08:00
zoecarver
3d6c8a7971 [cxx-interop] Fix assertion to allow variadic members.
Simply fixes an assertion to allow variadic member functions.
2020-11-30 11:55:56 -08:00
zoecarver
6e69918a18 [cxx-interop] Bail on functions that use unimportable types.
We already fixed this for "global" functions. This is a more generic
solution that works for "nested" functions as well. (Members or
functions in a namespace.)
2020-11-24 12:59:58 -08:00
zoecarver
9c58a95755 [cxx-interop] Support function templates inside a namespace.
Thread "templateParams" through the call to find the function parameter
types when the function is in a namespace.
2020-11-10 18:49:51 -08:00
Zoe Carver
cb7bb52060 [cxx-interop] Support reference types in function templates. (#34536) 2020-11-06 17:13:41 -08:00
zoecarver
949d657206 [cxx-interop] Bail on dependent types (instead of crashing).
Just returns "Type()" instead of crashing at the "llvm_unreachable".
This is a stop-gap solution until we support dependent types.
2020-11-02 21:37:37 -08:00
zoecarver
937e584608 [cxx-interop] Fix infinite recursion of PackExpansion type.
Fix infinite recursion issue in the SwiftTypeConverter. This patch is
purely to prevent crashes.
2020-11-01 10:12:35 -08:00
Zoe Carver
f0f2246793 [cxx-interop] Support C++ function templates in Swift. (#33053)
This patch adds rudimentary support for C++ template functions in swift.
2020-10-21 20:42:25 -07:00