Skip to content

Commit c81db8e

Browse files
committed
[AST] NonCopyableGenerics: Expand hasInverseMarking to support associated type declarations
This is required for `ASTPrinter` to wrap the protocol that has associated type with inverses in a feature block.
1 parent 68b49b6 commit c81db8e

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3933,6 +3933,10 @@ class AssociatedTypeDecl : public TypeDecl {
39333933
TypeDecl::getOverriddenDecl());
39343934
}
39353935

3936+
/// Determine whether this type has ~<target>` stated as
3937+
/// one of its inherited types.
3938+
InverseMarking::Mark hasInverseMarking(InvertibleProtocolKind target) const;
3939+
39363940
/// Retrieve the set of associated types overridden by this associated
39373941
/// type.
39383942
llvm::TinyPtrVector<AssociatedTypeDecl *> getOverriddenDecls() const;

lib/AST/ASTPrinter.cpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3382,23 +3382,40 @@ static bool hasInverse(
33823382
if (auto *extension = dyn_cast<ExtensionDecl>(decl)) {
33833383
if (auto *nominal = extension->getSelfNominalTypeDecl())
33843384
return hasInverse(nominal, ip, isRelevantInverse);
3385+
return false;
33853386
}
33863387

3387-
if (auto *TD = dyn_cast<TypeDecl>(decl))
3388-
return isRelevantInverse(TD->hasInverseMarking(ip));
3388+
auto hasInverseInType = [&](Type type) {
3389+
return type.findIf([&](Type type) -> bool {
3390+
if (auto *typeDecl = getTypeDecl(type))
3391+
return hasInverse(typeDecl, ip, isRelevantInverse);
3392+
return false;
3393+
});
3394+
};
33893395

3390-
if (auto value = dyn_cast<ValueDecl>(decl)) {
3391-
// Check for noncopyable types in the types of this declaration.
3392-
if (Type type = value->getInterfaceType()) {
3393-
bool foundInverse = type.findIf([&](Type type) -> bool {
3394-
if (auto *typeDecl = getTypeDecl(type))
3395-
return hasInverse(typeDecl, ip, isRelevantInverse);
3396-
return false;
3397-
});
3396+
if (auto *TD = dyn_cast<TypeDecl>(decl)) {
3397+
if (auto *alias = dyn_cast<TypeAliasDecl>(TD))
3398+
return hasInverseInType(alias->getUnderlyingType());
33983399

3399-
if (foundInverse)
3400+
if (auto *NTD = dyn_cast<NominalTypeDecl>(TD)) {
3401+
if (isRelevantInverse(NTD->hasInverseMarking(ip)))
34003402
return true;
34013403
}
3404+
3405+
if (auto *P = dyn_cast<ProtocolDecl>(TD)) {
3406+
// Check the protocol's associated types too.
3407+
return llvm::any_of(
3408+
P->getAssociatedTypeMembers(), [&](AssociatedTypeDecl *ATD) {
3409+
return isRelevantInverse(ATD->hasInverseMarking(ip));
3410+
});
3411+
}
3412+
3413+
return false;
3414+
}
3415+
3416+
if (auto *VD = dyn_cast<ValueDecl>(decl)) {
3417+
if (VD->hasInterfaceType())
3418+
return hasInverseInType(VD->getInterfaceType());
34023419
}
34033420

34043421
return false;

lib/AST/Decl.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6675,6 +6675,16 @@ bool NominalTypeDecl::hasMarking(InvertibleProtocolKind target) const {
66756675
return mark;
66766676
}
66776677

6678+
InverseMarking::Mark
6679+
AssociatedTypeDecl::hasInverseMarking(InvertibleProtocolKind target) const {
6680+
auto &ctx = getASTContext();
6681+
6682+
if (!ctx.LangOpts.hasFeature(Feature::NoncopyableGenerics))
6683+
return InverseMarking::Mark();
6684+
6685+
return findInverseInInheritance(getInherited(), target);
6686+
}
6687+
66786688
InverseMarking::Mark
66796689
NominalTypeDecl::hasInverseMarking(InvertibleProtocolKind target) const {
66806690
switch (target) {
@@ -6720,12 +6730,6 @@ NominalTypeDecl::hasInverseMarking(InvertibleProtocolKind target) const {
67206730
if (!gpList)
67216731
return InverseMarking::Mark();
67226732

6723-
auto isInverseTarget = [&](Type t) -> bool {
6724-
if (auto pct = t->getAs<ProtocolCompositionType>())
6725-
return pct->getInverses().contains(target);
6726-
return false;
6727-
};
6728-
67296733
llvm::SmallSet<GenericTypeParamDecl *, 4> params;
67306734

67316735
// Scan the inheritance clauses of generic parameters only for an inverse.

0 commit comments

Comments
 (0)