Skip to content

Commit

Permalink
Better typing for numeric ops
Browse files Browse the repository at this point in the history
Summary: Typing of numeric ops makes significant use of `MISSING_TYPE`. By validating the `hhi` signatures against `fbcode/hphp/runtime/ext/std/ext_std_math.php` and `fbcode/hphp/runtime/ext/std/ext_std_math.cpp`, we can do better.

Reviewed By: edwinsmith

Differential Revision: D51258611

fbshipit-source-id: 5e6ac8e0583cc07e7e65a0ec0742a75f4a28a24a
  • Loading branch information
Andrew Kennedy authored and facebook-github-bot committed Nov 13, 2023
1 parent 534afee commit c53dbdd
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 45 deletions.
54 changes: 27 additions & 27 deletions hphp/hack/hhi/stdlib/builtins_math.hhi
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const float M_LNPI;
const float M_EULER;

<<__PHPStdLib>>
function pi()[]: HH\FIXME\MISSING_RETURN_TYPE;
function pi()[]: float;
<<__PHPStdLib>>
function min(
HH\FIXME\MISSING_PARAM_TYPE $value,
Expand All @@ -48,9 +48,9 @@ function abs(
HH\FIXME\MISSING_PARAM_TYPE $number,
)[]: HH\FIXME\MISSING_RETURN_TYPE;
<<__PHPStdLib>>
function is_finite(float $val)[]: HH\FIXME\MISSING_RETURN_TYPE;
function is_finite(float $val)[]: bool;
<<__PHPStdLib>>
function is_infinite(float $val)[]: HH\FIXME\MISSING_RETURN_TYPE;
function is_infinite(float $val)[]: bool;
<<__PHPStdLib>>
function is_nan(float $val)[]: bool;
<<__PHPStdLib>>
Expand Down Expand Up @@ -97,51 +97,51 @@ function base_convert(
)[]: HH\FIXME\MISSING_RETURN_TYPE;
<<__PHPStdLib>>
function pow(
HH\FIXME\MISSING_PARAM_TYPE $base,
HH\FIXME\MISSING_PARAM_TYPE $exp,
)[]: HH\FIXME\MISSING_RETURN_TYPE;
num $base,
num $exp,
)[]: dynamic;
<<__PHPStdLib>>
function exp(float $arg)[]: HH\FIXME\MISSING_RETURN_TYPE;
function exp(float $arg)[]: float;
<<__PHPStdLib>>
function expm1(float $arg)[]: HH\FIXME\MISSING_RETURN_TYPE;
function expm1(float $arg)[]: float;
<<__PHPStdLib>>
function log10(float $arg)[]: HH\FIXME\MISSING_RETURN_TYPE;
function log10(float $arg)[]: float;
<<__PHPStdLib>>
function log1p(float $number)[]: HH\FIXME\MISSING_RETURN_TYPE;
function log1p(float $number)[]: float;
<<__PHPStdLib>>
function log(float $arg, float $base = 0.0)[]: HH\FIXME\MISSING_RETURN_TYPE;
function log(float $arg, float $base = 0.0)[]: float;
<<__PHPStdLib>>
function cos(float $arg)[]: HH\FIXME\MISSING_RETURN_TYPE;
function cos(float $arg)[]: float;
<<__PHPStdLib>>
function cosh(float $arg)[]: HH\FIXME\MISSING_RETURN_TYPE;
function cosh(float $arg)[]: float;
<<__PHPStdLib>>
function sin(float $arg)[]: HH\FIXME\MISSING_RETURN_TYPE;
function sin(float $arg)[]: float;
<<__PHPStdLib>>
function sinh(float $arg)[]: HH\FIXME\MISSING_RETURN_TYPE;
function sinh(float $arg)[]: float;
<<__PHPStdLib>>
function tan(float $arg)[]: HH\FIXME\MISSING_RETURN_TYPE;
function tan(float $arg)[]: float;
<<__PHPStdLib>>
function tanh(float $arg)[]: HH\FIXME\MISSING_RETURN_TYPE;
function tanh(float $arg)[]: float;
<<__PHPStdLib>>
function acos(float $arg)[]: HH\FIXME\MISSING_RETURN_TYPE;
function acos(float $arg)[]: float;
<<__PHPStdLib>>
function acosh(float $arg)[]: HH\FIXME\MISSING_RETURN_TYPE;
function acosh(float $arg)[]: float;
<<__PHPStdLib>>
function asin(float $arg)[]: HH\FIXME\MISSING_RETURN_TYPE;
function asin(float $arg)[]: float;
<<__PHPStdLib>>
function asinh(float $arg)[]: HH\FIXME\MISSING_RETURN_TYPE;
function asinh(float $arg)[]: float;
<<__PHPStdLib>>
function atan(float $arg)[]: HH\FIXME\MISSING_RETURN_TYPE;
function atan(float $arg)[]: float;
<<__PHPStdLib>>
function atanh(float $arg)[]: HH\FIXME\MISSING_RETURN_TYPE;
function atanh(float $arg)[]: float;
<<__PHPStdLib>>
function atan2(float $y, float $x)[]: HH\FIXME\MISSING_RETURN_TYPE;
function atan2(float $y, float $x)[]: float;
<<__PHPStdLib>>
function hypot(float $x, float $y)[]: HH\FIXME\MISSING_RETURN_TYPE;
function hypot(float $x, float $y)[]: float;
<<__PHPStdLib>>
function fmod(float $x, float $y)[]: HH\FIXME\MISSING_RETURN_TYPE;
function fmod(float $x, float $y)[]: float;
<<__PHPStdLib>>
function sqrt(float $arg)[]: HH\FIXME\MISSING_RETURN_TYPE;
function sqrt(float $arg)[]: float;
<<__PHPStdLib>>
function getrandmax()[]: HH\FIXME\MISSING_RETURN_TYPE;
<<__PHPStdLib>>
Expand Down
2 changes: 1 addition & 1 deletion hphp/hack/test/autocomplete/def_function.php.exp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ flush
(function(): ~HH\FIXME\MISSING_RETURN_TYPE)
fmod
INSERT fmod(${1:\$x}, ${2:\$y})
(function(float $x, float $y): ~HH\FIXME\MISSING_RETURN_TYPE)
(function(float $x, float $y): float)
fnmatch
INSERT fnmatch(${1:\$pattern}, ${2:\$filename})
(function(string $pattern, string $filename, int $flags = _): ~HH\FIXME\MISSING_RETURN_TYPE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ flush
(function(): ~HH\FIXME\MISSING_RETURN_TYPE)
fmod
INSERT fmod(${1:\$x}, ${2:\$y})
(function(float $x, float $y): ~HH\FIXME\MISSING_RETURN_TYPE)
(function(float $x, float $y): float)
fnmatch
INSERT fnmatch(${1:\$pattern}, ${2:\$filename})
(function(string $pattern, string $filename, int $flags = _): ~HH\FIXME\MISSING_RETURN_TYPE)
Expand Down
26 changes: 13 additions & 13 deletions hphp/hack/test/integration_ml/test_server_hover.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ abstract class ClassMembers {
// ^30:12
await ClassMembers::genLotsOfModifiers();
// ^32:11 ^32:25
$this->calculateDistance(5, -6, 12, 1);
$this->calculateDistance(5.0, -6.0, 12.0, 1.0);
// ^34:28
}
/** Another method doc block */
public function calculateDistance(
int $originalPositionX,
int $finalPositionX,
int $originalPositionY,
int $finalPositionY,
): int {
float $originalPositionX,
float $finalPositionX,
float $originalPositionY,
float $finalPositionY,
): float {
return sqrt(
pow($originalPositionX - $finalPositionX, 2) +
pow($originalPositionY - $finalPositionY, 2),
(float)pow($originalPositionX - $finalPositionX, 2) +
(float)pow($originalPositionY - $finalPositionY, 2),
);
}
}
Expand Down Expand Up @@ -156,11 +156,11 @@ let class_members_cases =
snippet =
"// Defined in ClassMembers\n"
^ "public function calculateDistance(\n"
^ " int $originalPositionX,\n"
^ " int $finalPositionX,\n"
^ " int $originalPositionY,\n"
^ " int $finalPositionY\n"
^ "): int";
^ " float $originalPositionX,\n"
^ " float $finalPositionX,\n"
^ " float $originalPositionY,\n"
^ " float $finalPositionY\n"
^ "): float";
addendum = ["Another method doc block"];
pos = pos_at (34, 12) (34, 28);
};
Expand Down
6 changes: 3 additions & 3 deletions hphp/runtime/ext/std/ext_std_math.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ function base_convert(mixed $number, int $frombase, int $tobase)[]: mixed;
* Returns base raised to the power of exp. Warning In PHP 4.0.6 and earlier
* pow() always returned a float, and did not issue warnings.
*
* @param mixed $base - The base to use
* @param mixed $exp - The exponent
* @param num $base - The base to use
* @param num $exp - The exponent
*
* @return mixed - base raised to the power of exp. If the result can be
* represented as integer it will be returned as type integer, else it will be
Expand All @@ -299,7 +299,7 @@ function base_convert(mixed $number, int $frombase, int $tobase)[]: mixed;
*
*/
<<__IsFoldable, __Native>>
function pow(mixed $base, mixed $exp)[]: mixed;
function pow(num $base, num $exp)[]: mixed;

/**
* Returns e raised to the power of arg. 'e' is the base of the natural
Expand Down

0 comments on commit c53dbdd

Please sign in to comment.