Skip to content

nonisolated(unsafe) to opt out of strict concurrency static checking for global variables #69280

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
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
15 changes: 11 additions & 4 deletions include/swift/AST/ActorIsolation.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class ActorIsolation {
/// meaning that it can be used from any actor but is also unable to
/// refer to the isolated state of any given actor.
Nonisolated,
/// The declaration is explicitly specified to be not isolated and with the
/// "unsafe" annotation, which means that we do not enforce isolation.
NonisolatedUnsafe,
/// The declaration is isolated to a global actor. It can refer to other
/// entities with the same global actor.
GlobalActor,
Expand Down Expand Up @@ -97,8 +100,8 @@ class ActorIsolation {
return ActorIsolation(Unspecified, nullptr);
}

static ActorIsolation forNonisolated() {
return ActorIsolation(Nonisolated, nullptr);
static ActorIsolation forNonisolated(bool unsafe) {
return ActorIsolation(unsafe ? NonisolatedUnsafe : Nonisolated, nullptr);
}

static ActorIsolation forActorInstanceSelf(NominalTypeDecl *actor) {
Expand All @@ -124,8 +127,10 @@ class ActorIsolation {
operator Kind() const { return getKind(); }

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

bool isNonisolated() const { return kind == Nonisolated; }

bool isNonisolated() const {
return (kind == Nonisolated) || (kind == NonisolatedUnsafe);
}

/// Retrieve the parameter to which actor-instance isolation applies.
///
Expand All @@ -144,6 +149,7 @@ class ActorIsolation {

case Unspecified:
case Nonisolated:
case NonisolatedUnsafe:
return false;
}
}
Expand Down Expand Up @@ -192,6 +198,7 @@ class ActorIsolation {

switch (lhs.getKind()) {
case Nonisolated:
case NonisolatedUnsafe:
case Unspecified:
return true;

Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/Attr.def
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ CONTEXTUAL_SIMPLE_DECL_ATTR(async, Async,
SIMPLE_DECL_ATTR(reasync, Reasync,
OnFunc | OnConstructor | RejectByParser | ABIBreakingToAdd | ABIBreakingToRemove | APIBreakingToAdd | APIBreakingToRemove,
109)
CONTEXTUAL_SIMPLE_DECL_ATTR(nonisolated, Nonisolated,
CONTEXTUAL_DECL_ATTR(nonisolated, Nonisolated,
DeclModifier | OnFunc | OnConstructor | OnVar | OnSubscript | ABIStableToAdd | ABIStableToRemove | APIBreakingToAdd | APIStableToRemove,
112)
CONTEXTUAL_SIMPLE_DECL_ATTR(distributed, DistributedActor,
Expand Down
24 changes: 24 additions & 0 deletions include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ class DeclAttribute : public AttributeBase {
SWIFT_INLINE_BITFIELD(ObjCImplementationAttr, DeclAttribute, 1,
isCategoryNameInvalid : 1
);

SWIFT_INLINE_BITFIELD(NonisolatedAttr, DeclAttribute, 1,
isUnsafe : 1
);
} Bits;
// clang-format on

Expand Down Expand Up @@ -2434,6 +2438,26 @@ class ObjCImplementationAttr final : public DeclAttribute {
}
};

/// Represents nonisolated modifier.
class NonisolatedAttr final : public DeclAttribute {
public:
NonisolatedAttr(SourceLoc atLoc, SourceRange range, bool unsafe,
bool implicit)
: DeclAttribute(DAK_Nonisolated, atLoc, range, implicit) {
Bits.NonisolatedAttr.isUnsafe = unsafe;
assert((isUnsafe() == unsafe) && "not enough bits for unsafe state");
}

NonisolatedAttr(bool unsafe, bool implicit)
: NonisolatedAttr({}, {}, unsafe, implicit) {}

bool isUnsafe() const { return Bits.NonisolatedAttr.isUnsafe; }

static bool classof(const DeclAttribute *DA) {
return DA->getKind() == DAK_Nonisolated;
}
};

/// A macro role attribute, spelled with either @attached or @freestanding,
/// which declares one of the roles that a given macro can inhabit.
class MacroRoleAttr final
Expand Down
5 changes: 4 additions & 1 deletion include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6128,7 +6128,10 @@ class VarDecl : public AbstractStorageDecl {
/// True if this is a top-level global variable from the main source file.
bool isTopLevelGlobal() const { return Bits.VarDecl.IsTopLevelGlobal; }
void setTopLevelGlobal(bool b) { Bits.VarDecl.IsTopLevelGlobal = b; }


/// True if this is any storage of static duration (global scope or static).
bool isGlobalStorage() const;

/// Retrieve the custom attributes that attach property wrappers to this
/// property. The returned list contains all of the attached property wrapper
/// attributes in source order, which means the outermost wrapper attribute
Expand Down
7 changes: 5 additions & 2 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1456,8 +1456,10 @@ namespace {
});
}

printFlag(D->getAttrs().hasAttribute<NonisolatedAttr>(), "nonisolated",
ExprModifierColor);
if (auto *attr = D->getAttrs().getAttribute<NonisolatedAttr>()) {
printFlag(attr->isUnsafe() ? "nonisolated(unsafe)" : "nonisolated",
ExprModifierColor);
}
printFlag(D->isDistributed(), "distributed", ExprModifierColor);
printFlag(D->isDistributedThunk(), "distributed_thunk",ExprModifierColor);
}
Expand Down Expand Up @@ -2735,6 +2737,7 @@ class PrintExpr : public ExprVisitor<PrintExpr, void, StringRef>,
switch (auto isolation = E->getActorIsolation()) {
case ActorIsolation::Unspecified:
case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
break;

case ActorIsolation::ActorInstance:
Expand Down
14 changes: 14 additions & 0 deletions lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,14 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
break;
}

case DAK_Nonisolated: {
Printer.printAttrName("nonisolated");
if (cast<NonisolatedAttr>(this)->isUnsafe()) {
Printer << "(unsafe)";
}
break;
}

case DAK_MacroRole: {
auto Attr = cast<MacroRoleAttr>(this);

Expand Down Expand Up @@ -1721,6 +1729,12 @@ StringRef DeclAttribute::getAttrName() const {
return "_section";
case DAK_Documentation:
return "_documentation";
case DAK_Nonisolated:
if (cast<NonisolatedAttr>(this)->isUnsafe()) {
return "nonisolated(unsafe)";
} else {
return "nonisolated";
}
case DAK_MacroRole:
switch (cast<MacroRoleAttr>(this)->getMacroSyntax()) {
case MacroSyntax::Freestanding:
Expand Down
11 changes: 11 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2460,6 +2460,7 @@ static bool deferMatchesEnclosingAccess(const FuncDecl *defer) {

switch (getActorIsolation(type)) {
case ActorIsolation::Unspecified:
case ActorIsolation::NonisolatedUnsafe:
case ActorIsolation::GlobalActorUnsafe:
break;

Expand Down Expand Up @@ -7893,6 +7894,15 @@ VarDecl *VarDecl::getLazyStorageProperty() const {
{});
}

bool VarDecl::isGlobalStorage() const {
if (!hasStorage()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be hasStorageOrWrapsStorage()? I wonder how this interacts with property wrapped static properties...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @xedin good point, and I addressed property wrapped static properties in the most recent commit.

return false;
}
const auto *dc = getDeclContext();
return isStatic() || dc->isModuleScopeContext() ||
(dc->isTypeContext() && !isInstanceMember());
}

bool VarDecl::isPropertyMemberwiseInitializedWithWrappedType() const {
auto customAttrs = getAttachedPropertyWrappers();
if (customAttrs.empty())
Expand Down Expand Up @@ -10500,6 +10510,7 @@ bool VarDecl::isSelfParamCaptureIsolated() const {
switch (auto isolation = closure->getActorIsolation()) {
case ActorIsolation::Unspecified:
case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
case ActorIsolation::GlobalActor:
case ActorIsolation::GlobalActorUnsafe:
return false;
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -921,8 +921,12 @@ static void formatDiagnosticArgument(StringRef Modifier,
}

case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
case ActorIsolation::Unspecified:
Out << "nonisolated";
if (isolation == ActorIsolation::NonisolatedUnsafe) {
Out << "(unsafe)";
}
break;
}
break;
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/TypeCheckRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1741,7 +1741,11 @@ void swift::simple_display(
break;

case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
out << "nonisolated";
if (state == ActorIsolation::NonisolatedUnsafe) {
out << "(unsafe)";
}
break;

case ActorIsolation::Unspecified:
Expand Down
3 changes: 2 additions & 1 deletion lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7844,7 +7844,8 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
// Hard-code @actorIndependent, until Objective-C clients start
// using nonisolated.
if (swiftAttr->getAttribute() == "@actorIndependent") {
auto attr = new (SwiftContext) NonisolatedAttr(/*isImplicit=*/true);
auto attr = new (SwiftContext)
NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true);
MappedDecl->getAttrs().add(attr);
continue;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/ClangImporter/SwiftDeclSynthesizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,8 @@ ValueDecl *SwiftDeclSynthesizer::createConstant(

// Mark the function transparent so that we inline it away completely.
func->getAttrs().add(new (C) TransparentAttr(/*implicit*/ true));
auto nonisolatedAttr = new (C) NonisolatedAttr(/*IsImplicit=*/true);
auto nonisolatedAttr =
new (C) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true);
var->getAttrs().add(nonisolatedAttr);

// Set the function up as the getter.
Expand Down
1 change: 1 addition & 0 deletions lib/IDE/CompletionLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ void CompletionLookup::analyzeActorIsolation(
}
case ActorIsolation::Unspecified:
case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
return;
}

Expand Down
14 changes: 14 additions & 0 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3839,6 +3839,20 @@ ParserStatus Parser::parseNewDeclAttribute(DeclAttributes &Attributes,
return makeParserSuccess();
break;
}
case DAK_Nonisolated: {
auto isUnsafe =
parseSingleAttrOption<bool>(*this, Loc, AttrRange, AttrName, DK,
{{Context.Id_unsafe, true}}, false);
if (!isUnsafe) {
return makeParserSuccess();
}

if (!DiscardAttribute) {
Attributes.add(new (Context) NonisolatedAttr(AtLoc, AttrRange, *isUnsafe,
/*implicit*/ false));
}
break;
}
case DAK_MacroRole: {
auto syntax = (AttrName == "freestanding" ? MacroSyntax::Freestanding
: MacroSyntax::Attached);
Expand Down
1 change: 1 addition & 0 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5521,6 +5521,7 @@ RValue SILGenFunction::emitApply(

case ActorIsolation::Unspecified:
case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
llvm_unreachable("Not isolated");
break;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/SILGen/SILGenConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ static bool ctorHopsInjectedByDefiniteInit(ConstructorDecl *ctor,

case ActorIsolation::Unspecified:
case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
case ActorIsolation::GlobalActor:
case ActorIsolation::GlobalActorUnsafe:
return false;
Expand Down Expand Up @@ -1554,6 +1555,7 @@ void SILGenFunction::emitMemberInitializer(DeclContext *dc, VarDecl *selfDecl,
// 'nonisolated' expressions can be evaluated from anywhere
case ActorIsolation::Unspecified:
case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
break;

case ActorIsolation::GlobalActor:
Expand Down
9 changes: 7 additions & 2 deletions lib/SILGen/SILGenProlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,7 @@ void SILGenFunction::emitProlog(
return false;

case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
case ActorIsolation::Unspecified:
return false;
}
Expand Down Expand Up @@ -1332,6 +1333,7 @@ void SILGenFunction::emitProlog(
switch (actorIsolation.getKind()) {
case ActorIsolation::Unspecified:
case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
break;

case ActorIsolation::ActorInstance: {
Expand Down Expand Up @@ -1384,6 +1386,7 @@ void SILGenFunction::emitProlog(
switch (actorIsolation.getKind()) {
case ActorIsolation::Unspecified:
case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
break;

case ActorIsolation::ActorInstance: {
Expand Down Expand Up @@ -1572,6 +1575,7 @@ SILGenFunction::emitExecutor(SILLocation loc, ActorIsolation isolation,
switch (isolation.getKind()) {
case ActorIsolation::Unspecified:
case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
return llvm::None;

case ActorIsolation::ActorInstance: {
Expand All @@ -1597,8 +1601,9 @@ void SILGenFunction::emitHopToActorValue(SILLocation loc, ManagedValue actor) {
getActorIsolationOfContext(FunctionDC, [](AbstractClosureExpr *CE) {
return CE->getActorIsolation();
});
if (isolation != ActorIsolation::Nonisolated
&& isolation != ActorIsolation::Unspecified) {
if (isolation != ActorIsolation::Nonisolated &&
isolation != ActorIsolation::NonisolatedUnsafe &&
isolation != ActorIsolation::Unspecified) {
// TODO: Explicit hop with no hop-back should only be allowed in nonisolated
// async functions. But it needs work for any closure passed to
// Task.detached, which currently has unspecified isolation.
Expand Down
1 change: 1 addition & 0 deletions lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,7 @@ void LifetimeChecker::injectActorHops() {

case ActorIsolation::Unspecified:
case ActorIsolation::Nonisolated:
case ActorIsolation::NonisolatedUnsafe:
case ActorIsolation::GlobalActorUnsafe:
case ActorIsolation::GlobalActor:
return;
Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,8 @@ bool swift::addNonIsolatedToSynthesized(NominalTypeDecl *nominal,
return false;

ASTContext &ctx = nominal->getASTContext();
value->getAttrs().add(new (ctx) NonisolatedAttr(/*isImplicit=*/true));
value->getAttrs().add(
new (ctx) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true));
return true;
}

Expand Down
7 changes: 4 additions & 3 deletions lib/Sema/CodeSynthesisDistributedActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ static VarDecl *addImplicitDistributedActorIDProperty(

// mark as nonisolated, allowing access to it from everywhere
propDecl->getAttrs().add(
new (C) NonisolatedAttr(/*IsImplicit=*/true));
new (C) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true));
// mark as @_compilerInitialized, since we synthesize the initializing
// assignment during SILGen.
propDecl->getAttrs().add(
Expand Down Expand Up @@ -158,7 +158,7 @@ static VarDecl *addImplicitDistributedActorActorSystemProperty(

// mark as nonisolated, allowing access to it from everywhere
propDecl->getAttrs().add(
new (C) NonisolatedAttr(/*IsImplicit=*/true));
new (C) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true));

auto idProperty = nominal->getDistributedActorIDProperty();
// If the id was not yet synthesized, we need to ensure that eventually
Expand Down Expand Up @@ -735,7 +735,8 @@ static FuncDecl *createDistributedThunkFunction(FuncDecl *func) {

thunk->setSynthesized(true);
thunk->setDistributedThunk(true);
thunk->getAttrs().add(new (C) NonisolatedAttr(/*isImplicit=*/true));
thunk->getAttrs().add(
new (C) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true));

if (isa<ClassDecl>(DC))
thunk->getAttrs().add(new (C) FinalAttr(/*isImplicit=*/true));
Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/DerivedConformanceActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ static ValueDecl *deriveActor_unownedExecutor(DerivedConformance &derived) {
property->getAttrs().add(new (ctx) SemanticsAttr(SEMANTICS_DEFAULT_ACTOR,
SourceLoc(), SourceRange(),
/*implicit*/ true));
property->getAttrs().add(new (ctx) NonisolatedAttr(/*IsImplicit=*/true));
property->getAttrs().add(
new (ctx) NonisolatedAttr(/*unsafe=*/false, /*implicit=*/true));

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