Commit Graph

36 Commits

Author SHA1 Message Date
Gábor Horváth
56e0ea6b6d [6.2][cxx-interop] Fix calling rvalue ref of a trivial type
Explanation: Fixes a runtime crash in the generated binary due to
mismatched calling convention when calling a function taking an rvalue
reference.
Scope: Affects C++ APIs taking rvalue references to directly passed
types (e.g., trivially destructible types).
Issue: rdar://148585343
Risk: Low, targeted to rvalue references which is a newly supported
feature.
Testing: Added tests to test suite
Reviewer: John Hui
2025-04-07 11:42:02 +01:00
Gabor Horvath
00fa738209 [cxx-interop] Fix calling convention for rvalue reference params
In C++, we always expected to invoke the dtor for moved-from objects.
This is not the case for swift. Fortunately, @inCxx calling convention
is already expressing that the caller supposed to destroy the object.
This fixes the missing dtor calls when calling C++ functions taking
rvalue references. Fixes #77894.

rdar://140786022
2025-03-03 11:47:12 +00:00
Gabor Horvath
1601564342 [cxx-interop] Import rvalue references as consuming parameters
Unfortunately, importing them as is results in ambiguous call sites.
E.g., std::vector::push_back has overloads for lvalue reference and
rvalue reference and we have no way to distinguish them at the call site
in Swift. To overcome this issue, functions with rvalue reference
parameters are imported with 'consuming:' argument labels.

Note that, in general, move only types and consuming is not properly
supported in Swift yet. We do not invoke the dtor for the moved-from
objects. This is a preexisting problem that can be observed with move
only types before this PR, so the fix will be done in a separate PR.
Fortunately, for most types, the moved-from objects do not require
additional cleanups.

rdar://125816354
2024-12-02 13:09:21 +00:00
Erik Eckstein
7cceaff5f3 SIL: don't print operand types in textual SIL
Type annotations for instruction operands are omitted, e.g.

```
  %3 = struct $S(%1, %2)
```

Operand types are redundant anyway and were only used for sanity checking in the SIL parser.

But: operand types _are_ printed if the definition of the operand value was not printed yet.
This happens:

* if the block with the definition appears after the block where the operand's instruction is located

* if a block or instruction is printed in isolation, e.g. in a debugger

The old behavior can be restored with `-Xllvm -sil-print-types`.
This option is added to many existing test files which check for operand types in their check-lines.
2024-11-21 18:49:52 +01:00
Alexander Cyon
db0b5db54e [test/Interop] Fix typos (#75032) 2024-08-28 09:41:09 -07: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
Egor Zhdan
1be7230876 [cxx-interop] Use C++17 standard by default
Clang is using C++17 standard version by default since Clang 16.

Swift’s ClangImporter should do the same, to make sure that clients who run clang and then swiftc without explicit std version see consistent behavior.

rdar://125777068
2024-04-02 16:23:32 +01:00
Arnold Schwaighofer
79894ff461 Fix test/Interop with opaque pointers 2023-07-03 03:36:07 -07:00
Arnold Schwaighofer
c1a93e0bde Move tests over to use the %use_no_opaque_pointers option 2023-06-14 10:49:48 -07:00
Alex Lorenz
196c717a3d [interop] do not import function template with templated rvalue / perfect forwarding ref
they can cause compiler crashes
2023-05-04 14:41:28 -07:00
Alex Lorenz
50edc0b79b [interop] add more specific diagnostic when rvalue reference parameter isn't imported 2023-05-03 18:29:26 -07:00
Alex Lorenz
62fd801828 [interop] C++ parameter of reference type should be imported as its pointee type in Swift even if parameter's type is type alias
This finally allows std::vector.push_back to be called with the pushed value directly passed into it
2023-05-03 18:23:03 -07:00
zoecarver
e05daed3b4 [cxx-interop] Disable rvalue references. We don't support them correctly (leading to lifetime issues). We are missing a consuming. 2023-05-01 23:13:33 -07:00
Alex Lorenz
9feb76419b [interop] ignore exceptions in existing interop tests 2023-02-22 11:00:51 -08:00
zoecarver
c00c762146 [tests] Fix SILGen tests to have Windows mangling 2023-02-10 09:27:11 -08:00
Puyan Lotfi
84a69c4143 [C++-Interop] Import const &T function parameters as @in_guaranteed
When passing value types as a const-ref to a C++ API, ensure that the
caller is tasked with handling the lifetime of the instance passed in.
2022-09-12 16:27:27 -04:00
Daniel Rodríguez Troitiño
691dcc9bb3 Test case for const T& in Obj-C++ methods
Add a test case that checks the behaviour introduced during the refactor
of `importFunctionParameterList` and `importMethodParamsAndReturnType`.

The test checks that both Obj-C++ methods and C++ functions treat `const
T&` parameters in the same way.
2022-09-02 11:51:15 -07:00
zoecarver
839839f924 [cxx-interop] Rename enable-cxx-interop -> enable-experimental-cxx-interop.
Also removes the driver flag, this will now also always be guarded on `-Xfrontend`.
2022-04-07 19:15:25 -07:00
Zoe Carver
b681b0a503 Merge pull request #41810 from zoecarver/fix-crash-ref-to-depentent-param 2022-03-29 18:34:51 -07:00
zoecarver
16c8d0b4d3 [cxx-interop] Bail on parameter that are reference types to dependent types.
Currently, just like for return types, we can't import references to dependent types. Refs https://github.com/apple/swift/pull/41660.
2022-03-29 14:58:55 -07:00
zoecarver
caf83c2375 [cxx-interop] Import const-refs as value types when importing clang function types.
Otherwise, it will pass the value as a poitner.
2022-03-29 11:42:57 -07:00
zoecarver
b839741d57 [cxx-interop] Support references to template types. 2022-03-04 13:54:42 -08:00
Zoe Carver
4c62dae594 Merge pull request #41660 from zoecarver/import-ref-to-dependent-type
[cxx-interop] Bail on dependent reference types. We don't know how to handle those yet.
2022-03-04 08:58:46 -08:00
zoecarver
5537b72c13 [cxx-interop] Bail on dependent reference types. We don't know how to handle those yet. 2022-03-03 17:15:33 -08:00
Alex Lorenz
ebb21d7ec1 [cxx-interop] Import const T& parameters as T.
This change allows Swift code to pass immutable values to const references in C++.
2022-03-03 14:42:27 -08:00
zoecarver
7c5b1608df [nfc][cxx-interop] Fix regex in reference irgen test.
We shouldn't rely on the specific name of the operand.
2021-09-27 10:39:34 -07:00
zoecarver
7a42b71bc0 [cxx-interop] Import reference types as inout not UnsafePointer. 2021-09-23 18:10:56 -07:00
Saleem Abdulrasool
9ba7bf78db Interop: use new target-clangxx for building C++ code
This enables control over the C++ flags used during testing for the C++
interop from a single location rather than having to alter all the
tests.
2021-01-12 08:29:24 -08:00
Saleem Abdulrasool
f5a08e45bc Interop: restore -std=c++11 for a few tests
Restore the explicit C++ standard for these tests as the C++ compiler
invocation on Darwin uses the system compiler rather than the just built
clang, which may be sufficiently different to have different default
standards.

This will be cleaned up with the next change to introduce a new
`%target-clangxx` to control the C++ standard.
2021-01-11 11:01:44 -08:00
Saleem Abdulrasool
21732c9551 Interop: remove unnecessary C++ standard
Do not override the C++ standard explicitly when running the tests.
This will eventually be useful in allowing tests to run against
different C++ standards.
2021-01-11 08:57:56 -08:00
Saleem Abdulrasool
df9d1e815f Interop/Cxx: explicitly require C++ for modules
The C++ interop modules require C++ support.  Explicitly require C++ as
a feature when building these modules.  This has no impact on the
changes as all the tests enable C++ already.
2021-01-06 16:59:42 -08:00
zoecarver
317272852b [cxx-interop] Bail if the type a reference type is wrapping is unkown.
If we can't find the type a reference type is wrapping (the pointee
type), then don't import that type instead of crashing later.
2020-11-01 12:55:44 -08:00
Marcel Hlopko
8fa18a42b8 Remove -Xcc -std=c++17 (#33449) 2020-08-14 16:42:15 +02:00
Marcel Hlopko
e293330563 [CxxInterop] Import C++ references. (#31784) 2020-05-15 10:42:37 +02:00
Saleem Abdulrasool
6bf7593fd5 Revert "[CxxInterop] Import C++ references." (#31777) 2020-05-14 10:43:22 +02:00
Marcel Hlopko
30b5fd52e8 [CxxInterop] Import C++ references. (#31702) 2020-05-13 13:34:28 +02:00