@@ -297,10 +297,16 @@ static Type addCurriedSelfType(ASTContext &ctx, Type type, DeclContext *dc) {
297297 if (!dc->isTypeContext ())
298298 return type;
299299
300- auto nominal = dc->getAsNominalTypeOrNominalTypeExtensionContext ();
301- auto selfTy = nominal->getInterfaceType ()->castTo <MetatypeType>()
302- ->getInstanceType ();
303- if (auto sig = nominal->getGenericSignatureOfContext ())
300+ GenericSignature *sig = dc->getGenericSignatureOfContext ();
301+ if (auto *genericFn = type->getAs <GenericFunctionType>()) {
302+ sig = genericFn->getGenericSignature ();
303+ type = FunctionType::get (genericFn->getInput (),
304+ genericFn->getResult (),
305+ genericFn->getExtInfo ());
306+ }
307+
308+ auto selfTy = dc->getDeclaredInterfaceType ();
309+ if (sig)
304310 return GenericFunctionType::get (sig, selfTy, type,
305311 AnyFunctionType::ExtInfo ());
306312 return FunctionType::get (selfTy, type);
@@ -315,31 +321,38 @@ static bool isDeclMoreConstrainedThan(ValueDecl *decl1, ValueDecl *decl2) {
315321
316322 if (decl1->getKind () != decl2->getKind () || isa<TypeDecl>(decl1))
317323 return false ;
318-
324+
325+ GenericParamList *gp1 = nullptr , *gp2 = nullptr ;
326+
319327 auto func1 = dyn_cast<FuncDecl>(decl1);
320328 auto func2 = dyn_cast<FuncDecl>(decl2);
321-
322329 if (func1 && func2) {
323-
324- auto gp1 = func1->getGenericParams ();
325- auto gp2 = func2->getGenericParams ();
326-
327- if (gp1 && gp2) {
328- auto params1 = gp1->getParams ();
329- auto params2 = gp2->getParams ();
330+ gp1 = func1->getGenericParams ();
331+ gp2 = func2->getGenericParams ();
332+ }
333+
334+ auto subscript1 = dyn_cast<SubscriptDecl>(decl1);
335+ auto subscript2 = dyn_cast<SubscriptDecl>(decl2);
336+ if (subscript1 && subscript2) {
337+ gp1 = subscript1->getGenericParams ();
338+ gp2 = subscript2->getGenericParams ();
339+ }
340+
341+ if (gp1 && gp2) {
342+ auto params1 = gp1->getParams ();
343+ auto params2 = gp2->getParams ();
330344
331- if (params1.size () == params2.size ()) {
332- for (size_t i = 0 ; i < params1.size (); i++) {
333- auto p1 = params1[i];
334- auto p2 = params2[i];
345+ if (params1.size () == params2.size ()) {
346+ for (size_t i = 0 ; i < params1.size (); i++) {
347+ auto p1 = params1[i];
348+ auto p2 = params2[i];
335349
336- int np1 = static_cast <int >(p1->getConformingProtocols ().size ());
337- int np2 = static_cast <int >(p2->getConformingProtocols ().size ());
338- int aDelta = np1 - np2;
350+ int np1 = static_cast <int >(p1->getConformingProtocols ().size ());
351+ int np2 = static_cast <int >(p2->getConformingProtocols ().size ());
352+ int aDelta = np1 - np2;
339353
340- if (aDelta)
341- return aDelta > 0 ;
342- }
354+ if (aDelta)
355+ return aDelta > 0 ;
343356 }
344357 }
345358 }
@@ -475,9 +488,14 @@ static bool isDeclAsSpecializedAs(TypeChecker &tc, DeclContext *dc,
475488 // A non-generic declaration is more specialized than a generic declaration.
476489 if (auto func1 = dyn_cast<AbstractFunctionDecl>(decl1)) {
477490 auto func2 = cast<AbstractFunctionDecl>(decl2);
478- if (static_cast <bool >(func1->getGenericParams ()) !=
479- static_cast <bool >(func2->getGenericParams ()))
480- return func2->getGenericParams ();
491+ if (func1->isGeneric () != func2->isGeneric ())
492+ return func2->isGeneric ();
493+ }
494+
495+ if (auto subscript1 = dyn_cast<SubscriptDecl>(decl1)) {
496+ auto subscript2 = cast<SubscriptDecl>(decl2);
497+ if (subscript1->isGeneric () != subscript2->isGeneric ())
498+ return subscript2->isGeneric ();
481499 }
482500
483501 // A witness is always more specialized than the requirement it satisfies.
@@ -531,8 +549,6 @@ static bool isDeclAsSpecializedAs(TypeChecker &tc, DeclContext *dc,
531549 }
532550 } else {
533551 // Add a curried 'self' type.
534- assert (!type1->is <GenericFunctionType>() && " Odd generic function type?" );
535- assert (!type2->is <GenericFunctionType>() && " Odd generic function type?" );
536552 type1 = addCurriedSelfType (tc.Context , type1, decl1->getDeclContext ());
537553 type2 = addCurriedSelfType (tc.Context , type2, decl2->getDeclContext ());
538554
0 commit comments