Skip to content
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

[SR-7263] Treat APIs returning unmanaged CF types as NS_RETURNS_INNER_POINTER #49811

Closed
belkadan opened this issue Mar 23, 2018 · 2 comments
Closed
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself good first issue Good for newcomers SILGen Area → compiler: The SIL generation stage

Comments

@belkadan
Copy link
Contributor

Previous ID SR-7263
Radar rdar://problem/37086219
Original Reporter @belkadan
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, ClangImporter, SILGen, StarterBug
Assignee @ChristopherRogers
Priority Medium

md5: 5e6365a19cffc113eb15c513b965b64c

Issue Description:

When an Objective-C method returns an Unmanaged CF type at +0, there's a delay between when the value is returned and when it ultimately gets retained (by a call to takeUnretainedValue()).

let x = Foo().axuiElementRef().takeUnretainedValue()

In Objective-C, the Foo instance is guaranteed not to be destroyed until the end of the statement, but Swift has no such rule. However, Objective-C also has a feature called NS_RETURNS_INNER_POINTER, which means that a raw pointer value is only valid as long as the enclosing object lives. We should apply that same behavior for any Unmanaged CF type, with the assumption that it won't affect the lifetime of the object very much because usually the first thing you do with Unmanaged is unpack the value (giving you an actual owning reference to it).

@belkadan
Copy link
Contributor Author

Starter bug instructions: The place where NS_RETURNS_INNER_POINTER is honored is in SILFunctionType.cpp (look for clang::ObjCReturnsInnerPointerAttr). In addition to checking for that attribute, we'd also want to check if the return type in Swift is the Unmanaged struct. You can get the return type from the TypeLowering object, and then see if it's a struct through SILType::getStructOrBoundGenericStruct, and then check it against ASTContext::getUnmanagedDecl.

If someone picks this up, I'll pull in a more SIL-friendly person to suggest a test case to copy from.

@ChristopherRogers
Copy link
Contributor

#16129

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself good first issue Good for newcomers SILGen Area → compiler: The SIL generation stage
Projects
None yet
Development

No branches or pull requests

2 participants