@@ -1250,27 +1250,34 @@ PotentialBindings::inferFromRelational(Constraint *constraint) {
1250
1250
return llvm::None;
1251
1251
1252
1252
if (TypeVar->getImpl ().isKeyPathType ()) {
1253
- auto *BGT = type->lookThroughAllOptionalTypes ()-> getAs <BoundGenericType> ();
1254
- if (!BGT || ! isKnownKeyPathType (BGT ))
1253
+ auto objectTy = type->lookThroughAllOptionalTypes ();
1254
+ if (!isKnownKeyPathType (objectTy ))
1255
1255
return llvm::None;
1256
-
1257
- // `PartialKeyPath<T>` represents a type-erased version of `KeyPath<T, V>`.
1256
+
1257
+ auto &ctx = CS.getASTContext ();
1258
+ auto *keyPathTypeLoc = TypeVar->getImpl ().getLocator ();
1259
+ auto *keyPath = castToExpr<KeyPathExpr>(keyPathTypeLoc->getAnchor ());
1260
+ // `AnyKeyPath` and `PartialKeyPath` represent type-erased versions of
1261
+ // `KeyPath<T, V>`.
1258
1262
//
1259
- // In situations where partial key path cannot be used directly i.e.
1260
- // passing an argument to a parameter represented by a partial key path,
1261
- // let's attempt a `KeyPath` binding which would then be converted to a
1262
- // partial key path since there is a subtype relationship between them.
1263
- if (BGT->isPartialKeyPath () && kind == AllowedBindingKind::Subtypes) {
1264
- auto &ctx = CS.getASTContext ();
1265
- auto *keyPathLoc = TypeVar->getImpl ().getLocator ();
1266
-
1267
- auto rootTy = BGT->getGenericArgs ()[0 ];
1263
+ // In situations where `AnyKeyPath` or `PartialKeyPath` cannot be used
1264
+ // directly i.e. passing an argument to a parameter represented by a
1265
+ // `AnyKeyPath` or `PartialKeyPath`, let's attempt a `KeyPath` binding which
1266
+ // would then be converted to a `AnyKeyPath` or `PartialKeyPath` since there
1267
+ // is a subtype relationship between them.
1268
+ if (objectTy->isAnyKeyPath ()) {
1269
+ auto root = CS.getKeyPathRootType (keyPath);
1270
+ auto value = CS.getKeyPathValueType (keyPath);
1271
+
1272
+ type = BoundGenericType::get (ctx.getKeyPathDecl (), Type (),
1273
+ {root, value});
1274
+ } else if (objectTy->isPartialKeyPath () &&
1275
+ kind == AllowedBindingKind::Subtypes) {
1276
+ auto rootTy = objectTy->castTo <BoundGenericType>()->getGenericArgs ()[0 ];
1268
1277
// Since partial key path is an erased version of `KeyPath`, the value
1269
1278
// type would never be used, which means that binding can use
1270
1279
// type variable generated for a result of key path expression.
1271
- auto valueTy =
1272
- keyPathLoc->castLastElementTo <LocatorPathElt::KeyPathType>()
1273
- .getValueType ();
1280
+ auto valueTy = CS.getKeyPathValueType (keyPath);
1274
1281
1275
1282
type = BoundGenericType::get (ctx.getKeyPathDecl (), Type (),
1276
1283
{rootTy, valueTy});
0 commit comments