Skip to content

Commit 2cb7e8d

Browse files
Implemented suppressing isolated deinit for compatibility with older compiler versions
1 parent ab4d338 commit 2cb7e8d

File tree

10 files changed

+320
-22
lines changed

10 files changed

+320
-22
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
12791279
std::optional<std::pair<CustomAttr *, NominalTypeDecl *>>
12801280
getGlobalActorAttr() const;
12811281

1282+
/// Determine whether there is an explicit isolation attribute
1283+
/// of any kind.
1284+
bool hasExplicitIsolationAttribute() const;
1285+
12821286
/// If an alternative module name is specified for this decl, e.g. using
12831287
/// @_originalDefinedIn attribute, this function returns this module name.
12841288
StringRef getAlternateModuleName() const;

include/swift/AST/PrintOptions.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class DeclContext;
3737
class Type;
3838
class ModuleDecl;
3939
enum class DeclAttrKind : unsigned;
40+
class DeclAttribute;
41+
class CustomAttr;
4042
class SynthesizedExtensionAnalyzer;
4143
struct PrintOptions;
4244
class SILPrintContext;
@@ -342,6 +344,10 @@ struct PrintOptions {
342344
/// Suppress emitting @available(*, noasync)
343345
bool SuppressNoAsyncAvailabilityAttr = false;
344346

347+
/// Suppress emitting isolated or async deinit, and emit open containing class
348+
/// as public
349+
bool SuppressIsolatedDeinit = false;
350+
345351
/// Whether to print the \c{/*not inherited*/} comment on factory initializers.
346352
bool PrintFactoryInitializerComment = true;
347353

@@ -403,6 +409,8 @@ struct PrintOptions {
403409
DeclAttrKind::FixedLayout, DeclAttrKind::ShowInInterface,
404410
};
405411

412+
std::vector<CustomAttr *> ExcludeCustomAttrList = {};
413+
406414
/// List of attribute kinds that should be printed exclusively.
407415
/// Empty means allow all.
408416
std::vector<AnyAttrKind> ExclusiveAttrList;
@@ -648,6 +656,8 @@ struct PrintOptions {
648656
return false;
649657
}
650658

659+
bool excludeAttr(const DeclAttribute *DA) const;
660+
651661
/// Retrieve the set of options for verbose printing to users.
652662
static PrintOptions printVerbose() {
653663
PrintOptions result;

include/swift/Basic/Features.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ EXPERIMENTAL_FEATURE(DebugDescriptionMacro, true)
401401

402402
EXPERIMENTAL_FEATURE(ReinitializeConsumeInMultiBlockDefer, false)
403403

404+
SUPPRESSIBLE_LANGUAGE_FEATURE(IsolatedDeinit, 371, "isolated/async deinit")
405+
404406
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
405407
#undef EXPERIMENTAL_FEATURE
406408
#undef UPCOMING_FEATURE

lib/AST/ASTPrinter.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,18 @@ static bool shouldPrintAllSemanticDetails(const PrintOptions &options) {
180180
return false;
181181
}
182182

183+
bool PrintOptions::excludeAttr(const DeclAttribute *DA) const {
184+
if (excludeAttrKind(DA->getKind())) {
185+
return true;
186+
}
187+
if (auto CA = dyn_cast<CustomAttr>(DA)) {
188+
if (std::any_of(ExcludeCustomAttrList.begin(), ExcludeCustomAttrList.end(),
189+
[CA](CustomAttr *other) { return other == CA; }))
190+
return true;
191+
}
192+
return false;
193+
}
194+
183195
PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
184196
bool preferTypeRepr,
185197
bool printFullConvention,
@@ -825,7 +837,13 @@ class PrintAST : public ASTVisitor<PrintAST> {
825837
if (D->getDeclContext()->isLocalContext())
826838
return;
827839

828-
printAccess(D->getFormalAccess());
840+
if (Options.SuppressIsolatedDeinit &&
841+
D->getFormalAccess() == AccessLevel::Open &&
842+
usesFeatureIsolatedDeinit(D)) {
843+
printAccess(AccessLevel::Public);
844+
} else {
845+
printAccess(D->getFormalAccess());
846+
}
829847
bool shouldSkipSetterAccess =
830848
llvm::is_contained(Options.ExcludeAttrList, DeclAttrKind::SetterAccess);
831849

@@ -1252,6 +1270,10 @@ void PrintAST::printAttributes(const Decl *D) {
12521270
// Save the current number of exclude attrs to restore once we're done.
12531271
unsigned originalExcludeAttrCount = Options.ExcludeAttrList.size();
12541272

1273+
// ExcludeCustomAttrList stores instances of attributes, which are specific
1274+
// for each decl They cannot be shared across different decls.
1275+
assert(Options.ExcludeCustomAttrList.empty());
1276+
12551277
if (Options.PrintImplicitAttrs) {
12561278

12571279
// Don't print a redundant 'final' if we are printing a 'static' decl.
@@ -1343,9 +1365,19 @@ void PrintAST::printAttributes(const Decl *D) {
13431365
Options.ExcludeAttrList.push_back(DeclAttrKind::Borrowing);
13441366
}
13451367

1368+
if (isa<DestructorDecl>(D) && Options.SuppressIsolatedDeinit) {
1369+
Options.ExcludeAttrList.push_back(DeclAttrKind::Nonisolated);
1370+
Options.ExcludeAttrList.push_back(DeclAttrKind::Isolated);
1371+
if (auto globalActor = D->getGlobalActorAttr()) {
1372+
Options.ExcludeCustomAttrList.push_back(globalActor->first);
1373+
}
1374+
}
1375+
13461376
attrs.print(Printer, Options, D);
13471377

1378+
13481379
Options.ExcludeAttrList.resize(originalExcludeAttrCount);
1380+
Options.ExcludeCustomAttrList.clear();
13491381
}
13501382

13511383
void PrintAST::printTypedPattern(const TypedPattern *TP) {
@@ -3206,6 +3238,13 @@ suppressingFeatureBitwiseCopyable2(PrintOptions &options,
32063238
options.ExcludeAttrList.resize(originalExcludeAttrCount);
32073239
}
32083240

3241+
static void
3242+
suppressingFeatureIsolatedDeinit(PrintOptions &options,
3243+
llvm::function_ref<void()> action) {
3244+
llvm::SaveAndRestore<bool> scope(options.SuppressIsolatedDeinit, true);
3245+
action();
3246+
}
3247+
32093248
/// Suppress the printing of a particular feature.
32103249
static void suppressingFeature(PrintOptions &options, Feature feature,
32113250
llvm::function_ref<void()> action) {

lib/AST/Attr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ void DeclAttributes::print(ASTPrinter &Printer, const PrintOptions &Options,
987987
if (!Options.PrintUserInaccessibleAttrs &&
988988
DeclAttribute::isUserInaccessible(DA->getKind()))
989989
continue;
990-
if (Options.excludeAttrKind(DA->getKind()))
990+
if (Options.excludeAttr(DA))
991991
continue;
992992

993993
// If we're supposed to suppress expanded macros, check whether this is

lib/AST/Decl.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,26 @@ std::optional<CustomAttrNominalPair> Decl::getGlobalActorAttr() const {
985985
ctx.evaluator, GlobalActorAttributeRequest{mutableThis}, std::nullopt);
986986
}
987987

988+
bool Decl::hasExplicitIsolationAttribute() const {
989+
if (auto nonisolatedAttr = getAttrs().getAttribute<NonisolatedAttr>()) {
990+
if (!nonisolatedAttr->isImplicit())
991+
return true;
992+
}
993+
994+
if (auto isolatedAttr = getAttrs().getAttribute<IsolatedAttr>()) {
995+
if (!isolatedAttr->isImplicit()) {
996+
return true;
997+
}
998+
}
999+
1000+
if (auto globalActorAttr = getGlobalActorAttr()) {
1001+
if (!globalActorAttr->first->isImplicit())
1002+
return true;
1003+
}
1004+
1005+
return false;
1006+
}
1007+
9881008
bool Decl::preconcurrency() const {
9891009
if (getAttrs().hasAttribute<PreconcurrencyAttr>())
9901010
return true;

lib/AST/FeatureSet.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,23 @@ static bool usesFeatureSensitive(Decl *decl) {
772772
UNINTERESTING_FEATURE(DebugDescriptionMacro)
773773
UNINTERESTING_FEATURE(ReinitializeConsumeInMultiBlockDefer)
774774

775+
bool swift::usesFeatureIsolatedDeinit(const Decl *decl) {
776+
if (auto cd = dyn_cast<ClassDecl>(decl)) {
777+
return cd->getFormalAccess() == AccessLevel::Open &&
778+
usesFeatureIsolatedDeinit(cd->getDestructor());
779+
} else if (auto dd = dyn_cast<DestructorDecl>(decl)) {
780+
if (dd->hasExplicitIsolationAttribute()) {
781+
return true;
782+
}
783+
if (auto superDD = dd->getSuperDeinit()) {
784+
return usesFeatureIsolatedDeinit(superDD);
785+
}
786+
return false;
787+
} else {
788+
return false;
789+
}
790+
}
791+
775792
// ----------------------------------------------------------------------------
776793
// MARK: - FeatureSet
777794
// ----------------------------------------------------------------------------

lib/AST/FeatureSet.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class FeatureSet {
6868
/// not part of the enclosing context.
6969
FeatureSet getUniqueFeaturesUsed(Decl *decl);
7070

71+
bool usesFeatureIsolatedDeinit(const Decl *decl);
72+
7173
} // end namespace swift
7274

7375
#endif /* SWIFT_AST_FEATURES_H */

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4378,23 +4378,6 @@ ActorIsolation swift::determineClosureActorIsolation(
43784378
return checker.determineClosureIsolation(closure);
43794379
}
43804380

4381-
/// Determine whethere there is an explicit isolation attribute
4382-
/// of any kind.
4383-
static bool hasExplicitIsolationAttribute(const Decl *decl) {
4384-
if (auto nonisolatedAttr =
4385-
decl->getAttrs().getAttribute<NonisolatedAttr>()) {
4386-
if (!nonisolatedAttr->isImplicit())
4387-
return true;
4388-
}
4389-
4390-
if (auto globalActorAttr = decl->getGlobalActorAttr()) {
4391-
if (!globalActorAttr->first->isImplicit())
4392-
return true;
4393-
}
4394-
4395-
return false;
4396-
}
4397-
43984381
/// Determine actor isolation solely from attributes.
43994382
///
44004383
/// \returns the actor isolation determined from attributes alone (with no
@@ -5797,14 +5780,14 @@ bool swift::contextRequiresStrictConcurrencyChecking(
57975780
} else if (auto decl = dc->getAsDecl()) {
57985781
// If any isolation attributes are present, we're using concurrency
57995782
// features.
5800-
if (hasExplicitIsolationAttribute(decl))
5783+
if (decl->hasExplicitIsolationAttribute())
58015784
return true;
58025785

58035786
// Extensions of explicitly isolated types are using concurrency
58045787
// features.
58055788
if (auto *extension = dyn_cast<ExtensionDecl>(decl)) {
58065789
auto *nominal = extension->getExtendedNominal();
5807-
if (nominal && hasExplicitIsolationAttribute(nominal) &&
5790+
if (nominal && nominal->hasExplicitIsolationAttribute() &&
58085791
!getActorIsolation(nominal).preconcurrency())
58095792
return true;
58105793
}
@@ -5817,7 +5800,7 @@ bool swift::contextRequiresStrictConcurrencyChecking(
58175800
// If we're in an accessor declaration, also check the storage
58185801
// declaration.
58195802
if (auto accessor = dyn_cast<AccessorDecl>(decl)) {
5820-
if (hasExplicitIsolationAttribute(accessor->getStorage()))
5803+
if (accessor->getStorage()->hasExplicitIsolationAttribute())
58215804
return true;
58225805
}
58235806
}

0 commit comments

Comments
 (0)