Skip to content
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

[Concurrency] Propagation and consistency checking for actor constraints. #34301

Merged
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
11 changes: 11 additions & 0 deletions include/swift/AST/ActorIsolation.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#ifndef SWIFT_AST_ACTORISOLATIONSTATE_H
#define SWIFT_AST_ACTORISOLATIONSTATE_H

#include "swift/AST/Type.h"
#include "llvm/ADT/Hashing.h"

namespace llvm {
Expand All @@ -24,6 +25,7 @@ class raw_ostream;

namespace swift {
class ClassDecl;
class SubstitutionMap;
class Type;

/// Determine whether the given types are (canonically) equal, declared here
Expand Down Expand Up @@ -84,6 +86,8 @@ class ActorIsolation {

operator Kind() const { return getKind(); }

bool isUnspecified() const { return kind == Unspecified; }

ClassDecl *getActor() const {
assert(getKind() == ActorInstance);
return actor;
Expand All @@ -94,6 +98,13 @@ class ActorIsolation {
return globalActor;
}

/// Determine whether this isolation will require substitution to be
/// evaluated.
bool requiresSubstitution() const;

/// Substitute into types within the actor isolation.
ActorIsolation subst(SubstitutionMap subs) const;

friend bool operator==(const ActorIsolation &lhs,
const ActorIsolation &rhs) {
if (lhs.kind != rhs.kind)
Expand Down
14 changes: 14 additions & 0 deletions include/swift/AST/DiagnosticEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#ifndef SWIFT_BASIC_DIAGNOSTICENGINE_H
#define SWIFT_BASIC_DIAGNOSTICENGINE_H

#include "swift/AST/ActorIsolation.h"
#include "swift/AST/DeclNameLoc.h"
#include "swift/AST/DiagnosticConsumer.h"
#include "swift/AST/TypeLoc.h"
Expand Down Expand Up @@ -93,6 +94,7 @@ namespace swift {
DeclAttribute,
VersionTuple,
LayoutConstraint,
ActorIsolation,
};

namespace diag {
Expand Down Expand Up @@ -122,6 +124,7 @@ namespace swift {
const DeclAttribute *DeclAttributeVal;
llvm::VersionTuple VersionVal;
LayoutConstraint LayoutConstraintVal;
ActorIsolation ActorIsolationVal;
};

public:
Expand Down Expand Up @@ -209,6 +212,12 @@ namespace swift {
DiagnosticArgument(LayoutConstraint L)
: Kind(DiagnosticArgumentKind::LayoutConstraint), LayoutConstraintVal(L) {
}

DiagnosticArgument(ActorIsolation AI)
: Kind(DiagnosticArgumentKind::ActorIsolation),
ActorIsolationVal(AI) {
}

/// Initializes a diagnostic argument using the underlying type of the
/// given enum.
template<
Expand Down Expand Up @@ -299,6 +308,11 @@ namespace swift {
assert(Kind == DiagnosticArgumentKind::LayoutConstraint);
return LayoutConstraintVal;
}

ActorIsolation getAsActorIsolation() const {
assert(Kind == DiagnosticArgumentKind::ActorIsolation);
return ActorIsolationVal;
}
};

struct DiagnosticFormatOptions {
Expand Down
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4262,6 +4262,10 @@ ERROR(actor_isolation_multiple_attr,none,
"%0 %1 has multiple actor-isolation attributes ('%2' and '%3')",
(DescriptiveDeclKind, DeclName, StringRef, StringRef))

ERROR(actor_isolation_override_mismatch,none,
"%0 %1 %2 has different actor isolation from %3 overridden declaration",
(ActorIsolation, DescriptiveDeclKind, DeclName, ActorIsolation))

//------------------------------------------------------------------------------
// MARK: Type Check Types
//------------------------------------------------------------------------------
Expand Down
20 changes: 20 additions & 0 deletions lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,26 @@ static void formatDiagnosticArgument(StringRef Modifier,
Out << FormatOpts.OpeningQuotationMark << Arg.getAsLayoutConstraint()
<< FormatOpts.ClosingQuotationMark;
break;
case DiagnosticArgumentKind::ActorIsolation:
switch (auto isolation = Arg.getAsActorIsolation()) {
case ActorIsolation::ActorInstance:
Out << "actor-isolated";
break;

case ActorIsolation::GlobalActor:
Out << "global actor " << FormatOpts.OpeningQuotationMark
<< isolation.getGlobalActor().getString()
<< FormatOpts.ClosingQuotationMark << "-isolated";
break;

case ActorIsolation::Independent:
Out << "actor-independent";
break;

case ActorIsolation::Unspecified:
Out << "non-actor-isolated";
break;
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions lib/AST/TypeCheckRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,29 @@ void CustomAttrTypeRequest::cacheResult(Type value) const {
attr->setType(value);
}

bool ActorIsolation::requiresSubstitution() const {
switch (kind) {
case ActorInstance:
case Independent:
case Unspecified:
return false;

case GlobalActor:
return getGlobalActor()->hasTypeParameter();
}
}

ActorIsolation ActorIsolation::subst(SubstitutionMap subs) const {
switch (kind) {
case ActorInstance:
case Independent:
case Unspecified:
return *this;

case GlobalActor:
return forGlobalActor(getGlobalActor().subst(subs));
}
}

void swift::simple_display(
llvm::raw_ostream &out, const ActorIsolation &state) {
Expand Down
Loading