Skip to content

[interop][SwiftToCxx] bridge ObjC class types to C++ (without Obj… #61268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2022

Conversation

hyp
Copy link
Contributor

@hyp hyp commented Sep 23, 2022

…C guards first)

In the future we should add #if __OBJC__ guards.

@hyp hyp added the c++ interop Feature: Interoperability with C++ label Sep 23, 2022
// CHECK-NEXT: void $s9UseObjCTy04takeB14CClassNullableyySo0B6CKlassCSgF(ObjCKlass *_Nullable x) SWIFT_NOEXCEPT SWIFT_CALL;

// CHECK: inline ObjCKlass *_Nonnull retObjClass() noexcept SWIFT_WARN_UNUSED_RESULT {
// CHECK-NEXT: return (__bridge_transfer __autoreleasing ObjCKlass *)(__bridge void *)_impl::$s9UseObjCTy03retB5ClassSo0B6CKlassCyF();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikeash is this the best approach to take here for ARC?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the goal of the casting? Can this just return _impl::$s9UseObjCTy03retB5ClassSo0B6CKlassCyF();?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case there's an extra retain on the object, so it's leaked.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, because we're following Swift conventions and returning at +1, rather than ObjC conventions returning at +0?

I believe the __autoreleasing is unnecessary, but the rest seems fine, then. I personally prefer the CFBridgingRelease function over __bridge_transfer as I think it's a lot more understandable, but it's probably not a good choice in this context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically Swift's s9UseObjCTy03retB5ClassSo0B6CKlassCyF returns at +2 here, so I need to autorelease once to get back to +1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good, I can try without __autoreleasing then. Cheers!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ARC will automatically autorelease the return value regardless.

@hyp
Copy link
Contributor Author

hyp commented Sep 23, 2022

@swift-ci please test

@hyp
Copy link
Contributor Author

hyp commented Sep 23, 2022

@swift-ci please test source compatibility

@hyp hyp force-pushed the eng/reinterop-objc-class-through branch from 87b9b16 to e6a94bf Compare September 23, 2022 20:45
@hyp
Copy link
Contributor Author

hyp commented Sep 23, 2022

@swift-ci please test

@hyp
Copy link
Contributor Author

hyp commented Sep 23, 2022

@swift-ci please test source compatibility

@hyp hyp force-pushed the eng/reinterop-objc-class-through branch from e6a94bf to bded6c5 Compare September 25, 2022 20:19
@hyp
Copy link
Contributor Author

hyp commented Sep 25, 2022

@swift-ci please test

@hyp
Copy link
Contributor Author

hyp commented Sep 25, 2022

@swift-ci please test source compatibility

@hyp
Copy link
Contributor Author

hyp commented Sep 26, 2022

@swift-ci please test macOS platform

@hyp hyp merged commit 4ee32b5 into swiftlang:main Sep 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants