hhvm icon indicating copy to clipboard operation
hhvm copied to clipboard

Introduce a new `/* HH_TRACE[$var] */` annotation

Open azjezz opened this issue 4 years ago • 3 comments

while analyzing a script, it would be nice to be able to inspect the type of a variable if it's not a parameter.

e.g:

$pattern = re"/(?P<language>PHP)/";

$pattern in this case is shape(0 => string, 'langauge' => string), however, the only way to know this statically, is by using an editor with hack plugin, which would ask hh_client for the information.

request

This issue requests a new hh_client "annotation" HH_TYPE/HH_TRACE which will result in an error that contains type information about given variable ( e.g: https://cloud.void.tn/c/36?version=4.114.0 )

This feature is inspired by psalm trace feature: https://psalm.dev/r/1602ca9415

azjezz avatar Jun 22 '21 01:06 azjezz

There's hh_client --type-at-pos too, but yes, this is useful.

We have something very similar already in hh_single_type_check (used for our unit tests): a fake function called hh_show() that pretty much acts exactly as you're describing.

Perhaps this should be made available in real hh_client/hh_server runs too as HH\show_type_and_raise_error($x) or something like that.

fredemmott avatar Jun 23 '21 16:06 fredemmott

I think there should be 2 functions.

here, the actual type is Regex\Pattern<shape(0 => string, 'language' => string)>, but the type to use in user-land is Regex\Pattern<shape('language' => string, ...)>

so we should have 2 variant of HH\show_type($pattern).

  1. to show the actual type ( internal, not possible in userland, could be a union, intersection, contains wildcard _, ... etc )
  2. to show the use-able type hint ( how can i type hint this in my code? )

azjezz avatar Jun 23 '21 16:06 azjezz

2 should be identical to 1, unless 1 is not denotable at all.

If we did do a separate (2) it'd be something like HH\show_unsafe_more_permissive_denotable_type_and_error(), though I don't know if we reliably track the information required to implement that.

fredemmott avatar Jun 23 '21 16:06 fredemmott