Skip to content

Commit 6f0a368

Browse files
committed
[AST] NFC: Add a convenient way to create implicit NonisolatedAttrs
1 parent 588b2ec commit 6f0a368

11 files changed

+29
-39
lines changed

include/swift/AST/Attr.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2978,14 +2978,9 @@ class NonisolatedAttr final : public DeclAttribute {
29782978
NonIsolatedModifier modifier, bool implicit)
29792979
: DeclAttribute(DeclAttrKind::Nonisolated, atLoc, range, implicit) {
29802980
Bits.NonisolatedAttr.Modifier = static_cast<unsigned>(modifier);
2981+
assert((getModifier() == modifier) && "not enough bits for modifier");
29812982
}
29822983

2983-
NonisolatedAttr(bool unsafe, bool implicit)
2984-
: NonisolatedAttr({}, {},
2985-
unsafe ? NonIsolatedModifier::Unsafe
2986-
: NonIsolatedModifier::None,
2987-
implicit) {}
2988-
29892984
NonIsolatedModifier getModifier() const {
29902985
return static_cast<NonIsolatedModifier>(Bits.NonisolatedAttr.Modifier);
29912986
}
@@ -2995,6 +2990,13 @@ class NonisolatedAttr final : public DeclAttribute {
29952990
return getModifier() == NonIsolatedModifier::NonSending;
29962991
}
29972992

2993+
static NonisolatedAttr *
2994+
createImplicit(ASTContext &ctx,
2995+
NonIsolatedModifier modifier = NonIsolatedModifier::None) {
2996+
return new (ctx) NonisolatedAttr(/*atLoc*/ {}, /*range*/ {}, modifier,
2997+
/*implicit=*/true);
2998+
}
2999+
29983000
static bool classof(const DeclAttribute *DA) {
29993001
return DA->getKind() == DeclAttrKind::Nonisolated;
30003002
}

lib/ClangImporter/ImportDecl.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8810,8 +8810,7 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
88108810
// Hard-code @actorIndependent, until Objective-C clients start
88118811
// using nonisolated.
88128812
if (swiftAttr->getAttribute() == "@actorIndependent") {
8813-
auto attr = new (SwiftContext)
8814-
NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true);
8813+
auto attr = NonisolatedAttr::createImplicit(SwiftContext);
88158814
MappedDecl->getAttrs().add(attr);
88168815
continue;
88178816
}
@@ -8944,8 +8943,8 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
89448943
auto *mappedVar = cast<VarDecl>(MappedDecl);
89458944
if (mappedVar->isStatic() && mappedVar->isLet() &&
89468945
isNSNotificationName(cast<clang::ValueDecl>(ClangDecl)->getType())) {
8947-
MappedDecl->getAttrs().add(new (SwiftContext) NonisolatedAttr(
8948-
/*unsafe=*/false, /*implicit=*/true));
8946+
MappedDecl->getAttrs().add(
8947+
NonisolatedAttr::createImplicit(SwiftContext));
89498948
}
89508949
}
89518950
}

lib/ClangImporter/SwiftDeclSynthesizer.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,7 @@ ValueDecl *SwiftDeclSynthesizer::createConstant(Identifier name,
436436

437437
// Mark the function transparent so that we inline it away completely.
438438
func->getAttrs().add(new (C) TransparentAttr(/*implicit*/ true));
439-
auto nonisolatedAttr =
440-
new (C) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true);
441-
var->getAttrs().add(nonisolatedAttr);
439+
var->getAttrs().add(NonisolatedAttr::createImplicit(C));
442440

443441
// Set the function up as the getter.
444442
ImporterImpl.makeComputed(var, func, nullptr);

lib/Sema/CSGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4585,8 +4585,8 @@ generateForEachStmtConstraints(ConstraintSystem &cs, DeclContext *dc,
45854585
// non-`Sendable` state across the isolation boundary. `next()` should
45864586
// inherit the isolation of the caller, but for now, use the opt out.
45874587
if (isAsync) {
4588-
auto *nonisolated = new (ctx)
4589-
NonisolatedAttr(/*unsafe=*/true, /*implicit=*/true);
4588+
auto *nonisolated =
4589+
NonisolatedAttr::createImplicit(ctx, NonIsolatedModifier::Unsafe);
45904590
makeIteratorVar->getAttrs().add(nonisolated);
45914591
}
45924592

lib/Sema/CodeSynthesis.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,8 +1738,7 @@ bool swift::addNonIsolatedToSynthesized(NominalTypeDecl *nominal,
17381738
return false;
17391739

17401740
ASTContext &ctx = nominal->getASTContext();
1741-
value->getAttrs().add(
1742-
new (ctx) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true));
1741+
value->getAttrs().add(NonisolatedAttr::createImplicit(ctx));
17431742
return true;
17441743
}
17451744

lib/Sema/CodeSynthesisDistributedActor.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ static VarDecl *addImplicitDistributedActorIDProperty(
110110
nominal);
111111

112112
// mark as nonisolated, allowing access to it from everywhere
113-
propDecl->getAttrs().add(
114-
new (C) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true));
113+
propDecl->getAttrs().add(NonisolatedAttr::createImplicit(C));
115114
// mark as @_compilerInitialized, since we synthesize the initializing
116115
// assignment during SILGen.
117116
propDecl->getAttrs().add(
@@ -161,8 +160,7 @@ static VarDecl *addImplicitDistributedActorActorSystemProperty(
161160
nominal);
162161

163162
// mark as nonisolated, allowing access to it from everywhere
164-
propDecl->getAttrs().add(
165-
new (C) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true));
163+
propDecl->getAttrs().add(NonisolatedAttr::createImplicit(C));
166164

167165
auto idProperty = nominal->getDistributedActorIDProperty();
168166
// If the id was not yet synthesized, we need to ensure that eventually
@@ -739,8 +737,7 @@ static FuncDecl *createSameSignatureDistributedThunkDecl(DeclContext *DC,
739737

740738
thunk->setSynthesized(true);
741739
thunk->setDistributedThunk(true);
742-
thunk->getAttrs().add(
743-
new (C) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true));
740+
thunk->getAttrs().add(NonisolatedAttr::createImplicit(C));
744741

745742
return thunk;
746743
}

lib/Sema/DerivedConformance/DerivedConformanceActor.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ static ValueDecl *deriveActor_unownedExecutor(DerivedConformance &derived) {
147147
property->getAttrs().add(new (ctx) SemanticsAttr(SEMANTICS_DEFAULT_ACTOR,
148148
SourceLoc(), SourceRange(),
149149
/*implicit*/ true));
150-
property->getAttrs().add(
151-
new (ctx) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true));
150+
property->getAttrs().add(NonisolatedAttr::createImplicit(ctx));
152151

153152
// Make the property implicitly final.
154153
property->getAttrs().add(new (ctx) FinalAttr(/*IsImplicit=*/true));

lib/Sema/DerivedConformance/DerivedConformanceCaseIterable.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ ValueDecl *DerivedConformance::deriveCaseIterable(ValueDecl *requirement) {
105105
SynthesizedIntroducer::Var, Context.Id_allCases, returnTy,
106106
/*isStatic=*/true, /*isFinal=*/true);
107107

108-
propDecl->getAttrs().add(
109-
new (C) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true));
108+
propDecl->getAttrs().add(NonisolatedAttr::createImplicit(C));
110109

111110
// Define the getter.
112111
auto *getterDecl = addGetterToReadOnlyDerivedProperty(propDecl);

lib/Sema/DerivedConformance/DerivedConformanceDistributedActor.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,7 @@ static ValueDecl *deriveDistributedActor_id(DerivedConformance &derived) {
465465
/*isStatic=*/false, /*isFinal=*/true);
466466

467467
// mark as nonisolated, allowing access to it from everywhere
468-
propDecl->getAttrs().add(
469-
new (C) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true));
468+
propDecl->getAttrs().add(NonisolatedAttr::createImplicit(C));
470469

471470
derived.addMemberToConformanceContext(pbDecl, /*insertAtHead=*/true);
472471
derived.addMemberToConformanceContext(propDecl, /*insertAtHead=*/true);
@@ -496,8 +495,7 @@ static ValueDecl *deriveDistributedActor_actorSystem(
496495
propertyType, /*isStatic=*/false, /*isFinal=*/true);
497496

498497
// mark as nonisolated, allowing access to it from everywhere
499-
propDecl->getAttrs().add(
500-
new (C) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true));
498+
propDecl->getAttrs().add(NonisolatedAttr::createImplicit(C));
501499

502500
// IMPORTANT: `id` MUST be the first field of a distributed actor, and
503501
// `actorSystem` MUST be the second field, because for a remote instance
@@ -795,8 +793,7 @@ static ValueDecl *deriveDistributedActor_unownedExecutor(DerivedConformance &der
795793
property->getAttrs().add(new (ctx) SemanticsAttr(SEMANTICS_DEFAULT_ACTOR,
796794
SourceLoc(), SourceRange(),
797795
/*implicit*/ true));
798-
property->getAttrs().add(
799-
new (ctx) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true));
796+
property->getAttrs().add(NonisolatedAttr::createImplicit(ctx));
800797

801798
// Make the property implicitly final.
802799
property->getAttrs().add(new (ctx) FinalAttr(/*IsImplicit=*/true));

lib/Sema/DerivedConformance/DerivedConformanceEquatableHashable.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,7 @@ deriveHashable_hashInto(
555555
// The derived hash(into:) for an actor must be non-isolated.
556556
if (!addNonIsolatedToSynthesized(derived, hashDecl) &&
557557
derived.Nominal->isActor())
558-
hashDecl->getAttrs().add(
559-
new (C) NonisolatedAttr(/*unsafe*/ false, /*implicit*/ true));
558+
hashDecl->getAttrs().add(NonisolatedAttr::createImplicit(C));
560559

561560
derived.addMembersToConformanceContext({hashDecl});
562561

@@ -912,8 +911,7 @@ static ValueDecl *deriveHashable_hashValue(DerivedConformance &derived) {
912911
// The derived hashValue of an actor must be nonisolated.
913912
if (!addNonIsolatedToSynthesized(derived, hashValueDecl) &&
914913
derived.Nominal->isActor())
915-
hashValueDecl->getAttrs().add(
916-
new (C) NonisolatedAttr(/*unsafe*/ false, /*implicit*/ true));
914+
hashValueDecl->getAttrs().add(NonisolatedAttr::createImplicit(C));
917915

918916
Pattern *hashValuePat =
919917
NamedPattern::createImplicit(C, hashValueDecl, intType);

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5691,8 +5691,10 @@ static void addAttributesForActorIsolation(ValueDecl *value,
56915691
break;
56925692
case ActorIsolation::Nonisolated:
56935693
case ActorIsolation::NonisolatedUnsafe: {
5694-
value->getAttrs().add(new (ctx) NonisolatedAttr(
5695-
isolation == ActorIsolation::NonisolatedUnsafe, /*implicit=*/true));
5694+
value->getAttrs().add(NonisolatedAttr::createImplicit(
5695+
ctx, isolation == ActorIsolation::NonisolatedUnsafe
5696+
? NonIsolatedModifier::Unsafe
5697+
: NonIsolatedModifier::None));
56965698
break;
56975699
}
56985700
case ActorIsolation::GlobalActor: {

0 commit comments

Comments
 (0)