Skip to content

Commit 7a038ff

Browse files
committed
AST: Refactor ExtensionDecl::createGenericParamsIfMissing()
Let's use the same code path for protocol and non-protocol extensions; we still have to do something special for protocol extensions though.
1 parent e0c1e81 commit 7a038ff

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

lib/AST/Decl.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,18 +1127,24 @@ void ExtensionDecl::createGenericParamsIfMissing(NominalTypeDecl *nominal) {
11271127

11281128
configureOuterGenericParams(nominal);
11291129

1130-
if (auto proto = dyn_cast<ProtocolDecl>(nominal)) {
1131-
// For a protocol extension, build the generic parameter list directly
1132-
// since we want it to have an inheritance clause.
1133-
setGenericParams(proto->createGenericParams(this));
1134-
} else if (auto genericParams = nominal->getGenericParamsOfContext()) {
1135-
// Clone the generic parameter list of a generic type.
1136-
setGenericParams(
1137-
cloneGenericParams(getASTContext(), this, genericParams));
1130+
// Create the generic parameter list for the extension by cloning the
1131+
// generic parameter lists of the nominal and any of its parent types.
1132+
auto &ctx = getASTContext();
1133+
auto *genericParams = nominal->getGenericParamsOfContext();
1134+
if (genericParams != nullptr)
1135+
genericParams = cloneGenericParams(ctx, this, genericParams);
1136+
setGenericParams(genericParams);
1137+
1138+
// Protocol extensions need an inheritance clause due to how name lookup
1139+
// is implemented.
1140+
if (auto *proto = dyn_cast<ProtocolDecl>(nominal)) {
1141+
auto protoType = proto->getDeclaredType();
1142+
TypeLoc selfInherited[1] = { TypeLoc::withoutLoc(protoType) };
1143+
genericParams->getParams().front()->setInherited(
1144+
ctx.AllocateCopy(selfInherited));
11381145
}
11391146

11401147
// Set the depth of every generic parameter.
1141-
auto *genericParams = getGenericParams();
11421148
for (auto *outerParams = genericParams;
11431149
outerParams != nullptr;
11441150
outerParams = outerParams->getOuterParameters())

0 commit comments

Comments
 (0)