Skip to content

Commit b7117aa

Browse files
committed
AST: Start untangling ModuleDecl from conformance lookup
1 parent 252abb5 commit b7117aa

File tree

4 files changed

+41
-43
lines changed

4 files changed

+41
-43
lines changed

include/swift/AST/Module.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -886,8 +886,8 @@ class ModuleDecl
886886
///
887887
/// \returns An invalid conformance if the search failed, otherwise an
888888
/// abstract, concrete or pack conformance, depending on the lookup type.
889-
ProtocolConformanceRef lookupConformance(Type type, ProtocolDecl *protocol,
890-
bool allowMissing = false);
889+
static ProtocolConformanceRef lookupConformance(Type type, ProtocolDecl *protocol,
890+
bool allowMissing = false);
891891

892892
/// Global conformance lookup, checks conditional requirements.
893893
/// Requires a contextualized type.
@@ -903,9 +903,10 @@ class ModuleDecl
903903
///
904904
/// \returns An invalid conformance if the search failed, otherwise an
905905
/// abstract, concrete or pack conformance, depending on the lookup type.
906-
ProtocolConformanceRef checkConformance(Type type, ProtocolDecl *protocol,
907-
// Note: different default from lookupConformance
908-
bool allowMissing = true);
906+
static ProtocolConformanceRef checkConformance(Type type, ProtocolDecl *protocol,
907+
// Note: different default from
908+
// lookupConformance
909+
bool allowMissing = true);
909910

910911
/// Global conformance lookup, checks conditional requirements.
911912
/// Accepts interface types without context. If the conformance cannot be
@@ -925,22 +926,23 @@ class ModuleDecl
925926
/// if the search succeeded. `std::nullopt` if the type could have
926927
/// conditionally conformed depending on the context of the interface types.
927928
std::optional<ProtocolConformanceRef>
928-
checkConformanceWithoutContext(Type type,
929-
ProtocolDecl *protocol,
930-
// Note: different default from lookupConformance
931-
bool allowMissing = true);
929+
static checkConformanceWithoutContext(Type type,
930+
ProtocolDecl *protocol,
931+
// Note: different default from
932+
// lookupConformance
933+
bool allowMissing = true);
932934

933935

934936
/// Look for the conformance of the given existential type to the given
935937
/// protocol.
936-
ProtocolConformanceRef lookupExistentialConformance(Type type,
937-
ProtocolDecl *protocol);
938+
static ProtocolConformanceRef lookupExistentialConformance(Type type,
939+
ProtocolDecl *protocol);
938940

939941
/// Collect the conformances of \c fromType to each of the protocols of an
940942
/// existential type's layout.
941943
ArrayRef<ProtocolConformanceRef>
942-
collectExistentialConformances(CanType fromType, CanType existential,
943-
bool allowMissing = false);
944+
static collectExistentialConformances(CanType fromType, CanType existential,
945+
bool allowMissing = false);
944946

945947
/// Find a member named \p name in \p container that was declared in this
946948
/// module.

include/swift/AST/NameLookupRequests.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -732,21 +732,18 @@ class DirectPrecedenceGroupLookupRequest
732732

733733
class LookupConformanceDescriptor final {
734734
public:
735-
ModuleDecl *Mod;
736735
Type Ty;
737736
ProtocolDecl *PD;
738737

739-
LookupConformanceDescriptor(ModuleDecl *Mod, Type Ty, ProtocolDecl *PD)
740-
: Mod(Mod), Ty(Ty), PD(PD) {}
738+
LookupConformanceDescriptor(Type Ty, ProtocolDecl *PD) : Ty(Ty), PD(PD) {}
741739

742740
friend llvm::hash_code hash_value(const LookupConformanceDescriptor &desc) {
743-
return llvm::hash_combine(desc.Mod, desc.Ty.getPointer(), desc.PD);
741+
return llvm::hash_combine(desc.Ty.getPointer(), desc.PD);
744742
}
745743

746744
friend bool operator==(const LookupConformanceDescriptor &lhs,
747745
const LookupConformanceDescriptor &rhs) {
748-
return lhs.Mod == rhs.Mod && lhs.Ty.getPointer() == rhs.Ty.getPointer() &&
749-
lhs.PD == rhs.PD;
746+
return lhs.Ty.getPointer() == rhs.Ty.getPointer() && lhs.PD == rhs.PD;
750747
}
751748

752749
friend bool operator!=(const LookupConformanceDescriptor &lhs,

lib/AST/ConformanceLookup.cpp

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ ModuleDecl::collectExistentialConformances(CanType fromType,
6262
conformances.push_back(conformance);
6363
}
6464

65-
return getASTContext().AllocateCopy(conformances);
65+
return fromType->getASTContext().AllocateCopy(conformances);
6666
}
6767

6868
ProtocolConformanceRef
6969
ModuleDecl::lookupExistentialConformance(Type type, ProtocolDecl *protocol) {
70-
ASTContext &ctx = getASTContext();
70+
ASTContext &ctx = protocol->getASTContext();
7171

7272
assert(type->isExistentialType());
7373

@@ -177,22 +177,24 @@ ProtocolConformanceRef ProtocolConformanceRef::forMissingOrInvalid(
177177
ProtocolConformanceRef ModuleDecl::lookupConformance(Type type,
178178
ProtocolDecl *protocol,
179179
bool allowMissing) {
180+
auto &eval = protocol->getASTContext().evaluator;
181+
180182
// If we are recursively checking for implicit conformance of a nominal
181183
// type to a KnownProtocol, fail without evaluating this request. This
182184
// squashes cycles.
183-
LookupConformanceInModuleRequest request{{this, type, protocol}};
185+
LookupConformanceInModuleRequest request{{type, protocol}};
184186
if (auto kp = protocol->getKnownProtocolKind()) {
185187
if (auto nominal = type->getAnyNominal()) {
186188
ImplicitKnownProtocolConformanceRequest icvRequest{nominal, *kp};
187-
if (getASTContext().evaluator.hasActiveRequest(icvRequest) ||
188-
getASTContext().evaluator.hasActiveRequest(request)) {
189+
if (eval.hasActiveRequest(icvRequest) ||
190+
eval.hasActiveRequest(request)) {
189191
return ProtocolConformanceRef::forInvalid();
190192
}
191193
}
192194
}
193195

194196
auto result = evaluateOrDefault(
195-
getASTContext().evaluator, request, ProtocolConformanceRef::forInvalid());
197+
eval, request, ProtocolConformanceRef::forInvalid());
196198

197199
// If we aren't supposed to allow missing conformances but we have one,
198200
// replace the result with an "invalid" result.
@@ -207,8 +209,7 @@ ProtocolConformanceRef ModuleDecl::lookupConformance(Type type,
207209
/// Synthesize a builtin tuple type conformance to the given protocol, if
208210
/// appropriate.
209211
static ProtocolConformanceRef getBuiltinTupleTypeConformance(
210-
Type type, const TupleType *tupleType, ProtocolDecl *protocol,
211-
ModuleDecl *module) {
212+
Type type, const TupleType *tupleType, ProtocolDecl *protocol) {
212213
ASTContext &ctx = protocol->getASTContext();
213214

214215
auto *tupleDecl = ctx.getBuiltinTupleDecl();
@@ -238,7 +239,7 @@ static ProtocolConformanceRef getBuiltinTupleTypeConformance(
238239
}
239240

240241
auto *conformance = cast<NormalProtocolConformance>(conformances.front());
241-
auto subMap = type->getContextSubstitutionMap(module,
242+
auto subMap = type->getContextSubstitutionMap(protocol->getParentModule(),
242243
conformance->getDeclContext());
243244

244245
// TODO: labels
@@ -432,7 +433,7 @@ getBuiltinBuiltinTypeConformance(Type type, const BuiltinType *builtinType,
432433
}
433434

434435
static ProtocolConformanceRef getPackTypeConformance(
435-
PackType *type, ProtocolDecl *protocol, ModuleDecl *mod) {
436+
PackType *type, ProtocolDecl *protocol) {
436437
SmallVector<ProtocolConformanceRef, 2> patternConformances;
437438

438439
for (auto packElement : type->getElementTypes()) {
@@ -442,17 +443,17 @@ static ProtocolConformanceRef getPackTypeConformance(
442443
auto patternConformance =
443444
(patternType->isTypeParameter()
444445
? ProtocolConformanceRef(protocol)
445-
: mod->lookupConformance(patternType, protocol,
446-
/*allowMissing=*/true));
446+
: ModuleDecl::lookupConformance(patternType, protocol,
447+
/*allowMissing=*/true));
447448
patternConformances.push_back(patternConformance);
448449
continue;
449450
}
450451

451452
auto patternConformance =
452453
(packElement->isTypeParameter()
453454
? ProtocolConformanceRef(protocol)
454-
: mod->lookupConformance(packElement, protocol,
455-
/*allowMissing=*/true));
455+
: ModuleDecl::lookupConformance(packElement, protocol,
456+
/*allowMissing=*/true));
456457
patternConformances.push_back(patternConformance);
457458
}
458459

@@ -463,10 +464,9 @@ static ProtocolConformanceRef getPackTypeConformance(
463464
ProtocolConformanceRef
464465
LookupConformanceInModuleRequest::evaluate(
465466
Evaluator &evaluator, LookupConformanceDescriptor desc) const {
466-
auto *mod = desc.Mod;
467467
auto type = desc.Ty;
468468
auto *protocol = desc.PD;
469-
ASTContext &ctx = mod->getASTContext();
469+
ASTContext &ctx = protocol->getASTContext();
470470

471471
// Remove SIL reference ownership wrapper, if present.
472472
type = type->getReferenceStorageReferent();
@@ -491,7 +491,7 @@ LookupConformanceInModuleRequest::evaluate(
491491
// able to be resolved by a substitution that makes the archetype
492492
// concrete.
493493
if (auto super = archetype->getSuperclass()) {
494-
auto inheritedConformance = mod->lookupConformance(
494+
auto inheritedConformance = ModuleDecl::lookupConformance(
495495
super, protocol, /*allowMissing=*/false);
496496
if (protocol->isSpecificProtocol(KnownProtocolKind::Sendable) &&
497497
inheritedConformance.hasUnavailableConformance())
@@ -514,7 +514,7 @@ LookupConformanceInModuleRequest::evaluate(
514514
// existential's list of conformances and the existential conforms to
515515
// itself.
516516
if (type->isExistentialType()) {
517-
auto result = mod->lookupExistentialConformance(type, protocol);
517+
auto result = ModuleDecl::lookupExistentialConformance(type, protocol);
518518
if (result.isInvalid())
519519
return ProtocolConformanceRef::forMissingOrInvalid(type, protocol);
520520
return result;
@@ -532,12 +532,12 @@ LookupConformanceInModuleRequest::evaluate(
532532

533533
// Pack types can conform to protocols.
534534
if (auto packType = type->getAs<PackType>()) {
535-
return getPackTypeConformance(packType, protocol, mod);
535+
return getPackTypeConformance(packType, protocol);
536536
}
537537

538538
// Tuple types can conform to protocols.
539539
if (auto tupleType = type->getAs<TupleType>()) {
540-
return getBuiltinTupleTypeConformance(type, tupleType, protocol, mod);
540+
return getBuiltinTupleTypeConformance(type, tupleType, protocol);
541541
}
542542

543543
// Function types can conform to protocols.
@@ -688,7 +688,7 @@ LookupConformanceInModuleRequest::evaluate(
688688
auto superclassTy = type->getSuperclassForDecl(conformingClass);
689689

690690
// Compute the conformance for the inherited type.
691-
auto inheritedConformance = mod->lookupConformance(
691+
auto inheritedConformance = ModuleDecl::lookupConformance(
692692
superclassTy, protocol, /*allowMissing=*/true);
693693
assert(inheritedConformance &&
694694
"We already found the inherited conformance");
@@ -728,7 +728,8 @@ LookupConformanceInModuleRequest::evaluate(
728728
conformanceDC = conformanceDC->getSelfNominalTypeDecl();
729729
}
730730

731-
auto subMap = type->getContextSubstitutionMap(mod, conformanceDC);
731+
auto subMap = type->getContextSubstitutionMap(protocol->getParentModule(),
732+
conformanceDC);
732733
return ProtocolConformanceRef(
733734
ctx.getSpecializedConformance(type, normalConf, subMap));
734735
}

lib/AST/NameLookupRequests.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,6 @@ void swift::simple_display(llvm::raw_ostream &out,
399399
simple_display(out, desc.PD);
400400
out << " for ";
401401
out << desc.Ty.getString();
402-
out << " in ";
403-
simple_display(out, desc.Mod);
404402
}
405403

406404
//----------------------------------------------------------------------------//

0 commit comments

Comments
 (0)