Skip to content

Commit 4076fd3

Browse files
authored
Merge pull request #60587 from slavapestov/requirement-subst-cleanup-5.7
AST: Change return type of Requirement::subst() to Requirement [5.7]
2 parents 9f3cdf6 + 330ea14 commit 4076fd3

16 files changed

+55
-72
lines changed

include/swift/AST/Requirement.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,15 @@ class Requirement
5353

5454
/// Subst the types involved in this requirement.
5555
///
56-
/// The \c args arguments are passed through to Type::subst. This doesn't
57-
/// touch the superclasses, protocols or layout constraints.
56+
/// The \c args arguments are passed through to Type::subst.
5857
template <typename ...Args>
59-
llvm::Optional<Requirement> subst(Args &&...args) const {
58+
Requirement subst(Args &&...args) const {
6059
auto newFirst = getFirstType().subst(std::forward<Args>(args)...);
61-
if (newFirst->hasError())
62-
return None;
63-
6460
switch (getKind()) {
6561
case RequirementKind::Conformance:
6662
case RequirementKind::Superclass:
6763
case RequirementKind::SameType: {
6864
auto newSecond = getSecondType().subst(std::forward<Args>(args)...);
69-
if (newSecond->hasError())
70-
return None;
7165
return Requirement(getKind(), newFirst, newSecond);
7266
}
7367
case RequirementKind::Layout:

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5340,9 +5340,8 @@ ASTContext::getOverrideGenericSignature(const ValueDecl *base,
53405340
};
53415341

53425342
for (auto reqt : baseGenericSig.getRequirements()) {
5343-
if (auto substReqt = reqt.subst(substFn, lookupConformanceFn)) {
5344-
addedRequirements.push_back(*substReqt);
5345-
}
5343+
auto substReqt = reqt.subst(substFn, lookupConformanceFn);
5344+
addedRequirements.push_back(substReqt);
53465345
}
53475346
}
53485347

lib/AST/ExistentialGeneralization.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,7 @@ class Generalizer : public CanTypeVisitor<Generalizer, Type> {
212212
if (origReq.getKind() != RequirementKind::Conformance) continue;
213213
auto origConformance = origConformances[i++];
214214

215-
auto optNewReq = origReq.subst(newSubs);
216-
assert(optNewReq && "generalization substitution failed");
217-
auto &newReq = *optNewReq;
218-
215+
auto newReq = origReq.subst(newSubs);
219216
addedRequirements.push_back(newReq);
220217

221218
substConformances.insert({{newReq.getFirstType()->getCanonicalType(),

lib/AST/GenericSignature.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,19 +373,14 @@ bool GenericSignatureImpl::isRequirementSatisfied(
373373
if (requirement.getFirstType()->hasTypeParameter()) {
374374
auto *genericEnv = getGenericEnvironment();
375375

376-
auto substituted = requirement.subst(
376+
requirement = requirement.subst(
377377
[&](SubstitutableType *type) -> Type {
378378
if (auto *paramType = type->getAs<GenericTypeParamType>())
379379
return genericEnv->mapTypeIntoContext(paramType);
380380

381381
return type;
382382
},
383383
LookUpConformanceInSignature(this));
384-
385-
if (!substituted)
386-
return false;
387-
388-
requirement = *substituted;
389384
}
390385

391386
// FIXME: Need to check conditional requirements here.

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5169,8 +5169,8 @@ class GenericSignatureBuilder::InferRequirementsWalker : public TypeWalker {
51695169
auto decl = TypeAlias->getDecl();
51705170
auto subMap = TypeAlias->getSubstitutionMap();
51715171
for (const auto &rawReq : decl->getGenericSignature().getRequirements()) {
5172-
if (auto req = rawReq.subst(subMap))
5173-
Builder.addRequirement(*req, source, nullptr);
5172+
auto req = rawReq.subst(subMap);
5173+
Builder.addRequirement(req, source, nullptr);
51745174
}
51755175

51765176
return Action::Continue;
@@ -5237,8 +5237,8 @@ class GenericSignatureBuilder::InferRequirementsWalker : public TypeWalker {
52375237
// Handle the requirements.
52385238
// FIXME: Inaccurate TypeReprs.
52395239
for (const auto &rawReq : genericSig.getRequirements()) {
5240-
if (auto req = rawReq.subst(subMap))
5241-
Builder.addRequirement(*req, source, nullptr);
5240+
auto req = rawReq.subst(subMap);
5241+
Builder.addRequirement(req, source, nullptr);
52425242
}
52435243

52445244
return Action::Continue;
@@ -8402,7 +8402,7 @@ AbstractGenericSignatureRequestGSB::evaluate(
84028402
},
84038403
MakeAbstractConformanceForGenericType(),
84048404
SubstFlags::AllowLoweredTypes);
8405-
resugaredRequirements.push_back(*resugaredReq);
8405+
resugaredRequirements.push_back(resugaredReq);
84068406
}
84078407

84088408
return GenericSignatureWithError(

lib/AST/ProtocolConformance.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -992,9 +992,9 @@ void SpecializedProtocolConformance::computeConditionalRequirements() const {
992992

993993
SmallVector<Requirement, 4> newReqs;
994994
for (auto oldReq : *parentCondReqs) {
995-
if (auto newReq = oldReq.subst(QuerySubstitutionMap{subMap},
996-
LookUpConformanceInModule(module)))
997-
newReqs.push_back(*newReq);
995+
auto newReq = oldReq.subst(QuerySubstitutionMap{subMap},
996+
LookUpConformanceInModule(module));
997+
newReqs.push_back(newReq);
998998
}
999999
auto &ctxt = getProtocol()->getASTContext();
10001000
ConditionalRequirements = ctxt.AllocateCopy(newReqs);
@@ -1201,7 +1201,7 @@ ProtocolConformance::subst(TypeSubstitutionFn subs,
12011201

12021202
SmallVector<Requirement, 2> requirements;
12031203
for (auto req : getConditionalRequirements()) {
1204-
requirements.push_back(*req.subst(subs, conformances, options));
1204+
requirements.push_back(req.subst(subs, conformances, options));
12051205
}
12061206

12071207
auto kind = cast<BuiltinProtocolConformance>(this)

lib/AST/RequirementEnvironment.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ RequirementEnvironment::RequirementEnvironment(
181181

182182
if (conformanceSig) {
183183
for (auto &rawReq : conformanceSig.getRequirements()) {
184-
if (auto req = rawReq.subst(conformanceToSyntheticTypeFn,
185-
conformanceToSyntheticConformanceFn))
186-
requirements.push_back(*req);
184+
auto req = rawReq.subst(conformanceToSyntheticTypeFn,
185+
conformanceToSyntheticConformanceFn);
186+
requirements.push_back(req);
187187
}
188188
}
189189

@@ -207,8 +207,8 @@ RequirementEnvironment::RequirementEnvironment(
207207
// Next, add each of the requirements (mapped from the requirement's
208208
// interface types into the abstract type parameters).
209209
for (auto &rawReq : reqSig.getRequirements()) {
210-
if (auto req = rawReq.subst(reqToSyntheticEnvMap))
211-
requirements.push_back(*req);
210+
auto req = rawReq.subst(reqToSyntheticEnvMap);
211+
requirements.push_back(req);
212212
}
213213

214214
// Produce the generic signature and environment.

lib/AST/RequirementMachine/RequirementLowering.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,7 @@ struct InferRequirementsWalker : public TypeWalker {
465465
auto decl = typeAlias->getDecl();
466466
auto subMap = typeAlias->getSubstitutionMap();
467467
for (const auto &rawReq : decl->getGenericSignature().getRequirements()) {
468-
if (auto req = rawReq.subst(subMap))
469-
desugarRequirement(*req, SourceLoc(), reqs, errors);
468+
desugarRequirement(rawReq.subst(subMap), SourceLoc(), reqs, errors);
470469
}
471470

472471
return Action::Continue;
@@ -532,8 +531,8 @@ struct InferRequirementsWalker : public TypeWalker {
532531
// Handle the requirements.
533532
// FIXME: Inaccurate TypeReprs.
534533
for (const auto &rawReq : genericSig.getRequirements()) {
535-
if (auto req = rawReq.subst(subMap))
536-
desugarRequirement(*req, SourceLoc(), reqs, errors);
534+
auto req = rawReq.subst(subMap);
535+
desugarRequirement(req, SourceLoc(), reqs, errors);
537536
}
538537

539538
return Action::Continue;

lib/AST/RequirementMachine/RequirementMachineRequests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ AbstractGenericSignatureRequestRQM::evaluate(
599599
},
600600
MakeAbstractConformanceForGenericType(),
601601
SubstFlags::AllowLoweredTypes);
602-
resugaredRequirements.push_back(*resugaredReq);
602+
resugaredRequirements.push_back(resugaredReq);
603603
}
604604

605605
return GenericSignatureWithError(

lib/AST/Type.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4527,26 +4527,16 @@ static Type substGenericFunctionType(GenericFunctionType *genericFnType,
45274527
for (const auto &req : genericFnType->getRequirements()) {
45284528
// Substitute into the requirement.
45294529
auto substReqt = req.subst(substitutions, lookupConformances, options);
4530-
if (!substReqt) {
4531-
anySemanticChanges = true;
4532-
continue;
4533-
}
45344530

45354531
// Did anything change?
45364532
if (!anySemanticChanges &&
4537-
(!req.getFirstType()->isEqual(substReqt->getFirstType()) ||
4533+
(!req.getFirstType()->isEqual(substReqt.getFirstType()) ||
45384534
(req.getKind() != RequirementKind::Layout &&
4539-
!req.getSecondType()->isEqual(substReqt->getSecondType())))) {
4535+
!req.getSecondType()->isEqual(substReqt.getSecondType())))) {
45404536
anySemanticChanges = true;
45414537
}
45424538

4543-
// Skip any erroneous requirements.
4544-
if (substReqt->getFirstType()->hasError() ||
4545-
(substReqt->getKind() != RequirementKind::Layout &&
4546-
substReqt->getSecondType()->hasError()))
4547-
continue;
4548-
4549-
requirements.push_back(*substReqt);
4539+
requirements.push_back(substReqt);
45504540
}
45514541

45524542
GenericSignature genericSig;

lib/IDE/IDETypeChecking.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,15 +323,13 @@ struct SynthesizedExtensionAnalyzer::Implementation {
323323
if (!BaseType->isExistentialType()) {
324324
// Apply any substitutions we need to map the requirements from a
325325
// a protocol extension to an extension on the conforming type.
326-
auto SubstReq = Req.subst(subMap);
327-
if (!SubstReq) {
326+
Req = Req.subst(subMap);
327+
if (Req.hasError()) {
328328
// Substitution with interface type bases can only fail
329329
// if a concrete type fails to conform to a protocol.
330330
// In this case, just give up on the extension altogether.
331331
return true;
332332
}
333-
334-
Req = *SubstReq;
335333
}
336334

337335
assert(!Req.getFirstType()->hasArchetype());
@@ -346,13 +344,13 @@ struct SynthesizedExtensionAnalyzer::Implementation {
346344
return type;
347345
},
348346
LookUpConformanceInModule(M));
349-
if (!SubstReq)
347+
if (SubstReq.hasError())
350348
return true;
351349

352350
// FIXME: Need to handle conditional requirements here!
353351
ArrayRef<Requirement> conditionalRequirements;
354-
if (!SubstReq->isSatisfied(conditionalRequirements)) {
355-
if (!SubstReq->canBeSatisfied())
352+
if (!SubstReq.isSatisfied(conditionalRequirements)) {
353+
if (!SubstReq.canBeSatisfied())
356354
return true;
357355

358356
MergeInfo.addRequirement(Req);

lib/SILOptimizer/Utils/Generics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,7 @@ void FunctionSignaturePartialSpecializer::addRequirements(
15401540
for (auto &reqReq : Reqs) {
15411541
LLVM_DEBUG(llvm::dbgs() << "\n\nRe-mapping the requirement:\n";
15421542
reqReq.dump(llvm::dbgs()));
1543-
AllRequirements.push_back(*reqReq.subst(SubsMap));
1543+
AllRequirements.push_back(reqReq.subst(SubsMap));
15441544
}
15451545
}
15461546

lib/Sema/CodeSynthesis.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,10 @@ computeDesignatedInitOverrideSignature(ASTContext &ctx,
529529
};
530530

531531
SmallVector<Requirement, 2> requirements;
532-
for (auto reqt : superclassCtorSig.getRequirements())
533-
if (auto substReqt = reqt.subst(substFn, lookupConformanceFn))
534-
requirements.push_back(*substReqt);
532+
for (auto reqt : superclassCtorSig.getRequirements()) {
533+
auto substReqt = reqt.subst(substFn, lookupConformanceFn);
534+
requirements.push_back(substReqt);
535+
}
535536

536537
// Now form the substitution map that will be used to remap parameter
537538
// types.

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -925,9 +925,10 @@ CheckGenericArgumentsResult TypeChecker::checkGenericArgumentsForDiagnostics(
925925
Requirement substReq = req;
926926
if (isPrimaryReq) {
927927
// Primary requirements do not have substitutions applied.
928-
if (auto resolved =
929-
req.subst(substitutions, LookUpConformanceInModule(module))) {
930-
substReq = *resolved;
928+
auto resolved =
929+
req.subst(substitutions, LookUpConformanceInModule(module));
930+
if (!resolved.hasError()) {
931+
substReq = resolved;
931932
} else {
932933
// Another requirement might fail later; just continue.
933934
hadSubstFailure = true;
@@ -969,9 +970,10 @@ CheckGenericArgumentsResult::Kind TypeChecker::checkGenericArguments(
969970
bool valid = true;
970971

971972
for (auto req : requirements) {
972-
if (auto resolved = req.subst(substitutions,
973-
LookUpConformanceInModule(module), options)) {
974-
worklist.push_back(*resolved);
973+
auto resolved = req.subst(substitutions,
974+
LookUpConformanceInModule(module), options);
975+
if (!resolved.hasError()) {
976+
worklist.push_back(resolved);
975977
} else {
976978
valid = false;
977979
}

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,8 @@ matchWitnessDifferentiableAttr(DeclContext *dc, ValueDecl *req,
391391
witnessConfig.derivativeGenericSignature.getRequirements()) {
392392
auto substReq = req.subst(result.WitnessSubstitutions);
393393
bool reqDiffGenSigSatisfies =
394-
reqDiffGenSig && substReq &&
395-
reqDiffGenSig->isRequirementSatisfied(*substReq);
394+
reqDiffGenSig && !substReq.hasError() &&
395+
reqDiffGenSig->isRequirementSatisfied(substReq);
396396
bool conformanceGenSigSatisfies =
397397
conformanceGenSig &&
398398
conformanceGenSig->isRequirementSatisfied(req);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %target-swift-ide-test -code-completion -code-completion-token COMPLETE -source-filename %s
2+
3+
protocol PreferenceKey {
4+
associatedtype Value
5+
}
6+
protocol View {}
7+
8+
func propagateHeight<K: PreferenceKey>() -> some View where K.Value == #^COMPLETE^#

0 commit comments

Comments
 (0)