Skip to content

Commit ab0ffe3

Browse files
authored
Merge pull request #74998 from slavapestov/global-conformance-lookup
AST: Stop pretending that conformance lookup takes the module into account
2 parents b9149e9 + ca9c09f commit ab0ffe3

File tree

161 files changed

+724
-1024
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+724
-1024
lines changed

include/swift/AST/DistributedDecl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ getDistributedActorAsActorConformanceRef(ASTContext &C);
118118
/// the DistributedActor-as-Actor conformance.
119119
ExtensionDecl *
120120
findDistributedActorAsActorExtension(
121-
ProtocolDecl *distributedActorProto, ModuleDecl *module);
121+
ProtocolDecl *distributedActorProto);
122122

123123
bool isDistributedActorAsLocalActorComputedProperty(VarDecl *var);
124124

include/swift/AST/GenericSignature.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,6 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
322322
SmallVector<Requirement, 2> &reqs,
323323
SmallVector<InverseRequirement, 2> &inverses) const;
324324

325-
/// Look up a stored conformance in the generic signature. These are formed
326-
/// from same-type constraints placed on associated types of generic
327-
/// parameters which have conformance constraints on them.
328-
ProtocolConformanceRef lookupConformance(CanType depTy,
329-
ProtocolDecl *proto) const;
330-
331325
/// Iterate over all generic parameters, passing a flag to the callback
332326
/// indicating if the generic parameter is canonical or not.
333327
void forEachParam(

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,

include/swift/AST/Requirement.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,7 @@ checkRequirementsWithoutContext(ArrayRef<Requirement> requirements);
241241
/// Check if each requirement is satisfied after applying the given
242242
/// substitutions. The substitutions must replace all type parameters that
243243
/// appear in the requirement with concrete types or archetypes.
244-
CheckRequirementsResult checkRequirements(ModuleDecl *module,
245-
ArrayRef<Requirement> requirements,
244+
CheckRequirementsResult checkRequirements(ArrayRef<Requirement> requirements,
246245
TypeSubstitutionFn substitutions,
247246
SubstOptions options = std::nullopt);
248247

include/swift/AST/Type.h

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,8 @@ using LookupConformanceFn = llvm::function_ref<GenericFunction>;
9797
/// Functor class suitable for use as a \c LookupConformanceFn to look up a
9898
/// conformance through a module.
9999
class LookUpConformanceInModule {
100-
ModuleDecl *M;
101100
public:
102-
explicit LookUpConformanceInModule(ModuleDecl *M)
103-
: M(M) {}
101+
explicit LookUpConformanceInModule() {}
104102

105103
ProtocolConformanceRef operator()(CanType dependentType,
106104
Type conformingReplacementType,
@@ -116,21 +114,6 @@ class MakeAbstractConformanceForGenericType {
116114
Type conformingReplacementType,
117115
ProtocolDecl *conformedProtocol) const;
118116
};
119-
120-
/// Functor class suitable for use as a \c LookupConformanceFn that fetches
121-
/// conformances from a generic signature.
122-
class LookUpConformanceInSignature {
123-
const GenericSignatureImpl *Sig;
124-
public:
125-
LookUpConformanceInSignature(const GenericSignatureImpl *Sig)
126-
: Sig(Sig) {
127-
assert(Sig && "Cannot lookup conformance in null signature!");
128-
}
129-
130-
ProtocolConformanceRef operator()(CanType dependentType,
131-
Type conformingReplacementType,
132-
ProtocolDecl *conformedProtocol) const;
133-
};
134117

135118
/// Flags that can be passed when substituting into a type.
136119
enum class SubstFlags {

include/swift/AST/Types.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,8 +1309,7 @@ class alignas(1 << TypeAlignInBits) TypeBase
13091309
/// \param genericEnv If non-null and the type is nested inside of a
13101310
/// generic function, generic parameters of the outer context are
13111311
/// mapped to context archetypes of this generic environment.
1312-
SubstitutionMap getContextSubstitutionMap(ModuleDecl *module,
1313-
const DeclContext *dc,
1312+
SubstitutionMap getContextSubstitutionMap(const DeclContext *dc,
13141313
GenericEnvironment *genericEnv=nullptr);
13151314

13161315
/// Deprecated version of the above.
@@ -1322,8 +1321,7 @@ class alignas(1 << TypeAlignInBits) TypeBase
13221321
///
13231322
/// \param genericEnv If non-null, generic parameters of the member are
13241323
/// mapped to context archetypes of this generic environment.
1325-
SubstitutionMap getMemberSubstitutionMap(ModuleDecl *module,
1326-
const ValueDecl *member,
1324+
SubstitutionMap getMemberSubstitutionMap(const ValueDecl *member,
13271325
GenericEnvironment *genericEnv=nullptr);
13281326

13291327
/// Deprecated version of the above.
@@ -1340,7 +1338,7 @@ class alignas(1 << TypeAlignInBits) TypeBase
13401338
/// \param member The property whose type we are substituting.
13411339
///
13421340
/// \returns The resulting property type.
1343-
Type getTypeOfMember(ModuleDecl *module, const VarDecl *member);
1341+
Type getTypeOfMember(const VarDecl *member);
13441342

13451343
/// Retrieve the type of the given member as seen through the given base
13461344
/// type, substituting generic arguments where necessary.
@@ -1371,8 +1369,7 @@ class alignas(1 << TypeAlignInBits) TypeBase
13711369
/// method's generic parameters.
13721370
///
13731371
/// \returns The resulting member type.
1374-
Type getTypeOfMember(ModuleDecl *module, const ValueDecl *member,
1375-
Type memberType);
1372+
Type getTypeOfMember(const ValueDecl *member, Type memberType);
13761373

13771374
/// Get the type of a superclass member as seen from the subclass,
13781375
/// substituting generic parameters, dynamic Self return, and the
@@ -7075,7 +7072,7 @@ class DependentMemberType : public TypeBase {
70757072
/// Substitute the base type, looking up our associated type in it if it is
70767073
/// non-dependent. Returns null if the member could not be found in the new
70777074
/// base.
7078-
Type substBaseType(ModuleDecl *M, Type base);
7075+
Type substBaseType(Type base);
70797076

70807077
/// Substitute the base type, looking up our associated type in it if it is
70817078
/// non-dependent. Returns null if the member could not be found in the new

include/swift/SIL/DynamicCasts.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,14 @@ void emitIndirectConditionalCastWithScalar(
109109
ProfileCounter FalseCount = ProfileCounter());
110110

111111
/// Does the type conform to the _ObjectiveCBridgeable protocol.
112-
bool isObjectiveCBridgeable(ModuleDecl *M, CanType Ty);
112+
bool isObjectiveCBridgeable(CanType Ty);
113113

114114
/// Get the bridged NS class of a CF class if it exists. Returns
115115
/// an empty CanType if such class does not exist.
116-
CanType getNSBridgedClassOfCFClass(ModuleDecl *M, CanType type);
116+
CanType getNSBridgedClassOfCFClass(CanType type);
117117

118118
/// Does the type conform to Error.
119-
bool isError(ModuleDecl *M, CanType Ty);
119+
bool isError(CanType Ty);
120120

121121
struct SILDynamicCastKind {
122122
enum innerty : std::underlying_type<SILInstructionKind>::type {

include/swift/SIL/SILCloner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct SubstitutionMapWithLocalArchetypes {
5050
Type substType,
5151
ProtocolDecl *proto) {
5252
if (isa<LocalArchetypeType>(origType))
53-
return proto->getParentModule()->lookupConformance(substType, proto);
53+
return ModuleDecl::lookupConformance(substType, proto);
5454
if (SubsMap)
5555
return SubsMap->lookupConformance(origType, proto);
5656

include/swift/SIL/TypeSubstCloner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
331331
remappedOrigFnType->getDifferentiabilityResultIndices(),
332332
dfei->getDerivativeFunctionKind(),
333333
getBuilder().getModule().Types,
334-
LookUpConformanceInModule(SwiftMod))
334+
LookUpConformanceInModule())
335335
->getWithoutDifferentiability();
336336
SILType remappedDerivativeFnType = getOpType(dfei->getType());
337337
// If remapped derivative type and derivative remapped type are equal, do

include/swift/SILOptimizer/Analysis/DifferentiableActivityAnalysis.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ class DifferentiableActivityInfo {
139139
type->mapTypeOutOfContext());
140140
}
141141
// Look up conformance in the current module.
142-
auto lookupConformance =
143-
LookUpConformanceInModule(getFunction().getModule().getSwiftModule());
142+
auto lookupConformance = LookUpConformanceInModule();
144143
return type->getAutoDiffTangentSpace(lookupConformance).has_value();
145144
}
146145

include/swift/SILOptimizer/OptimizerBridgingImpl.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,7 @@ void BridgedPassContext::loadFunction(BridgedFunction function, bool loadCallees
416416
BridgedSubstitutionMap BridgedPassContext::getContextSubstitutionMap(BridgedType type) const {
417417
swift::SILType ty = type.unbridged();
418418
auto *ntd = ty.getASTType()->getAnyNominal();
419-
auto *mod = invocation->getPassManager()->getModule()->getSwiftModule();
420-
return ty.getASTType()->getContextSubstitutionMap(mod, ntd);
419+
return ty.getASTType()->getContextSubstitutionMap(ntd);
421420
}
422421

423422
BridgedType BridgedPassContext::getBuiltinIntegerType(SwiftInt bitWidth) const {

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,7 @@ ASTContext::getBuiltinInitDecl(NominalTypeDecl *decl,
15311531

15321532
auto type = decl->getDeclaredInterfaceType();
15331533
auto builtinProtocol = getProtocol(builtinProtocolKind);
1534-
auto builtinConformance = getStdlibModule()->lookupConformance(
1534+
auto builtinConformance = ModuleDecl::lookupConformance(
15351535
type, builtinProtocol);
15361536
if (builtinConformance.isInvalid()) {
15371537
assert(false && "Missing required conformance");
@@ -1561,7 +1561,7 @@ ConcreteDeclRef ASTContext::getRegexInitDecl(Type regexType) const {
15611561
results);
15621562
assert(results.size() == 1);
15631563
auto *foundDecl = cast<ConstructorDecl>(results[0]);
1564-
auto subs = regexType->getMemberSubstitutionMap(spModule, foundDecl);
1564+
auto subs = regexType->getMemberSubstitutionMap(foundDecl);
15651565
return ConcreteDeclRef(foundDecl, subs);
15661566
}
15671567

@@ -5671,7 +5671,7 @@ ASTContext::getForeignRepresentationInfo(NominalTypeDecl *nominal,
56715671
if (nominal != dc->getASTContext().getOptionalDecl()) {
56725672
if (auto objcBridgeable
56735673
= getProtocol(KnownProtocolKind::ObjectiveCBridgeable)) {
5674-
auto conformance = dc->getParentModule()->lookupConformance(
5674+
auto conformance = ModuleDecl::lookupConformance(
56755675
nominal->getDeclaredInterfaceType(), objcBridgeable);
56765676
if (conformance) {
56775677
result =
@@ -5823,7 +5823,7 @@ Type ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
58235823
if (!proto)
58245824
return ProtocolConformanceRef::forInvalid();
58255825

5826-
return dc->getParentModule()->lookupConformance(type, proto);
5826+
return ModuleDecl::lookupConformance(type, proto);
58275827
};
58285828

58295829
// Do we conform to _ObjectiveCBridgeable?

lib/AST/ASTDemangler.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ Type ASTBuilder::createTypeAliasType(GenericTypeDecl *decl, Type parent) {
199199
return declaredType;
200200

201201
auto *dc = aliasDecl->getDeclContext();
202-
auto subs = parent->getContextSubstitutionMap(dc->getParentModule(), dc);
202+
auto subs = parent->getContextSubstitutionMap(dc);
203203

204204
return declaredType.subst(subs);
205205
}
@@ -239,7 +239,7 @@ Type ASTBuilder::createBoundGenericType(GenericTypeDecl *decl,
239239
// Build a SubstitutionMap.
240240
auto genericSig = nominalDecl->getGenericSignature();
241241
auto subs = createSubstitutionMapFromGenericArgs(
242-
genericSig, args, LookUpConformanceInModule(decl->getParentModule()));
242+
genericSig, args, LookUpConformanceInModule());
243243
if (!subs)
244244
return Type();
245245
auto origType = nominalDecl->getDeclaredInterfaceType();
@@ -278,7 +278,7 @@ Type ASTBuilder::resolveOpaqueType(NodePointer opaqueDescriptor,
278278

279279
SubstitutionMap subs = createSubstitutionMapFromGenericArgs(
280280
opaqueDecl->getGenericSignature(), allArgs,
281-
LookUpConformanceInModule(parentModule));
281+
LookUpConformanceInModule());
282282
Type interfaceType = opaqueDecl->getOpaqueGenericParams()[ordinal];
283283
return OpaqueTypeArchetypeType::get(opaqueDecl, interfaceType, subs);
284284
}
@@ -318,11 +318,9 @@ Type ASTBuilder::createBoundGenericType(GenericTypeDecl *decl,
318318
substTy;
319319
}
320320

321-
// FIXME: This is the wrong module
322-
auto *moduleDecl = decl->getParentModule();
323321
auto subMap = SubstitutionMap::get(genericSig,
324322
QueryTypeSubstitutionMap{subs},
325-
LookUpConformanceInModule(moduleDecl));
323+
LookUpConformanceInModule());
326324
if (!subMap)
327325
return Type();
328326

@@ -955,7 +953,7 @@ Type ASTBuilder::createSILBoxTypeWithLayout(
955953
if (signature)
956954
substs = createSubstitutionMapFromGenericArgs(
957955
signature, replacements,
958-
LookUpConformanceInSignature(signature.getPointer()));
956+
LookUpConformanceInModule());
959957
return SILBoxType::get(Ctx, layout, substs);
960958
}
961959

@@ -1054,7 +1052,7 @@ SubstitutionMap
10541052
ASTBuilder::createSubstitutionMap(BuiltGenericSignature sig,
10551053
ArrayRef<BuiltType> replacements) {
10561054
return SubstitutionMap::get(sig, replacements,
1057-
LookUpConformanceInSignature(sig.getPointer()));
1055+
LookUpConformanceInModule());
10581056
}
10591057

10601058
Type ASTBuilder::subst(Type subject, const BuiltSubstitutionMap &Subs) const {

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,9 +1969,8 @@ void ASTMangler::appendRetroactiveConformances(SubstitutionMap subMap,
19691969
void ASTMangler::appendRetroactiveConformances(Type type, GenericSignature sig) {
19701970
// Dig out the substitution map to use.
19711971
SubstitutionMap subMap;
1972-
ModuleDecl *module;
1972+
19731973
if (auto typeAlias = dyn_cast<TypeAliasType>(type.getPointer())) {
1974-
module = Mod ? Mod : typeAlias->getDecl()->getModuleContext();
19751974
subMap = typeAlias->getSubstitutionMap();
19761975
} else {
19771976
if (type->hasUnboundGenericType())
@@ -1980,8 +1979,7 @@ void ASTMangler::appendRetroactiveConformances(Type type, GenericSignature sig)
19801979
auto nominal = type->getAnyNominal();
19811980
if (!nominal) return;
19821981

1983-
module = Mod ? Mod : nominal->getModuleContext();
1984-
subMap = type->getContextSubstitutionMap(module, nominal);
1982+
subMap = type->getContextSubstitutionMap(nominal);
19851983
}
19861984

19871985
appendRetroactiveConformances(subMap, sig);

0 commit comments

Comments
 (0)