Skip to content

Commit c25d623

Browse files
committed
AST: Simplify ValueDecl::findExistentialSelfReferences()
1 parent d3da1b8 commit c25d623

File tree

5 files changed

+26
-33
lines changed

5 files changed

+26
-33
lines changed

include/swift/AST/Decl.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3335,9 +3335,8 @@ class ValueDecl : public Decl {
33353335
/// that this declaration dynamically replaces.
33363336
ValueDecl *getDynamicallyReplacedDecl() const;
33373337

3338-
/// Find references to 'Self' in the type signature of this declaration in the
3339-
/// context of the given existential base type.
3340-
GenericParameterReferenceInfo findExistentialSelfReferences(Type baseTy) const;
3338+
/// Find references to 'Self' in the type signature of this declaration.
3339+
GenericParameterReferenceInfo findExistentialSelfReferences() const;
33413340
};
33423341

33433342
/// This is a common base class for declarations which declare a type.

lib/AST/Decl.cpp

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5125,7 +5125,8 @@ swift::findGenericParameterReferences(const ValueDecl *value,
51255125
GenericTypeParamType *origParam,
51265126
GenericTypeParamType *openedParam,
51275127
std::optional<unsigned> skipParamIndex) {
5128-
assert(!isa<TypeDecl>(value));
5128+
if (isa<TypeDecl>(value))
5129+
return GenericParameterReferenceInfo();
51295130

51305131
auto type = value->getInterfaceType();
51315132

@@ -5150,26 +5151,13 @@ swift::findGenericParameterReferences(const ValueDecl *value,
51505151
/*canBeCovariantResult=*/true);
51515152
}
51525153

5153-
GenericParameterReferenceInfo ValueDecl::findExistentialSelfReferences(
5154-
Type baseTy) const {
5155-
assert(baseTy->isExistentialType());
5156-
assert(!baseTy->hasTypeParameter());
5157-
5158-
// Type declarations don't really have type signatures.
5159-
if (isa<TypeDecl>(this))
5160-
return GenericParameterReferenceInfo();
5161-
5162-
// Skip invalid declarations.
5163-
if (getInterfaceType()->hasError())
5164-
return GenericParameterReferenceInfo();
5154+
GenericParameterReferenceInfo ValueDecl::findExistentialSelfReferences() const {
5155+
auto *dc = getDeclContext();
5156+
ASSERT(dc->getSelfProtocolDecl());
51655157

5166-
// Note: a non-null GenericSignature would violate the invariant that
5167-
// the protocol 'Self' type referenced from the requirement's interface
5168-
// type is the same as the existential 'Self' type.
5169-
auto sig = getASTContext().getOpenedExistentialSignature(baseTy,
5170-
GenericSignature());
5158+
auto sig = dc->getGenericSignatureOfContext().getCanonicalSignature();
5159+
auto genericParam = dc->getSelfInterfaceType()->castTo<GenericTypeParamType>();
51715160

5172-
auto genericParam = sig.getGenericParams().front();
51735161
return findGenericParameterReferences(this, sig, genericParam, genericParam,
51745162
std::nullopt);
51755163
}

lib/Sema/ConstraintSystem.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7544,11 +7544,20 @@ bool ConstraintSystem::isMemberAvailableOnExistential(
75447544

75457545
// If the type of the member references 'Self' or a 'Self'-rooted associated
75467546
// type in non-covariant position, we cannot reference the member.
7547-
//
7548-
// N.B. We pass the module context because this check does not care about the
7549-
// the actual signature of the opened archetype in context, rather it cares
7550-
// about whether you can "hold" `baseTy.member` properly in the abstract.
7551-
const auto info = member->findExistentialSelfReferences(baseTy);
7547+
assert(baseTy->isExistentialType());
7548+
assert(!baseTy->hasTypeParameter());
7549+
7550+
// Note: a non-null GenericSignature would violate the invariant that
7551+
// the protocol 'Self' type referenced from the requirement's interface
7552+
// type is the same as the existential 'Self' type.
7553+
auto sig = getASTContext().getOpenedExistentialSignature(baseTy,
7554+
GenericSignature());
7555+
7556+
auto genericParam = sig.getGenericParams().front();
7557+
auto info = findGenericParameterReferences(
7558+
member, sig, genericParam, genericParam,
7559+
std::nullopt);
7560+
75527561
if (info.selfRef > TypePosition::Covariant ||
75537562
info.assocTypeRef > TypePosition::Covariant) {
75547563
return false;

lib/Sema/TypeCheckDecl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,8 +733,7 @@ bool HasSelfOrAssociatedTypeRequirementsRequest::evaluate(
733733

734734
// For value members, look at their type signatures.
735735
if (auto valueMember = dyn_cast<ValueDecl>(member)) {
736-
const auto info = valueMember->findExistentialSelfReferences(
737-
decl->getDeclaredInterfaceType());
736+
const auto info = valueMember->findExistentialSelfReferences();
738737
if (info.selfRef > TypePosition::Covariant || info.assocTypeRef) {
739738
return true;
740739
}

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,8 +1132,7 @@ swift::matchWitness(WitnessChecker::RequirementEnvironmentCache &reqEnvCache,
11321132
// defining a non-final class conforming to 'Collection' which uses
11331133
// the default witness for 'Collection.Iterator', which is defined
11341134
// as 'IndexingIterator<Self>'.
1135-
const auto selfRefInfo = req->findExistentialSelfReferences(
1136-
proto->getDeclaredInterfaceType());
1135+
const auto selfRefInfo = req->findExistentialSelfReferences();
11371136
if (!selfRefInfo.assocTypeRef) {
11381137
covariantSelf = classDecl;
11391138
}
@@ -4035,8 +4034,7 @@ void ConformanceChecker::checkNonFinalClassWitness(ValueDecl *requirement,
40354034

40364035
// Check whether this requirement uses Self in a way that might
40374036
// prevent conformance from succeeding.
4038-
const auto selfRefInfo = requirement->findExistentialSelfReferences(
4039-
Proto->getDeclaredInterfaceType());
4037+
const auto selfRefInfo = requirement->findExistentialSelfReferences();
40404038

40414039
if (selfRefInfo.selfRef == TypePosition::Invariant ||
40424040
(selfRefInfo.selfRef == TypePosition::Covariant &&

0 commit comments

Comments
 (0)