@@ -1162,12 +1162,27 @@ namespace {
11621162
11631163 // Add the constraint that the index expression's type be convertible
11641164 // to the input type of the subscript operator.
1165- // We add this as an unsolved constraint before adding the member
1166- // constraint since some member constraints such as key-path dynamic
1167- // member require the applicable function constraint to be available.
1168- CS.addUnsolvedConstraint (Constraint::createApplicableFunction (
1169- CS, FunctionType::get (params, outputTy), memberTy,
1170- /* trailingClosureMatching=*/ std::nullopt , CurDC, fnLocator));
1165+ auto addApplicableFn = [&]() {
1166+ CS.addApplicationConstraint (
1167+ FunctionType::get (params, outputTy), memberTy,
1168+ /* trailingClosureMatching=*/ std::nullopt , CurDC, fnLocator);
1169+ };
1170+
1171+ // If we have a dynamic member base we need to add the applicable function
1172+ // first since solving the member constraint requires the constraint to be
1173+ // available since it may retire it. We can't yet do this in the general
1174+ // case since the simplifying of the applicable fn in CSGen is currently
1175+ // load-bearing for existential opening.
1176+ // FIXME: Once existential opening is no longer sensitive to solving
1177+ // order, we ought to be able to just always record the applicable fn as
1178+ // an unsolved constraint before the member.
1179+ auto hasDynamicMemberLookup = CS.simplifyType (baseTy)
1180+ ->getRValueType ()
1181+ ->getMetatypeInstanceType ()
1182+ ->eraseDynamicSelfType ()
1183+ ->hasDynamicMemberLookupAttribute ();
1184+ if (hasDynamicMemberLookup)
1185+ addApplicableFn ();
11711186
11721187 // FIXME: synthesizeMaterializeForSet() wants to statically dispatch to
11731188 // a known subscript here. This might be cleaner if we split off a new
@@ -1184,6 +1199,11 @@ namespace {
11841199 /* outerAlternatives=*/ {}, memberLocator);
11851200 }
11861201
1202+ // If we don't have a dynamic member, we add the application after the
1203+ // member, see the above comment.
1204+ if (!hasDynamicMemberLookup)
1205+ addApplicableFn ();
1206+
11871207 if (CS.performanceHacksEnabled ()) {
11881208 Type fixedOutputType =
11891209 CS.getFixedTypeRecursive (outputTy, /* wantRValue=*/ false );
0 commit comments