-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[cxx-interop] Add SWIFT_RETURNS_RETAINED and SWIFT_RETURNS_UNRETAINED… #75897
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
fahadnayyar
merged 1 commit into
swiftlang:main
from
fahadnayyar:f-dev-frt-return-ownership-annotations-pr
Sep 6, 2024
Merged
Changes from all commits
Commits
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
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
231 changes: 231 additions & 0 deletions
231
test/Interop/Cxx/foreign-reference/Inputs/cxx-functions-and-methods-returning-frt.h
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 |
---|---|---|
@@ -0,0 +1,231 @@ | ||
#pragma once | ||
|
||
fahadnayyar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// FRT or SWIFT_SHARED_REFERENCE type | ||
struct FRTStruct { | ||
// Friend function declarations | ||
friend FRTStruct *returnInstanceOfFRTStruct(int v); | ||
friend FRTStruct *returnInstanceOfFRTStructWithAttrReturnsRetained(int v); | ||
friend FRTStruct *returnInstanceOfFRTStructWithAttrReturnsUnretained(int v); | ||
} __attribute__((swift_attr("import_reference"))) | ||
__attribute__((swift_attr("retain:retainFRTStruct"))) | ||
__attribute__((swift_attr("release:releaseFRTStruct"))); | ||
|
||
// Retain function for the FRT or SWIFT_SHARED_REFERENCE type FRTStruct | ||
void retainFRTStruct(FRTStruct *_Nonnull b) {} | ||
|
||
// Release function for the FRT or SWIFT_SHARED_REFERENCE type FRTStruct | ||
void releaseFRTStruct(FRTStruct *_Nonnull b) {} | ||
|
||
// Friend function definition | ||
FRTStruct *returnInstanceOfFRTStruct() { return new FRTStruct; } | ||
|
||
// Friend function definition | ||
FRTStruct *returnInstanceOfFRTStructWithAttrReturnsRetained() | ||
__attribute__((swift_attr("returns_retained"))) { | ||
return new FRTStruct; | ||
} | ||
|
||
// Friend function definition | ||
FRTStruct *returnInstanceOfFRTStructWithAttrReturnsUnretained() | ||
__attribute__((swift_attr("returns_unretained"))) { | ||
return new FRTStruct; | ||
} | ||
|
||
// Global/free C++ functions returning FRT without any attributes | ||
FRTStruct *_Nonnull global_function_returning_FRT(); | ||
FRTStruct *_Nonnull global_function_returning_copy(); | ||
FRTStruct *_Nonnull global_function_returning_create(); | ||
FRTStruct *_Nonnull global_function_returning_init(); | ||
FRTStruct *_Nonnull global_function_returning_clone(); | ||
|
||
// Global/free C++ functions returning FRT with attribute | ||
// swift_attr("returns_retained") or SWIFT_RETURNS_RETAINED | ||
FRTStruct *_Nonnull global_function_returning_FRT_with_attr_returns_retained() | ||
__attribute__((swift_attr("returns_retained"))); | ||
FRTStruct *_Nonnull global_function_returning_copy_with_attr_returns_retained() | ||
__attribute__((swift_attr("returns_retained"))); | ||
FRTStruct | ||
*_Nonnull global_function_returning_create_with_attr_returns_retained() | ||
__attribute__((swift_attr("returns_retained"))); | ||
FRTStruct *_Nonnull global_function_returning_init_with_attr_returns_retained() | ||
__attribute__((swift_attr("returns_retained"))); | ||
FRTStruct *_Nonnull global_function_returning_clone_with_attr_returns_retained() | ||
__attribute__((swift_attr("returns_retained"))); | ||
|
||
// Global/free C++ functions returning FRT with attribute | ||
// swift_attr("returns_unretained") or SWIFT_RETURNS_UNRETAINED | ||
FRTStruct *_Nonnull global_function_returning_FRT_with_attr_returns_unretained() | ||
__attribute__((swift_attr("returns_unretained"))); | ||
FRTStruct | ||
*_Nonnull global_function_returning_copy_with_attr_returns_unretained() | ||
__attribute__((swift_attr("returns_unretained"))); | ||
FRTStruct | ||
*_Nonnull global_function_returning_create_with_attr_returns_unretained() | ||
__attribute__((swift_attr("returns_unretained"))); | ||
FRTStruct | ||
*_Nonnull global_function_returning_init_with_attr_returns_unretained() | ||
__attribute__((swift_attr("returns_unretained"))); | ||
FRTStruct | ||
*_Nonnull global_function_returning_clone_with_attr_returns_unretained() | ||
__attribute__((swift_attr("returns_unretained"))); | ||
|
||
// Static Global/free functions returning FRT | ||
static FRTStruct *_Nonnull global_static_function_returning_FRT(); | ||
static FRTStruct | ||
*_Nonnull global_static_function_returning_FRT_with_attr_returns_retained() | ||
__attribute__((swift_attr("returns_retained"))); | ||
static FRTStruct | ||
*_Nonnull global_static_function_returning_FRT_with_attr_returns_unretained() | ||
__attribute__((swift_attr("returns_unretained"))); | ||
static FRTStruct *_Nonnull global_static_function_returning_copy(); | ||
static FRTStruct *_Nonnull global_static_function_returning_create(); | ||
static FRTStruct | ||
*_Nonnull global_static_function_returning_copy_with_attr_returns_retained() | ||
__attribute__((swift_attr("returns_retained"))); | ||
static FRTStruct | ||
*_Nonnull global_static_function_returning_copy_with_attr_returns_unretained() | ||
__attribute__((swift_attr("returns_unretained"))); | ||
|
||
// Global/free functions returning FRT without _Nonnull | ||
FRTStruct *global_function_returning_FRT_wihout_Nonnull(); | ||
FRTStruct * | ||
global_function_returning_FRT_with_attr_returns_retained_wihout_Nonnull() | ||
__attribute__((swift_attr("returns_retained"))); | ||
FRTStruct * | ||
global_function_returning_FRT_with_attr_returns_unretained_wihout_Nonnull() | ||
__attribute__((swift_attr("returns_unretained"))); | ||
FRTStruct *global_function_returning_copy_wihout_Nonnull(); | ||
FRTStruct *global_function_returning_create_wihout_Nonnull(); | ||
FRTStruct * | ||
global_function_returning_copy_with_attr_returns_retained_wihout_Nonnull() | ||
__attribute__((swift_attr("returns_retained"))); | ||
FRTStruct * | ||
global_function_returning_copy_with_attr_returns_unretained_wihout_Nonnull() | ||
__attribute__((swift_attr("returns_unretained"))); | ||
|
||
// Struct having static methods returning FRT without any attributes | ||
struct StructWithStaticMethodsReturningFRTWithoutAttributes { | ||
static FRTStruct *_Nonnull StaticMethodReturningFRT(); | ||
static FRTStruct *_Nonnull StaticMethodReturningFRT_copy(); | ||
static FRTStruct *_Nonnull StaticMethodReturningFRT_create(); | ||
static FRTStruct *_Nonnull StaticMethodReturningFRT_init(); | ||
static FRTStruct *_Nonnull StaticMethodReturningFRT_clone(); | ||
}; | ||
|
||
// Struct having static methods returning FRT with attribute | ||
// swift_attr("returns_retained") or SWIFT_RETURNS_RETAINED | ||
struct StructWithStaticMethodsReturningFRTWithAttributeReturnsRetained { | ||
static FRTStruct *_Nonnull StaticMethodReturningFRT() | ||
__attribute__((swift_attr("returns_retained"))); | ||
static FRTStruct *_Nonnull StaticMethodReturningFRT_copy() | ||
__attribute__((swift_attr("returns_retained"))); | ||
static FRTStruct *_Nonnull StaticMethodReturningFRT_create() | ||
__attribute__((swift_attr("returns_retained"))); | ||
static FRTStruct *_Nonnull StaticMethodReturningFRT_init() | ||
__attribute__((swift_attr("returns_retained"))); | ||
static FRTStruct *_Nonnull StaticMethodReturningFRT_clone() | ||
__attribute__((swift_attr("returns_retained"))); | ||
}; | ||
|
||
// Struct having static methods returning FRT with attribute | ||
// swift_attr("returns_unretained") or SWIFT_RETURNS_UNRETAINED | ||
struct StructWithStaticMethodsReturningFRTWithAttributeReturnsUnretained { | ||
static FRTStruct *_Nonnull StaticMethodReturningFRT() | ||
__attribute__((swift_attr("returns_unretained"))); | ||
static FRTStruct *_Nonnull StaticMethodReturningFRT_copy() | ||
__attribute__((swift_attr("returns_unretained"))); | ||
static FRTStruct *_Nonnull StaticMethodReturningFRT_create() | ||
__attribute__((swift_attr("returns_unretained"))); | ||
static FRTStruct *_Nonnull StaticMethodReturningFRT_init() | ||
__attribute__((swift_attr("returns_unretained"))); | ||
static FRTStruct *_Nonnull StaticMethodReturningFRT_clone() | ||
__attribute__((swift_attr("returns_unretained"))); | ||
}; | ||
|
||
// Global/free C++ functions returning FRT with both attributes | ||
// swift_attr("returns_unretained") and swift_attr("returns_retained") | ||
FRTStruct | ||
*_Nonnull global_function_returning_FRT_with_both_attrs_returns_retained_returns_unretained() | ||
__attribute__((swift_attr("returns_retained"))) | ||
__attribute__((swift_attr("returns_unretained"))); | ||
|
||
// Struct having static method returning FRT with both attributes | ||
// swift_attr("returns_unretained") and swift_attr("returns_retained") | ||
struct | ||
StructWithStaticMethodsReturningFRTWithBothAttributesReturnsRetainedAndReturnsUnretained { | ||
static FRTStruct *_Nonnull StaticMethodReturningFRT() | ||
__attribute__((swift_attr("returns_retained"))) | ||
__attribute__((swift_attr("returns_unretained"))); | ||
}; | ||
|
||
struct NonFRTStruct {}; | ||
|
||
// Global/free C++ functions returning non-FRT | ||
NonFRTStruct *_Nonnull global_function_returning_non_FRT(); | ||
NonFRTStruct | ||
*_Nonnull global_function_returning_non_FRT_with_attr_returns_retained() | ||
__attribute__((swift_attr("returns_retained"))); | ||
NonFRTStruct | ||
*_Nonnull global_function_returning_non_FRT_with_attr_returns_unretained() | ||
__attribute__((swift_attr("returns_unretained"))); | ||
NonFRTStruct *_Nonnull global_function_returning_non_FRT_create(); | ||
NonFRTStruct *_Nonnull global_function_returning_non_FRT_copy(); | ||
|
||
// Struct having static method returning non-FRT | ||
struct StructWithStaticMethodsReturningNonFRT { | ||
static NonFRTStruct *_Nonnull StaticMethodReturningNonFRT(); | ||
static NonFRTStruct | ||
*_Nonnull StaticMethodReturningNonFRTWithAttrReturnsRetained() | ||
__attribute__((swift_attr("returns_retained"))); | ||
static NonFRTStruct | ||
*_Nonnull StaticMethodReturningNonFRTWithAttrReturnsUnretained() | ||
__attribute__((swift_attr("returns_unretained"))); | ||
static NonFRTStruct *_Nonnull StaticMethodReturningNonFRT_create(); | ||
static NonFRTStruct *_Nonnull StaticMethodReturningNonFRT_copy(); | ||
}; | ||
|
||
fahadnayyar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Tests for templated functions | ||
template <typename T> | ||
FRTStruct *_Nonnull global_templated_function_returning_FRT(T a); | ||
|
||
template <typename T> | ||
FRTStruct *_Nonnull global_templated_function_returning_FRT_copy(T a); | ||
|
||
template <typename T> | ||
FRTStruct *_Nonnull global_templated_function_returning_FRT_create(T a); | ||
|
||
template <typename T> | ||
FRTStruct *_Nonnull global_templated_function_returning_FRT_init(T a); | ||
|
||
template <typename T> | ||
FRTStruct *_Nonnull global_templated_function_returning_FRT_clone(T a); | ||
|
||
template <typename T> | ||
FRTStruct | ||
*_Nonnull global_templated_function_returning_FRT_with_attr_returns_retained( | ||
T a) __attribute__((swift_attr("returns_retained"))); | ||
|
||
template <typename T> | ||
FRTStruct | ||
*_Nonnull global_templated_function_returning_FRT_copy_with_attr_returns_retained( | ||
T a) __attribute__((swift_attr("returns_retained"))); | ||
|
||
template <typename T> | ||
FRTStruct | ||
*_Nonnull global_templated_function_returning_FRT_create_with_attr_returns_retained( | ||
T a) __attribute__((swift_attr("returns_retained"))); | ||
|
||
template <typename T> | ||
FRTStruct | ||
*_Nonnull global_templated_function_returning_FRT_with_attr_returns_unretained( | ||
T a) __attribute__((swift_attr("returns_unretained"))); | ||
|
||
template <typename T> | ||
FRTStruct | ||
*_Nonnull global_templated_function_returning_FRT_copy_with_attr_returns_unretained( | ||
T a) __attribute__((swift_attr("returns_unretained"))); | ||
|
||
template <typename T> | ||
FRTStruct | ||
*_Nonnull global_templated_function_returning_FRT_create_with_attr_returns_unretained( | ||
T a) __attribute__((swift_attr("returns_unretained"))); |
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
10 changes: 10 additions & 0 deletions
10
test/Interop/Cxx/foreign-reference/frt-retained-unretained-attributes-error.swift
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// RUN: rm -rf %t | ||
// RUN: not %target-swift-frontend -typecheck -I %S/Inputs %s -cxx-interoperability-mode=upcoming-swift 2>&1 | %FileCheck %s | ||
|
||
import FunctionsAndMethodsReturningFRT | ||
|
||
let frtLocalVar1 = global_function_returning_FRT_with_both_attrs_returns_retained_returns_unretained() | ||
// CHECK: error: 'global_function_returning_FRT_with_both_attrs_returns_retained_returns_unretained' cannot be annotated with both swift_attr('returns_retained') and swift_attr('returns_unretained') attributes | ||
|
||
let frtLocalVar2 = StructWithStaticMethodsReturningFRTWithBothAttributesReturnsRetainedAndReturnsUnretained.StaticMethodReturningFRT() | ||
// CHECK: error: 'StaticMethodReturningFRT' cannot be annotated with both swift_attr('returns_retained') and swift_attr('returns_unretained') attributes |
Oops, something went wrong.
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.