Skip to content

[6.0][SE-0423] Promote DynamicActorIsolation to an upcoming feature and add a way to disable checks #73104

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
2 changes: 0 additions & 2 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -5687,8 +5687,6 @@ ERROR(preconcurrency_not_inheritance_clause,none,
"'preconcurrency' attribute only applies in inheritance clauses", ())
ERROR(preconcurrency_not_existential,none,
"'preconcurrency' attribute cannot apply to non-protocol type %0", (Type))
ERROR(preconcurrency_attr_disabled,none,
"attribute requires '-enable-experimental-feature DynamicActorIsolation'", ())

ERROR(redundant_any_in_existential,none,
"redundant 'any' in type %0",
Expand Down
6 changes: 1 addition & 5 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ UPCOMING_FEATURE(GlobalConcurrency, 412, 6)
UPCOMING_FEATURE(InferSendableFromCaptures, 418, 6)
UPCOMING_FEATURE(ImplicitOpenExistentials, 352, 6)
UPCOMING_FEATURE(RegionBasedIsolation, 414, 6)
UPCOMING_FEATURE(DynamicActorIsolation, 423, 6)
UPCOMING_FEATURE(MoveOnlyPartialConsumption, 429, 6)

// Swift 7
Expand Down Expand Up @@ -350,11 +351,6 @@ EXPERIMENTAL_FEATURE(GroupActorErrors, true)
// Allow for the 'transferring' keyword to be applied to arguments and results.
EXPERIMENTAL_FEATURE(TransferringArgsAndResults, true)

// Enable `@preconcurrency` attribute on protocol conformances and runtime checks
// of actor isolation in @obj thunks and arguments of APIs that haven't yet adopted
// strict concurrency checking.
EXPERIMENTAL_FEATURE(DynamicActorIsolation, false)

// Allow for `switch` of noncopyable values to be borrowing or consuming.
EXPERIMENTAL_FEATURE(BorrowingSwitch, true)

Expand Down
8 changes: 8 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,9 @@ namespace swift {
/// type-checking, SIL verification, and IR emission,
bool BypassResilienceChecks = false;

/// Disables `DynamicActorIsolation` feature.
bool DisableDynamicActorIsolation = false;

/// Whether or not to allow experimental features that are only available
/// in "production".
#ifdef NDEBUG
Expand All @@ -605,6 +608,11 @@ namespace swift {
return ActiveConcurrencyModel == ConcurrencyModel::TaskToThread;
}

bool isDynamicActorIsolationCheckingEnabled() const {
return !DisableDynamicActorIsolation &&
hasFeature(Feature::DynamicActorIsolation);
}

LangOptions();

/// Sets the target we are building for and updates platform conditions
Expand Down
5 changes: 5 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,11 @@ def disable_actor_data_race_checks :
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
HelpText<"Disable runtime checks for actor data races">;

def disable_dynamic_actor_isolation :
Flag<["-"], "disable-dynamic-actor-isolation">,
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
HelpText<"Disable dynamic actor isolation checks">;

def enable_bare_slash_regex : Flag<["-"], "enable-bare-slash-regex">,
Flags<[FrontendOption, ModuleInterfaceOption]>,
HelpText<"Enable the use of forward slash regular-expression literal syntax">;
Expand Down
24 changes: 1 addition & 23 deletions lib/AST/FeatureSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,29 +643,7 @@ static bool usesFeatureTransferringArgsAndResults(Decl *decl) {
return false;
}

static bool usesFeatureDynamicActorIsolation(Decl *decl) {
auto usesPreconcurrencyConformance = [&](const InheritedTypes &inherited) {
return llvm::any_of(
inherited.getEntries(),
[](const InheritedEntry &entry) { return entry.isPreconcurrency(); });
};

if (auto *T = dyn_cast<TypeDecl>(decl))
return usesPreconcurrencyConformance(T->getInherited());

if (auto *E = dyn_cast<ExtensionDecl>(decl)) {
// If type has `@preconcurrency` conformance(s) all of its
// extensions have to be guarded by the flag too.
if (auto *T = dyn_cast<TypeDecl>(E->getExtendedNominal())) {
if (usesPreconcurrencyConformance(T->getInherited()))
return true;
}

return usesPreconcurrencyConformance(E->getInherited());
}

return false;
}
UNINTERESTING_FEATURE(DynamicActorIsolation)

UNINTERESTING_FEATURE(BorrowingSwitch)

Expand Down
1 change: 1 addition & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
inputArgs.AddLastArg(arguments,
options::OPT_enable_actor_data_race_checks,
options::OPT_disable_actor_data_race_checks);
inputArgs.AddLastArg(arguments, options::OPT_disable_dynamic_actor_isolation);
inputArgs.AddLastArg(arguments, options::OPT_warn_concurrency);
inputArgs.AddLastArg(arguments, options::OPT_strict_concurrency);
inputArgs.AddAllArgs(arguments, options::OPT_enable_experimental_feature);
Expand Down
3 changes: 3 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
if (Opts.hasFeature(Feature::DebugDescriptionMacro))
Opts.enableFeature(Feature::SymbolLinkageMarkers);

Opts.DisableDynamicActorIsolation |=
Args.hasArg(OPT_disable_dynamic_actor_isolation);

#if SWIFT_ENABLE_EXPERIMENTAL_PARSER_VALIDATION
/// Enable round trip parsing via the new swift parser unless it is disabled
/// explicitly. The new Swift parser can have mismatches with C++ parser -
Expand Down
6 changes: 2 additions & 4 deletions lib/SILGen/SILGenBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1593,14 +1593,12 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
loc.markAutoGenerated();
Scope scope(Cleanups, CleanupLocation(loc));

bool emitExecutorPrecondition =
getASTContext().LangOpts.hasFeature(Feature::DynamicActorIsolation);

std::optional<ActorIsolation> isolation;
if (F.isAsync()) {
if (thunk.hasDecl())
isolation = getActorIsolation(thunk.getDecl());
} else if (emitExecutorPrecondition) {
} else if (getASTContext()
.LangOpts.isDynamicActorIsolationCheckingEnabled()) {
if (thunk.hasDecl()) {
isolation = getActorIsolation(thunk.getDecl());
} else if (auto globalActor = nativeInfo.FormalType->getGlobalActor()) {
Expand Down
5 changes: 4 additions & 1 deletion lib/SILGen/SILGenPoly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7021,7 +7021,10 @@ void SILGenFunction::emitProtocolWitness(

if (!F.isAsync()) {
assert(isPreconcurrency);
emitPreconditionCheckExpectedExecutor(loc, *enterIsolation, actorSelf);

if (getASTContext().LangOpts.isDynamicActorIsolationCheckingEnabled()) {
emitPreconditionCheckExpectedExecutor(loc, *enterIsolation, actorSelf);
}
} else {
emitHopToTargetActor(loc, enterIsolation, actorSelf);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7462,7 +7462,7 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
}
}

if (ctx.LangOpts.hasFeature(Feature::DynamicActorIsolation)) {
if (ctx.LangOpts.isDynamicActorIsolationCheckingEnabled()) {
// Passing a synchronous global actor-isolated function value and
// parameter that expects a synchronous non-isolated function type could
// require a runtime check to ensure that function is always called in
Expand Down
29 changes: 11 additions & 18 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3329,28 +3329,21 @@ TypeResolver::resolveAttributedType(TypeRepr *repr, TypeResolutionOptions option
}

if (auto preconcurrencyAttr = claim<PreconcurrencyTypeAttr>(attrs)) {
auto &ctx = getASTContext();
if (ctx.LangOpts.hasFeature(Feature::DynamicActorIsolation)) {
if (ty->hasError())
return ty;

if (!options.is(TypeResolverContext::Inherited) ||
getDeclContext()->getSelfProtocolDecl()) {
diagnoseInvalid(repr, preconcurrencyAttr->getAtLoc(),
diag::preconcurrency_not_inheritance_clause);
ty = ErrorType::get(getASTContext());
} else if (!ty->isConstraintType()) {
diagnoseInvalid(repr, preconcurrencyAttr->getAtLoc(),
diag::preconcurrency_not_existential, ty);
ty = ErrorType::get(getASTContext());
}
if (ty->hasError())
return ty;

// Nothing to record in the type.
} else {
if (!options.is(TypeResolverContext::Inherited) ||
getDeclContext()->getSelfProtocolDecl()) {
diagnoseInvalid(repr, preconcurrencyAttr->getAtLoc(),
diag::preconcurrency_attr_disabled);
diag::preconcurrency_not_inheritance_clause);
ty = ErrorType::get(getASTContext());
} else if (!ty->isConstraintType()) {
diagnoseInvalid(repr, preconcurrencyAttr->getAtLoc(),
diag::preconcurrency_not_existential, ty);
ty = ErrorType::get(getASTContext());
}

// Nothing to record in the type.
}

if (auto retroactiveAttr = claim<RetroactiveTypeAttr>(attrs)) {
Expand Down
81 changes: 80 additions & 1 deletion test/ClangImporter/preconcurrency_conformances.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -disable-availability-checking -enable-experimental-feature DynamicActorIsolation -emit-silgen %s | %FileCheck %s
// RUN: %empty-directory(%t/src)
// RUN: split-file %s %t/src

// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -disable-availability-checking -enable-upcoming-feature DynamicActorIsolation -emit-silgen -module-name preconcurrency_conformances %t/src/checks.swift | %FileCheck %t/src/checks.swift
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -disable-availability-checking -swift-version 6 -disable-dynamic-actor-isolation -emit-silgen -module-name preconcurrency_conformances %t/src/checks_disabled.swift | %FileCheck %t/src/checks_disabled.swift

// REQUIRES: asserts
// REQUIRES: concurrency
// REQUIRES: objc_interop

//--- checks.swift
import Foundation

actor MyActor {
Expand Down Expand Up @@ -130,3 +135,77 @@ class Sub : Super {
// CHECK-NEXT: [[EXEC:%.*]] = extract_executor [[MAIN_ACTOR]] : $MainActor
// CHECK: [[PRECONDITION:%.*]] = function_ref @$ss22_checkExpectedExecutor14_filenameStart01_D6Length01_D7IsASCII5_line9_executoryBp_BwBi1_BwBetF : $@convention(thin) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, Builtin.Word, Builtin.Executor) -> ()
// CHECK-NEXT: {{.*}} = apply [[PRECONDITION]]({{.*}}, [[EXEC]]) : $@convention(thin) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, Builtin.Word, Builtin.Executor) -> ()

//--- checks_disabled.swift
import Foundation

actor MyActor {
}

@globalActor
struct GlobalActor {
static let shared: MyActor = MyActor()
}

@objc protocol P {
var data: String? { get set }

init()
func test() -> String?
}

@MainActor
final class K : @preconcurrency P {
var data: String? {
get { nil }
set {}
}

init() {}
@GlobalActor func test() -> String? { nil }
}

// @objc K.data.getter
// CHECK-LABEL: sil private [thunk] [ossa] @$s27preconcurrency_conformances1KC4dataSSSgvgTo : $@convention(objc_method) (K) -> @autoreleased Optional<NSString>
// CHECK-NOT: [[PRECONDITION:%.*]] = function_ref @$ss22_checkExpectedExecutor14_filenameStart01_D6Length01_D7IsASCII5_line9_executoryBp_BwBi1_BwBetF : $@convention(thin) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, Builtin.Word, Builtin.Executor) -> ()

// @objc K.data.setter
// CHECK-LABEL: sil private [thunk] [ossa] @$s27preconcurrency_conformances1KC4dataSSSgvsTo : $@convention(objc_method) (Optional<NSString>, K) -> ()
// CHECK-NOT: [[PRECONDITION:%.*]] = function_ref @$ss22_checkExpectedExecutor14_filenameStart01_D6Length01_D7IsASCII5_line9_executoryBp_BwBi1_BwBetF : $@convention(thin) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, Builtin.Word, Builtin.Executor) -> ()

// @objc K.init()
// CHECK-LABEL: sil private [thunk] [ossa] @$s27preconcurrency_conformances1KCACycfcTo : $@convention(objc_method) (@owned K) -> @owned K
// CHECK-NOT: [[PRECONDITION:%.*]] = function_ref @$ss22_checkExpectedExecutor14_filenameStart01_D6Length01_D7IsASCII5_line9_executoryBp_BwBi1_BwBetF : $@convention(thin) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, Builtin.Word, Builtin.Executor) -> ()

// @objc K.test()
// CHECK-LABEL: sil private [thunk] [ossa] @$s27preconcurrency_conformances1KC4testSSSgyFTo : $@convention(objc_method) (K) -> @autoreleased Optional<NSString>
// CHECK-NOT: [[PRECONDITION:%.*]] = function_ref @$ss22_checkExpectedExecutor14_filenameStart01_D6Length01_D7IsASCII5_line9_executoryBp_BwBi1_BwBetF : $@convention(thin) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, Builtin.Word, Builtin.Executor) -> ()

@MainActor
class TestObjCMethod {
@objc func testImplicit() -> Int { 42 }

@GlobalActor
@objc func testExplicit() {}
}

// CHECK-LABEL: sil private [thunk] [ossa] @$s27preconcurrency_conformances14TestObjCMethodC12testImplicitSiyFTo : $@convention(objc_method) (TestObjCMethod) -> Int
// CHECK-NOT: [[PRECONDITION:%.*]] = function_ref @$ss22_checkExpectedExecutor14_filenameStart01_D6Length01_D7IsASCII5_line9_executoryBp_BwBi1_BwBetF : $@convention(thin) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, Builtin.Word, Builtin.Executor) -> ()

// CHECK-LABEL: sil private [thunk] [ossa] @$s27preconcurrency_conformances14TestObjCMethodC12testExplicityyFTo : $@convention(objc_method) (TestObjCMethod) -> ()
// CHECK-NOT: [[PRECONDITION:%.*]] = function_ref @$ss22_checkExpectedExecutor14_filenameStart01_D6Length01_D7IsASCII5_line9_executoryBp_BwBi1_BwBetF : $@convention(thin) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, Builtin.Word, Builtin.Executor) -> ()

@objcMembers
class Super {
@MainActor func test() {}
}

class Sub : Super {
override func test() {}
}

// CHECK-LABEL: sil private [thunk] [ossa] @$s27preconcurrency_conformances5SuperC4testyyFTo : $@convention(objc_method) (Super) -> ()
// CHECK-NOT: [[PRECONDITION:%.*]] = function_ref @$ss22_checkExpectedExecutor14_filenameStart01_D6Length01_D7IsASCII5_line9_executoryBp_BwBi1_BwBetF : $@convention(thin) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, Builtin.Word, Builtin.Executor) -> ()

// CHECK-LABEL: sil private [thunk] [ossa] @$s27preconcurrency_conformances3SubC4testyyFTo : $@convention(objc_method) (Sub) -> ()
// CHECK-NOT: [[PRECONDITION:%.*]] = function_ref @$ss22_checkExpectedExecutor14_filenameStart01_D6Length01_D7IsASCII5_line9_executoryBp_BwBi1_BwBetF : $@convention(thin) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, Builtin.Word, Builtin.Executor) -> ()
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// RUN: -I %t \
// RUN: -disable-availability-checking \
// RUN: -module-name Client \
// RUN: -enable-experimental-feature DynamicActorIsolation \
// RUN: -enable-upcoming-feature DynamicActorIsolation \
// RUN: %t/src/Client.swift -verify | %FileCheck %s

// Delete swiftmodule to test building against swiftinterface
Expand All @@ -23,7 +23,7 @@
// RUN: -I %t \
// RUN: -disable-availability-checking \
// RUN: -module-name Client \
// RUN: -enable-experimental-feature DynamicActorIsolation \
// RUN: -enable-upcoming-feature DynamicActorIsolation \
// RUN: %t/src/Client.swift -verify | %FileCheck %s

// REQUIRES: asserts
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -target arm64-apple-macosx10.15 -enable-experimental-feature DynamicActorIsolation -emit-silgen -verify %s | %FileCheck %s
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -target arm64-apple-macosx10.15 -enable-upcoming-feature DynamicActorIsolation -emit-silgen -verify %s | %FileCheck %s
// REQUIRES: objc_interop
// REQUIRES: asserts

Expand Down
2 changes: 1 addition & 1 deletion test/Concurrency/preconcurrency_conformances.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend -swift-version 5 -disable-availability-checking %s -emit-sil -o /dev/null -verify -enable-experimental-feature DynamicActorIsolation -strict-concurrency=complete -verify-additional-prefix complete-tns-
// RUN: %target-swift-frontend -swift-version 5 -disable-availability-checking %s -emit-sil -o /dev/null -verify -enable-upcoming-feature DynamicActorIsolation -strict-concurrency=complete -verify-additional-prefix complete-tns-

// REQUIRES: concurrency
// REQUIRES: asserts
Expand Down
10 changes: 5 additions & 5 deletions test/Interpreter/preconcurrency_conformances.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
// RUN: -target %target-cpu-apple-macosx10.15 \
// RUN: -I %t -L %t -l Interface \
// RUN: -emit-module-interface-path %t/Types.swiftinterface \
// RUN: -Xfrontend -enable-experimental-feature -Xfrontend DynamicActorIsolation
// RUN: -Xfrontend -enable-upcoming-feature -Xfrontend DynamicActorIsolation

// RUN: %target-build-swift -Xfrontend -enable-experimental-feature -Xfrontend DynamicActorIsolation -I %t -L %t -l Types %t/src/Crash1.swift -o %t/crash1.out
// RUN: %target-build-swift -Xfrontend -enable-upcoming-feature -Xfrontend DynamicActorIsolation -I %t -L %t -l Types %t/src/Crash1.swift -o %t/crash1.out
// RUN: %target-codesign %t/crash1.out
// RUN: not --crash env SWIFT_UNEXPECTED_EXECUTOR_LOG_LEVEL=2 %target-run %t/crash1.out 2>&1 | %FileCheck %t/src/Crash1.swift

// RUN: %target-build-swift -Xfrontend -enable-experimental-feature -Xfrontend DynamicActorIsolation -I %t -L %t -l Types %t/src/Crash2.swift -o %t/crash2.out
// RUN: %target-build-swift -Xfrontend -enable-upcoming-feature -Xfrontend DynamicActorIsolation -I %t -L %t -l Types %t/src/Crash2.swift -o %t/crash2.out
// RUN: %target-codesign %t/crash2.out
// RUN: not --crash env SWIFT_UNEXPECTED_EXECUTOR_LOG_LEVEL=2 %target-run %t/crash2.out 2>&1 | %FileCheck %t/src/Crash2.swift

// RUN: %target-build-swift -Xfrontend -enable-experimental-feature -Xfrontend DynamicActorIsolation -I %t -L %t -l Types %t/src/Crash3.swift -o %t/crash3.out
// RUN: %target-build-swift -Xfrontend -enable-upcoming-feature -Xfrontend DynamicActorIsolation -I %t -L %t -l Types %t/src/Crash3.swift -o %t/crash3.out
// RUN: %target-codesign %t/crash3.out
// RUN: not --crash env SWIFT_UNEXPECTED_EXECUTOR_LOG_LEVEL=2 %target-run %t/crash3.out 2>&1 | %FileCheck %t/src/Crash3.swift

// RUN: %target-build-swift -Xfrontend -enable-experimental-feature -Xfrontend DynamicActorIsolation -I %t -L %t -l Types %t/src/Crash4.swift -o %t/crash4.out
// RUN: %target-build-swift -Xfrontend -enable-upcoming-feature -Xfrontend DynamicActorIsolation -I %t -L %t -l Types %t/src/Crash4.swift -o %t/crash4.out
// RUN: %target-codesign %t/crash4.out
// RUN: not --crash env SWIFT_UNEXPECTED_EXECUTOR_LOG_LEVEL=2 %target-run %t/crash4.out 2>&1 | %FileCheck %t/src/Crash4.swift

Expand Down
Loading