@@ -4181,13 +4181,6 @@ namespace {
4181
4181
leafTy = keyPathTy->getGenericArgs ()[1 ];
4182
4182
}
4183
4183
4184
- // Updates the constraint system with the type of the last resolved
4185
- // component. We do it this way because we sometimes insert new
4186
- // components.
4187
- auto updateCSWithResolvedComponent = [&]() {
4188
- cs.setType (E, resolvedComponents.size () - 1 , baseTy);
4189
- };
4190
-
4191
4184
for (unsigned i : indices (E->getComponents ())) {
4192
4185
auto &origComponent = E->getMutableComponents ()[i];
4193
4186
@@ -4248,7 +4241,6 @@ namespace {
4248
4241
}
4249
4242
}
4250
4243
4251
- KeyPathExpr::Component component;
4252
4244
switch (kind) {
4253
4245
case KeyPathExpr::Component::Kind::UnresolvedProperty: {
4254
4246
// If we couldn't resolve the component, leave it alone.
@@ -4257,19 +4249,16 @@ namespace {
4257
4249
break ;
4258
4250
}
4259
4251
4260
- component = buildKeyPathPropertyComponent (
4252
+ auto component = buildKeyPathPropertyComponent (
4261
4253
*foundDecl, origComponent.getLoc (), locator);
4262
4254
4263
- baseTy = component.getComponentType ();
4264
4255
resolvedComponents.push_back (component);
4265
4256
4266
4257
if (shouldForceUnwrapResult (foundDecl->choice , locator)) {
4267
- updateCSWithResolvedComponent ();
4268
4258
auto objectTy = getObjectType (baseTy);
4269
4259
auto loc = origComponent.getLoc ();
4270
- component = KeyPathExpr::Component::forOptionalForce (objectTy, loc);
4271
- baseTy = component.getComponentType ();
4272
- resolvedComponents.push_back (component);
4260
+ resolvedComponents.push_back (
4261
+ KeyPathExpr::Component::forOptionalForce (objectTy, loc));
4273
4262
}
4274
4263
break ;
4275
4264
}
@@ -4284,20 +4273,16 @@ namespace {
4284
4273
if (!isDynamicMember)
4285
4274
subscriptLabels = origComponent.getSubscriptLabels ();
4286
4275
4287
- component = buildKeyPathSubscriptComponent (
4276
+ auto component = buildKeyPathSubscriptComponent (
4288
4277
*foundDecl, origComponent.getLoc (), origComponent.getIndexExpr (),
4289
4278
subscriptLabels, locator);
4290
-
4291
- baseTy = component.getComponentType ();
4292
4279
resolvedComponents.push_back (component);
4293
4280
4294
4281
if (shouldForceUnwrapResult (foundDecl->choice , locator)) {
4295
- updateCSWithResolvedComponent ();
4296
4282
auto objectTy = getObjectType (baseTy);
4297
4283
auto loc = origComponent.getLoc ();
4298
- component = KeyPathExpr::Component::forOptionalForce (objectTy, loc);
4299
- baseTy = component.getComponentType ();
4300
- resolvedComponents.push_back (component);
4284
+ resolvedComponents.push_back (
4285
+ KeyPathExpr::Component::forOptionalForce (objectTy, loc));
4301
4286
}
4302
4287
break ;
4303
4288
}
@@ -4312,41 +4297,39 @@ namespace {
4312
4297
assert (objectTy);
4313
4298
4314
4299
auto loc = origComponent.getLoc ();
4315
- component = KeyPathExpr::Component::forOptionalChain (objectTy, loc);
4316
-
4317
- baseTy = component.getComponentType ();
4318
- resolvedComponents.push_back (component);
4300
+ resolvedComponents.push_back (
4301
+ KeyPathExpr::Component::forOptionalChain (objectTy, loc));
4319
4302
break ;
4320
4303
}
4321
4304
case KeyPathExpr::Component::Kind::OptionalForce: {
4322
4305
auto objectTy = getObjectType (baseTy);
4323
4306
auto loc = origComponent.getLoc ();
4324
- component = KeyPathExpr::Component::forOptionalForce (objectTy, loc);
4325
- baseTy = component.getComponentType ();
4326
- resolvedComponents.push_back (component);
4307
+ resolvedComponents.push_back (
4308
+ KeyPathExpr::Component::forOptionalForce (objectTy, loc));
4327
4309
break ;
4328
4310
}
4329
- case KeyPathExpr::Component::Kind::Invalid:
4330
- component = origComponent;
4311
+ case KeyPathExpr::Component::Kind::Invalid: {
4312
+ auto component = origComponent;
4331
4313
component.setComponentType (leafTy);
4332
-
4333
- baseTy = component.getComponentType ();
4334
4314
resolvedComponents.push_back (component);
4335
4315
break ;
4336
- case KeyPathExpr::Component::Kind::Identity:
4337
- component = origComponent;
4316
+ }
4317
+ case KeyPathExpr::Component::Kind::Identity: {
4318
+ auto component = origComponent;
4338
4319
component.setComponentType (baseTy);
4339
4320
resolvedComponents.push_back (component);
4340
4321
break ;
4322
+ }
4341
4323
case KeyPathExpr::Component::Kind::Property:
4342
4324
case KeyPathExpr::Component::Kind::Subscript:
4343
4325
case KeyPathExpr::Component::Kind::OptionalWrap:
4344
4326
case KeyPathExpr::Component::Kind::TupleElement:
4345
4327
llvm_unreachable (" already resolved" );
4346
4328
}
4347
4329
4348
- // By now, "baseTy" is the result type of this component.
4349
- updateCSWithResolvedComponent ();
4330
+ // Update "baseTy" with the result type of the last component.
4331
+ assert (!resolvedComponents.empty ());
4332
+ baseTy = resolvedComponents.back ().getComponentType ();
4350
4333
}
4351
4334
4352
4335
// Wrap a non-optional result if there was chaining involved.
@@ -4359,10 +4342,12 @@ namespace {
4359
4342
auto component = KeyPathExpr::Component::forOptionalWrap (leafTy);
4360
4343
resolvedComponents.push_back (component);
4361
4344
baseTy = leafTy;
4362
- updateCSWithResolvedComponent ();
4363
4345
}
4346
+
4347
+ // Set the resolved components, and cache their types.
4364
4348
E->resolveComponents (cs.getASTContext (), resolvedComponents);
4365
-
4349
+ cs.cacheExprTypes (E);
4350
+
4366
4351
// See whether there's an equivalent ObjC key path string we can produce
4367
4352
// for interop purposes.
4368
4353
if (cs.getASTContext ().LangOpts .EnableObjCInterop ) {
0 commit comments