Skip to content

Commit

Permalink
Merge pull request #34301 from DougGregor/concurrency-actor-constrain…
Browse files Browse the repository at this point in the history
…t-inference

[Concurrency] Propagation and consistency checking for actor constraints.
  • Loading branch information
DougGregor committed Oct 14, 2020
2 parents 6a19d37 + 3a651a6 commit db8402f
Show file tree
Hide file tree
Showing 12 changed files with 526 additions and 52 deletions.
11 changes: 11 additions & 0 deletions include/swift/AST/ActorIsolation.h
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
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
Expand Up @@ -4268,6 +4268,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
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
Expand Up @@ -1487,6 +1487,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

0 comments on commit db8402f

Please sign in to comment.