@@ -5116,7 +5116,7 @@ namespace {
5116
5116
!componentTy->getWithoutSpecifierType ()->isEqual (leafTy)) {
5117
5117
auto component = KeyPathExpr::Component::forOptionalWrap (leafTy);
5118
5118
resolvedComponents.push_back (component);
5119
- componentTy = leafTy ;
5119
+ componentTy = OptionalType::get (componentTy) ;
5120
5120
}
5121
5121
5122
5122
// Set the resolved components, and cache their types.
@@ -5132,13 +5132,23 @@ namespace {
5132
5132
5133
5133
// If we've gotten here, the user has used key path literal syntax to form
5134
5134
// a closure. The type checker has given E a function type to indicate
5135
- // this; we're going to change E's type to KeyPath<baseTy, leafTy> and
5136
- // then wrap it in a larger closure expression with the appropriate type.
5135
+ // this.
5136
+ //
5137
+ // Since functions support more conversions than generic types, we may
5138
+ // have ended up with a type of (baseTy) -> leafTy, where the actual type
5139
+ // of the key path is some subclass of KeyPath<baseTy, componentTy>, and
5140
+ // with componentTy: leafTy.
5141
+ //
5142
+ // We're going to change E's type to KeyPath<baseTy, componentTy> and
5143
+ // then wrap it in a larger closure expression which we will convert to
5144
+ // appropriate type.
5145
+
5146
+ auto kpResultTy = componentTy->getWithoutSpecifierType ();
5137
5147
5138
5148
// Compute KeyPath<baseTy, leafTy> and set E's type back to it.
5139
5149
auto kpDecl = cs.getASTContext ().getKeyPathDecl ();
5140
5150
auto keyPathTy =
5141
- BoundGenericType::get (kpDecl, nullptr , { baseTy, leafTy });
5151
+ BoundGenericType::get (kpDecl, nullptr , { baseTy, kpResultTy });
5142
5152
E->setType (keyPathTy);
5143
5153
cs.cacheType (E);
5144
5154
@@ -5153,9 +5163,10 @@ namespace {
5153
5163
5154
5164
FunctionType::ExtInfo closureInfo;
5155
5165
auto closureTy =
5156
- FunctionType::get ({FunctionType::Param (baseTy)}, leafTy, closureInfo);
5166
+ FunctionType::get ({FunctionType::Param (baseTy)}, kpResultTy,
5167
+ closureInfo);
5157
5168
auto closure = new (ctx)
5158
- AutoClosureExpr (/* set body later*/ nullptr , leafTy , dc);
5169
+ AutoClosureExpr (/* set body later*/ nullptr , kpResultTy , dc);
5159
5170
5160
5171
auto param = new (ctx) ParamDecl (
5161
5172
SourceLoc (),
@@ -5207,7 +5218,7 @@ namespace {
5207
5218
auto *application = new (ctx)
5208
5219
KeyPathApplicationExpr (paramRef,
5209
5220
E->getStartLoc (), outerParamRef, E->getEndLoc (),
5210
- leafTy , /* implicit=*/ true );
5221
+ kpResultTy , /* implicit=*/ true );
5211
5222
cs.cacheType (application);
5212
5223
5213
5224
// Finish up the inner closure.
0 commit comments