@@ -1127,18 +1127,24 @@ void ExtensionDecl::createGenericParamsIfMissing(NominalTypeDecl *nominal) {
1127
1127
1128
1128
configureOuterGenericParams (nominal);
1129
1129
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));
1138
1145
}
1139
1146
1140
1147
// Set the depth of every generic parameter.
1141
- auto *genericParams = getGenericParams ();
1142
1148
for (auto *outerParams = genericParams;
1143
1149
outerParams != nullptr ;
1144
1150
outerParams = outerParams->getOuterParameters ())
0 commit comments