Skip to content

Commit 46677a5

Browse files
authored
Merge pull request swiftlang#83786 from Xazax-hun/diag-for-templated-classes
[cxx-interop] Make sure we emit lifetime warnings for templated classes
2 parents fcc1d00 + 5d8a033 commit 46677a5

File tree

1 file changed

+46
-20
lines changed

1 file changed

+46
-20
lines changed

test/Interop/Cxx/class/nonescapable-errors.swift

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ struct SWIFT_ESCAPABLE Owner {
2727
int data;
2828
};
2929

30+
template<typename T>
31+
struct SWIFT_ESCAPABLE TemplatedOwner {
32+
T data;
33+
};
34+
35+
using TemplatedIntOwner = TemplatedOwner<int>;
36+
using TemplatedFloatOwner = TemplatedOwner<float>;
37+
3038
Owner f(int* x [[clang::lifetimebound]]) {
3139
return Owner{0};
3240
}
@@ -35,6 +43,14 @@ Owner f2(int* x [[clang::lifetimebound]], int* y [[clang::lifetimebound]]) {
3543
return Owner{0};
3644
}
3745

46+
TemplatedIntOwner f3(int* x [[clang::lifetimebound]]) {
47+
return TemplatedOwner<int>{0};
48+
}
49+
50+
TemplatedFloatOwner f4(int* x [[clang::lifetimebound]]) {
51+
return TemplatedOwner<float>{0};
52+
}
53+
3854
View g(int* x) {
3955
return View(x);
4056
}
@@ -111,42 +127,52 @@ public func importInvalid(_ x: Invalid) {
111127
// CHECK: error: a function with a ~Escapable result needs a parameter to depend on
112128
// CHECK-NO-LIFETIMES: test.swift:11:32: error: a function cannot return a ~Escapable result
113129
public func noAnnotations() -> View {
114-
// CHECK: nonescapable.h:16:7: warning: the returned type 'Owner' is annotated as escapable; it cannot have lifetime dependencies [#ClangDeclarationImport]
115-
// CHECK-NO-LIFETIMES: nonescapable.h:16:7: warning: the returned type 'Owner' is annotated as escapable; it cannot have lifetime dependencies [#ClangDeclarationImport]
130+
// CHECK: nonescapable.h:24:7: warning: the returned type 'Owner' is annotated as escapable; it cannot have lifetime dependencies [#ClangDeclarationImport]
131+
// CHECK-NO-LIFETIMES: nonescapable.h:24:7: warning: the returned type 'Owner' is annotated as escapable; it cannot have lifetime dependencies [#ClangDeclarationImport]
116132
f(nil)
117-
// CHECK: nonescapable.h:20:7: warning: the returned type 'Owner' is annotated as escapable; it cannot have lifetime dependencies [#ClangDeclarationImport]
118-
// CHECK-NO-LIFETIMES: nonescapable.h:20:7: warning: the returned type 'Owner' is annotated as escapable; it cannot have lifetime dependencies [#ClangDeclarationImport]
133+
// CHECK: nonescapable.h:28:7: warning: the returned type 'Owner' is annotated as escapable; it cannot have lifetime dependencies [#ClangDeclarationImport]
134+
// CHECK-NO-LIFETIMES: nonescapable.h:28:7: warning: the returned type 'Owner' is annotated as escapable; it cannot have lifetime dependencies [#ClangDeclarationImport]
119135
// No duplicate warning for f2:
120-
// CHECK-NOT: nonescapable.h:20
136+
// CHECK-NOT: nonescapable.h:28
121137
f2(nil, nil)
122-
// CHECK: nonescapable.h:24:6: warning: the returned type 'View' is annotated as non-escapable; its lifetime dependencies must be annotated [#ClangDeclarationImport]
123-
// CHECK-NO-LIFETIMES: nonescapable.h:24:6: warning: the returned type 'View' is annotated as non-escapable; its lifetime dependencies must be annotated [#ClangDeclarationImport]
124-
// CHECK-NO-LIFETIMES: nonescapable.h:24:6: error: a function cannot return a ~Escapable result
138+
// CHECK: nonescapable.h:32:19: warning: the returned type 'TemplatedIntOwner' is annotated as escapable; it cannot have lifetime dependencies [#ClangDeclarationImport]
139+
// CHECK-NO-LIFETIMES: nonescapable.h:32:19: warning: the returned type 'TemplatedIntOwner' is annotated as escapable; it cannot have lifetime dependencies [#ClangDeclarationImport]
140+
// No duplicate warning for f3:
141+
// CHECK-NOT: nonescapable.h:32
142+
f3(nil)
143+
// CHECK: nonescapable.h:36:21: warning: the returned type 'TemplatedFloatOwner' is annotated as escapable; it cannot have lifetime dependencies [#ClangDeclarationImport]
144+
// CHECK-NO-LIFETIMES: nonescapable.h:36:21: warning: the returned type 'TemplatedFloatOwner' is annotated as escapable; it cannot have lifetime dependencies [#ClangDeclarationImport]
145+
// No duplicate warning for f4:
146+
// CHECK-NOT: nonescapable.h:34
147+
f4(nil)
148+
// CHECK: nonescapable.h:40:6: warning: the returned type 'View' is annotated as non-escapable; its lifetime dependencies must be annotated [#ClangDeclarationImport]
149+
// CHECK-NO-LIFETIMES: nonescapable.h:40:6: warning: the returned type 'View' is annotated as non-escapable; its lifetime dependencies must be annotated [#ClangDeclarationImport]
150+
// CHECK-NO-LIFETIMES: nonescapable.h:40:6: error: a function cannot return a ~Escapable result
125151
g(nil)
126152
h1(nil)
127-
// CHECK-NO-LIFETIMES: nonescapable.h:34:21: error: a function cannot return a ~Escapable result
153+
// CHECK-NO-LIFETIMES: nonescapable.h:50:21: error: a function cannot return a ~Escapable result
128154
h2(nil)
129-
// CHECK-NO-LIFETIMES: nonescapable.h:35:21: error: a function cannot return a ~Escapable result
155+
// CHECK-NO-LIFETIMES: nonescapable.h:51:21: error: a function cannot return a ~Escapable result
130156
h3(nil)
131157
i1()
132-
// CHECK: nonescapable.h:39:39: error: template parameter 'Missing' does not exist
133-
// CHECK-NO-LIFETIMES: nonescapable.h:39:39: error: template parameter 'Missing' does not exist
158+
// CHECK: nonescapable.h:55:39: error: template parameter 'Missing' does not exist
159+
// CHECK-NO-LIFETIMES: nonescapable.h:55:39: error: template parameter 'Missing' does not exist
134160
i2()
135-
// CHECK: nonescapable.h:45:33: error: template parameter 'S' expected to be a type parameter
136-
// CHECK-NO-LIFETIMES: nonescapable.h:45:33: error: template parameter 'S' expected to be a type parameter
161+
// CHECK: nonescapable.h:61:33: error: template parameter 'S' expected to be a type parameter
162+
// CHECK-NO-LIFETIMES: nonescapable.h:61:33: error: template parameter 'S' expected to be a type parameter
137163
j1()
138-
// CHECK-NO-LIFETIMES: nonescapable.h:63:41: error: a function cannot return a ~Escapable result
164+
// CHECK-NO-LIFETIMES: nonescapable.h:79:41: error: a function cannot return a ~Escapable result
139165
j2()
140-
// CHECK-NO-LIFETIMES: nonescapable.h:64:41: error: a function cannot return a ~Escapable result
166+
// CHECK-NO-LIFETIMES: nonescapable.h:80:41: error: a function cannot return a ~Escapable result
141167
j3()
142168
k1();
143-
// CHECK-NO-LIFETIMES: nonescapable.h:70:15: error: a function cannot return a ~Escapable result
169+
// CHECK-NO-LIFETIMES: nonescapable.h:86:15: error: a function cannot return a ~Escapable result
144170
k2();
145-
// CHECK-NO-LIFETIMES: nonescapable.h:71:22: error: a function cannot return a ~Escapable result
171+
// CHECK-NO-LIFETIMES: nonescapable.h:87:22: error: a function cannot return a ~Escapable result
146172
k3();
147173
l1();
148-
// CHECK: nonescapable.h:77:12: error: a function with a ~Escapable result needs a parameter to depend on
149-
// CHECK-NO-LIFETIMES: nonescapable.h:77:12: error: a function cannot return a ~Escapable result
174+
// CHECK: nonescapable.h:93:12: error: a function with a ~Escapable result needs a parameter to depend on
175+
// CHECK-NO-LIFETIMES: nonescapable.h:93:12: error: a function cannot return a ~Escapable result
150176
l2();
151177
return View()
152178
}

0 commit comments

Comments
 (0)