Skip to content

HH\FormatString Typing Errors in sprintf() and invariant() #448

@justinwray

Description

@justinwray

Within the Facebook CTF project, we just upgraded from HHVM 3.14.5 to 3.18.1 in our latest PR facebookarchive/fbctf#535.

Now that we are on a newer version of HHVM, we are getting typing errors for a few sprintf() and invariant() calls. The errors indicate the argument is incompatible with HH\FormatString.

Our understanding is the HH\FormatString definition looks like such:

namespace HH {
  newtype FormatString<T> = string;
}

We can confirm that the various [relevant] values passed into sprintf() and invariant() are of type string. Even adding in strval() or additional is_string() checks made no difference to the typing errors.

Unfortunately, there doesn't seem to be any documentation available on HH\FormatString usage or the expectations for it's typing in functions like sprintf(), as issue #209 is still outstanding. We were hoping someone might have some insight on how to properly use HH\FormatString or correct this typing error.

Here is an example typing error, from hh_client:

src/Utils.php:12:5,52: Invalid argument (Typing[4110])
  /tmp/hh_server/hhi_217a2dc9/printf.hhi:92:30,45: This is an object of type HH\FormatString
  /tmp/hh_server/hhi_217a2dc9/printf.hhi:87:70,75: It is incompatible with a string
src/Utils.php:12:5,52: This argument must be a literal string (Typing[4027])

Here is the code-block from the above example error:

function must_have_idx<Tk, Tv>(?KeyedContainer<Tk, Tv> $arr, Tk $idx): Tv {
  invariant($arr !== null, 'Container is null');
  $result = idx($arr, $idx);
  invariant(
    $result !== null,
    sprintf('Index %s not found in container', $idx),
  );
  return $result;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions