Skip to content

Commit be3e118

Browse files
committed
[c-interop] Add test for SWIFT_RETURNS_RETAINED annotations in C interop mode
rdar://144976203
1 parent b4cfaaa commit be3e118

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

test/Interop/C/struct/Inputs/module.modulemap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ module StructAsOptionSet {
1818
module NoncopyableStructs {
1919
header "noncopyable-struct.h"
2020
}
21+
22+
module ReturnForeignReference {
23+
header "return-foreign-reference.h"
24+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef __RETURN_FOREIGN_REFERENCE_H
2+
#define __RETURN_FOREIGN_REFERENCE_H
3+
4+
struct
5+
__attribute__((swift_attr("import_reference")))
6+
__attribute__((swift_attr("retain:FRTRetain")))
7+
__attribute__((swift_attr("release:FRTRelease")))
8+
FRT {
9+
int value;
10+
};
11+
12+
struct Value { int value; };
13+
14+
void FRTRetain(struct FRT *x);
15+
void FRTRelease(struct FRT *x);
16+
17+
struct FRT *getFRTNoAnnotations(void); // expected-note {{annotate}}
18+
struct FRT *getFRTRetained() __attribute__((swift_attr("returns_retained")));
19+
struct FRT *getFRTUnretained() __attribute__((swift_attr("returns_unretained")));
20+
21+
struct FRT *getFRTUnretained() __attribute__((swift_attr("returns_unretained")));
22+
23+
struct FRT *getFRTConflictingAnnotations()
24+
__attribute__((swift_attr("returns_retained")))
25+
__attribute__((swift_attr("returns_unretained")));
26+
// expected-error@-3 {{cannot be annotated with both}}
27+
28+
struct Value *getValueRetained() __attribute__((swift_attr("returns_retained")));
29+
// expected-warning@-1 {{should not be annotated}}
30+
struct Value *getValueUnretained() __attribute__((swift_attr("returns_unretained")));
31+
// expected-warning@-1 {{should not be annotated}}
32+
33+
#endif /* __RETURN_FOREIGN_REFERENCE_H */
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %target-typecheck-verify-swift -cxx-interoperability-mode=off \
2+
// RUN: -disable-availability-checking \
3+
// RUN: -I %S%{fs-sep}Inputs \
4+
// RUN: -verify-additional-file %S%{fs-sep}Inputs%{fs-sep}return-foreign-reference.h
5+
6+
import ReturnForeignReference
7+
8+
let _: FRT = getFRTNoAnnotations() // expected-warning {{cannot infer ownership}}
9+
let _: FRT = getFRTRetained()
10+
let _: FRT = getFRTUnretained()
11+
12+
13+
// These calls to incorrectly annotated functions are necessary to force
14+
// diagnostics to appear in the header file.
15+
let _: FRT = getFRTConflictingAnnotations()
16+
let _ = getValueRetained()
17+
let _ = getValueUnretained()

0 commit comments

Comments
 (0)