From e6abfdebc88f150c86d635c6f4086ea99be626e4 Mon Sep 17 00:00:00 2001 From: Fred Emmott Date: Fri, 13 Dec 2019 10:00:39 -0800 Subject: [PATCH] Fix types for key-based dict/keyset sorting functions (#103) Summary: The builtins are no longer typed as returning a generic, as they can convert a `vec` to a `dict` when keys have defined behavior in the function. Changed by D18937742 Pull Request resolved: https://github.com/hhvm/hsl/pull/103 Test Plan: TravisCI Reviewed By: rodmk Differential Revision: D19027560 Pulled By: fredemmott fbshipit-source-id: d3d63ab8427046bae73ba8748170ee4a6e31f018 --- src/dict/order.php | 19 +++++++------------ src/keyset/order.php | 5 ++--- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/dict/order.php b/src/dict/order.php index 6c8d732e..56eb242f 100644 --- a/src/dict/order.php +++ b/src/dict/order.php @@ -64,8 +64,7 @@ function shuffle( function sort( <<__MaybeMutable, __OnlyRxIfImpl(\HH\Rx\KeyedTraversable::class)>> KeyedTraversable $traversable, - <<__AtMostRxAsFunc>> - ?(function(Tv, Tv): int) $value_comparator = null, + <<__AtMostRxAsFunc>> ?(function(Tv, Tv): int) $value_comparator = null, ): dict { $result = dict($traversable); if ($value_comparator) { @@ -79,7 +78,7 @@ function sort( /* HH_IGNORE_ERROR[4107] __PHPStdLib */ \asort(inout $result); } - return $result; + return dict($result); } /** @@ -100,14 +99,11 @@ function sort( function sort_by( <<__MaybeMutable, __OnlyRxIfImpl(\HH\Rx\KeyedTraversable::class)>> KeyedTraversable $traversable, - <<__AtMostRxAsFunc>> - (function(Tv): Ts) $scalar_func, - <<__AtMostRxAsFunc>> - ?(function(Ts, Ts): int) $scalar_comparator = null, + <<__AtMostRxAsFunc>> (function(Tv): Ts) $scalar_func, + <<__AtMostRxAsFunc>> ?(function(Ts, Ts): int) $scalar_comparator = null, ): dict { $tuple_comparator = $scalar_comparator - ? ((Ts, Tv) $a, (Ts, Tv) $b) ==> - $scalar_comparator($a[0], $b[0]) + ? ((Ts, Tv) $a, (Ts, Tv) $b) ==> $scalar_comparator($a[0], $b[0]) /* HH_FIXME[4240] need Scalar type */ : ((Ts, Tv) $a, (Ts, Tv) $b) ==> $a[0] <=> $b[0]; return $traversable @@ -132,8 +128,7 @@ function sort_by( function sort_by_key( <<__MaybeMutable, __OnlyRxIfImpl(\HH\Rx\KeyedTraversable::class)>> KeyedTraversable $traversable, - <<__AtMostRxAsFunc>> - ?(function(Tk, Tk): int) $key_comparator = null, + <<__AtMostRxAsFunc>> ?(function(Tk, Tk): int) $key_comparator = null, ): dict { $result = dict($traversable); if ($key_comparator) { @@ -147,5 +142,5 @@ function sort_by_key( /* HH_IGNORE_ERROR[4107] __PHPStdLib */ \ksort(inout $result); } - return $result; + return dict($result); } diff --git a/src/keyset/order.php b/src/keyset/order.php index 753e0616..9961ae91 100644 --- a/src/keyset/order.php +++ b/src/keyset/order.php @@ -23,8 +23,7 @@ function sort( <<__MaybeMutable, __OnlyRxIfImpl(\HH\Rx\Traversable::class)>> Traversable $traversable, - <<__AtMostRxAsFunc>> - ?(function(Tv, Tv): int) $comparator = null, + <<__AtMostRxAsFunc>> ?(function(Tv, Tv): int) $comparator = null, ): keyset { $keyset = keyset($traversable); if ($comparator) { @@ -38,5 +37,5 @@ function sort( /* HH_IGNORE_ERROR[4107] __PHPStdLib */ \ksort(inout $keyset); } - return $keyset; + return keyset($keyset); }