Skip to content

Commit bf71bbf

Browse files
authored
Merge pull request #72613 from tshortli/ncgenerics-typedthrows-condfails-6.0
[6.0] Fix condfails for TypedThrows, NoncopyableGenerics, and IsolatedAny
2 parents 7d89bbf + aa43d60 commit bf71bbf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+256
-125
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,13 @@ BridgedAlignmentAttr_createParsed(BridgedASTContext cContext,
483483
BridgedSourceLoc cAtLoc,
484484
BridgedSourceRange cRange, size_t cValue);
485485

486-
SWIFT_NAME("BridgedAllowFeatureSuppressionAttr.createParsed(_:atLoc:range:features:)")
486+
SWIFT_NAME("BridgedAllowFeatureSuppressionAttr.createParsed(_:atLoc:range:inverted:features:)")
487487
BridgedAllowFeatureSuppressionAttr
488488
BridgedAllowFeatureSuppressionAttr_createParsed(
489489
BridgedASTContext cContext,
490490
BridgedSourceLoc cAtLoc,
491491
BridgedSourceRange cRange,
492+
bool inverted,
492493
BridgedArrayRef cFeatures);
493494

494495
SWIFT_NAME("BridgedCDeclAttr.createParsed(_:atLoc:range:name:)")

include/swift/AST/Attr.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,11 @@ class DeclAttribute : public AttributeBase {
192192
isUnsafe : 1
193193
);
194194

195-
SWIFT_INLINE_BITFIELD_FULL(AllowFeatureSuppressionAttr, DeclAttribute, 32,
195+
SWIFT_INLINE_BITFIELD_FULL(AllowFeatureSuppressionAttr, DeclAttribute, 1+31,
196196
: NumPadBits,
197-
NumFeatures : 32
197+
Inverted : 1,
198+
199+
NumFeatures : 31
198200
);
199201
} Bits;
200202
// clang-format on
@@ -2628,14 +2630,17 @@ class AllowFeatureSuppressionAttr final
26282630
private llvm::TrailingObjects<AllowFeatureSuppressionAttr, Identifier> {
26292631
friend TrailingObjects;
26302632

2631-
/// Create an implicit @objc attribute with the given (optional) name.
2632-
AllowFeatureSuppressionAttr(SourceLoc atLoc, SourceRange range,
2633-
bool implicit, ArrayRef<Identifier> features);
2633+
AllowFeatureSuppressionAttr(SourceLoc atLoc, SourceRange range, bool implicit,
2634+
bool inverted, ArrayRef<Identifier> features);
2635+
26342636
public:
26352637
static AllowFeatureSuppressionAttr *create(ASTContext &ctx, SourceLoc atLoc,
26362638
SourceRange range, bool implicit,
2639+
bool inverted,
26372640
ArrayRef<Identifier> features);
26382641

2642+
bool getInverted() const { return Bits.AllowFeatureSuppressionAttr.Inverted; }
2643+
26392644
ArrayRef<Identifier> getSuppressedFeatures() const {
26402645
return {getTrailingObjects<Identifier>(),
26412646
Bits.AllowFeatureSuppressionAttr.NumFeatures};

include/swift/AST/DeclAttr.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ SIMPLE_DECL_ATTR(_noObjCBridging, NoObjCBridging,
489489
DECL_ATTR(_allowFeatureSuppression, AllowFeatureSuppression,
490490
OnAnyDecl | UserInaccessible | NotSerialized | ABIStableToAdd | APIStableToAdd | ABIStableToRemove | APIStableToRemove,
491491
157)
492+
DECL_ATTR_ALIAS(_disallowFeatureSuppression, AllowFeatureSuppression)
492493
SIMPLE_DECL_ATTR(_preInverseGenerics, PreInverseGenerics,
493494
OnAbstractFunction | OnSubscript | OnVar | OnExtension | UserInaccessible | ABIBreakingToAdd | ABIBreakingToRemove | APIStableToAdd | APIStableToRemove,
494495
158)

include/swift/AST/PrintOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ struct PrintOptions {
385385
/// Suppress Noncopyable generics.
386386
bool SuppressNoncopyableGenerics = false;
387387

388+
/// Suppress printing of `borrowing` and `consuming`.
389+
bool SuppressNoncopyableOwnershipModifiers = false;
390+
388391
/// List of attribute kinds that should not be printed.
389392
std::vector<AnyAttrKind> ExcludeAttrList = {
390393
DeclAttrKind::Transparent, DeclAttrKind::Effects,

include/swift/Basic/Features.def

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
// imply the existence of earlier features. (This only needs to apply to
3535
// suppressible features.)
3636
//
37+
// Sometimes, certain declarations will conflict with existing declarations
38+
// when printed with a suppressible feature disabled. The attribute
39+
// @_disallowFeatureSuppression can be used to suppress feature suppression on
40+
// a particular declaration.
41+
//
3742
// If suppressing a feature in general is problematic, but it's okay to
3843
// suppress it for specific declarations, the feature can be made
3944
// conditionally suppressible. Declarations opt in to suppression with
@@ -314,6 +319,9 @@ EXPERIMENTAL_FEATURE(Embedded, true)
314319
/// Enables noncopyable generics
315320
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(NoncopyableGenerics, true)
316321

322+
// Alias for NoncopyableGenerics
323+
EXPERIMENTAL_FEATURE(NoncopyableGenerics2, true)
324+
317325
/// Allow destructuring stored `let` bindings in structs.
318326
EXPERIMENTAL_FEATURE(StructLetDestructuring, true)
319327

@@ -353,6 +361,9 @@ EXPERIMENTAL_FEATURE(ClosureIsolation, true)
353361
// Enable isolated(any) attribute on function types.
354362
CONDITIONALLY_SUPPRESSIBLE_EXPERIMENTAL_FEATURE(IsolatedAny, true)
355363

364+
// Alias for IsolatedAny
365+
EXPERIMENTAL_FEATURE(IsolatedAny2, true)
366+
356367
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
357368
#undef EXPERIMENTAL_FEATURE
358369
#undef UPCOMING_FEATURE

include/swift/Parse/Parser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,8 @@ class Parser {
11571157
std::optional<AccessLevel> &Visibility);
11581158

11591159
ParserResult<AllowFeatureSuppressionAttr>
1160-
parseAllowFeatureSuppressionAttribute(SourceLoc atLoc, SourceLoc loc);
1160+
parseAllowFeatureSuppressionAttribute(bool inverted, SourceLoc atLoc,
1161+
SourceLoc loc);
11611162

11621163
/// Parse the @attached or @freestanding attribute that specifies a macro
11631164
/// role.

include/swift/Runtime/TracingCommon.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ static inline bool shouldEnableTracing() {
3333
return false;
3434
if (__progname && (strcmp(__progname, "logd") == 0 ||
3535
strcmp(__progname, "diagnosticd") == 0 ||
36-
strcmp(__progname, "notifyd") == 0) ||
37-
strcmp(__progname, "xpcproxy") == 0)
36+
strcmp(__progname, "notifyd") == 0 ||
37+
strcmp(__progname, "xpcproxy") == 0))
3838
return false;
3939
return true;
4040
}

lib/AST/ASTBridging.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,12 +433,14 @@ BridgedAllowFeatureSuppressionAttr
433433
BridgedAllowFeatureSuppressionAttr_createParsed(BridgedASTContext cContext,
434434
BridgedSourceLoc cAtLoc,
435435
BridgedSourceRange cRange,
436+
bool inverted,
436437
BridgedArrayRef cFeatures) {
437438
SmallVector<Identifier> features;
438439
for (auto elem : cFeatures.unbridged<BridgedIdentifier>())
439440
features.push_back(elem.unbridged());
440-
return AllowFeatureSuppressionAttr::create(cContext.unbridged(),
441-
cAtLoc.unbridged(), cRange.unbridged(), /*implicit*/ false, features);
441+
return AllowFeatureSuppressionAttr::create(
442+
cContext.unbridged(), cAtLoc.unbridged(), cRange.unbridged(),
443+
/*implicit*/ false, inverted, features);
442444
}
443445

444446
BridgedCDeclAttr BridgedCDeclAttr_createParsed(BridgedASTContext cContext,

lib/AST/ASTPrinter.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,16 @@ class PrintAST : public ASTVisitor<PrintAST> {
11541154
Printer.callPrintDeclPre(D, Options.BracketOptions);
11551155

11561156
if (Options.PrintCompatibilityFeatureChecks) {
1157-
printWithCompatibilityFeatureChecks(Printer, Options, D, [&]{
1157+
printWithCompatibilityFeatureChecks(Printer, Options, D, [&] {
1158+
// If we are in a scope where non-copyable generics are being suppressed
1159+
// and we are also printing a decl that has @_preInverseGenerics, make
1160+
// sure we also suppress printing ownership modifiers that were added
1161+
// to satisfy the requirements of non-copyability.
1162+
llvm::SaveAndRestore<bool> scope(
1163+
Options.SuppressNoncopyableOwnershipModifiers,
1164+
Options.SuppressNoncopyableGenerics &&
1165+
D->getAttrs().hasAttribute<PreInverseGenericsAttr>());
1166+
11581167
ASTVisitor::visit(D);
11591168
});
11601169
} else {
@@ -3139,9 +3148,12 @@ static void suppressingFeatureAssociatedTypeImplements(PrintOptions &options,
31393148
static void suppressingFeatureNoncopyableGenerics(
31403149
PrintOptions &options,
31413150
llvm::function_ref<void()> action) {
3151+
unsigned originalExcludeAttrCount = options.ExcludeAttrList.size();
3152+
options.ExcludeAttrList.push_back(DeclAttrKind::PreInverseGenerics);
31423153
llvm::SaveAndRestore<bool> scope(
31433154
options.SuppressNoncopyableGenerics, true);
31443155
action();
3156+
options.ExcludeAttrList.resize(originalExcludeAttrCount);
31453157
}
31463158

31473159
/// Suppress the printing of a particular feature.
@@ -3696,10 +3708,14 @@ static void printParameterFlags(ASTPrinter &printer,
36963708
printer.printKeyword("inout", options, " ");
36973709
break;
36983710
case ParamSpecifier::Borrowing:
3699-
printer.printKeyword("borrowing", options, " ");
3711+
if (!options.SuppressNoncopyableOwnershipModifiers) {
3712+
printer.printKeyword("borrowing", options, " ");
3713+
}
37003714
break;
37013715
case ParamSpecifier::Consuming:
3702-
printer.printKeyword("consuming", options, " ");
3716+
if (!options.SuppressNoncopyableOwnershipModifiers) {
3717+
printer.printKeyword("consuming", options, " ");
3718+
}
37033719
break;
37043720
case ParamSpecifier::LegacyShared:
37053721
printer.printKeyword("__shared", options, " ");

lib/AST/Attr.cpp

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,14 +1275,17 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
12751275
Printer << "(" << cast<AlignmentAttr>(this)->getValue() << ")";
12761276
break;
12771277

1278-
case DeclAttrKind::AllowFeatureSuppression:
1279-
Printer.printAttrName("@_allowFeatureSuppression");
1278+
case DeclAttrKind::AllowFeatureSuppression: {
1279+
auto Attr = cast<AllowFeatureSuppressionAttr>(this);
1280+
Printer.printAttrName(Attr->getInverted() ? "@_disallowFeatureSuppression"
1281+
: "@_allowFeatureSuppression");
12801282
Printer << "(";
1281-
interleave(cast<AllowFeatureSuppressionAttr>(this)->getSuppressedFeatures(),
1282-
[&](Identifier ident) { Printer << ident; },
1283-
[&] { Printer << ", "; });
1283+
interleave(
1284+
Attr->getSuppressedFeatures(),
1285+
[&](Identifier ident) { Printer << ident; }, [&] { Printer << ", "; });
12841286
Printer << ")";
12851287
break;
1288+
}
12861289

12871290
case DeclAttrKind::SILGenName:
12881291
Printer.printAttrName("@_silgen_name");
@@ -1947,7 +1950,11 @@ StringRef DeclAttribute::getAttrName() const {
19471950
case DeclAttrKind::Extern:
19481951
return "_extern";
19491952
case DeclAttrKind::AllowFeatureSuppression:
1950-
return "_allowFeatureSuppression";
1953+
if (cast<AllowFeatureSuppressionAttr>(this)->getInverted()) {
1954+
return "_disallowFeatureSuppression";
1955+
} else {
1956+
return "_allowFeatureSuppression";
1957+
}
19511958
}
19521959
llvm_unreachable("bad DeclAttrKind");
19531960
}
@@ -2932,24 +2939,24 @@ StorageRestrictionsAttr::getAccessesProperties(AccessorDecl *attachedTo) const {
29322939
{});
29332940
}
29342941

2935-
AllowFeatureSuppressionAttr::AllowFeatureSuppressionAttr(SourceLoc atLoc,
2936-
SourceRange range,
2937-
bool implicit,
2938-
ArrayRef<Identifier> features)
2939-
: DeclAttribute(DeclAttrKind::AllowFeatureSuppression,
2940-
atLoc, range, implicit) {
2942+
AllowFeatureSuppressionAttr::AllowFeatureSuppressionAttr(
2943+
SourceLoc atLoc, SourceRange range, bool implicit, bool inverted,
2944+
ArrayRef<Identifier> features)
2945+
: DeclAttribute(DeclAttrKind::AllowFeatureSuppression, atLoc, range,
2946+
implicit) {
2947+
Bits.AllowFeatureSuppressionAttr.Inverted = inverted;
29412948
Bits.AllowFeatureSuppressionAttr.NumFeatures = features.size();
29422949
std::uninitialized_copy(features.begin(), features.end(),
29432950
getTrailingObjects<Identifier>());
29442951
}
29452952

2946-
AllowFeatureSuppressionAttr *
2947-
AllowFeatureSuppressionAttr::create(ASTContext &ctx, SourceLoc atLoc,
2948-
SourceRange range, bool implicit,
2949-
ArrayRef<Identifier> features) {
2953+
AllowFeatureSuppressionAttr *AllowFeatureSuppressionAttr::create(
2954+
ASTContext &ctx, SourceLoc atLoc, SourceRange range, bool implicit,
2955+
bool inverted, ArrayRef<Identifier> features) {
29502956
unsigned size = totalSizeToAlloc<Identifier>(features.size());
29512957
auto *mem = ctx.Allocate(size, alignof(AllowFeatureSuppressionAttr));
2952-
return new (mem) AllowFeatureSuppressionAttr(atLoc, range, implicit, features);
2958+
return new (mem)
2959+
AllowFeatureSuppressionAttr(atLoc, range, implicit, inverted, features);
29532960
}
29542961

29552962
void swift::simple_display(llvm::raw_ostream &out, const DeclAttribute *attr) {

lib/AST/FeatureSet.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,9 @@ static bool usesFeatureRawLayout(Decl *decl) {
506506
UNINTERESTING_FEATURE(Embedded)
507507

508508
static bool usesFeatureNoncopyableGenerics(Decl *decl) {
509+
if (decl->getAttrs().hasAttribute<PreInverseGenericsAttr>())
510+
return true;
511+
509512
if (auto *valueDecl = dyn_cast<ValueDecl>(decl)) {
510513
if (isa<StructDecl, EnumDecl, ClassDecl>(decl)) {
511514
auto *nominalDecl = cast<NominalTypeDecl>(valueDecl);
@@ -568,6 +571,8 @@ static bool usesFeatureNoncopyableGenerics(Decl *decl) {
568571
return !inverseReqs.empty();
569572
}
570573

574+
UNINTERESTING_FEATURE(NoncopyableGenerics2)
575+
571576
static bool usesFeatureStructLetDestructuring(Decl *decl) {
572577
auto sd = dyn_cast<StructDecl>(decl);
573578
if (!sd)
@@ -676,6 +681,8 @@ static bool usesFeatureIsolatedAny(Decl *decl) {
676681
});
677682
}
678683

684+
UNINTERESTING_FEATURE(IsolatedAny2)
685+
679686
// ----------------------------------------------------------------------------
680687
// MARK: - FeatureSet
681688
// ----------------------------------------------------------------------------
@@ -691,9 +698,14 @@ void FeatureSet::collectSuppressibleFeature(Feature feature,
691698
operation == Insert);
692699
}
693700

694-
static bool shouldSuppressFeature(StringRef featureName, Decl *decl) {
701+
static bool hasFeatureSuppressionAttribute(Decl *decl, StringRef featureName,
702+
bool inverted) {
695703
auto attr = decl->getAttrs().getAttribute<AllowFeatureSuppressionAttr>();
696-
if (!attr) return false;
704+
if (!attr)
705+
return false;
706+
707+
if (attr->getInverted() != inverted)
708+
return false;
697709

698710
for (auto suppressedFeature : attr->getSuppressedFeatures()) {
699711
if (suppressedFeature.is(featureName))
@@ -703,6 +715,14 @@ static bool shouldSuppressFeature(StringRef featureName, Decl *decl) {
703715
return false;
704716
}
705717

718+
static bool disallowFeatureSuppression(StringRef featureName, Decl *decl) {
719+
return hasFeatureSuppressionAttribute(decl, featureName, true);
720+
}
721+
722+
static bool allowFeatureSuppression(StringRef featureName, Decl *decl) {
723+
return hasFeatureSuppressionAttribute(decl, featureName, false);
724+
}
725+
706726
/// Go through all the features used by the given declaration and
707727
/// either add or remove them to this set.
708728
void FeatureSet::collectFeaturesUsed(Decl *decl, InsertOrRemove operation) {
@@ -712,11 +732,15 @@ void FeatureSet::collectFeaturesUsed(Decl *decl, InsertOrRemove operation) {
712732
if (usesFeature##FeatureName(decl)) \
713733
collectRequiredFeature(Feature::FeatureName, operation);
714734
#define SUPPRESSIBLE_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
715-
if (usesFeature##FeatureName(decl)) \
716-
collectSuppressibleFeature(Feature::FeatureName, operation);
735+
if (usesFeature##FeatureName(decl)) { \
736+
if (disallowFeatureSuppression(#FeatureName, decl)) \
737+
collectRequiredFeature(Feature::FeatureName, operation); \
738+
else \
739+
collectSuppressibleFeature(Feature::FeatureName, operation); \
740+
}
717741
#define CONDITIONALLY_SUPPRESSIBLE_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
718742
if (usesFeature##FeatureName(decl)) { \
719-
if (shouldSuppressFeature(#FeatureName, decl)) \
743+
if (allowFeatureSuppression(#FeatureName, decl)) \
720744
collectSuppressibleFeature(Feature::FeatureName, operation); \
721745
else \
722746
collectRequiredFeature(Feature::FeatureName, operation); \

lib/ASTGen/Sources/ASTGen/DeclAttrs.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,16 @@ extension ASTGenVisitor {
345345
return nil
346346
}
347347

348+
let inverted: Bool
349+
switch node.attributeName {
350+
case "_allowFeatureSuppression":
351+
inverted = false
352+
case "_disallowFeatureSuppression":
353+
inverted = true
354+
default:
355+
return nil
356+
}
357+
348358
let features = args.compactMap(in: self) { arg -> BridgedIdentifier? in
349359
guard arg.label == nil,
350360
let declNameExpr = arg.expression.as(DeclReferenceExprSyntax.self),
@@ -361,6 +371,7 @@ extension ASTGenVisitor {
361371
self.ctx,
362372
atLoc: self.generateSourceLoc(node.atSign),
363373
range: self.generateSourceRange(node),
374+
inverted: inverted,
364375
features: features)
365376
}
366377

@@ -388,7 +399,7 @@ extension ASTGenVisitor {
388399

389400
func generateAvailableAttr(attribute node: AttributeSyntax) -> [BridgedAvailableAttr] {
390401
guard
391-
// `@_OriginallyDefinedIn` has special argument list syntax.
402+
// `@available` has special argument list syntax.
392403
let args = node.arguments?.as(AvailabilityArgumentListSyntax.self)
393404
else {
394405
// TODO: Diagnose.

lib/Frontend/CompilerInvocation.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,12 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
883883
#else
884884
Opts.enableFeature(*feature);
885885
#endif
886+
887+
if (*feature == Feature::NoncopyableGenerics2)
888+
Opts.enableFeature(Feature::NoncopyableGenerics);
889+
890+
if (*feature == Feature::IsolatedAny2)
891+
Opts.enableFeature(Feature::IsolatedAny);
886892
}
887893

888894
// Hack: In order to support using availability macros in SPM packages, we

0 commit comments

Comments
 (0)