swift
[cxx-interop] allow shared ref retain function to return self
#77837
Merged

[cxx-interop] allow shared ref retain function to return self #77837

lhoward
lhoward226 days ago

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++.

More: https://forums.swift.org/t/swift-shared-reference-signature-error/76194/7

lhoward lhoward requested a review from zoecarver zoecarver 226 days ago
lhoward lhoward requested a review from hyp hyp 226 days ago
lhoward lhoward requested a review from egorzhdan egorzhdan 226 days ago
lhoward lhoward requested a review from Xazax-hun Xazax-hun 226 days ago
lhoward lhoward requested a review from beccadax beccadax 226 days ago
lhoward lhoward requested a review from ian-twilightcoder ian-twilightcoder 226 days ago
lhoward
lhoward226 days ago

Note to self: need separate error message for invalid retain return value.

lhoward lhoward force pushed from c077f574 to 85ea760c 226 days ago
lhoward lhoward requested a review from hborla hborla 226 days ago
lhoward lhoward requested a review from slavapestov slavapestov 226 days ago
lhoward lhoward requested a review from xedin xedin 226 days ago
Xazax-hun Xazax-hun requested a review from fahadnayyar fahadnayyar 226 days ago
Xazax-hun
Xazax-hun commented on 2024-11-26
Conversation is marked as resolved
Show resolved
include/swift/AST/DiagnosticsClangImporter.def
232232 (bool, StringRef, StringRef))
233233
234ERROR(foreign_reference_types_retain_release_non_void_return_type, none,
234
ERROR(foreign_reference_types_retain_non_void_or_self_return_type, none,
Xazax-hun226 days ago

If one of these only apply for retain and the other for release, we could get rid of the select in both.

lhoward lhoward force pushed from 85ea760c to d9903f14 225 days ago
lhoward lhoward force pushed from d9903f14 to 46c2da41 225 days ago
lhoward
lhoward225 days ago

Tests are now failing, investigating.

lhoward lhoward force pushed from 46c2da41 to 8d56f5a0 224 days ago
lhoward
lhoward224 days ago

Tests passing again.

egorzhdan egorzhdan added c++ interop
zoecarver
zoecarver223 days ago❤ 1

LGTM

lhoward
lhoward223 days ago

Just double checking we don't need to do anything extra at the retain call site to ignore the return value.

DougGregor
DougGregor commented on 2024-12-03
lib/ClangImporter/ImportDecl.cpp
2566 auto resultInterfaceType = operationFn->getResultInterfaceType();
2567 if (!resultInterfaceType->isVoid()) {
2568 if (operationKind == CustomRefCountingOperationKind::release ||
2569
resultInterfaceType->getAnyNominal() != paramDecl)
DougGregor219 days ago

This check isn't correct: it's comparing the declaration result's nominal type (if there is one) against the parameter declaration itself (which has, e.g., the name of the parameter in it). Instead, you need to compare the result type against the parameter type. I also expect that we need to look through an optional type on the result type, in case the retain function handles null pointers in some manner. Specifically, I think it should be:

Suggested change
resultInterfaceType->getAnyNominal() != paramDecl)
!resultInterfaceType->lookThroughSingleOptionalType()->isEqual(paramType))
DougGregor219 days ago

I'm confused about why this PR worked without this change suggested above. Running tests in CI without the change because I would expect it to fail.

lhoward219 days ago

Fix applied (shouldn't disrupt running builds to check previous commit).

lib/ClangImporter/ImportDecl.cpp
DougGregor219 days ago

Ah, thank you for the typo fix here!

DougGregor
DougGregor219 days ago

@swift-ci please smoke test macOS

lhoward [cxx-interop] fix typo in RetainReleaseOperationKind enum
51fd3940
lhoward [cxx-interop] allow shared ref retain function to return self
da23bcf1
lhoward lhoward force pushed from 8d56f5a0 to da23bcf1 219 days ago
DougGregor
DougGregor approved these changes on 2024-12-03
DougGregor
DougGregor219 days ago

@swift-ci please smoke test

lhoward
lhoward219 days ago

What's the next release one might expect to see this in?

DougGregor
DougGregor219 days ago❤ 2

Once it's merged to main, we can cherry-pick to the release/6.1 branch for the 6.1 release (Q1 2025)

finagolfin
finagolfin217 days ago

@swift-ci please smoke test

egorzhdan
egorzhdan approved these changes on 2024-12-04
egorzhdan217 days ago❤ 1

This seems very reasonable, LGTM, thank you!

DougGregor
DougGregor217 days ago

@swift-ci please smoke test Linux

DougGregor
DougGregor216 days ago

@lhoward I'm going to merge now. Would you cherry-pick this to the release/6.1 branch? There are instructions at https://forums.swift.org/t/swift-6-1-release-process/75442#p-344524-pull-requests-for-release-branch-8

DougGregor DougGregor merged 41073a88 into main 216 days ago
lhoward
lhoward148 days ago

Retain functions that return self need to be manually annotated with SWIFT_RETURNS_RETAINED. Perhaps this could be inferred in a future patch.

Login to write a write a comment.

Login via GitHub

Assignees
No one assigned
Labels
Milestone