Skip to content

Commit 980fde2

Browse files
committed
Lift requirementsNotSatisfiedBy Into GenericSignature
1 parent a061a96 commit 980fde2

File tree

10 files changed

+53
-67
lines changed

10 files changed

+53
-67
lines changed

include/swift/AST/GenericSignature.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ class GenericSignature {
201201
/// signature.
202202
GenericEnvironment *getGenericEnvironment() const;
203203

204+
/// Return the requirements of this generic signature that are not also
205+
/// satisfied by \c otherSig.
206+
///
207+
/// \param otherSig Another generic signature whose generic parameters are
208+
/// equivalent to or a subset of the generic parameters in this signature.
209+
SmallVector<Requirement, 4>
210+
requirementsNotSatisfiedBy(GenericSignature otherSig) const;
211+
204212
/// Check invariants.
205213
void verify() const;
206214
};
@@ -373,14 +381,6 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
373381
bool isRequirementSatisfied(
374382
Requirement requirement, bool allowMissing = false) const;
375383

376-
/// Return the requirements of this generic signature that are not also
377-
/// satisfied by \c otherSig.
378-
///
379-
/// \param otherSig Another generic signature whose generic parameters are
380-
/// equivalent to or a subset of the generic parameters in this signature.
381-
SmallVector<Requirement, 4> requirementsNotSatisfiedBy(
382-
GenericSignature otherSig) const;
383-
384384
/// Return the canonical version of the given type under this generic
385385
/// signature.
386386
CanType getCanonicalTypeInContext(Type type) const;
@@ -462,6 +462,14 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
462462
/// (archetypes) that correspond to the interface types in this generic
463463
/// signature.
464464
GenericEnvironment *getGenericEnvironment() const;
465+
466+
/// Return the requirements of this generic signature that are not also
467+
/// satisfied by \c otherSig.
468+
///
469+
/// \param otherSig Another generic signature whose generic parameters are
470+
/// equivalent to or a subset of the generic parameters in this signature.
471+
SmallVector<Requirement, 4>
472+
requirementsNotSatisfiedBy(GenericSignature otherSig) const;
465473
};
466474

467475
void simple_display(raw_ostream &out, GenericSignature sig);

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5159,9 +5159,9 @@ bool ASTContext::overrideGenericSignatureReqsSatisfied(
51595159

51605160
switch (direction) {
51615161
case OverrideGenericSignatureReqCheck::BaseReqSatisfiedByDerived:
5162-
return sig->requirementsNotSatisfiedBy(derivedSig).empty();
5162+
return sig.requirementsNotSatisfiedBy(derivedSig).empty();
51635163
case OverrideGenericSignatureReqCheck::DerivedReqSatisfiedByBase:
5164-
return derivedSig->requirementsNotSatisfiedBy(sig).empty();
5164+
return derivedSig.requirementsNotSatisfiedBy(sig).empty();
51655165
}
51665166
llvm_unreachable("Unhandled OverrideGenericSignatureReqCheck in switch");
51675167
}

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2645,7 +2645,7 @@ bool ASTMangler::appendGenericSignature(GenericSignature sig,
26452645
genericParams = canSig.getGenericParams();
26462646
requirements = canSig.getRequirements();
26472647
} else {
2648-
requirementsBuffer = canSig->requirementsNotSatisfiedBy(contextSig);
2648+
requirementsBuffer = canSig.requirementsNotSatisfiedBy(contextSig);
26492649
requirements = requirementsBuffer;
26502650
}
26512651
} else {

lib/AST/Attr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,8 +991,7 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
991991
auto genericSig = FnDecl->getGenericSignature();
992992

993993
if (auto sig = attr->getSpecializedSignature()) {
994-
requirementsScratch = sig->requirementsNotSatisfiedBy(
995-
genericSig);
994+
requirementsScratch = sig.requirementsNotSatisfiedBy(genericSig);
996995
requirements = requirementsScratch;
997996
}
998997
}

lib/AST/GenericSignature.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,16 @@ bool GenericSignatureImpl::isRequirementSatisfied(
967967
return requirement.isSatisfied(conditionalRequirements, allowMissing);
968968
}
969969

970+
SmallVector<Requirement, 4>
971+
GenericSignature::requirementsNotSatisfiedBy(GenericSignature otherSig) const {
972+
// The null generic signature has no requirements, therefore all requirements
973+
// are satisfied by any signature.
974+
if (isNull()) {
975+
return {};
976+
}
977+
return getPointer()->requirementsNotSatisfiedBy(otherSig);
978+
}
979+
970980
SmallVector<Requirement, 4> GenericSignatureImpl::requirementsNotSatisfiedBy(
971981
GenericSignature otherSig) const {
972982
SmallVector<Requirement, 4> result;

lib/AST/ProtocolConformance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ ConditionalRequirementsRequest::evaluate(Evaluator &evaluator,
543543

544544
// Find the requirements in the extension that aren't proved by the original
545545
// type, these are the ones that make the conformance conditional.
546-
const auto unsatReqs = extensionSig->requirementsNotSatisfiedBy(typeSig);
546+
const auto unsatReqs = extensionSig.requirementsNotSatisfiedBy(typeSig);
547547
if (unsatReqs.empty())
548548
return {};
549549

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3542,21 +3542,18 @@ TypeConverter::getConstantOverrideInfo(TypeExpansionContext context,
35423542

35433543
assert(base.requiresNewVTableEntry() && "base must not be an override");
35443544

3545-
// Figure out the generic signature for the class method call. This is the
3546-
// signature of the derived class, with requirements transplanted from
3547-
// the base method. The derived method is allowed to have fewer
3548-
// requirements, in which case the thunk will translate the calling
3549-
// convention appropriately before calling the derived method.
3550-
bool hasGenericRequirementDifference = false;
35513545

35523546
auto derivedSig = derived.getDecl()->getAsGenericContext()
35533547
->getGenericSignature();
35543548
auto genericSig = Context.getOverrideGenericSignature(base.getDecl(),
35553549
derived.getDecl());
3556-
if (genericSig) {
3557-
hasGenericRequirementDifference =
3558-
!genericSig->requirementsNotSatisfiedBy(derivedSig).empty();
3559-
}
3550+
// Figure out the generic signature for the class method call. This is the
3551+
// signature of the derived class, with requirements transplanted from
3552+
// the base method. The derived method is allowed to have fewer
3553+
// requirements, in which case the thunk will translate the calling
3554+
// convention appropriately before calling the derived method.
3555+
bool hasGenericRequirementDifference =
3556+
!genericSig.requirementsNotSatisfiedBy(derivedSig).empty();
35603557

35613558
auto baseInfo = getConstantInfo(context, base);
35623559
auto derivedInfo = getConstantInfo(context, derived);

lib/SIL/IR/SILPrinter.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3706,17 +3706,8 @@ void SILSpecializeAttr::print(llvm::raw_ostream &OS) const {
37063706
if (genericEnv)
37073707
genericSig = genericEnv->getGenericSignature();
37083708

3709-
ArrayRef<Requirement> requirements;
3710-
SmallVector<Requirement, 4> requirementsScratch;
3711-
if (auto specializedSig = getSpecializedSignature()) {
3712-
if (genericSig) {
3713-
requirementsScratch = specializedSig->requirementsNotSatisfiedBy(
3714-
genericSig);
3715-
requirements = requirementsScratch;
3716-
} else {
3717-
requirements = specializedSig.getRequirements();
3718-
}
3719-
}
3709+
auto requirements =
3710+
getSpecializedSignature().requirementsNotSatisfiedBy(genericSig);
37203711
if (targetFunction) {
37213712
OS << "target: \"" << targetFunction->getName() << "\", ";
37223713
}

lib/Sema/TypeCheckAttr.cpp

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,7 +2148,7 @@ static void checkSpecializeAttrRequirements(SpecializeAttr *attr,
21482148
ASTContext &ctx) {
21492149
bool hadError = false;
21502150

2151-
auto specializedReqs = specializedSig->requirementsNotSatisfiedBy(originalSig);
2151+
auto specializedReqs = specializedSig.requirementsNotSatisfiedBy(originalSig);
21522152
for (auto specializedReq : specializedReqs) {
21532153
if (!specializedReq.getFirstType()->is<GenericTypeParamType>()) {
21542154
ctx.Diags.diagnose(attr->getLocation(),
@@ -4687,18 +4687,10 @@ static bool typeCheckDerivativeAttr(ASTContext &Ctx, Decl *D,
46874687
// Returns true if the generic parameters in `source` satisfy the generic
46884688
// requirements in `target`.
46894689
std::function<bool(GenericSignature, GenericSignature)>
4690-
checkGenericSignatureSatisfied = [&](GenericSignature source,
4691-
GenericSignature target) {
4692-
// If target is null, then its requirements are satisfied.
4693-
if (!target)
4694-
return true;
4695-
// If source is null but target is not null, then target's
4696-
// requirements are not satisfied.
4697-
if (!source)
4698-
return false;
4699-
4700-
return target->requirementsNotSatisfiedBy(source).empty();
4701-
};
4690+
checkGenericSignatureSatisfied =
4691+
[&](GenericSignature source, GenericSignature target) {
4692+
return target.requirementsNotSatisfiedBy(source).empty();
4693+
};
47024694

47034695
// Returns true if the derivative function and original function candidate are
47044696
// defined in compatible type contexts. If the derivative function and the
@@ -5326,18 +5318,10 @@ void AttributeChecker::visitTransposeAttr(TransposeAttr *attr) {
53265318
// Returns true if the generic parameters in `source` satisfy the generic
53275319
// requirements in `target`.
53285320
std::function<bool(GenericSignature, GenericSignature)>
5329-
checkGenericSignatureSatisfied = [&](GenericSignature source,
5330-
GenericSignature target) {
5331-
// If target is null, then its requirements are satisfied.
5332-
if (!target)
5333-
return true;
5334-
// If source is null but target is not null, then target's
5335-
// requirements are not satisfied.
5336-
if (!source)
5337-
return false;
5338-
5339-
return target->requirementsNotSatisfiedBy(source).empty();
5340-
};
5321+
checkGenericSignatureSatisfied =
5322+
[&](GenericSignature source, GenericSignature target) {
5323+
return target.requirementsNotSatisfiedBy(source).empty();
5324+
};
53415325

53425326
auto isValidOriginalCandidate = [&](AbstractFunctionDecl *originalCandidate)
53435327
-> Optional<AbstractFunctionDeclLookupErrorKind> {

lib/Sema/TypeCheckProtocolInference.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,10 @@ AssociatedTypeInference::inferTypeWitnessesViaValueWitnesses(
215215
if (!proto) {
216216
// Retrieve the generic signature of the extension.
217217
const auto extensionSig = extension->getGenericSignature();
218-
219-
// Extensions of non-generic nominals are always viable for inference.
220-
if (!extensionSig)
221-
return true;
222-
223-
return extensionSig->requirementsNotSatisfiedBy(
224-
conformanceCtx->getGenericSignatureOfContext()).empty();
218+
return extensionSig
219+
.requirementsNotSatisfiedBy(
220+
conformanceCtx->getGenericSignatureOfContext())
221+
.empty();
225222
}
226223

227224
// The condition here is a bit more fickle than

0 commit comments

Comments
 (0)