Commit Graph

4 Commits

Author SHA1 Message Date
Egor Zhdan
d7212ff7f5 [cxx-interop] Add one more test for std::function
While initializing a `std::function` that takes `const std::string&` as a parameter currently crashes, changing the parameter type to `std::string` should work fine.
2024-07-22 13:54:09 +01:00
Egor Zhdan
3243228897 [cxx-interop] Allow initializing std::function from Swift closures
This adds a Swift initializer to instantiations of `std::function` that accepts a Swift closure with `@convention(c)`.

rdar://103979602
2024-06-14 19:05:45 +01:00
Daniel Rodríguez Troitiño
8b4821f1a8 [cxx-interop] Fix test header to declare linkage for a implicit template (#72522)
The header was defining a function, the function created a lambda, and
the lambda was transformed into a `std::function`. This transformation
is incorrect because the function scope does not have linkage, so the
instantiated types will not have linkage either, causing the error
below.

```
.../include/c++/11.2.0/bits/invoke.h:104:5:
error: function 'std::__invoke_r<int, (lambda at .../swift/test/Interop/Cxx/stdlib/Inputs/std-function.h:9:10) &, int>' is used but not defined in this translation unit, and cannot be defined in any other translation unit because its type does not have linkage
102 │   template<typename _Res, typename _Callable, typename... _Args>
103 │     constexpr enable_if_t<is_invocable_r_v<_Res, _Callable,
    _Args...>, _Res>
    104 │     __invoke_r(_Callable&& __fn, _Args&&... __args)
        │     ╰─ error: function 'std::__invoke_r<int, (lambda at .../swift/test/Interop/Cxx/stdlib/Inputs/std-function.h:9:10) &, int>' is used but not defined in this translation unit, and cannot be defined in any other translation unit because its type does not have linkage
        105 │     noexcept(is_nothrow_invocable_r_v<_Res, _Callable,
            _Args...>)
  106 │     {

  }
```

Declaring the function `inline` forces each TU to have their own copies
of the function, which avoids the instantiated templates from being in a
different TU than the one using them.

The header would not have worked in a normal C++ program, since none of
the functions declare linkage, they would have been defined in each TU
that included the header and it would fail linking as soon as two of
those TU tried to be linked together. Declaring the functions `inline`
avoids the problem (a more normal header, only with declarations, would
also worked).
2024-03-23 07:55:28 -07:00
Egor Zhdan
2d0863aba2 [cxx-interop] Initial tests for std::function usage
This makes sure that `std::function` is imported consistently on supported platforms, and that it allows basic usage: calling a function with `callAsFunction`, initializing an empty function, and passing a function retrieved from C++ back to C++ as a parameter.

rdar://103979602
2024-01-11 12:02:12 +00:00