We were previously marking synthesized FRT initializers as
"returns_retained" unconditionally (for non-immortal types), which can
cause memory errors and also suppresses diagnostics about the lack of
such annotations.
This patch fixes that behavior, and also adjusts the source locations of
the synthesized C++ factory method to point to the actual constructor
decl when its location is available, so that the diagnostics make sense
when warning about the lack of annotations.
rdar://163127315
This attribute introduces some conversions between the annotated smart
pointer type and the native swift reference type. In the future we plan
to introduce bridging so the smart pointer type can be hidden in the
signature and we can automatically convert it to the native Swift
reference type on the ABI boundaries.
This PR is using a new way to use swift_attr attributes. Instead of
doing the parsing in the clang importer it falls back to Swift's
attribute parsing and makes sure that the annotation has valid Swift
attribute syntax.
rdar://156521316
We build forwarding methods to call the virtual methods. The forwarding
methods tried to copy move-only types which resulted in a compiler
crash. Now we try to detect this scenario and insert the required cast
to make sure we get a move instead.
rdar://162195228
Type-level ownership annotations
(`SWIFT_RETURNED_AS_UNRETAINED_BY_DEFAULT`) were not suppressing warning
for C++ functions (which are not annotated with
`SWIFT_RETURNS_(UN)RETAIND`) returning references (`&`) to foreign
reference types. The annotation only worked for pointer returns (`*`),
forcing developers to manually annotate every function returning a
reference.
This PR fixes this problem by changing the logic of
`matchSwiftAttrOnRecordPtr` to consider `ReferenceType` also along with
`PointerType`.
This PR also adds comprehensive lit tests for ownership conventions and
diagnostics related to C++ functions that return references to foreign
reference types. These tests were previously not covered by our lit
tests.
rdar://161226212
Some foreign reference types such as IUnknown define retain/release operations as methods of the type.
Previously Swift only supported retain/release operations as standalone functions.
The syntax for member functions would be `SWIFT_SHARED_REFERENCE(.doRetain, .doRelease)`.
rdar://160696723
We usually have unowned values when dealing with foreign types. Make
sure the implicit value ctors will do a +1 to balance the releases. In a
release build we had a use after free over-releasing the object. In
assert builds we had an assertion failure:
Assertion failed: (value->getOwnershipKind() == OwnershipKind::Guaranteed),
function forBorrowedObjectRValue, file ManagedValue.h, line 181.
rdar://160232360
When calling a C++ function that takes a reference to a pointer to a foreign reference type, Swift would previously pass a pointer to the foreign reference type as an argument (instead of a reference to a pointer), which resulted in invalid memory accesses.
This was observed when using `std::vector<ImmortalRef*>::push_back`.
rdar://150791778
We generate forwarding calls for virutal methods. These forwarding calls
had a type error when the original parameter had an rvalue reference
type. In those scenarios we need to insert a static cast to make the
type checker happy.
rdar://154969620
Normally, Swift cannot import an incomplete type. However, when we are
importing a SWIFT_SHARED_REFERENCE type, we're always dealing with
pointers to the type, and there is no need for the underlying type to
be complete. This permits a common pattern in C libraries where the
actual underlying storage is opaque and all APIs traffic in the
pointer, e.g.,
typedef struct MyTypeImpl *MyType;
void MyTypeRetain(MyType ptr);
void MyTypeRelease(MyType ptr);
to use SWIFT_SHARED_REFERENCE to import such types as foreign
references, rather than as OpaquePointer.
Fixes rdar://155970441.
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
This PR adds a feature to import static factory functions returning
foreign reference types to be imported as initializers using the
SWIFT_NAME annotation.
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
If a foreign reference type has a custom retain function, emit a call to
it instead of emitting a call to objc_retainAutoreleasedReturnValue.
rdar://117353222