Skip to content

AST: Stop pretending that conformance lookup takes the module into account #74998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/DistributedDecl.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ getDistributedActorAsActorConformanceRef(ASTContext &C);
/// the DistributedActor-as-Actor conformance.
ExtensionDecl *
findDistributedActorAsActorExtension(
ProtocolDecl *distributedActorProto, ModuleDecl *module);
ProtocolDecl *distributedActorProto);

bool isDistributedActorAsLocalActorComputedProperty(VarDecl *var);

Expand Down
6 changes: 0 additions & 6 deletions include/swift/AST/GenericSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,6 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
SmallVector<Requirement, 2> &reqs,
SmallVector<InverseRequirement, 2> &inverses) const;

/// Look up a stored conformance in the generic signature. These are formed
/// from same-type constraints placed on associated types of generic
/// parameters which have conformance constraints on them.
ProtocolConformanceRef lookupConformance(CanType depTy,
ProtocolDecl *proto) const;

/// Iterate over all generic parameters, passing a flag to the callback
/// indicating if the generic parameter is canonical or not.
void forEachParam(
Expand Down
28 changes: 15 additions & 13 deletions include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -886,8 +886,8 @@ class ModuleDecl
///
/// \returns An invalid conformance if the search failed, otherwise an
/// abstract, concrete or pack conformance, depending on the lookup type.
ProtocolConformanceRef lookupConformance(Type type, ProtocolDecl *protocol,
bool allowMissing = false);
static ProtocolConformanceRef lookupConformance(Type type, ProtocolDecl *protocol,
bool allowMissing = false);

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

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


/// Look for the conformance of the given existential type to the given
/// protocol.
ProtocolConformanceRef lookupExistentialConformance(Type type,
ProtocolDecl *protocol);
static ProtocolConformanceRef lookupExistentialConformance(Type type,
ProtocolDecl *protocol);

/// Collect the conformances of \c fromType to each of the protocols of an
/// existential type's layout.
ArrayRef<ProtocolConformanceRef>
collectExistentialConformances(CanType fromType, CanType existential,
bool allowMissing = false);
static collectExistentialConformances(CanType fromType, CanType existential,
bool allowMissing = false);

/// Find a member named \p name in \p container that was declared in this
/// module.
Expand Down
9 changes: 3 additions & 6 deletions include/swift/AST/NameLookupRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -732,21 +732,18 @@ class DirectPrecedenceGroupLookupRequest

class LookupConformanceDescriptor final {
public:
ModuleDecl *Mod;
Type Ty;
ProtocolDecl *PD;

LookupConformanceDescriptor(ModuleDecl *Mod, Type Ty, ProtocolDecl *PD)
: Mod(Mod), Ty(Ty), PD(PD) {}
LookupConformanceDescriptor(Type Ty, ProtocolDecl *PD) : Ty(Ty), PD(PD) {}

friend llvm::hash_code hash_value(const LookupConformanceDescriptor &desc) {
return llvm::hash_combine(desc.Mod, desc.Ty.getPointer(), desc.PD);
return llvm::hash_combine(desc.Ty.getPointer(), desc.PD);
}

friend bool operator==(const LookupConformanceDescriptor &lhs,
const LookupConformanceDescriptor &rhs) {
return lhs.Mod == rhs.Mod && lhs.Ty.getPointer() == rhs.Ty.getPointer() &&
lhs.PD == rhs.PD;
return lhs.Ty.getPointer() == rhs.Ty.getPointer() && lhs.PD == rhs.PD;
}

friend bool operator!=(const LookupConformanceDescriptor &lhs,
Expand Down
3 changes: 1 addition & 2 deletions include/swift/AST/Requirement.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ checkRequirementsWithoutContext(ArrayRef<Requirement> requirements);
/// Check if each requirement is satisfied after applying the given
/// substitutions. The substitutions must replace all type parameters that
/// appear in the requirement with concrete types or archetypes.
CheckRequirementsResult checkRequirements(ModuleDecl *module,
ArrayRef<Requirement> requirements,
CheckRequirementsResult checkRequirements(ArrayRef<Requirement> requirements,
TypeSubstitutionFn substitutions,
SubstOptions options = std::nullopt);

Expand Down
19 changes: 1 addition & 18 deletions include/swift/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,8 @@ using LookupConformanceFn = llvm::function_ref<GenericFunction>;
/// Functor class suitable for use as a \c LookupConformanceFn to look up a
/// conformance through a module.
class LookUpConformanceInModule {
ModuleDecl *M;
public:
explicit LookUpConformanceInModule(ModuleDecl *M)
: M(M) {}
explicit LookUpConformanceInModule() {}

ProtocolConformanceRef operator()(CanType dependentType,
Type conformingReplacementType,
Expand All @@ -116,21 +114,6 @@ class MakeAbstractConformanceForGenericType {
Type conformingReplacementType,
ProtocolDecl *conformedProtocol) const;
};

/// Functor class suitable for use as a \c LookupConformanceFn that fetches
/// conformances from a generic signature.
class LookUpConformanceInSignature {
const GenericSignatureImpl *Sig;
public:
LookUpConformanceInSignature(const GenericSignatureImpl *Sig)
: Sig(Sig) {
assert(Sig && "Cannot lookup conformance in null signature!");
}

ProtocolConformanceRef operator()(CanType dependentType,
Type conformingReplacementType,
ProtocolDecl *conformedProtocol) const;
};

/// Flags that can be passed when substituting into a type.
enum class SubstFlags {
Expand Down
13 changes: 5 additions & 8 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1309,8 +1309,7 @@ class alignas(1 << TypeAlignInBits) TypeBase
/// \param genericEnv If non-null and the type is nested inside of a
/// generic function, generic parameters of the outer context are
/// mapped to context archetypes of this generic environment.
SubstitutionMap getContextSubstitutionMap(ModuleDecl *module,
const DeclContext *dc,
SubstitutionMap getContextSubstitutionMap(const DeclContext *dc,
GenericEnvironment *genericEnv=nullptr);

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

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

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

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

/// Substitute the base type, looking up our associated type in it if it is
/// non-dependent. Returns null if the member could not be found in the new
Expand Down
6 changes: 3 additions & 3 deletions include/swift/SIL/DynamicCasts.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ void emitIndirectConditionalCastWithScalar(
ProfileCounter FalseCount = ProfileCounter());

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

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

/// Does the type conform to Error.
bool isError(ModuleDecl *M, CanType Ty);
bool isError(CanType Ty);

struct SILDynamicCastKind {
enum innerty : std::underlying_type<SILInstructionKind>::type {
Expand Down
2 changes: 1 addition & 1 deletion include/swift/SIL/SILCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct SubstitutionMapWithLocalArchetypes {
Type substType,
ProtocolDecl *proto) {
if (isa<LocalArchetypeType>(origType))
return proto->getParentModule()->lookupConformance(substType, proto);
return ModuleDecl::lookupConformance(substType, proto);
if (SubsMap)
return SubsMap->lookupConformance(origType, proto);

Expand Down
2 changes: 1 addition & 1 deletion include/swift/SIL/TypeSubstCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class TypeSubstCloner : public SILClonerWithScopes<ImplClass> {
remappedOrigFnType->getDifferentiabilityResultIndices(),
dfei->getDerivativeFunctionKind(),
getBuilder().getModule().Types,
LookUpConformanceInModule(SwiftMod))
LookUpConformanceInModule())
->getWithoutDifferentiability();
SILType remappedDerivativeFnType = getOpType(dfei->getType());
// If remapped derivative type and derivative remapped type are equal, do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ class DifferentiableActivityInfo {
type->mapTypeOutOfContext());
}
// Look up conformance in the current module.
auto lookupConformance =
LookUpConformanceInModule(getFunction().getModule().getSwiftModule());
auto lookupConformance = LookUpConformanceInModule();
return type->getAutoDiffTangentSpace(lookupConformance).has_value();
}

Expand Down
3 changes: 1 addition & 2 deletions include/swift/SILOptimizer/OptimizerBridgingImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,7 @@ void BridgedPassContext::loadFunction(BridgedFunction function, bool loadCallees
BridgedSubstitutionMap BridgedPassContext::getContextSubstitutionMap(BridgedType type) const {
swift::SILType ty = type.unbridged();
auto *ntd = ty.getASTType()->getAnyNominal();
auto *mod = invocation->getPassManager()->getModule()->getSwiftModule();
return ty.getASTType()->getContextSubstitutionMap(mod, ntd);
return ty.getASTType()->getContextSubstitutionMap(ntd);
}

BridgedType BridgedPassContext::getBuiltinIntegerType(SwiftInt bitWidth) const {
Expand Down
8 changes: 4 additions & 4 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1531,7 +1531,7 @@ ASTContext::getBuiltinInitDecl(NominalTypeDecl *decl,

auto type = decl->getDeclaredInterfaceType();
auto builtinProtocol = getProtocol(builtinProtocolKind);
auto builtinConformance = getStdlibModule()->lookupConformance(
auto builtinConformance = ModuleDecl::lookupConformance(
type, builtinProtocol);
if (builtinConformance.isInvalid()) {
assert(false && "Missing required conformance");
Expand Down Expand Up @@ -1561,7 +1561,7 @@ ConcreteDeclRef ASTContext::getRegexInitDecl(Type regexType) const {
results);
assert(results.size() == 1);
auto *foundDecl = cast<ConstructorDecl>(results[0]);
auto subs = regexType->getMemberSubstitutionMap(spModule, foundDecl);
auto subs = regexType->getMemberSubstitutionMap(foundDecl);
return ConcreteDeclRef(foundDecl, subs);
}

Expand Down Expand Up @@ -5671,7 +5671,7 @@ ASTContext::getForeignRepresentationInfo(NominalTypeDecl *nominal,
if (nominal != dc->getASTContext().getOptionalDecl()) {
if (auto objcBridgeable
= getProtocol(KnownProtocolKind::ObjectiveCBridgeable)) {
auto conformance = dc->getParentModule()->lookupConformance(
auto conformance = ModuleDecl::lookupConformance(
nominal->getDeclaredInterfaceType(), objcBridgeable);
if (conformance) {
result =
Expand Down Expand Up @@ -5823,7 +5823,7 @@ Type ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
if (!proto)
return ProtocolConformanceRef::forInvalid();

return dc->getParentModule()->lookupConformance(type, proto);
return ModuleDecl::lookupConformance(type, proto);
};

// Do we conform to _ObjectiveCBridgeable?
Expand Down
14 changes: 6 additions & 8 deletions lib/AST/ASTDemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ Type ASTBuilder::createTypeAliasType(GenericTypeDecl *decl, Type parent) {
return declaredType;

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

return declaredType.subst(subs);
}
Expand Down Expand Up @@ -239,7 +239,7 @@ Type ASTBuilder::createBoundGenericType(GenericTypeDecl *decl,
// Build a SubstitutionMap.
auto genericSig = nominalDecl->getGenericSignature();
auto subs = createSubstitutionMapFromGenericArgs(
genericSig, args, LookUpConformanceInModule(decl->getParentModule()));
genericSig, args, LookUpConformanceInModule());
if (!subs)
return Type();
auto origType = nominalDecl->getDeclaredInterfaceType();
Expand Down Expand Up @@ -278,7 +278,7 @@ Type ASTBuilder::resolveOpaqueType(NodePointer opaqueDescriptor,

SubstitutionMap subs = createSubstitutionMapFromGenericArgs(
opaqueDecl->getGenericSignature(), allArgs,
LookUpConformanceInModule(parentModule));
LookUpConformanceInModule());
Type interfaceType = opaqueDecl->getOpaqueGenericParams()[ordinal];
return OpaqueTypeArchetypeType::get(opaqueDecl, interfaceType, subs);
}
Expand Down Expand Up @@ -318,11 +318,9 @@ Type ASTBuilder::createBoundGenericType(GenericTypeDecl *decl,
substTy;
}

// FIXME: This is the wrong module
auto *moduleDecl = decl->getParentModule();
auto subMap = SubstitutionMap::get(genericSig,
QueryTypeSubstitutionMap{subs},
LookUpConformanceInModule(moduleDecl));
LookUpConformanceInModule());
if (!subMap)
return Type();

Expand Down Expand Up @@ -955,7 +953,7 @@ Type ASTBuilder::createSILBoxTypeWithLayout(
if (signature)
substs = createSubstitutionMapFromGenericArgs(
signature, replacements,
LookUpConformanceInSignature(signature.getPointer()));
LookUpConformanceInModule());
return SILBoxType::get(Ctx, layout, substs);
}

Expand Down Expand Up @@ -1054,7 +1052,7 @@ SubstitutionMap
ASTBuilder::createSubstitutionMap(BuiltGenericSignature sig,
ArrayRef<BuiltType> replacements) {
return SubstitutionMap::get(sig, replacements,
LookUpConformanceInSignature(sig.getPointer()));
LookUpConformanceInModule());
}

Type ASTBuilder::subst(Type subject, const BuiltSubstitutionMap &Subs) const {
Expand Down
6 changes: 2 additions & 4 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1969,9 +1969,8 @@ void ASTMangler::appendRetroactiveConformances(SubstitutionMap subMap,
void ASTMangler::appendRetroactiveConformances(Type type, GenericSignature sig) {
// Dig out the substitution map to use.
SubstitutionMap subMap;
ModuleDecl *module;

if (auto typeAlias = dyn_cast<TypeAliasType>(type.getPointer())) {
module = Mod ? Mod : typeAlias->getDecl()->getModuleContext();
subMap = typeAlias->getSubstitutionMap();
} else {
if (type->hasUnboundGenericType())
Expand All @@ -1980,8 +1979,7 @@ void ASTMangler::appendRetroactiveConformances(Type type, GenericSignature sig)
auto nominal = type->getAnyNominal();
if (!nominal) return;

module = Mod ? Mod : nominal->getModuleContext();
subMap = type->getContextSubstitutionMap(module, nominal);
subMap = type->getContextSubstitutionMap(nominal);
}

appendRetroactiveConformances(subMap, sig);
Expand Down
Loading