Skip to content

Commit 8f2f9d9

Browse files
Merge pull request #65132 from AnthonyLatsis/fukamushicha
[NFC] ExistentialRequiresAnyRequest → HasSelfOrAssociatedTypeRequirementsRequest
2 parents 804716c + 6a7f04c commit 8f2f9d9

File tree

9 files changed

+59
-46
lines changed

9 files changed

+59
-46
lines changed

include/swift/AST/Decl.h

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -553,11 +553,12 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
553553
/// Whether the existential of this protocol conforms to itself.
554554
ExistentialConformsToSelf : 1,
555555

556-
/// Whether the \c ExistentialRequiresAny bit is valid.
557-
ExistentialRequiresAnyValid : 1,
556+
/// Whether the \c HasSelfOrAssociatedTypeRequirements bit is valid.
557+
HasSelfOrAssociatedTypeRequirementsValid : 1,
558558

559-
/// Whether the existential of this protocol must be spelled with \c any.
560-
ExistentialRequiresAny : 1,
559+
/// Whether this protocol has \c Self or associated type requirements.
560+
/// See \c hasSelfOrAssociatedTypeRequirements() for clarification.
561+
HasSelfOrAssociatedTypeRequirements : 1,
561562

562563
/// True if the protocol has requirements that cannot be satisfied (e.g.
563564
/// because they could not be imported from Objective-C).
@@ -4756,19 +4757,19 @@ class ProtocolDecl final : public NominalTypeDecl {
47564757
Bits.ProtocolDecl.ExistentialConformsToSelf = result;
47574758
}
47584759

4759-
/// Returns the cached result of \c existentialRequiresAny or \c None if it
4760-
/// hasn't yet been computed.
4761-
Optional<bool> getCachedExistentialRequiresAny() {
4762-
if (Bits.ProtocolDecl.ExistentialRequiresAnyValid)
4763-
return Bits.ProtocolDecl.ExistentialRequiresAny;
4760+
/// Returns the cached result of \c hasSelfOrAssociatedTypeRequirements or
4761+
/// \c None if it hasn't yet been computed.
4762+
Optional<bool> getCachedHasSelfOrAssociatedTypeRequirements() {
4763+
if (Bits.ProtocolDecl.HasSelfOrAssociatedTypeRequirementsValid)
4764+
return Bits.ProtocolDecl.HasSelfOrAssociatedTypeRequirements;
47644765

47654766
return None;
47664767
}
47674768

4768-
/// Caches the result of \c existentialRequiresAny
4769-
void setCachedExistentialRequiresAny(bool requiresAny) {
4770-
Bits.ProtocolDecl.ExistentialRequiresAnyValid = true;
4771-
Bits.ProtocolDecl.ExistentialRequiresAny = requiresAny;
4769+
/// Caches the result of \c hasSelfOrAssociatedTypeRequirements
4770+
void setCachedHasSelfOrAssociatedTypeRequirements(bool value) {
4771+
Bits.ProtocolDecl.HasSelfOrAssociatedTypeRequirementsValid = true;
4772+
Bits.ProtocolDecl.HasSelfOrAssociatedTypeRequirements = value;
47724773
}
47734774

47744775
bool hasLazyRequirementSignature() const {
@@ -4785,11 +4786,9 @@ class ProtocolDecl final : public NominalTypeDecl {
47854786
friend class TypeAliasRequirementsRequest;
47864787
friend class ProtocolDependenciesRequest;
47874788
friend class RequirementSignatureRequest;
4788-
friend class RequirementSignatureRequestRQM;
4789-
friend class RequirementSignatureRequestGSB;
47904789
friend class ProtocolRequiresClassRequest;
47914790
friend class ExistentialConformsToSelfRequest;
4792-
friend class ExistentialRequiresAnyRequest;
4791+
friend class HasSelfOrAssociatedTypeRequirementsRequest;
47934792
friend class InheritedProtocolsRequest;
47944793
friend class PrimaryAssociatedTypesRequest;
47954794
friend class ProtocolRequirementsRequest;
@@ -4888,15 +4887,20 @@ class ProtocolDecl final : public NominalTypeDecl {
48884887
/// Does this protocol require a self-conformance witness table?
48894888
bool requiresSelfConformanceWitnessTable() const;
48904889

4891-
/// Determine whether an existential type must be explicitly prefixed
4892-
/// with \c any. \c any is required if one of the following conditions is met
4893-
/// for this protocol or an inherited protocol:
4890+
/// Determine whether this protocol has `Self` or associated type
4891+
/// requirements.
4892+
///
4893+
/// This is true if one of the following conditions is met for this protocol
4894+
/// or an inherited protocol:
48944895
/// - The protocol has an associated type requirement.
48954896
/// - `Self` appears in non-covariant position in the type signature of a
48964897
/// value requirement.
4898+
bool hasSelfOrAssociatedTypeRequirements() const;
4899+
4900+
/// Determine whether an existential type constrained by this protocol must
4901+
/// be written using `any` syntax.
48974902
///
4898-
/// @Note This method does not take the state of language features into
4899-
/// account.
4903+
/// \Note This method takes language feature state into account.
49004904
bool existentialRequiresAny() const;
49014905

49024906
/// Returns a list of protocol requirements that must be assessed to

include/swift/AST/TypeCheckRequests.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,8 @@ class ExistentialConformsToSelfRequest :
300300

301301
/// Determine whether an existential type conforming to this protocol
302302
/// requires the \c any syntax.
303-
class ExistentialRequiresAnyRequest :
304-
public SimpleRequest<ExistentialRequiresAnyRequest,
303+
class HasSelfOrAssociatedTypeRequirementsRequest :
304+
public SimpleRequest<HasSelfOrAssociatedTypeRequirementsRequest,
305305
bool(ProtocolDecl *),
306306
RequestFlags::SeparatelyCached> {
307307
public:

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ SWIFT_REQUEST(TypeChecker, EnumRawTypeRequest,
9595
Type(EnumDecl *), Cached, NoLocationInfo)
9696
SWIFT_REQUEST(TypeChecker, ExistentialConformsToSelfRequest,
9797
bool(ProtocolDecl *), SeparatelyCached, NoLocationInfo)
98-
SWIFT_REQUEST(TypeChecker, ExistentialRequiresAnyRequest,
98+
SWIFT_REQUEST(TypeChecker, HasSelfOrAssociatedTypeRequirementsRequest,
9999
bool(ProtocolDecl *), SeparatelyCached, NoLocationInfo)
100100
SWIFT_REQUEST(TypeChecker, ExtendedTypeRequest, Type(ExtensionDecl *), Cached,
101101
NoLocationInfo)

lib/AST/Decl.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6018,9 +6018,18 @@ bool ProtocolDecl::existentialConformsToSelf() const {
60186018
ExistentialConformsToSelfRequest{const_cast<ProtocolDecl *>(this)}, true);
60196019
}
60206020

6021-
bool ProtocolDecl::existentialRequiresAny() const {
6021+
bool ProtocolDecl::hasSelfOrAssociatedTypeRequirements() const {
60226022
return evaluateOrDefault(getASTContext().evaluator,
6023-
ExistentialRequiresAnyRequest{const_cast<ProtocolDecl *>(this)}, true);
6023+
HasSelfOrAssociatedTypeRequirementsRequest{
6024+
const_cast<ProtocolDecl *>(this)},
6025+
true);
6026+
}
6027+
6028+
bool ProtocolDecl::existentialRequiresAny() const {
6029+
if (getASTContext().LangOpts.hasFeature(Feature::ExistentialAny))
6030+
return true;
6031+
6032+
return hasSelfOrAssociatedTypeRequirements();
60246033
}
60256034

60266035
ArrayRef<AssociatedTypeDecl *>

lib/AST/TypeCheckRequests.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,28 +263,31 @@ void ExistentialConformsToSelfRequest::cacheResult(bool value) const {
263263
}
264264

265265
//----------------------------------------------------------------------------//
266-
// existentialRequiresAny computation.
266+
// hasSelfOrAssociatedTypeRequirementsRequest computation.
267267
//----------------------------------------------------------------------------//
268268

269-
void ExistentialRequiresAnyRequest::diagnoseCycle(DiagnosticEngine &diags) const {
269+
void HasSelfOrAssociatedTypeRequirementsRequest::diagnoseCycle(
270+
DiagnosticEngine &diags) const {
270271
auto decl = std::get<0>(getStorage());
271272
diags.diagnose(decl, diag::circular_protocol_def, decl->getName());
272273
}
273274

274-
void ExistentialRequiresAnyRequest::noteCycleStep(DiagnosticEngine &diags) const {
275+
void HasSelfOrAssociatedTypeRequirementsRequest::noteCycleStep(
276+
DiagnosticEngine &diags) const {
275277
auto requirement = std::get<0>(getStorage());
276278
diags.diagnose(requirement, diag::kind_declname_declared_here,
277279
DescriptiveDeclKind::Protocol, requirement->getName());
278280
}
279281

280-
Optional<bool> ExistentialRequiresAnyRequest::getCachedResult() const {
282+
Optional<bool>
283+
HasSelfOrAssociatedTypeRequirementsRequest::getCachedResult() const {
281284
auto decl = std::get<0>(getStorage());
282-
return decl->getCachedExistentialRequiresAny();
285+
return decl->getCachedHasSelfOrAssociatedTypeRequirements();
283286
}
284287

285-
void ExistentialRequiresAnyRequest::cacheResult(bool value) const {
288+
void HasSelfOrAssociatedTypeRequirementsRequest::cacheResult(bool value) const {
286289
auto decl = std::get<0>(getStorage());
287-
decl->setCachedExistentialRequiresAny(value);
290+
decl->setCachedHasSelfOrAssociatedTypeRequirements(value);
288291
}
289292

290293
//----------------------------------------------------------------------------//

lib/Sema/TypeCheckDecl.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -711,9 +711,8 @@ ExistentialConformsToSelfRequest::evaluate(Evaluator &evaluator,
711711
return true;
712712
}
713713

714-
bool
715-
ExistentialRequiresAnyRequest::evaluate(Evaluator &evaluator,
716-
ProtocolDecl *decl) const {
714+
bool HasSelfOrAssociatedTypeRequirementsRequest::evaluate(
715+
Evaluator &evaluator, ProtocolDecl *decl) const {
717716
// ObjC protocols do not require `any`.
718717
if (decl->isObjC())
719718
return false;
@@ -736,7 +735,7 @@ ExistentialRequiresAnyRequest::evaluate(Evaluator &evaluator,
736735

737736
// Check whether any of the inherited protocols require `any`.
738737
for (auto proto : decl->getInheritedProtocols()) {
739-
if (proto->existentialRequiresAny())
738+
if (proto->hasSelfOrAssociatedTypeRequirements())
740739
return true;
741740
}
742741

lib/Sema/TypeCheckType.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5202,8 +5202,7 @@ class ExistentialTypeVisitor
52025202
OS << ")";
52035203

52045204
if (auto *proto = dyn_cast_or_null<ProtocolDecl>(T->getBoundDecl())) {
5205-
if (Ctx.LangOpts.hasFeature(Feature::ExistentialAny) ||
5206-
proto->existentialRequiresAny()) {
5205+
if (proto->existentialRequiresAny()) {
52075206
Ctx.Diags.diagnose(T->getNameLoc(),
52085207
diag::existential_requires_any,
52095208
proto->getDeclaredInterfaceType(),
@@ -5221,8 +5220,7 @@ class ExistentialTypeVisitor
52215220
if (type->isConstraintType()) {
52225221
auto layout = type->getExistentialLayout();
52235222
for (auto *protoDecl : layout.getProtocols()) {
5224-
if (!Ctx.LangOpts.hasFeature(Feature::ExistentialAny) &&
5225-
!protoDecl->existentialRequiresAny())
5223+
if (!protoDecl->existentialRequiresAny())
52265224
continue;
52275225

52285226
Ctx.Diags.diagnose(T->getNameLoc(),

lib/Serialization/Deserialization.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3995,14 +3995,14 @@ class DeclDeserializer {
39953995
StringRef blobData) {
39963996
IdentifierID nameID;
39973997
DeclContextID contextID;
3998-
bool isImplicit, isClassBounded, isObjC, existentialRequiresAny;
3998+
bool isImplicit, isClassBounded, isObjC, hasSelfOrAssocTypeRequirements;
39993999
uint8_t rawAccessLevel;
40004000
unsigned numInheritedTypes;
40014001
ArrayRef<uint64_t> rawInheritedAndDependencyIDs;
40024002

40034003
decls_block::ProtocolLayout::readRecord(scratch, nameID, contextID,
40044004
isImplicit, isClassBounded, isObjC,
4005-
existentialRequiresAny,
4005+
hasSelfOrAssocTypeRequirements,
40064006
rawAccessLevel, numInheritedTypes,
40074007
rawInheritedAndDependencyIDs);
40084008

@@ -4030,8 +4030,8 @@ class DeclDeserializer {
40304030

40314031
ctx.evaluator.cacheOutput(ProtocolRequiresClassRequest{proto},
40324032
std::move(isClassBounded));
4033-
ctx.evaluator.cacheOutput(ExistentialRequiresAnyRequest{proto},
4034-
std::move(existentialRequiresAny));
4033+
ctx.evaluator.cacheOutput(HasSelfOrAssociatedTypeRequirementsRequest{proto},
4034+
std::move(hasSelfOrAssocTypeRequirements));
40354035

40364036
if (auto accessLevel = getActualAccessLevel(rawAccessLevel))
40374037
proto->setAccess(*accessLevel);

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4090,7 +4090,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
40904090
const_cast<ProtocolDecl *>(proto)
40914091
->requiresClass(),
40924092
proto->isObjC(),
4093-
proto->existentialRequiresAny(),
4093+
proto->hasSelfOrAssociatedTypeRequirements(),
40944094
rawAccessLevel, numInherited,
40954095
inheritedAndDependencyTypes);
40964096

0 commit comments

Comments
 (0)