@@ -914,6 +914,35 @@ namespace {
914
914
};
915
915
} // end anonymous namespace
916
916
917
+ namespace {
918
+ // Check if \p E is a call expression to curried thunk of "KeyPath as function".
919
+ // i.e. '{ `$kp$` in { $0[keyPath: $kp$] } }(keypath)'
920
+ static bool isKeyPathCurriedThunkCallExpr (Expr *E) {
921
+ auto CE = dyn_cast<CallExpr>(E);
922
+ if (!CE)
923
+ return false ;
924
+ auto thunk = dyn_cast<AutoClosureExpr>(CE->getFn ());
925
+ if (!thunk)
926
+ return false ;
927
+ if (thunk->getParameters ()->size () != 1 ||
928
+ thunk->getParameters ()->get (0 )->getParameterName ().str () != " $kp$" )
929
+ return false ;
930
+
931
+ auto PE = dyn_cast<ParenExpr>(CE->getArg ());
932
+ if (!PE)
933
+ return false ;
934
+ return isa<KeyPathExpr>(PE->getSubExpr ());
935
+ }
936
+
937
+ // Extract the keypath expression from the curried thunk expression.
938
+ static Expr *extractKeyPathFromCurryThunkCall (Expr *E) {
939
+ assert (isKeyPathCurriedThunkCallExpr (E));
940
+ auto call = cast<CallExpr>(E);
941
+ auto arg = cast<ParenExpr>(call->getArg ());
942
+ return arg->getSubExpr ();
943
+ }
944
+ } // end anonymous namespace
945
+
917
946
namespace {
918
947
919
948
class ConstraintGenerator : public ExprVisitor <ConstraintGenerator, Type> {
@@ -3787,13 +3816,20 @@ namespace {
3787
3816
continue ;
3788
3817
}
3789
3818
3819
+ // Extract keypath from '{ `$kp$` in { $0[keyPath: $kp$] } }(keypath)'
3820
+ if (isKeyPathCurriedThunkCallExpr (expr)) {
3821
+ expr = extractKeyPathFromCurryThunkCall (expr);
3822
+ continue ;
3823
+ }
3824
+
3790
3825
// Restore '@autoclosure'd value.
3791
3826
if (auto ACE = dyn_cast<AutoClosureExpr>(expr)) {
3792
3827
// This is only valid if the closure doesn't have parameters.
3793
3828
if (ACE->getParameters ()->size () == 0 ) {
3794
3829
expr = ACE->getSingleExpressionBody ();
3795
3830
continue ;
3796
3831
}
3832
+ llvm_unreachable (" other AutoClosureExpr must be handled specially" );
3797
3833
}
3798
3834
3799
3835
// Remove any semantic expression injected by typechecking.
0 commit comments