@@ -400,6 +400,38 @@ _findExtendedTypeContextDescriptor(const ContextDescriptor *maybeExtension,
400
400
Demangle::NodePointer &node = demangledNode ? *demangledNode : localNode;
401
401
402
402
auto mangledName = extension->getMangledExtendedContext ();
403
+
404
+ // A extension of the form `extension Protocol where Self == ConcreteType`
405
+ // is formally a protocol extension, so the formal generic parameter list
406
+ // is `<Self>`, but because of the same type constraint, the extended context
407
+ // looks like a reference to that nominal type. We want to match the
408
+ // extension's formal generic environment rather than the nominal type's
409
+ // in this case, so we should skip out on this case.
410
+ //
411
+ // We can detect this by looking at whether the generic context of the
412
+ // extension has a first generic parameter, which would be the Self parameter,
413
+ // with a same type constraint matching the extended type.
414
+ for (auto &reqt : extension->getGenericRequirements ()) {
415
+ if (reqt.getKind () != GenericRequirementKind::SameType) {
416
+ continue ;
417
+ }
418
+ // 'x' is the mangling of the first generic parameter
419
+ if (!reqt.getParam ().equals (" x" )) {
420
+ continue ;
421
+ }
422
+ // Is the generic parameter same-type-constrained to the same type
423
+ // we're extending? Then this is a `Self == ExtendedType` constraint.
424
+ // This is impossible for normal generic nominal type extensions because
425
+ // that would mean that you had:
426
+ // struct Foo<T> {...}
427
+ // extension Foo where T == Foo<T> {...}
428
+ // which would mean that the extended type is the infinite expansion
429
+ // Foo<Foo<Foo<Foo<...>>>>, which we don't allow.
430
+ if (reqt.getMangledTypeName ().data () == mangledName.data ()) {
431
+ return nullptr ;
432
+ }
433
+ }
434
+
403
435
node = demangler.demangleType (mangledName,
404
436
ResolveAsSymbolicReference (demangler));
405
437
if (!node)
@@ -1252,7 +1284,8 @@ _gatherGenericParameters(const ContextDescriptor *context,
1252
1284
(void )_gatherGenericParameterCounts (context,
1253
1285
genericParamCounts, demangler);
1254
1286
unsigned numTotalGenericParams =
1255
- genericParamCounts.empty () ? 0 : genericParamCounts.back ();
1287
+ genericParamCounts.empty () ? context->getNumGenericParams ()
1288
+ : genericParamCounts.back ();
1256
1289
1257
1290
// Check whether we have the right number of generic arguments.
1258
1291
if (genericArgs.size () == getLocalGenericParams (context).size ()) {
0 commit comments