@@ -4175,7 +4175,7 @@ namespace {
4175
4175
Type baseTy, leafTy;
4176
4176
Type exprType = cs.getType (E);
4177
4177
if (auto fnTy = exprType->getAs <FunctionType>()) {
4178
- baseTy = fnTy->getParams ()[0 ].getPlainType ();
4178
+ baseTy = fnTy->getParams ()[0 ].getParameterType ();
4179
4179
leafTy = fnTy->getResult ();
4180
4180
isFunctionType = true ;
4181
4181
} else {
@@ -4184,12 +4184,15 @@ namespace {
4184
4184
leafTy = keyPathTy->getGenericArgs ()[1 ];
4185
4185
}
4186
4186
4187
+ // Track the type of the current component. Once we finish projecting
4188
+ // through each component of the key path, we should reach the leafTy.
4189
+ auto componentTy = baseTy;
4187
4190
for (unsigned i : indices (E->getComponents ())) {
4188
4191
auto &origComponent = E->getMutableComponents ()[i];
4189
4192
4190
4193
// If there were unresolved types, we may end up with a null base for
4191
4194
// following components.
4192
- if (!baseTy ) {
4195
+ if (!componentTy ) {
4193
4196
resolvedComponents.push_back (origComponent);
4194
4197
continue ;
4195
4198
}
@@ -4258,9 +4261,9 @@ namespace {
4258
4261
didOptionalChain = true ;
4259
4262
// Chaining always forces the element to be an rvalue.
4260
4263
auto objectTy =
4261
- baseTy ->getWithoutSpecifierType ()->getOptionalObjectType ();
4262
- if (baseTy ->hasUnresolvedType () && !objectTy) {
4263
- objectTy = baseTy ;
4264
+ componentTy ->getWithoutSpecifierType ()->getOptionalObjectType ();
4265
+ if (componentTy ->hasUnresolvedType () && !objectTy) {
4266
+ objectTy = componentTy ;
4264
4267
}
4265
4268
assert (objectTy);
4266
4269
@@ -4280,7 +4283,7 @@ namespace {
4280
4283
}
4281
4284
case KeyPathExpr::Component::Kind::Identity: {
4282
4285
auto component = origComponent;
4283
- component.setComponentType (baseTy );
4286
+ component.setComponentType (componentTy );
4284
4287
resolvedComponents.push_back (component);
4285
4288
break ;
4286
4289
}
@@ -4291,21 +4294,20 @@ namespace {
4291
4294
llvm_unreachable (" already resolved" );
4292
4295
}
4293
4296
4294
- // Update "baseTy " with the result type of the last component.
4297
+ // Update "componentTy " with the result type of the last component.
4295
4298
assert (!resolvedComponents.empty ());
4296
- baseTy = resolvedComponents.back ().getComponentType ();
4299
+ componentTy = resolvedComponents.back ().getComponentType ();
4297
4300
}
4298
4301
4299
4302
// Wrap a non-optional result if there was chaining involved.
4300
- if (didOptionalChain &&
4301
- baseTy &&
4302
- !baseTy->hasUnresolvedType () &&
4303
- !baseTy->getWithoutSpecifierType ()->isEqual (leafTy)) {
4303
+ if (didOptionalChain && componentTy &&
4304
+ !componentTy->hasUnresolvedType () &&
4305
+ !componentTy->getWithoutSpecifierType ()->isEqual (leafTy)) {
4304
4306
assert (leafTy->getOptionalObjectType ()->isEqual (
4305
- baseTy ->getWithoutSpecifierType ()));
4307
+ componentTy ->getWithoutSpecifierType ()));
4306
4308
auto component = KeyPathExpr::Component::forOptionalWrap (leafTy);
4307
4309
resolvedComponents.push_back (component);
4308
- baseTy = leafTy;
4310
+ componentTy = leafTy;
4309
4311
}
4310
4312
4311
4313
// Set the resolved components, and cache their types.
@@ -4331,8 +4333,8 @@ namespace {
4331
4333
4332
4334
// The final component type ought to line up with the leaf type of the
4333
4335
// key path.
4334
- assert (!baseTy || baseTy ->hasUnresolvedType ()
4335
- || baseTy ->getWithoutSpecifierType ()->isEqual (leafTy));
4336
+ assert (!componentTy || componentTy ->hasUnresolvedType ()
4337
+ || componentTy ->getWithoutSpecifierType ()->isEqual (leafTy));
4336
4338
4337
4339
if (!isFunctionType)
4338
4340
return E;
@@ -4342,9 +4344,6 @@ namespace {
4342
4344
// this; we're going to change E's type to KeyPath<baseTy, leafTy> and
4343
4345
// then wrap it in a larger closure expression with the appropriate type.
4344
4346
4345
- // baseTy has been overwritten by the loop above; restore it.
4346
- baseTy = exprType->getAs <FunctionType>()->getParams ()[0 ].getPlainType ();
4347
-
4348
4347
// Compute KeyPath<baseTy, leafTy> and set E's type back to it.
4349
4348
auto kpDecl = cs.getASTContext ().getKeyPathDecl ();
4350
4349
auto keyPathTy =
0 commit comments