Skip to content

Commit 56a82ab

Browse files
authored
Merge pull request swiftlang#38298 from DougGregor/actor-no-class
2 parents 7c63b5a + 102c001 commit 56a82ab

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed

lib/AST/Decl.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,10 +1595,12 @@ SourceRange PatternBindingDecl::getSourceRange() const {
15951595
}
15961596

15971597
static StaticSpellingKind getCorrectStaticSpellingForDecl(const Decl *D) {
1598-
if (!D->getDeclContext()->getSelfClassDecl())
1599-
return StaticSpellingKind::KeywordStatic;
1598+
if (auto classDecl = D->getDeclContext()->getSelfClassDecl()) {
1599+
if (!classDecl->isActor())
1600+
return StaticSpellingKind::KeywordClass;
1601+
}
16001602

1601-
return StaticSpellingKind::KeywordClass;
1603+
return StaticSpellingKind::KeywordStatic;
16021604
}
16031605

16041606
StaticSpellingKind PatternBindingDecl::getCorrectStaticSpelling() const {

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,6 +2107,14 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
21072107
}
21082108
}
21092109

2110+
// Reject "class" methods on actors.
2111+
if (SD->getStaticSpelling() == StaticSpellingKind::KeywordClass &&
2112+
SD->getDeclContext()->getSelfClassDecl() &&
2113+
SD->getDeclContext()->getSelfClassDecl()->isActor()) {
2114+
SD->diagnose(diag::class_subscript_not_in_class, false)
2115+
.fixItReplace(SD->getStaticLoc(), "static");
2116+
}
2117+
21102118
// Now check all the accessors.
21112119
SD->visitEmittedAccessors([&](AccessorDecl *accessor) {
21122120
visit(accessor);
@@ -2753,6 +2761,14 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
27532761
}
27542762
}
27552763

2764+
// Reject "class" methods on actors.
2765+
if (StaticSpelling == StaticSpellingKind::KeywordClass &&
2766+
FD->getDeclContext()->getSelfClassDecl() &&
2767+
FD->getDeclContext()->getSelfClassDecl()->isActor()) {
2768+
FD->diagnose(diag::class_func_not_in_class, false)
2769+
.fixItReplace(FD->getStaticLoc(), "static");
2770+
}
2771+
27562772
// Member functions need some special validation logic.
27572773
if (FD->getDeclContext()->isTypeContext()) {
27582774
if (FD->isOperator() && !isMemberOperator(FD, nullptr)) {

lib/Sema/TypeCheckStorage.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@ PatternBindingEntryRequest::evaluate(Evaluator &eval,
233233
}
234234
}
235235

236+
// Reject "class" methods on actors.
237+
if (StaticSpelling == StaticSpellingKind::KeywordClass &&
238+
binding->getDeclContext()->getSelfClassDecl() &&
239+
binding->getDeclContext()->getSelfClassDecl()->isActor()) {
240+
binding->diagnose(diag::class_var_not_in_class, false)
241+
.fixItReplace(binding->getStaticLoc(), "static");
242+
}
243+
236244
// Check the pattern.
237245
auto contextualPattern =
238246
ContextualPattern::forPatternBindingDecl(binding, entryNumber);

test/Concurrency/actor_isolation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ actor MyActor: MySuperActor { // expected-error{{actor types do not support inhe
7171
return self.name // expected-error{{property 'name' isolated to global actor 'MainActor' can not be referenced from actor 'MyActor' in a synchronous context}}
7272
}
7373

74-
class func synchronousClass() { }
74+
static func synchronousClass() { }
7575
static func synchronousStatic() { }
7676

7777
func synchronous() -> String { text.first ?? "nothing" } // expected-note 9{{calls to instance method 'synchronous()' from outside of its actor context are implicitly asynchronous}}

test/decl/class/actor/basic.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,24 @@ actor public class BarbraStreisand {}
2727
public actor struct JulieAndrews {}
2828
// expected-error@+1{{keyword 'public' cannot be used as an identifier here}}
2929
actor public enum TomHanks {}
30+
31+
open actor A1 { } // expected-error{{only classes and overridable class members can be declared 'open'; use 'public'}}
32+
33+
actor A2 {
34+
required init() { } // expected-error{{'required' initializer in non-class type 'A2'}}
35+
open func f() { } // expected-error{{only classes and overridable class members can be declared 'open'; use 'public'}}
36+
37+
final func g() { } // okay for now
38+
class func h() { } // expected-error{{class methods are only allowed within classes; use 'static' to declare a static method}}
39+
static func i() { } // okay
40+
41+
class var someProp: Int { 0 } // expected-error{{class properties are only allowed within classes; use 'static' to declare a static property}}
42+
}
43+
44+
extension A2 {
45+
class func h2() { } // expected-error{{class methods are only allowed within classes; use 'static' to declare a static method}}
46+
static func i2() { } // okay
47+
48+
class subscript(i: Int) -> Int { i } // expected-error{{class subscripts are only allowed within classes; use 'static' to declare a static subscript}}
49+
static subscript(s: String) -> String { s }
50+
}

0 commit comments

Comments
 (0)