@@ -2222,23 +2222,23 @@ namespace {
22222222 Expr *buildKeyPathDynamicMemberArgExpr (BoundGenericType *keyPathTy,
22232223 SourceLoc dotLoc,
22242224 ConstraintLocator *memberLoc) {
2225+ using Component = KeyPathExpr::Component;
22252226 auto &ctx = cs.getASTContext ();
22262227 auto *anchor = getAsExpr (memberLoc->getAnchor ());
22272228
2228- SmallVector<KeyPathExpr::Component, 2 > components;
2229+ auto makeKeyPath = [&](ArrayRef<Component> components) -> Expr * {
2230+ auto *kp = KeyPathExpr::createImplicit (ctx, /* backslashLoc*/ dotLoc,
2231+ components, anchor->getEndLoc ());
2232+ kp->setType (keyPathTy);
2233+ cs.cacheExprTypes (kp);
22292234
2230- // Let's create a KeyPath expression and fill in "parsed path"
2231- // after component is built.
2232- auto *keyPath = new (ctx) KeyPathExpr (/* backslashLoc=*/ dotLoc,
2233- /* parsedRoot=*/ nullptr ,
2234- /* parsedPath=*/ anchor,
2235- /* hasLeadingDot=*/ false ,
2236- /* isImplicit=*/ true );
2237- // Type of the keypath expression we are forming is known
2238- // in advance, so let's set it right away.
2239- keyPath->setType (keyPathTy);
2240- cs.cacheType (keyPath);
2235+ // See whether there's an equivalent ObjC key path string we can produce
2236+ // for interop purposes.
2237+ checkAndSetObjCKeyPathString (kp);
2238+ return kp;
2239+ };
22412240
2241+ SmallVector<Component, 2 > components;
22422242 auto *componentLoc = cs.getConstraintLocator (
22432243 memberLoc,
22442244 LocatorPathElt::KeyPathDynamicMember (keyPathTy->getAnyNominal ()));
@@ -2251,95 +2251,44 @@ namespace {
22512251 case OverloadChoiceKind::KeyPathDynamicMemberLookup: {
22522252 buildKeyPathSubscriptComponent (overload, dotLoc, /* args=*/ nullptr ,
22532253 componentLoc, components);
2254- keyPath->resolveComponents (ctx, components);
2255- cs.cacheExprTypes (keyPath);
2256- return keyPath;
2254+ return makeKeyPath (components);
22572255 }
22582256
22592257 default :
22602258 break ;
22612259 }
22622260
2263- // We can't reuse existing expression because type-check
2264- // based diagnostics could hold the reference to original AST.
2265- Expr *componentExpr = nullptr ;
2266- auto *dotExpr = new (ctx) KeyPathDotExpr (dotLoc);
2267-
2268- // Determines whether this index is built to be used for
2269- // one of the existing keypath components e.g. `\Lens<[Int]>.count`
2270- // instead of a regular expression e.g. `lens[0]`.
2271- bool forKeyPathComponent = false ;
2272- // Looks like keypath dynamic member lookup was used inside
2273- // of a keypath expression e.g. `\Lens<[Int]>.count` where
2274- // `count` is referenced using dynamic lookup.
22752261 if (auto *KPE = dyn_cast<KeyPathExpr>(anchor)) {
2262+ // Looks like keypath dynamic member lookup was used inside
2263+ // of a keypath expression e.g. `\Lens<[Int]>.count` where
2264+ // `count` is referenced using dynamic lookup.
22762265 auto kpElt = memberLoc->findFirst <LocatorPathElt::KeyPathComponent>();
22772266 assert (kpElt && " no keypath component node" );
2278- auto &origComponent = KPE->getComponents ()[kpElt->getIndex ()];
2279-
2280- using ComponentKind = KeyPathExpr::Component::Kind;
2281- if (origComponent.getKind () == ComponentKind::UnresolvedProperty) {
2282- anchor = new (ctx) UnresolvedDotExpr (
2283- dotExpr, dotLoc, origComponent.getUnresolvedDeclName (),
2284- DeclNameLoc (origComponent.getLoc ()),
2285- /* Implicit=*/ true );
2286- } else if (origComponent.getKind () ==
2287- ComponentKind::UnresolvedSubscript) {
2288- anchor = SubscriptExpr::create (
2289- ctx, dotExpr, origComponent.getSubscriptArgs (), ConcreteDeclRef (),
2290- /* implicit=*/ true , AccessSemantics::Ordinary);
2267+ auto &comp = KPE->getComponents ()[kpElt->getIndex ()];
2268+
2269+ if (comp.getKind () == Component::Kind::UnresolvedProperty) {
2270+ buildKeyPathPropertyComponent (overload, comp.getLoc (), componentLoc,
2271+ components);
2272+ } else if (comp.getKind () == Component::Kind::UnresolvedSubscript) {
2273+ buildKeyPathSubscriptComponent (overload, comp.getLoc (),
2274+ comp.getSubscriptArgs (), componentLoc,
2275+ components);
22912276 } else {
22922277 return nullptr ;
22932278 }
2294-
2295- anchor->setType (simplifyType (overload.openedType ));
2296- cs.cacheType (anchor);
2297- forKeyPathComponent = true ;
2279+ return makeKeyPath (components);
22982280 }
22992281
23002282 if (auto *UDE = dyn_cast<UnresolvedDotExpr>(anchor)) {
2301- componentExpr =
2302- forKeyPathComponent
2303- ? UDE
2304- : new (ctx) UnresolvedDotExpr (dotExpr, dotLoc, UDE->getName (),
2305- UDE->getNameLoc (),
2306- /* Implicit=*/ true );
2307-
23082283 buildKeyPathPropertyComponent (overload, UDE->getLoc (), componentLoc,
23092284 components);
23102285 } else if (auto *SE = dyn_cast<SubscriptExpr>(anchor)) {
2311- componentExpr = SE;
2312- // If this is not for a keypath component, we have to copy
2313- // original subscript expression because expression based
2314- // diagnostics might have a reference to it, so it couldn't
2315- // be modified.
2316- if (!forKeyPathComponent) {
2317- componentExpr = SubscriptExpr::create (
2318- ctx, dotExpr, SE->getArgs (),
2319- SE->hasDecl () ? SE->getDecl () : ConcreteDeclRef (),
2320- /* implicit=*/ true , SE->getAccessSemantics ());
2321- }
2322-
23232286 buildKeyPathSubscriptComponent (overload, SE->getLoc (), SE->getArgs (),
23242287 componentLoc, components);
23252288 } else {
23262289 return nullptr ;
23272290 }
2328-
2329- assert (componentExpr);
2330- Type ty = simplifyType (cs.getType (anchor));
2331- componentExpr->setType (ty);
2332- cs.cacheType (componentExpr);
2333-
2334- keyPath->setParsedPath (componentExpr);
2335- keyPath->resolveComponents (ctx, components);
2336- cs.cacheExprTypes (keyPath);
2337-
2338- // See whether there's an equivalent ObjC key path string we can produce
2339- // for interop purposes.
2340- checkAndSetObjCKeyPathString (keyPath);
2341-
2342- return keyPath;
2291+ return makeKeyPath (components);
23432292 }
23442293
23452294 // / Bridge the given value (which is an error type) to NSError.
@@ -4898,7 +4847,7 @@ namespace {
48984847 }
48994848
49004849 // Set the resolved components, and cache their types.
4901- E->resolveComponents (cs.getASTContext (), resolvedComponents);
4850+ E->setComponents (cs.getASTContext (), resolvedComponents);
49024851 cs.cacheExprTypes (E);
49034852
49044853 // See whether there's an equivalent ObjC key path string we can produce
0 commit comments