@@ -2510,8 +2510,27 @@ namespace {
25102510 // Coerce the index argument.
25112511 auto openedFullFnType = simplifyType (selected.adjustedOpenedFullType )
25122512 ->castTo <FunctionType>();
2513+
2514+ auto openedFullFnTypeSelf = openedFullFnType;
2515+
2516+ // Now, deal with DynamicSelfType.
2517+ if (selected.adjustedOpenedFullType ->hasDynamicSelfType ()) {
2518+ openedFullFnTypeSelf = simplifyType (
2519+ selected.adjustedOpenedFullType ->eraseDynamicSelfType ())
2520+ ->castTo <FunctionType>();
2521+ auto replacementTy = getDynamicSelfReplacementType (
2522+ baseTy, subscript, memberLoc);
2523+ openedFullFnType = simplifyType (
2524+ selected.adjustedOpenedFullType
2525+ ->replaceDynamicSelfType (replacementTy))
2526+ ->castTo <FunctionType>();
2527+ }
2528+
25132529 auto fullSubscriptTy = openedFullFnType->getResult ()
25142530 ->castTo <FunctionType>();
2531+ auto fullSubscriptTySelf = openedFullFnTypeSelf->getResult ()
2532+ ->castTo <FunctionType>();
2533+
25152534 auto appliedWrappers =
25162535 solution.getAppliedPropertyWrappers (memberLoc->getAnchor ());
25172536 args = coerceCallArguments (
@@ -2565,38 +2584,28 @@ namespace {
25652584 if (!base)
25662585 return nullptr ;
25672586
2568- bool hasDynamicSelf = fullSubscriptTy->hasDynamicSelfType ();
2569-
25702587 // Form the subscript expression.
25712588 auto subscriptExpr = SubscriptExpr::create (
25722589 ctx, base, args, subscriptRef, isImplicit, semantics);
25732590 subscriptExpr->setIsSuper (isSuper);
25742591
2575- if (!hasDynamicSelf) {
2576- cs.setType (subscriptExpr, fullSubscriptTy->getResult ());
2577- } else {
2578- cs.setType (subscriptExpr,
2579- fullSubscriptTy->getResult ()
2580- ->replaceDynamicSelfType (containerTy));
2581- }
2592+ cs.setType (subscriptExpr, fullSubscriptTySelf->getResult ());
25822593
25832594 Expr *result = subscriptExpr;
2584- closeExistentials (result, locator);
25852595
25862596 // If the element is of dynamic 'Self' type, wrap an implicit conversion
25872597 // around the resulting expression, with the destination type having
25882598 // 'Self' swapped for the appropriate replacement type -- usually the
25892599 // base object type.
2590- if (hasDynamicSelf) {
2591- const auto conversionTy = simplifyType (
2592- selected.adjustedOpenedType ->castTo <FunctionType>()->getResult ());
2593-
2594- if (!containerTy->isEqual (conversionTy)) {
2595- result = cs.cacheType (
2596- new (ctx) CovariantReturnConversionExpr (result, conversionTy));
2597- }
2600+ if (!fullSubscriptTy->getResult ()->isEqual (
2601+ fullSubscriptTySelf->getResult ())) {
2602+ result = cs.cacheType (
2603+ new (ctx) CovariantReturnConversionExpr (
2604+ result, fullSubscriptTy->getResult ()));
25982605 }
25992606
2607+ closeExistentials (result, locator);
2608+
26002609 return result;
26012610 }
26022611
0 commit comments