Skip to content

[Parse] Disallow use of actor as a declaration modifier #37516

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
merged 1 commit into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 8 additions & 24 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3612,41 +3612,25 @@ bool Parser::parseDeclModifierList(DeclAttributes &Attributes,
// that scope, so we have to store enough state to emit the diagnostics
// outside of the scope.
bool isActorModifier = false;
bool isClassNext = false;
SourceLoc actorLoc = Tok.getLoc();
SourceLoc classLoc;

{
BacktrackingScope Scope(*this);

// Is this the class token before the identifier?
auto atClassDecl = [this]() -> bool {
return peekToken().is(tok::identifier) ||
Tok.is(tok::kw_class) ||
Tok.is(tok::kw_enum) ||
Tok.is(tok::kw_struct);
};
consumeToken(); // consume actor
isActorModifier = isStartOfSwiftDecl();
if (isActorModifier) {
isClassNext = atClassDecl();
while (!atClassDecl())
consumeToken();
classLoc = Tok.getLoc();
}
}

if (!isActorModifier)
break;

auto diag = diagnose(actorLoc,
diag::renamed_platform_condition_argument, "actor class", "actor");
if (isClassNext)
diag.fixItRemove(classLoc);
else
diag.fixItReplace(classLoc, "actor")
.fixItRemove(actorLoc);
Attributes.add(new (Context) ActorAttr({}, Tok.getLoc()));
consumeToken();
// Actor is a standalone keyword now, so it can't be used
// as a modifier. Let's diagnose and recover.
isError = true;

consumeToken(); // consume 'actor'

diagnose(actorLoc, diag::keyword_cant_be_identifier, Tok.getText());
continue;
}

Expand Down
3 changes: 1 addition & 2 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ bool IsActorRequest::evaluate(
if (!classDecl)
return false;

return classDecl->isExplicitActor() ||
classDecl->getAttrs().getAttribute<ActorAttr>();
return classDecl->isExplicitActor();
}

bool IsDefaultActorRequest::evaluate(
Expand Down
14 changes: 0 additions & 14 deletions test/SourceKit/CursorInfo/cursor_info_concurrency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ func test(act: MyActor) async throws {
try await act.asyncFunc {}
}

public actor class MyActorClass {
public func asyncFunc(fn: () async -> Void) async throws {}
}

func test(act: MyActorClass) async throws {
try await act.asyncFunc {}
}

// BEGIN App.swift
import MyModule

Expand All @@ -35,15 +27,9 @@ func test(act: MyActor) async throws {
// RUN: %sourcekitd-test -req=cursor -pos=5:16 %t/MyModule.swift -- %t/MyModule.swift -target %target-triple -Xfrontend -enable-experimental-concurrency | %FileCheck -check-prefix=ACTOR %s
// RUN: %sourcekitd-test -req=cursor -pos=6:19 %t/MyModule.swift -- %t/MyModule.swift -target %target-triple -Xfrontend -enable-experimental-concurrency | %FileCheck -check-prefix=FUNC %s

// RUN: %sourcekitd-test -req=cursor -pos=9:20 %t/MyModule.swift -- %t/MyModule.swift -target %target-triple -Xfrontend -enable-experimental-concurrency | %FileCheck -check-prefix=CLASSACTOR %s
// RUN: %sourcekitd-test -req=cursor -pos=13:16 %t/MyModule.swift -- %t/MyModule.swift -target %target-triple -Xfrontend -enable-experimental-concurrency | %FileCheck -check-prefix=CLASSACTOR %s

// ACTOR: <Declaration>public actor MyActor</Declaration>
// ACTOR: <decl.class><syntaxtype.keyword>public</syntaxtype.keyword> <syntaxtype.keyword>actor</syntaxtype.keyword> <decl.name>MyActor</decl.name></decl.class>

// CLASSACTOR: <Declaration>public actor class MyActorClass</Declaration>
// CLASSACTOR: <decl.class><syntaxtype.keyword>public</syntaxtype.keyword> <syntaxtype.keyword>actor</syntaxtype.keyword> <syntaxtype.keyword>class</syntaxtype.keyword> <decl.name>MyActorClass</decl.name></decl.class>

// FUNC: <Declaration>public func asyncFunc(fn: () async -&gt; <Type usr="s:s4Voida">Void</Type>) async throws</Declaration>
// FUNC: <decl.function.method.instance><syntaxtype.keyword>public</syntaxtype.keyword> <syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>asyncFunc</decl.name>(<decl.var.parameter><decl.var.parameter.argument_label>fn</decl.var.parameter.argument_label>: <decl.var.parameter.type>() <syntaxtype.keyword>async</syntaxtype.keyword> -&gt; <decl.function.returntype><ref.typealias usr="s:s4Voida">Void</ref.typealias></decl.function.returntype></decl.var.parameter.type></decl.var.parameter>) <syntaxtype.keyword>async</syntaxtype.keyword> <syntaxtype.keyword>throws</syntaxtype.keyword></decl.function.method.instance>

Expand Down
6 changes: 2 additions & 4 deletions test/attr/attr_objc_async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,11 @@ actor MyActor {
@objc nonisolated func synchronousGood() { }
}

// CHECK: actor class MyActor2
actor class MyActor2 { }
// expected-warning@-1{{'actor class' has been renamed to 'actor'}}{{7-13=}}
// expected-error@-1 {{keyword 'class' cannot be used as an identifier here}}

// CHECK: @objc actor MyObjCActor
@objc actor MyObjCActor: NSObject { }

// CHECK: @objc actor class MyObjCActor2
@objc actor class MyObjCActor2: NSObject {}
// expected-warning@-1{{'actor class' has been renamed to 'actor'}}{{13-19=}}
// expected-error@-1 {{keyword 'class' cannot be used as an identifier here}}
12 changes: 5 additions & 7 deletions test/decl/class/actor/basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@ class MyActorSubclass1: MyActor { } // expected-error{{actor types do not suppor

actor MyActorSubclass2: MyActor { } // expected-error{{actor types do not support inheritance}}

// expected-warning@+1{{'actor class' has been renamed to 'actor'}}{{7-13=}}
// expected-error@+1{{keyword 'class' cannot be used as an identifier here}}
actor class MyActorClass { }

class NonActor { }

actor NonActorSubclass : NonActor { } // expected-error{{actor types do not support inheritance}}

// expected-warning@+1{{'actor class' has been renamed to 'actor'}}{{14-20=}}
// expected-error@+1{{keyword 'class' cannot be used as an identifier here}}
public actor class BobHope {}
// expected-warning@+1{{'actor class' has been renamed to 'actor'}}{{14-19=actor}}{{1-7=}}
// expected-error@+1{{keyword 'public' cannot be used as an identifier here}}
actor public class BarbraStreisand {}
// expected-warning@+2{{'actor class' has been renamed to 'actor'}}{{14-21=}}
// expected-error@+1{{'actor' may only be used on 'class' declarations}}
// expected-error@+1{{keyword 'struct' cannot be used as an identifier here}}
public actor struct JulieAndrews {}
// expected-warning@+2{{'actor class' has been renamed to 'actor'}}{{14-18=actor}}{{1-7=}}
// expected-error@+1{{'actor' may only be used on 'class' declarations}}
// expected-error@+1{{keyword 'public' cannot be used as an identifier here}}
actor public enum TomHanks {}