Commit Graph

123 Commits

Author SHA1 Message Date
Egor Zhdan
45b8045e75 [cxx-interop] Fix test failure on older macOS
The runtime logic for printing a foreign reference type is behind `if #available(SwiftStdlib 6.2, *)`, which means it won't run on older versions of macOS, even if you use a newer Swift runtime.

rdar://153735437
(cherry picked from commit a292113ef2)
2025-06-24 13:40:32 +01:00
Egor Zhdan
b16816b4ee [cxx-interop] Disable test with an older runtime
848fad00 introduced support for printing foreign reference types. It changes both the compiler and the runtime, and having the runtime change applied is required for the test to pass. Let's not try to run it with an old runtime.

This change also splits up a test for printing of value types from a test for printing of foreign reference types, since we don't have any runtime restrictions for value types.

rdar://153205860
(cherry picked from commit 62d56067c8)
2025-06-23 18:16:24 +01:00
Egor Zhdan
069c42159f [cxx-interop] Relax a SILVerifier assertion for immortal reference types
Immortal C++ foreign reference types get TrivialTypeLowering instead of ReferenceTypeLowering, since they do not have retain/release lifetime operations. This was tripping up an assertion in SILVerifier.

rdar://147251759 / resolves https://github.com/swiftlang/swift/issues/80065
(cherry picked from commit 2e3df1c0f3)
2025-05-21 13:06:01 +01:00
fahadnayyar
5ac1e63ad6 [6.2] [cxx-interop] Disabling should be annotated with SWIFT_RETURNS_(UN)RETAINED warning (#81390)
**Explanation**:
In Swift 6.1, we introduced warnings for C++ APIs returning
`SWIFT_SHARED_REFERENCE` types that lack the
`SWIFT_RETURNS_(UN)RETAINED` annotations. These warnings serve as a
reminder to annotate APIs, as the absence of these annotations can lead
to arbitrary assumptions about the ownership of returned
`SWIFT_SHARED_REFERENCE` values. This could result in both
use-after-free (memory safety) bugs and memory leaks.
We have received feedback from a few adopters indicating potential false
positive cases where these warnings are triggered. Consequently, we have
decided to disable these warnings in Swift 6.2 and re-qualify the
warnings on larger projects to ensure their effectiveness and eliminate
false positives.

- **Scope**: Disabling a previously shipped warning. There is no
likelihood of any source breakage or semantic alterations.
- **Issues**: rdar://150937617 , rdar://150800115
- **Original PRs**: N/A
- **Risk**: Low
- **Testing**: Lit test and adopted manually on a demo Xcode project
- **Reviewers**: N/A Our intention is to re-enable the warnings in later
beta releases of Swift 6.2 once we have stronger evidence of their
effectiveness on large codebases and proof that there are no false
positives.
2025-05-09 08:08:01 -07:00
Allan Shortlidge
e867788d65 ClangImporter: Introduce the #ClangDeclarationImport diagnostic group.
The warnings that ClangImporter emits about issues it encounters while
importing declarations from Clang modules should all belong to a diagnostic
group so that users of `-warnings-as-errors` can control their behavior using
the compiler flags introduce with SE-0443. It's especially important that these
diagnostics be controllable since they are often caused by external
dependencies and therefore the developer may not have any control over whether
they are emitted.

The `#ClangDeclarationImport` diagnostic group is intentionally broad so that
developers have a way to control all of these diagnostics with a single
`-Wwarning` flag. I fully expect that we'll introduce finer-grained diagnostic
groups for some of these diagnostics in the future, but those groups should be
hierarchically nested under `#ClangDeclarationImport`, which is supported by
SE-0443.

Resolves rdar://150524204.
2025-05-05 14:19:04 -07:00
Gábor Horváth
c3a7203221 [6.2][cxx-interop] Fix runtime crash passing FRTs as const FRT*
Explanation: There was an inconsistency between non-const and const FRT
pointers. The former used Direct_Unowned the latter used Indirect
calling convention. We want to use Direct_Unowned for both cases. The
crash was the result of a calling convention mismatch between the
SILFunctionType of a Swift closure and the SILFunctionType of the C++
function's formal parameter that is taking a function pointer. The
compiler tried to insert a conversion between the two function types
that does not exist and caused an assertion in debug compilers and
miscompilation in production compilers.
Issue: rdar://149398905
Risk: Low, the fix is targeted and we change to a well-tested behavior
with non-const FRT pointers.
Testing: Regression test added.
Original PR: #81070
Reviewer: @j-hui
2025-04-25 10:59:45 +01:00
fahadnayyar
2f6fead77c Merge pull request #80706 from swiftlang/fahadnayyar/returns-unretained/diag-relax-templated-return-type
🍒 [6.2] [cxx-interop] Relax error when using SWIFT_RETURNS_(UN)RETAINED on templated return types resolving to non-frts
2025-04-18 16:14:32 -07:00
fahadnayyar
f3ecb7ea8b [cxx-interop] convert CXXForeignReferenceTypeInitializers into SuppressCXXForeignReferenceTypeInitializers to be an opt-out flag
Turning the feature "ctor of C++ foreign reference types is callable as Swift Initializers" on by default.

rdar://148285972
2025-04-10 05:55:05 -07:00
fahadnayyar
b7d05052bf [cxx-interop] Relax error when using SWIFT_RETURNS_(UN)RETAINED on templated return types resolving to non-frts (#78968)
We should not complain about usage of SWIFT_RETURNS_(UN)RETAINED for templated C++ APIs when instantiated with a non SWIFT_SHARED_REFERENCE types

rdar://143732201
2025-04-09 23:12:23 -07:00
Gábor Horváth
789359ced3 [cxx-interop] Fix not importing return type for certain functions
Explanation: Fixes that functions imported from C++ namespaces are
taking a different code path than functions importing from the global
namespace. This also fixes an error where the return type of a templated
function is sometimes not imported.
Scope: C++ forward interop.
Issue: rdar://148735986
Risk: Low, the fix is targeted to make C++ functions in namespaces take
a well tested code path that we already use for C++ functions in the
global scope.
Testing: Added tests to test suite
Reviewer: John Hui
2025-04-07 11:52:12 +01:00
Egor Zhdan
ba8ba57c77 [cxx-interop] Layout reference types that use tail padding of their bases correctly
This is a follow-up to d3e43bbe which resolved the issue for value types, but not for foreign reference types.

This change teaches IRGen that a base type of a C++ type might occupy less memory than its `sizeof`, specifically, it might only use `dsize` amount of memory.

rdar://147527755
2025-03-27 16:03:51 +00:00
Egor Zhdan
2abed96efc [cxx-interop] Cleanup a test 2025-03-26 17:55:03 +00:00
Gábor Horváth
8f58ad67cf Update test/Interop/Cxx/foreign-reference/extensions.swift
Co-authored-by: John Hui <j-hui@apple.com>
2025-03-20 12:35:56 +00:00
Gabor Horvath
7bdd496f61 Address review comments. 2025-03-19 12:56:56 +00:00
Gabor Horvath
b632534528 [cxx-interop] Interpret Self as a static shorthand for FRTs
We do not have dynamic self metadata for foreign reference types.

rdar://145066864
2025-03-19 12:56:56 +00:00
Gabor Horvath
14d92ff7a1 [cxx-interop] Test static factory backed initializers with value types
We can use swift_name to import a static factory function as a Swift
initializer. This was tested with foreign reference types but not with
value types. This PR adds a test case for the latter.

rdar://117531428
2025-03-11 17:36:54 +00:00
Gabor Horvath
2f46d55b85 [cxx-interop] Support importing static factories as initializers
This PR adds a feature to import static factory functions returning
foreign reference types to be imported as initializers using the
SWIFT_NAME annotation.
2025-03-03 11:25:05 +00:00
Gabor Horvath
bbd60c81d4 [cxx-interop] Fix circular reference, remove feature flag check
It turns out the query to check the reference semantics of a type had
the side effect of importing some functions/types. This could introduce
circular reference errors with our queries. This PR removes this side
effect from the query and updates the test files. Since we do the same
work later on in another query, the main change is just the wording of
the diagnostics (and we now can also infer immortal references based on
the base types). This PR also reorders some operations, specifically now
we mark base classes as imported before we attempt to import template
arguments.

After these changes it is possible to remove the last feature check for
strict memory safe mode. We want to mark types as @unsafe in both
language modes so we can diagnose redundant unsafe markers even when the
feature is off.
2025-02-28 15:05:02 +00:00
fahadnayyar
d8f919778d [cxx-interop] [cxx-interop] Infer SWIFT_SHARED_REFERENCE for types inheriting from a C++ foreign reference type
rdar://97914474
2025-02-20 08:46:57 -08:00
Erik Eckstein
ba4081ee76 Optimizer: replace PredictableMemoryAccessOptimizations with MandatoryRedundantLoadElimination in the pass pipeline
PredictableMemoryAccessOptimizations has become unmaintainable as-is.
RedundantLoadElimination does (almost) the same thing as PredictableMemoryAccessOptimizations.
It's not as powerful but good enough because PredictableMemoryAccessOptimizations is actually only needed for promoting integer values for mandatory constant propagation.
And most importantly: RedundantLoadElimination does not insert additional copies which was a big problem in PredictableMemoryAccessOptimizations.

Fixes rdar://142814676
2025-02-07 11:30:35 +01:00
fahadnayyar
0f2637c32e Merge pull request #78230 from fahadnayyar/returns-retained-objc-support
[cxx-interop] Add support for SWIFT_RETURNS_(UN)RETAINED for ObjC APIs returning C++ FRT
2024-12-18 07:27:41 -08:00
Egor Zhdan
28d090f6ea Merge pull request #78252 from swiftlang/egorzhdan/frt-conditional-cast-test
[cxx-interop] Add tests for conditional cast to foreign reference type
2024-12-18 11:34:05 +00:00
Fahad Nayyar
426d471d1e [cxx-interop] Add support for SWIFT_RETURNS_(UN)RETAINED for ObjC APIs returning C++ FRT
rdar://135360972
2024-12-17 17:31:47 -08:00
Egor Zhdan
d4cb7b78d7 [cxx-interop] Add tests for conditional cast to foreign reference type
This is a follow-up to 84ae5fbe.

rdar://141227849
2024-12-17 19:44:19 +00:00
John Hui
796075ab88 [cxx-interop] Fix spurious ambiguous member lookup (#78132)
Nested calls to importBaseMemberDecl() subvert its cache and compromise its idempotence, causing the semantic checker to spuriously report ambiguous member lookups when multiple ClangRecordMemberLookup requests are made (e.g., because of an unrelated missing member lookup).

One such scenario is documented as a test case: test/Interop/Cxx/class/inheritance/inherited-lookup-typechecker.swift fails without this patch because of the expected error from the missing member. Meanwhile, test/Interop/Cxx/class/inheritance/inherited-lookup-executable.swift works because it does not attempt to access a missing member.

This patch fixes the issue by only calling importBaseMemberDecl() in the most derived class (where the ClangRecordMemberLookup originated, i.e., not in recursive requests).

As a consequence of my patch, synthesized member accessors in the derived class directly invoke the member from the base class where the member is inherited from, rather than incurring an indirection at each level of inheritance. As such, the synthesized symbol names are different (and shorter). I've taken this opportunity to update the relevant tests to // CHECK for more of the mangled symbol, rather than only the synthesized symbol prefix, for more precise testing and slightly better readability.

rdar://141069984
2024-12-17 11:40:18 -08:00
Egor Zhdan
f2ad9f3d90 Merge pull request #78223 from swiftlang/egorzhdan/frt-as-existential-crash
[cxx-interop] Fix runtime crash when casting from an existential to a foreign reference type
2024-12-17 12:59:34 +00:00
Egor Zhdan
84ae5fbe8c [cxx-interop] Fix runtime crash when casting from an existential to a foreign reference type
When a C++ foreign reference type is conformed to a Swift protocol via a Swift extension, trying to cast `any MyProtocol` to the foreign reference type crashes the runtime.

This was because `selectCasterForDest` wasn't handling C++ foreign reference types, and we were hitting `swift_unreachable`.

This change makes sure the runtime doesn't crash for such casts.

Notably, Swift doesn't have enough metadata to determine if the conditional cast actually succeeded. This is also a problem for CF types. Casting CF types in a similar fashion triggers a typechecker diagnostic. That diagnostic will be amended in a follow-up patch to also trigger for foreign reference types.

rdar://141227849
2024-12-16 20:23:17 +00:00
Egor Zhdan
57c7ecd244 [cxx-interop] Fix memory layout for structs with out-of-order base types
In C++, a primary base class that is placed in the beginning of the type's memory layout isn't always the type that is the first in the list of bases – the base types might be laid out in memory in a different order.

This makes sure that IRGen handles base types of C++ structs in the correct order.

This fixes an assertion in asserts-enabled compilers, and an out-of-memory error in asserts-disabled compilers. The issue was happening for both value types and foreign reference types. This change also includes a small refactoring to reuse the logic between the two code paths.

rdar://140848603
2024-12-16 15:53:43 +00:00
Egor Zhdan
1f36fda7e1 [cxx-interop] Use the right assertion function in tests
`assert`s get stripped away in release builds. To make sure the checks in tests still run in release builds, this switches to `expectTrue`/`expectFalse` instead of `assert`.
2024-12-14 22:03:06 +00:00
Doug Gregor
41073a881c Merge pull request #77837 from lhoward/lhoward/retain-return-self
[cxx-interop] allow shared ref retain function to return self
2024-12-05 11:25:31 -08:00
fahadnayyar
2e501754dd [cxx-interop] Improve the warnings for unannotated c++ APIs returning SWIFT_SHARED_REFERENCE types 2024-12-03 18:26:13 -08:00
Luke Howard
da23bcf1c6 [cxx-interop] allow shared ref retain function to return self
Many existing C APIs for retaining references, including Apple's own, return
the reference. Support this pattern, along with the existing void return
signature, with when importing reference types from C++.
2024-12-03 11:45:29 +11: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
fahadnayyar
e640ed637d [cxx-interop] Warning unannotated C++ APIs returning SWIFT_SHARED_REFERENCE types 2024-10-29 17:31:15 -07:00
John Hui
ec506f824b [cxx-interop] Fix swift::Type to clang::QualType conversion to correctly handle foreign reference types
For types imported from C++, the original clang::TypeDecl is saved in
the swift::Type and reused for conversions back to clang::QualType.
However, the conversion did not account for foreign reference types,
which should be mapped to a pointer to the C++ record type, rather than
the record type itself.

This bug first appeared as a function template instantiation failure
due to a sanity check performed by SwiftDeclConverter::foreignReferenceTypePassedByRef()
but may also affect other round-tripping type conversions.
The added test case demonstrates the template instantiation scenario,
where templates were being used to overcome Swift/C++ interop's current
lack of support for class inheritance.

rdar://134979343
2024-10-07 14:40:38 -07:00
fahadnayyar
bd9095e69f Merge pull request #76733 from fahadnayyar/cxx-interop-frt-retain-release-diagnostics-patch-2
[cxx-interop] Add diagnostic notes when retain/release are not import…
2024-09-27 22:37:07 -07:00
fahadnayyar
d1b380ffd6 [cxx-interop] Add diagnostic notes when retain/release are not imported into swift for SWIFT_SHARED_REFERENCE types 2024-09-27 18:57:31 -07:00
Egor Zhdan
82c076c9d3 [cxx-interop] C++ reference types are not AnyObjects
C++ foreign reference types have custom reference counting mechanisms, so they cannot conform to `AnyObject`.

Currently Swift's type system treats C++ FRTs as `AnyObject`s on non-Darwin platforms, which is incorrect. This change makes sure the behavior is consistent with Darwin platform, i.e. a cast of C++ FRT to `AnyObject` is rejected by the typechecker.

rdar://136664617
2024-09-25 15:15:11 +01:00
fahadnayyar
7753ad0885 Merge pull request #76131 from fahadnayyar/cxx-interop-frt-retain-release-diagnostics
Fixed diagnostics for incorrect parameters of retain/release function…
2024-09-11 10:24:20 -07:00
fahadnayyar
f3f4e1913b Fixed diagnostics for incorrect signature of retain/release functions of SWIFT_SHARED_REFERENCE C++ types. 2024-09-10 16:15:07 -07:00
fahadnayyar
ea4328384d [cxx-interop] Add SWIFT_RETURNS_RETAINED and SWIFT_RETURNS_UNRETAINED annotations to specify ownership of FRT returned by a C++ method or function
rdar://135306908
2024-09-05 11:44:31 -07:00
Alexander Cyon
db0b5db54e [test/Interop] Fix typos (#75032) 2024-08-28 09:41:09 -07:00
Akira Hatanaka
0e2a8bd0de Fix a test that was failing on 32-bit platforms (#75808)
rdar://133549324
2024-08-09 16:14:35 -07:00
Akira Hatanaka
e21c117831 [cxx-interop] Use unowned return convention for ObjC methods returning foreign reference types (#75640)
This fixes a runtime crash that occurs when a pointer to a foreign
reference type is passed to objc_retainAutoreleasedReturnValue.

This reverts 335cec0e8d.

rdar://117353222
2024-08-07 21:16:47 -07:00
Andrew Trick
030f2b8e90 Merge pull request #75050 from atrick/vardecl_lifetime
[SILGen] Create SIL variable declaration scopes for trivial values.
2024-07-26 16:23:07 -07:00
Andrew Trick
a1fe258692 Update tests for new trivial moves and extend_lifetimes. 2024-07-26 08:27:48 -07:00
Finagolfin
15e1c73969 [android][test] Enable several C++ Interop and other tests
Also, fix lit.cfg for running the test suite natively in Android and mark one
SILOptimizer executable_test as such.
2024-07-20 17:56:51 +05:30
Egor Zhdan
deea1e8b02 [cxx-interop] Add tests for methods of reference types returning reference types
rdar://130686970 / resolves https://github.com/swiftlang/swift/issues/64315
2024-06-28 18:32:17 +01:00
Egor Zhdan
70b49c983b [cxx-interop] Only run tests for reference types against a recent Swift runtime, pt 2
rdar://128681577
2024-06-11 16:47:44 +01:00
Egor Zhdan
876c056153 Merge pull request #74261 from apple/egorzhdan/old-runtime-test
[cxx-interop] Only run tests for reference types against a recent Swift runtime
2024-06-11 12:51:52 +01:00