@@ -4219,7 +4219,7 @@ namespace {
4219
4219
Type baseTy, leafTy;
4220
4220
Type exprType = cs.getType (E);
4221
4221
if (auto fnTy = exprType->getAs <FunctionType>()) {
4222
- baseTy = fnTy->getParams ()[0 ].getPlainType ();
4222
+ baseTy = fnTy->getParams ()[0 ].getParameterType ();
4223
4223
leafTy = fnTy->getResult ();
4224
4224
isFunctionType = true ;
4225
4225
} else {
@@ -4228,12 +4228,15 @@ namespace {
4228
4228
leafTy = keyPathTy->getGenericArgs ()[1 ];
4229
4229
}
4230
4230
4231
+ // Track the type of the current component. Once we finish projecting
4232
+ // through each component of the key path, we should reach the leafTy.
4233
+ auto componentTy = baseTy;
4231
4234
for (unsigned i : indices (E->getComponents ())) {
4232
4235
auto &origComponent = E->getMutableComponents ()[i];
4233
4236
4234
4237
// If there were unresolved types, we may end up with a null base for
4235
4238
// following components.
4236
- if (!baseTy ) {
4239
+ if (!componentTy ) {
4237
4240
resolvedComponents.push_back (origComponent);
4238
4241
continue ;
4239
4242
}
@@ -4255,7 +4258,7 @@ namespace {
4255
4258
if (!foundDecl) {
4256
4259
// If we couldn't resolve the component, leave it alone.
4257
4260
resolvedComponents.push_back (origComponent);
4258
- baseTy = origComponent.getComponentType ();
4261
+ componentTy = origComponent.getComponentType ();
4259
4262
continue ;
4260
4263
}
4261
4264
@@ -4294,9 +4297,9 @@ namespace {
4294
4297
didOptionalChain = true ;
4295
4298
// Chaining always forces the element to be an rvalue.
4296
4299
auto objectTy =
4297
- baseTy ->getWithoutSpecifierType ()->getOptionalObjectType ();
4298
- if (baseTy ->hasUnresolvedType () && !objectTy) {
4299
- objectTy = baseTy ;
4300
+ componentTy ->getWithoutSpecifierType ()->getOptionalObjectType ();
4301
+ if (componentTy ->hasUnresolvedType () && !objectTy) {
4302
+ objectTy = componentTy ;
4300
4303
}
4301
4304
assert (objectTy);
4302
4305
@@ -4316,7 +4319,7 @@ namespace {
4316
4319
}
4317
4320
case KeyPathExpr::Component::Kind::Identity: {
4318
4321
auto component = origComponent;
4319
- component.setComponentType (baseTy );
4322
+ component.setComponentType (componentTy );
4320
4323
resolvedComponents.push_back (component);
4321
4324
break ;
4322
4325
}
@@ -4327,21 +4330,20 @@ namespace {
4327
4330
llvm_unreachable (" already resolved" );
4328
4331
}
4329
4332
4330
- // Update "baseTy " with the result type of the last component.
4333
+ // Update "componentTy " with the result type of the last component.
4331
4334
assert (!resolvedComponents.empty ());
4332
- baseTy = resolvedComponents.back ().getComponentType ();
4335
+ componentTy = resolvedComponents.back ().getComponentType ();
4333
4336
}
4334
4337
4335
4338
// Wrap a non-optional result if there was chaining involved.
4336
- if (didOptionalChain &&
4337
- baseTy &&
4338
- !baseTy->hasUnresolvedType () &&
4339
- !baseTy->getWithoutSpecifierType ()->isEqual (leafTy)) {
4339
+ if (didOptionalChain && componentTy &&
4340
+ !componentTy->hasUnresolvedType () &&
4341
+ !componentTy->getWithoutSpecifierType ()->isEqual (leafTy)) {
4340
4342
assert (leafTy->getOptionalObjectType ()->isEqual (
4341
- baseTy ->getWithoutSpecifierType ()));
4343
+ componentTy ->getWithoutSpecifierType ()));
4342
4344
auto component = KeyPathExpr::Component::forOptionalWrap (leafTy);
4343
4345
resolvedComponents.push_back (component);
4344
- baseTy = leafTy;
4346
+ componentTy = leafTy;
4345
4347
}
4346
4348
4347
4349
// Set the resolved components, and cache their types.
@@ -4367,8 +4369,8 @@ namespace {
4367
4369
4368
4370
// The final component type ought to line up with the leaf type of the
4369
4371
// key path.
4370
- assert (!baseTy || baseTy ->hasUnresolvedType ()
4371
- || baseTy ->getWithoutSpecifierType ()->isEqual (leafTy));
4372
+ assert (!componentTy || componentTy ->hasUnresolvedType ()
4373
+ || componentTy ->getWithoutSpecifierType ()->isEqual (leafTy));
4372
4374
4373
4375
if (!isFunctionType)
4374
4376
return E;
@@ -4378,9 +4380,6 @@ namespace {
4378
4380
// this; we're going to change E's type to KeyPath<baseTy, leafTy> and
4379
4381
// then wrap it in a larger closure expression with the appropriate type.
4380
4382
4381
- // baseTy has been overwritten by the loop above; restore it.
4382
- baseTy = exprType->getAs <FunctionType>()->getParams ()[0 ].getPlainType ();
4383
-
4384
4383
// Compute KeyPath<baseTy, leafTy> and set E's type back to it.
4385
4384
auto kpDecl = cs.getASTContext ().getKeyPathDecl ();
4386
4385
auto keyPathTy =
0 commit comments