Skip to content

Commit 6655f51

Browse files
committed
Sema: Reject invalid 'reasync' functions
1 parent ddc975b commit 6655f51

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3015,6 +3015,8 @@ ERROR(rethrows_without_throwing_parameter,none,
30153015
ERROR(override_reasync_with_non_reasync,none,
30163016
"override of 'reasync' %select{method|initializer}0 should also "
30173017
"be 'reasync'", (bool))
3018+
ERROR(reasync_without_async_parameter,none,
3019+
"'reasync' function must take an 'async' function argument", ())
30183020

30193021
ERROR(autoclosure_function_type,none,
30203022
"@autoclosure attribute only applies to function types",

lib/Sema/TypeCheckAttr.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
131131
IGNORED_ATTR(NoDerivative)
132132
IGNORED_ATTR(SpecializeExtension)
133133
IGNORED_ATTR(Concurrent)
134+
IGNORED_ATTR(AtRethrows)
135+
IGNORED_ATTR(AtReasync)
134136
#undef IGNORED_ATTR
135137

136138
void visitAlignmentAttr(AlignmentAttr *attr) {
@@ -225,7 +227,6 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
225227
void visitNSCopyingAttr(NSCopyingAttr *attr);
226228
void visitRequiredAttr(RequiredAttr *attr);
227229
void visitRethrowsAttr(RethrowsAttr *attr);
228-
void visitAtRethrowsAttr(AtRethrowsAttr *attr);
229230

230231
void checkApplicationMainAttribute(DeclAttribute *attr,
231232
Identifier Id_ApplicationDelegate,
@@ -280,7 +281,6 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
280281
void visitMarkerAttr(MarkerAttr *attr);
281282

282283
void visitReasyncAttr(ReasyncAttr *attr);
283-
void visitAtReasyncAttr(AtReasyncAttr *attr);
284284
};
285285
} // end anonymous namespace
286286

@@ -2054,8 +2054,8 @@ void AttributeChecker::visitRequiredAttr(RequiredAttr *attr) {
20542054
}
20552055

20562056
void AttributeChecker::visitRethrowsAttr(RethrowsAttr *attr) {
2057-
// 'rethrows' only applies to functions that take throwing functions
2058-
// as parameters.
2057+
// Make sure the function takes a 'throws' function argument or a
2058+
// conformance to a '@rethrows' protocol.
20592059
auto fn = dyn_cast<AbstractFunctionDecl>(D);
20602060
if (fn->getPolymorphicEffectKind(EffectKind::Throws)
20612061
!= PolymorphicEffectKind::Invalid) {
@@ -2066,8 +2066,6 @@ void AttributeChecker::visitRethrowsAttr(RethrowsAttr *attr) {
20662066
attr->setInvalid();
20672067
}
20682068

2069-
void AttributeChecker::visitAtRethrowsAttr(AtRethrowsAttr *attr) {}
2070-
20712069
/// Collect all used generic parameter types from a given type.
20722070
static void collectUsedGenericParameters(
20732071
Type Ty, SmallPtrSetImpl<TypeBase *> &ConstrainedGenericParams) {
@@ -5489,10 +5487,17 @@ void AttributeChecker::visitMarkerAttr(MarkerAttr *attr) {
54895487
}
54905488

54915489
void AttributeChecker::visitReasyncAttr(ReasyncAttr *attr) {
5492-
// FIXME
5493-
}
5490+
// Make sure the function takes a 'throws' function argument or a
5491+
// conformance to a '@rethrows' protocol.
5492+
auto fn = dyn_cast<AbstractFunctionDecl>(D);
5493+
if (fn->getPolymorphicEffectKind(EffectKind::Async)
5494+
!= PolymorphicEffectKind::Invalid) {
5495+
return;
5496+
}
54945497

5495-
void AttributeChecker::visitAtReasyncAttr(AtReasyncAttr *attr) {}
5498+
diagnose(attr->getLocation(), diag::reasync_without_async_parameter);
5499+
attr->setInvalid();
5500+
}
54965501

54975502
namespace {
54985503

test/Concurrency/reasync.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ func asyncReasyncFunction(_: () async throws -> ()) async reasync {}
1414
func reasyncParam(_: () reasync -> ()) {}
1515
// expected-error@-1 {{only function declarations may be marked 'reasync'; did you mean 'async'?}}{{25-32=async}}
1616

17+
//// Invalid cases
18+
19+
func noReasyncParams() reasync {}
20+
// expected-error@-1 {{'reasync' function must take an 'async' function argument}}
21+
22+
//// Method override attribute checking
23+
1724
class Base {
1825
func reasyncMethod(_: () async -> ()) reasync {}
1926
// expected-note@-1 {{overridden declaration is here}}
@@ -22,4 +29,4 @@ class Base {
2229
class Derived : Base {
2330
override func reasyncMethod(_: () async -> ()) async {}
2431
// expected-error@-1 {{override of 'reasync' method should also be 'reasync'}}
25-
}
32+
}

0 commit comments

Comments
 (0)