Skip to content

Commit 4f1d8e3

Browse files
slavapestovkavon
authored andcommitted
Sema: Clean up InferredGenericSignatureRequest
- It's wasteful to cache because each invocation is unique - Inference sources only need to be Types and not TypeLocs - We can pass in an explicit SourceLoc for diagnostics (cherry picked from commit 4e39dac)
1 parent 06b1aee commit 4f1d8e3

File tree

8 files changed

+38
-68
lines changed

8 files changed

+38
-68
lines changed

include/swift/AST/TypeCheckRequests.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,9 +2008,9 @@ class InferredGenericSignatureRequest :
20082008
GenericParamList *,
20092009
WhereClauseOwner,
20102010
SmallVector<Requirement, 2>,
2011-
SmallVector<TypeLoc, 2>,
2012-
bool, bool),
2013-
RequestFlags::Cached> {
2011+
SmallVector<TypeBase *, 2>,
2012+
SourceLoc, bool, bool),
2013+
RequestFlags::Uncached> {
20142014
public:
20152015
using SimpleRequest::SimpleRequest;
20162016

@@ -2024,13 +2024,10 @@ class InferredGenericSignatureRequest :
20242024
GenericParamList *genericParams,
20252025
WhereClauseOwner whereClause,
20262026
SmallVector<Requirement, 2> addedRequirements,
2027-
SmallVector<TypeLoc, 2> inferenceSources,
2028-
bool isExtension, bool allowInverses) const;
2027+
SmallVector<TypeBase *, 2> inferenceSources,
2028+
SourceLoc loc, bool isExtension, bool allowInverses) const;
20292029

20302030
public:
2031-
// Separate caching.
2032-
bool isCached() const { return true; }
2033-
20342031
/// Inferred generic signature requests don't have source-location info.
20352032
SourceLoc getNearestLoc() const {
20362033
return SourceLoc();

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,9 @@ SWIFT_REQUEST(TypeChecker, InferredGenericSignatureRequest,
197197
GenericParamList *,
198198
WhereClauseOwner,
199199
SmallVector<Requirement, 2>,
200-
SmallVector<TypeLoc, 2>, bool, bool),
201-
Cached, NoLocationInfo)
200+
SmallVector<TypeBase *, 2>,
201+
SourceLoc, bool, bool),
202+
Uncached, NoLocationInfo)
202203
SWIFT_REQUEST(TypeChecker, DistributedModuleIsAvailableRequest,
203204
bool(ModuleDecl *), Cached, NoLocationInfo)
204205
SWIFT_REQUEST(TypeChecker, InheritedTypeRequest,

lib/AST/RequirementMachine/RequirementLowering.cpp

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,7 @@ struct InferRequirementsWalker : public TypeWalker {
676676
/// We automatically infer 'T : Hashable' from the fact that 'struct Set'
677677
/// declares a Hashable requirement on its generic parameter.
678678
void swift::rewriting::inferRequirements(
679-
Type type, SourceLoc loc,
680-
ModuleDecl *module, DeclContext *dc,
679+
Type type, ModuleDecl *module, DeclContext *dc,
681680
SmallVectorImpl<StructuralRequirement> &result) {
682681
if (!type)
683682
return;
@@ -707,13 +706,8 @@ void swift::rewriting::realizeRequirement(
707706
auto secondType = req.getSecondType();
708707

709708
if (shouldInferRequirements) {
710-
auto firstLoc = (reqRepr ? reqRepr->getSubjectRepr()->getStartLoc()
711-
: SourceLoc());
712-
inferRequirements(firstType, firstLoc, moduleForInference, dc, result);
713-
714-
auto secondLoc = (reqRepr ? reqRepr->getConstraintRepr()->getStartLoc()
715-
: SourceLoc());
716-
inferRequirements(secondType, secondLoc, moduleForInference, dc, result);
709+
inferRequirements(firstType, moduleForInference, dc, result);
710+
inferRequirements(secondType, moduleForInference, dc, result);
717711
}
718712

719713
realizeTypeRequirement(dc, firstType, secondType, loc, result, errors);
@@ -723,9 +717,7 @@ void swift::rewriting::realizeRequirement(
723717
case RequirementKind::Layout: {
724718
if (shouldInferRequirements) {
725719
auto firstType = req.getFirstType();
726-
auto firstLoc = (reqRepr ? reqRepr->getSubjectRepr()->getStartLoc()
727-
: SourceLoc());
728-
inferRequirements(firstType, firstLoc, moduleForInference, dc, result);
720+
inferRequirements(firstType, moduleForInference, dc, result);
729721
}
730722

731723
result.push_back({req, loc});
@@ -735,14 +727,10 @@ void swift::rewriting::realizeRequirement(
735727
case RequirementKind::SameType: {
736728
if (shouldInferRequirements) {
737729
auto firstType = req.getFirstType();
738-
auto firstLoc = (reqRepr ? reqRepr->getFirstTypeRepr()->getStartLoc()
739-
: SourceLoc());
740-
inferRequirements(firstType, firstLoc, moduleForInference, dc, result);
730+
inferRequirements(firstType, moduleForInference, dc, result);
741731

742732
auto secondType = req.getSecondType();
743-
auto secondLoc = (reqRepr ? reqRepr->getSecondTypeRepr()->getStartLoc()
744-
: SourceLoc());
745-
inferRequirements(secondType, secondLoc, moduleForInference, dc, result);
733+
inferRequirements(secondType, moduleForInference, dc, result);
746734
}
747735

748736
result.push_back({req, loc});
@@ -843,14 +831,14 @@ void swift::rewriting::realizeInheritedRequirements(
843831

844832
if (!inheritedType) continue;
845833

846-
auto *typeRepr = inheritedTypes.getTypeRepr(index);
847-
SourceLoc loc = (typeRepr ? typeRepr->getStartLoc() : SourceLoc());
848-
849834
if (shouldInferRequirements) {
850-
inferRequirements(inheritedType, loc, moduleForInference,
835+
inferRequirements(inheritedType, moduleForInference,
851836
decl->getInnermostDeclContext(), result);
852837
}
853838

839+
auto *typeRepr = inheritedTypes.getTypeRepr(index);
840+
SourceLoc loc = (typeRepr ? typeRepr->getStartLoc() : SourceLoc());
841+
854842
realizeTypeRequirement(dc, type, inheritedType, loc, result, errors);
855843
}
856844

lib/AST/RequirementMachine/RequirementLowering.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ void desugarRequirement(Requirement req, SourceLoc loc,
4747
SmallVectorImpl<InverseRequirement> &inverses,
4848
SmallVectorImpl<RequirementError> &errors);
4949

50-
void inferRequirements(Type type, SourceLoc loc,
51-
ModuleDecl *module, DeclContext *dc,
50+
void inferRequirements(Type type, ModuleDecl *module, DeclContext *dc,
5251
SmallVectorImpl<StructuralRequirement> &result);
5352

5453
void realizeRequirement(DeclContext *dc,

lib/AST/RequirementMachine/RequirementMachineRequests.cpp

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -753,8 +753,8 @@ InferredGenericSignatureRequest::evaluate(
753753
GenericParamList *genericParamList,
754754
WhereClauseOwner whereClause,
755755
SmallVector<Requirement, 2> addedRequirements,
756-
SmallVector<TypeLoc, 2> inferenceSources,
757-
bool isExtension, bool allowInverses) const {
756+
SmallVector<TypeBase *, 2> inferenceSources,
757+
SourceLoc loc, bool isExtension, bool allowInverses) const {
758758
GenericSignature parentSig(parentSigImpl);
759759

760760
SmallVector<GenericTypeParamType *, 4> genericParams(
@@ -771,26 +771,6 @@ InferredGenericSignatureRequest::evaluate(
771771
SmallVector<RequirementError, 2> errors;
772772
SmallVector<InverseRequirement, 2> inverses;
773773

774-
SourceLoc loc = [&]() {
775-
if (genericParamList) {
776-
auto loc = genericParamList->getLAngleLoc();
777-
if (loc.isValid())
778-
return loc;
779-
}
780-
if (whereClause) {
781-
auto loc = whereClause.getLoc();
782-
if (loc.isValid())
783-
return loc;
784-
}
785-
for (auto sourcePair : inferenceSources) {
786-
auto *typeRepr = sourcePair.getTypeRepr();
787-
auto loc = typeRepr ? typeRepr->getStartLoc() : SourceLoc();
788-
if (loc.isValid())
789-
return loc;
790-
}
791-
return SourceLoc();
792-
}();
793-
794774
for (const auto &req : parentSig.getRequirements())
795775
requirements.push_back({req, loc});
796776

@@ -861,12 +841,8 @@ InferredGenericSignatureRequest::evaluate(
861841

862842
// Perform requirement inference from function parameter and result
863843
// types and such.
864-
for (auto sourcePair : inferenceSources) {
865-
auto *typeRepr = sourcePair.getTypeRepr();
866-
auto typeLoc = typeRepr ? typeRepr->getStartLoc() : SourceLoc();
867-
868-
inferRequirements(sourcePair.getType(), typeLoc, moduleForInference,
869-
lookupDC, requirements);
844+
for (auto source : inferenceSources) {
845+
inferRequirements(source, moduleForInference, lookupDC, requirements);
870846
}
871847

872848
// Finish by adding any remaining requirements. This is used to introduce

lib/Sema/TypeCheckAttr.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3070,6 +3070,7 @@ SerializeAttrGenericSignatureRequest::evaluate(Evaluator &evaluator,
30703070
WhereClauseOwner(const_cast<AbstractFunctionDecl *>(FD), attr),
30713071
/*addedRequirements=*/{},
30723072
/*inferenceSources=*/{},
3073+
attr->getLocation(),
30733074
/*isExtension=*/false,
30743075
/*allowInverses=*/true};
30753076

@@ -5593,6 +5594,7 @@ bool resolveDifferentiableAttrDerivativeGenericSignature(
55935594
WhereClauseOwner(original, attr),
55945595
/*addedRequirements=*/{},
55955596
/*inferenceSources=*/{},
5597+
attr->getLocation(),
55965598
/*isExtension=*/false,
55975599
/*allowInverses=*/true};
55985600

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ OpaqueResultTypeRequest::evaluate(Evaluator &evaluator,
118118
WhereClauseOwner(),
119119
/*addedRequirements=*/{},
120120
/*inferenceSources=*/{},
121+
repr->getLoc(),
121122
/*isExtension=*/false,
122123
/*allowInverses=*/true};
123124

@@ -681,9 +682,13 @@ GenericSignatureRequest::evaluate(Evaluator &evaluator,
681682
}
682683

683684
GenericSignature parentSig;
684-
SmallVector<TypeLoc, 2> inferenceSources;
685+
SmallVector<TypeBase *, 2> inferenceSources;
685686
SmallVector<Requirement, 2> extraReqs;
687+
SourceLoc loc;
688+
686689
if (auto VD = dyn_cast<ValueDecl>(GC->getAsDecl())) {
690+
loc = VD->getLoc();
691+
687692
parentSig = GC->getParentForLookup()->getGenericSignatureOfContext();
688693

689694
auto func = dyn_cast<AbstractFunctionDecl>(VD);
@@ -728,7 +733,7 @@ GenericSignatureRequest::evaluate(Evaluator &evaluator,
728733
const auto type =
729734
resolution.withOptions(paramOptions).resolveType(typeRepr);
730735

731-
inferenceSources.emplace_back(typeRepr, type);
736+
inferenceSources.push_back(type.getPointer());
732737
}
733738

734739
// Handle the thrown error type.
@@ -743,7 +748,7 @@ GenericSignatureRequest::evaluate(Evaluator &evaluator,
743748
.resolveType(thrownTypeRepr);
744749

745750
// Add this type as an inference source.
746-
inferenceSources.emplace_back(thrownTypeRepr, thrownType);
751+
inferenceSources.push_back(thrownType.getPointer());
747752

748753
// Add conformance of this type to the Error protocol.
749754
if (auto errorProtocol = ctx.getErrorDecl()) {
@@ -771,10 +776,12 @@ GenericSignatureRequest::evaluate(Evaluator &evaluator,
771776
resolution.withOptions(TypeResolverContext::FunctionResult)
772777
.resolveType(resultTypeRepr);
773778

774-
inferenceSources.emplace_back(resultTypeRepr, resultType);
779+
inferenceSources.push_back(resultType.getPointer());
775780
}
776781
}
777782
} else if (auto *ext = dyn_cast<ExtensionDecl>(GC)) {
783+
loc = ext->getLoc();
784+
778785
collectAdditionalExtensionRequirements(ext->getExtendedType(), extraReqs);
779786

780787
auto *extendedNominal = ext->getExtendedNominal();
@@ -804,7 +811,7 @@ GenericSignatureRequest::evaluate(Evaluator &evaluator,
804811
auto request = InferredGenericSignatureRequest{
805812
parentSig.getPointer(),
806813
genericParams, WhereClauseOwner(GC),
807-
extraReqs, inferenceSources,
814+
extraReqs, inferenceSources, loc,
808815
/*isExtension=*/isa<ExtensionDecl>(GC),
809816
/*allowInverses=*/true};
810817
return evaluateOrDefault(ctx.evaluator, request,

lib/Sema/TypeChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ swift::handleSILGenericParams(GenericParamList *genericParams,
529529
auto request = InferredGenericSignatureRequest{
530530
/*parentSig=*/nullptr,
531531
nestedList.back(), WhereClauseOwner(),
532-
{}, {},
532+
{}, {}, genericParams->getLAngleLoc(),
533533
/*isExtension=*/false,
534534
allowInverses};
535535
return evaluateOrDefault(DC->getASTContext().evaluator, request,

0 commit comments

Comments
 (0)