Skip to content

Commit db8402f

Browse files
authored
Merge pull request #34301 from DougGregor/concurrency-actor-constraint-inference
[Concurrency] Propagation and consistency checking for actor constraints.
2 parents 6a19d37 + 3a651a6 commit db8402f

File tree

12 files changed

+526
-52
lines changed

12 files changed

+526
-52
lines changed

include/swift/AST/ActorIsolation.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#ifndef SWIFT_AST_ACTORISOLATIONSTATE_H
1717
#define SWIFT_AST_ACTORISOLATIONSTATE_H
1818

19+
#include "swift/AST/Type.h"
1920
#include "llvm/ADT/Hashing.h"
2021

2122
namespace llvm {
@@ -24,6 +25,7 @@ class raw_ostream;
2425

2526
namespace swift {
2627
class ClassDecl;
28+
class SubstitutionMap;
2729
class Type;
2830

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

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

89+
bool isUnspecified() const { return kind == Unspecified; }
90+
8791
ClassDecl *getActor() const {
8892
assert(getKind() == ActorInstance);
8993
return actor;
@@ -94,6 +98,13 @@ class ActorIsolation {
9498
return globalActor;
9599
}
96100

101+
/// Determine whether this isolation will require substitution to be
102+
/// evaluated.
103+
bool requiresSubstitution() const;
104+
105+
/// Substitute into types within the actor isolation.
106+
ActorIsolation subst(SubstitutionMap subs) const;
107+
97108
friend bool operator==(const ActorIsolation &lhs,
98109
const ActorIsolation &rhs) {
99110
if (lhs.kind != rhs.kind)

include/swift/AST/DiagnosticEngine.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef SWIFT_BASIC_DIAGNOSTICENGINE_H
1919
#define SWIFT_BASIC_DIAGNOSTICENGINE_H
2020

21+
#include "swift/AST/ActorIsolation.h"
2122
#include "swift/AST/DeclNameLoc.h"
2223
#include "swift/AST/DiagnosticConsumer.h"
2324
#include "swift/AST/TypeLoc.h"
@@ -93,6 +94,7 @@ namespace swift {
9394
DeclAttribute,
9495
VersionTuple,
9596
LayoutConstraint,
97+
ActorIsolation,
9698
};
9799

98100
namespace diag {
@@ -122,6 +124,7 @@ namespace swift {
122124
const DeclAttribute *DeclAttributeVal;
123125
llvm::VersionTuple VersionVal;
124126
LayoutConstraint LayoutConstraintVal;
127+
ActorIsolation ActorIsolationVal;
125128
};
126129

127130
public:
@@ -209,6 +212,12 @@ namespace swift {
209212
DiagnosticArgument(LayoutConstraint L)
210213
: Kind(DiagnosticArgumentKind::LayoutConstraint), LayoutConstraintVal(L) {
211214
}
215+
216+
DiagnosticArgument(ActorIsolation AI)
217+
: Kind(DiagnosticArgumentKind::ActorIsolation),
218+
ActorIsolationVal(AI) {
219+
}
220+
212221
/// Initializes a diagnostic argument using the underlying type of the
213222
/// given enum.
214223
template<
@@ -299,6 +308,11 @@ namespace swift {
299308
assert(Kind == DiagnosticArgumentKind::LayoutConstraint);
300309
return LayoutConstraintVal;
301310
}
311+
312+
ActorIsolation getAsActorIsolation() const {
313+
assert(Kind == DiagnosticArgumentKind::ActorIsolation);
314+
return ActorIsolationVal;
315+
}
302316
};
303317

304318
struct DiagnosticFormatOptions {

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4268,6 +4268,10 @@ ERROR(actor_isolation_multiple_attr,none,
42684268
"%0 %1 has multiple actor-isolation attributes ('%2' and '%3')",
42694269
(DescriptiveDeclKind, DeclName, StringRef, StringRef))
42704270

4271+
ERROR(actor_isolation_override_mismatch,none,
4272+
"%0 %1 %2 has different actor isolation from %3 overridden declaration",
4273+
(ActorIsolation, DescriptiveDeclKind, DeclName, ActorIsolation))
4274+
42714275
//------------------------------------------------------------------------------
42724276
// MARK: Type Check Types
42734277
//------------------------------------------------------------------------------

lib/AST/DiagnosticEngine.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,26 @@ static void formatDiagnosticArgument(StringRef Modifier,
660660
Out << FormatOpts.OpeningQuotationMark << Arg.getAsLayoutConstraint()
661661
<< FormatOpts.ClosingQuotationMark;
662662
break;
663+
case DiagnosticArgumentKind::ActorIsolation:
664+
switch (auto isolation = Arg.getAsActorIsolation()) {
665+
case ActorIsolation::ActorInstance:
666+
Out << "actor-isolated";
667+
break;
668+
669+
case ActorIsolation::GlobalActor:
670+
Out << "global actor " << FormatOpts.OpeningQuotationMark
671+
<< isolation.getGlobalActor().getString()
672+
<< FormatOpts.ClosingQuotationMark << "-isolated";
673+
break;
674+
675+
case ActorIsolation::Independent:
676+
Out << "actor-independent";
677+
break;
678+
679+
case ActorIsolation::Unspecified:
680+
Out << "non-actor-isolated";
681+
break;
682+
}
663683
}
664684
}
665685

lib/AST/TypeCheckRequests.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,29 @@ void CustomAttrTypeRequest::cacheResult(Type value) const {
14871487
attr->setType(value);
14881488
}
14891489

1490+
bool ActorIsolation::requiresSubstitution() const {
1491+
switch (kind) {
1492+
case ActorInstance:
1493+
case Independent:
1494+
case Unspecified:
1495+
return false;
1496+
1497+
case GlobalActor:
1498+
return getGlobalActor()->hasTypeParameter();
1499+
}
1500+
}
1501+
1502+
ActorIsolation ActorIsolation::subst(SubstitutionMap subs) const {
1503+
switch (kind) {
1504+
case ActorInstance:
1505+
case Independent:
1506+
case Unspecified:
1507+
return *this;
1508+
1509+
case GlobalActor:
1510+
return forGlobalActor(getGlobalActor().subst(subs));
1511+
}
1512+
}
14901513

14911514
void swift::simple_display(
14921515
llvm::raw_ostream &out, const ActorIsolation &state) {

0 commit comments

Comments
 (0)