Skip to content

Commit 9586cd7

Browse files
authored
Merge pull request #17278 from DougGregor/inherited-protos-recurse
[AST] Block recursion through ProtocolDecl::getInheritedProtocols().
2 parents 969fb35 + ac9b3cb commit 9586cd7

6 files changed

+15
-6
lines changed

include/swift/AST/Decl.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ class alignas(1 << DeclAlignInBits) Decl {
487487
HasValidatedLayout : 1
488488
);
489489

490-
SWIFT_INLINE_BITFIELD_FULL(ProtocolDecl, NominalTypeDecl, 1+1+1+1+1+1+1+2+8+16,
490+
SWIFT_INLINE_BITFIELD_FULL(ProtocolDecl, NominalTypeDecl, 1+1+1+1+1+1+1+2+8+16+1,
491491
/// Whether the \c RequiresClass bit is valid.
492492
RequiresClassValid : 1,
493493

@@ -520,7 +520,10 @@ class alignas(1 << DeclAlignInBits) Decl {
520520
KnownProtocol : 8, // '8' for speed. This only needs 6.
521521

522522
/// The number of requirements in the requirement signature.
523-
NumRequirementsInSignature : 16
523+
NumRequirementsInSignature : 16,
524+
525+
/// Whether we are currently computing inherited protocols.
526+
ComputingInheritedProtocols : 1
524527
);
525528

526529
SWIFT_INLINE_BITFIELD(ClassDecl, NominalTypeDecl, 1+2+1+2+1+3+1+1,

lib/AST/Decl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3170,6 +3170,7 @@ ProtocolDecl::ProtocolDecl(DeclContext *DC, SourceLoc ProtocolLoc,
31703170
Bits.ProtocolDecl.NumRequirementsInSignature = 0;
31713171
Bits.ProtocolDecl.HasMissingRequirements = false;
31723172
Bits.ProtocolDecl.KnownProtocol = 0;
3173+
Bits.ProtocolDecl.ComputingInheritedProtocols = false;
31733174
}
31743175

31753176
llvm::TinyPtrVector<ProtocolDecl *>
@@ -3180,6 +3181,10 @@ ProtocolDecl::getInheritedProtocols() const {
31803181
// We shouldn't need this, but it shows up in recursive invocations.
31813182
if (!isRequirementSignatureComputed()) {
31823183
SmallPtrSet<ProtocolDecl *, 4> known;
3184+
if (Bits.ProtocolDecl.ComputingInheritedProtocols) return result;
3185+
3186+
auto *self = const_cast<ProtocolDecl *>(this);
3187+
self->Bits.ProtocolDecl.ComputingInheritedProtocols = true;
31833188
for (unsigned index : indices(getInherited())) {
31843189
if (auto type = getInheritedType(index)) {
31853190
// Only protocols can appear in the inheritance clause
@@ -3195,6 +3200,7 @@ ProtocolDecl::getInheritedProtocols() const {
31953200
}
31963201
}
31973202
}
3203+
self->Bits.ProtocolDecl.ComputingInheritedProtocols = false;
31983204
return result;
31993205
}
32003206

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -emit-ir
8+
// RUN: not %target-swift-frontend %s -emit-ir
99
protocol b:Self.a&(Int
1010
protocol b{}

validation-test/compiler_crashers/28844-swift-typebase-getcanonicaltype.swift renamed to validation-test/compiler_crashers_fixed/28844-swift-typebase-getcanonicaltype.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -emit-ir
8+
// RUN: not %target-swift-frontend %s -emit-ir
99
protocol A:Self.a&( A

validation-test/compiler_crashers/28862-swift-protocoldecl-getinheritedprotocols-const.swift renamed to validation-test/compiler_crashers_fixed/28862-swift-protocoldecl-getinheritedprotocols-const.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -emit-ir
8+
// RUN: not %target-swift-frontend %s -emit-ir
99
protocol A:RangeReplaceableCollection&(t:A{extension{}a e:class a{}class a
1010
{{}lass a=_{case.a<a =nil??as?=
1111
:a<a
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -emit-ir
8+
// RUN: not %target-swift-frontend %s -emit-ir
99
protocol A:a
1010
& {}typealias a:A

0 commit comments

Comments
 (0)