Skip to content

Commit ee33f12

Browse files
committed
IDE: Refactor IsDeclApplicableRequest to use Requirement::isSatisfied()
1 parent 6cb64c4 commit ee33f12

File tree

3 files changed

+32
-32
lines changed

3 files changed

+32
-32
lines changed

lib/Sema/CSGen.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4135,31 +4135,6 @@ void ConstraintSystem::optimizeConstraints(Expr *e) {
41354135
e->walk(optimizer);
41364136
}
41374137

4138-
bool swift::areGenericRequirementsSatisfied(
4139-
const DeclContext *DC, GenericSignature sig,
4140-
SubstitutionMap Substitutions, bool isExtension) {
4141-
4142-
ConstraintSystemOptions Options;
4143-
ConstraintSystem CS(const_cast<DeclContext *>(DC), Options);
4144-
auto Loc = CS.getConstraintLocator({});
4145-
4146-
// For every requirement, add a constraint.
4147-
for (auto Req : sig->getRequirements()) {
4148-
if (auto resolved = Req.subst(
4149-
QuerySubstitutionMap{Substitutions},
4150-
LookUpConformanceInModule(DC->getParentModule()))) {
4151-
CS.addConstraint(*resolved, Loc);
4152-
} else if (isExtension) {
4153-
return false;
4154-
}
4155-
// Unresolved requirements are requirements of the function itself. This
4156-
// does not prevent it from being applied. E.g. func foo<T: Sequence>(x: T).
4157-
}
4158-
4159-
// Having a solution implies the requirements have been fulfilled.
4160-
return CS.solveSingle().hasValue();
4161-
}
4162-
41634138
struct ResolvedMemberResult::Implementation {
41644139
llvm::SmallVector<ValueDecl*, 4> AllDecls;
41654140
unsigned ViableStartIdx;

lib/Sema/IDETypeCheckingRequests.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,36 @@ class ContainsSpecializableArchetype : public TypeWalker {
116116
}
117117
};
118118

119+
static bool areGenericRequirementsSatisfied(GenericSignature sig,
120+
SubstitutionMap substMap,
121+
bool isExtension) {
122+
SmallVector<Requirement, 4> worklist;
123+
124+
for (auto req : sig->getRequirements()) {
125+
if (auto resolved = req.subst(
126+
QuerySubstitutionMap{substMap},
127+
LookUpConformanceInSignature(sig.getPointer()))) {
128+
worklist.push_back(*resolved);
129+
} else if (isExtension) {
130+
return false;
131+
}
132+
// Unresolved requirements are requirements of the function itself. This
133+
// does not prevent it from being applied. E.g. func foo<T: Sequence>(x: T).
134+
}
135+
136+
while (!worklist.empty()) {
137+
auto req = worklist.pop_back_val();
138+
ArrayRef<Requirement> conditionalRequirements;
139+
if (req.isSatisfied(conditionalRequirements) != Requirement::Satisfied)
140+
return false;
141+
142+
worklist.append(conditionalRequirements.begin(),
143+
conditionalRequirements.end());
144+
}
145+
146+
return true;
147+
}
148+
119149
static bool isExtensionAppliedInternal(const DeclContext *DC, Type BaseTy,
120150
const ExtensionDecl *ED) {
121151
// We can't do anything if the base type has unbound generic parameters.
@@ -133,7 +163,7 @@ static bool isExtensionAppliedInternal(const DeclContext *DC, Type BaseTy,
133163
GenericSignature genericSig = ED->getGenericSignature();
134164
SubstitutionMap substMap = BaseTy->getContextSubstitutionMap(
135165
DC->getParentModule(), ED->getExtendedNominal());
136-
return areGenericRequirementsSatisfied(DC, genericSig, substMap,
166+
return areGenericRequirementsSatisfied(genericSig, substMap,
137167
/*isExtension=*/true);
138168
}
139169

@@ -157,7 +187,7 @@ static bool isMemberDeclAppliedInternal(const DeclContext *DC, Type BaseTy,
157187

158188
SubstitutionMap substMap = BaseTy->getContextSubstitutionMap(
159189
DC->getParentModule(), VD->getDeclContext());
160-
return areGenericRequirementsSatisfied(DC, genericSig, substMap,
190+
return areGenericRequirementsSatisfied(genericSig, substMap,
161191
/*isExtension=*/false);
162192
}
163193

lib/Sema/TypeChecker.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,11 +1285,6 @@ bool diagnoseObjCUnsatisfiedOptReqConflicts(SourceFile &sf);
12851285
std::pair<unsigned, DeclName> getObjCMethodDiagInfo(
12861286
AbstractFunctionDecl *method);
12871287

1288-
bool areGenericRequirementsSatisfied(const DeclContext *DC,
1289-
GenericSignature sig,
1290-
SubstitutionMap Substitutions,
1291-
bool isExtension);
1292-
12931288
/// Check for restrictions on the use of the @unknown attribute on a
12941289
/// case statement.
12951290
void checkUnknownAttrRestrictions(

0 commit comments

Comments
 (0)