Skip to content

Commit e7c2f9d

Browse files
committed
Sema: Simplify GenericSignatureRequest::evaluate()
1 parent f38b90e commit e7c2f9d

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ static void collectAdditionalExtensionRequirements(
488488
type = type->getCanonicalType();
489489

490490
// A parameterized protocol type is not a nominal. Unwrap it to get
491-
// the underlying nominal, and record a same-type requirement for
491+
// the underlying nominal, and record same-type requirements for
492492
// the primary associated types.
493493
if (auto *paramProtoTy = type->getAs<ParameterizedProtocolType>()) {
494494
auto *protoTy = paramProtoTy->getBaseType();
@@ -569,24 +569,24 @@ GenericSignatureRequest::evaluate(Evaluator &evaluator,
569569
return sig;
570570
}
571571

572+
if (auto accessor = dyn_cast<AccessorDecl>(GC))
573+
if (auto subscript = dyn_cast<SubscriptDecl>(accessor->getStorage()))
574+
return subscript->getGenericSignature();
575+
572576
bool allowConcreteGenericParams = false;
573577
auto *genericParams = GC->getGenericParams();
574578
const auto *where = GC->getTrailingWhereClause();
575579

580+
if (!genericParams && !where) {
581+
// We can fast-path computing the generic signature of non-generic
582+
// declarations by re-using the parent context's signature.
583+
return GC->getParentForLookup()->getGenericSignatureOfContext();
584+
}
585+
576586
if (genericParams) {
577587
// Setup the depth of the generic parameters.
578588
const_cast<GenericParamList *>(genericParams)
579589
->setDepth(GC->getGenericContextDepth());
580-
581-
// Accessors can always use the generic context of their storage
582-
// declarations. This is a compile-time optimization since it lets us
583-
// avoid the requirements-gathering phase, but it also simplifies that
584-
// work for accessors which don't mention the value type in their formal
585-
// signatures (like the read and modify coroutines, since yield types
586-
// aren't tracked in the AST type yet).
587-
if (auto accessor = dyn_cast<AccessorDecl>(GC->getAsDecl())) {
588-
return cast<SubscriptDecl>(accessor->getStorage())->getGenericSignature();
589-
}
590590
}
591591

592592
// ...or we may only have a contextual where clause.
@@ -605,28 +605,20 @@ GenericSignatureRequest::evaluate(Evaluator &evaluator,
605605
if (!genericParams && where)
606606
allowConcreteGenericParams = true;
607607

608-
if (!genericParams && !where) {
609-
// We can fast-path computing the generic signature of non-generic
610-
// declarations by re-using the parent context's signature.
611-
if (auto accessor = dyn_cast<AccessorDecl>(GC->getAsDecl()))
612-
if (auto subscript = dyn_cast<SubscriptDecl>(accessor->getStorage()))
613-
return subscript->getGenericSignature();
614-
615-
return GC->getParentForLookup()->getGenericSignatureOfContext();
616-
}
617-
618608
GenericSignature parentSig;
619609
SmallVector<TypeLoc, 2> inferenceSources;
620610
SmallVector<Requirement, 2> sameTypeReqs;
621-
if (auto VD = dyn_cast_or_null<ValueDecl>(GC->getAsDecl())) {
611+
if (auto VD = dyn_cast<ValueDecl>(GC->getAsDecl())) {
622612
parentSig = GC->getParentForLookup()->getGenericSignatureOfContext();
623613

624614
auto func = dyn_cast<AbstractFunctionDecl>(VD);
625615
auto subscr = dyn_cast<SubscriptDecl>(VD);
626616
auto macro = dyn_cast<MacroDecl>(VD);
617+
assert(func || subscr || macro || isa<NominalTypeDecl>(VD) ||
618+
isa<TypeAliasDecl>(VD));
627619

628620
// For functions and subscripts, resolve the parameter and result types and
629-
// note them as inference sources.
621+
// note them as requirement inference sources.
630622
if (subscr || func || (macro && macro->parameterList)) {
631623
const auto baseOptions =
632624
TypeResolutionOptions(func ? TypeResolverContext::AbstractFunctionDecl
@@ -688,14 +680,16 @@ GenericSignatureRequest::evaluate(Evaluator &evaluator,
688680
genericParams = nullptr;
689681

690682
collectAdditionalExtensionRequirements(ext->getExtendedType(), sameTypeReqs);
691-
692-
// Re-use the signature of the type being extended by default.
683+
684+
// Re-use the signature of the type being extended by default.
693685
if (sameTypeReqs.empty() && !ext->getTrailingWhereClause()) {
694686
return parentSig;
695687
}
696688

697-
// Allow parameters to be equated with concrete types.
689+
// Extensions allow parameters to be equated with concrete types.
698690
allowConcreteGenericParams = true;
691+
} else {
692+
llvm_unreachable("Unknown generic declaration kind");
699693
}
700694

701695
auto request = InferredGenericSignatureRequest{

0 commit comments

Comments
 (0)