-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[cxx-interop] allow shared ref retain function to return self #77837
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2530,24 +2530,23 @@ namespace { | |
void validateForeignReferenceType(const clang::CXXRecordDecl *decl, | ||
ClassDecl *classDecl) { | ||
|
||
enum class RetainReleaseOperatonKind { | ||
enum class RetainReleaseOperationKind { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, thank you for the typo fix here! |
||
notAfunction, | ||
doesntReturnVoid, | ||
doesntReturnVoidOrSelf, | ||
invalidParameters, | ||
valid | ||
}; | ||
|
||
auto getOperationValidity = | ||
[&](ValueDecl *operation) -> RetainReleaseOperatonKind { | ||
[&](ValueDecl *operation, | ||
CustomRefCountingOperationKind operationKind) | ||
-> RetainReleaseOperationKind { | ||
auto operationFn = dyn_cast<FuncDecl>(operation); | ||
if (!operationFn) | ||
return RetainReleaseOperatonKind::notAfunction; | ||
|
||
if (!operationFn->getResultInterfaceType()->isVoid()) | ||
return RetainReleaseOperatonKind::doesntReturnVoid; | ||
return RetainReleaseOperationKind::notAfunction; | ||
|
||
if (operationFn->getParameters()->size() != 1) | ||
return RetainReleaseOperatonKind::invalidParameters; | ||
return RetainReleaseOperationKind::invalidParameters; | ||
|
||
Type paramType = | ||
operationFn->getParameters()->get(0)->getInterfaceType(); | ||
|
@@ -2557,20 +2556,31 @@ namespace { | |
} | ||
|
||
swift::NominalTypeDecl *paramDecl = paramType->getAnyNominal(); | ||
|
||
// The return type should be void (for release functions), or void | ||
// or the parameter type (for retain functions). | ||
auto resultInterfaceType = operationFn->getResultInterfaceType(); | ||
if (!resultInterfaceType->isVoid()) { | ||
if (operationKind == CustomRefCountingOperationKind::release || | ||
!resultInterfaceType->lookThroughSingleOptionalType()->isEqual(paramType)) | ||
return RetainReleaseOperationKind::doesntReturnVoidOrSelf; | ||
} | ||
|
||
// The parameter of the retain/release function should be pointer to the | ||
// same FRT or a base FRT. | ||
if (paramDecl != classDecl) { | ||
if (const clang::Decl *paramClangDecl = paramDecl->getClangDecl()) { | ||
if (const auto *paramTypeDecl = | ||
dyn_cast<clang::CXXRecordDecl>(paramClangDecl)) { | ||
if (decl->isDerivedFrom(paramTypeDecl)) { | ||
return RetainReleaseOperatonKind::valid; | ||
return RetainReleaseOperationKind::valid; | ||
} | ||
} | ||
} | ||
return RetainReleaseOperatonKind::invalidParameters; | ||
return RetainReleaseOperationKind::invalidParameters; | ||
} | ||
return RetainReleaseOperatonKind::valid; | ||
|
||
return RetainReleaseOperationKind::valid; | ||
}; | ||
|
||
auto retainOperation = evaluateOrDefault( | ||
|
@@ -2607,28 +2617,29 @@ namespace { | |
false, retainOperation.name, decl->getNameAsString()); | ||
} else if (retainOperation.kind == | ||
CustomRefCountingOperationResult::foundOperation) { | ||
RetainReleaseOperatonKind operationKind = | ||
getOperationValidity(retainOperation.operation); | ||
RetainReleaseOperationKind operationKind = | ||
getOperationValidity(retainOperation.operation, | ||
CustomRefCountingOperationKind::retain); | ||
HeaderLoc loc(decl->getLocation()); | ||
switch (operationKind) { | ||
case RetainReleaseOperatonKind::notAfunction: | ||
case RetainReleaseOperationKind::notAfunction: | ||
Impl.diagnose( | ||
loc, | ||
diag::foreign_reference_types_retain_release_not_a_function_decl, | ||
false, retainOperation.name); | ||
break; | ||
case RetainReleaseOperatonKind::doesntReturnVoid: | ||
case RetainReleaseOperationKind::doesntReturnVoidOrSelf: | ||
Impl.diagnose( | ||
loc, | ||
diag::foreign_reference_types_retain_release_non_void_return_type, | ||
false, retainOperation.name); | ||
diag::foreign_reference_types_retain_non_void_or_self_return_type, | ||
retainOperation.name); | ||
break; | ||
case RetainReleaseOperatonKind::invalidParameters: | ||
case RetainReleaseOperationKind::invalidParameters: | ||
Impl.diagnose(loc, | ||
diag::foreign_reference_types_invalid_retain_release, | ||
false, retainOperation.name, classDecl->getNameStr()); | ||
break; | ||
case RetainReleaseOperatonKind::valid: | ||
case RetainReleaseOperationKind::valid: | ||
break; | ||
} | ||
} else { | ||
|
@@ -2671,28 +2682,29 @@ namespace { | |
true, releaseOperation.name, decl->getNameAsString()); | ||
} else if (releaseOperation.kind == | ||
CustomRefCountingOperationResult::foundOperation) { | ||
RetainReleaseOperatonKind operationKind = | ||
getOperationValidity(releaseOperation.operation); | ||
RetainReleaseOperationKind operationKind = | ||
getOperationValidity(releaseOperation.operation, | ||
CustomRefCountingOperationKind::release); | ||
HeaderLoc loc(decl->getLocation()); | ||
switch (operationKind) { | ||
case RetainReleaseOperatonKind::notAfunction: | ||
case RetainReleaseOperationKind::notAfunction: | ||
Impl.diagnose( | ||
loc, | ||
diag::foreign_reference_types_retain_release_not_a_function_decl, | ||
true, releaseOperation.name); | ||
break; | ||
case RetainReleaseOperatonKind::doesntReturnVoid: | ||
case RetainReleaseOperationKind::doesntReturnVoidOrSelf: | ||
Impl.diagnose( | ||
loc, | ||
diag::foreign_reference_types_retain_release_non_void_return_type, | ||
true, releaseOperation.name); | ||
diag::foreign_reference_types_release_non_void_return_type, | ||
releaseOperation.name); | ||
break; | ||
case RetainReleaseOperatonKind::invalidParameters: | ||
case RetainReleaseOperationKind::invalidParameters: | ||
Impl.diagnose(loc, | ||
diag::foreign_reference_types_invalid_retain_release, | ||
true, releaseOperation.name, classDecl->getNameStr()); | ||
break; | ||
case RetainReleaseOperatonKind::valid: | ||
case RetainReleaseOperationKind::valid: | ||
break; | ||
} | ||
} else { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.