Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Clean up reference to varray, darray, and HH_FIXME #69

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ jobs:
# Run tests on all OS's and HHVM versions, even if one fails
fail-fast: false
matrix:
os: [ ubuntu ]
os: [ ubuntu-20.04 ]
hhvm:
- "4.128"
- latest
- nightly
runs-on: ${{matrix.os}}-latest
- "4.153"
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v2
- uses: hhvm/actions/hack-lint-test@master
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"keywords": ["hack", "TypeAssert"],
"require": {
"hhvm": "^4.128"
"hhvm": "^4.153"
},
"extra": {
"branch-alias": {
Expand Down
17 changes: 0 additions & 17 deletions src/TypeSpec.hack
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@ function constvector<Tv>(TypeSpec<Tv> $inner): TypeSpec<\ConstVector<Tv>> {
return new __Private\VectorSpec(\ConstVector::class, $inner);
}

function darray<Tk as arraykey, Tv>(
TypeSpec<Tk> $tsk,
TypeSpec<Tv> $tsv,
): TypeSpec<darray<Tk, Tv>> {
return new __Private\DarraySpec($tsk, $tsv);
}

function dict<Tk as arraykey, Tv>(
TypeSpec<Tk> $tsk,
TypeSpec<Tv> $tsv,
Expand Down Expand Up @@ -147,10 +140,6 @@ function string(): TypeSpec<string> {
return new __Private\StringSpec();
}

function varray<Tv>(TypeSpec<Tv> $tsv): TypeSpec<varray<Tv>> {
return new __Private\VarraySpec($tsv);
}

function vec<Tv>(TypeSpec<Tv> $inner): TypeSpec<vec<Tv>> {
return new __Private\VecSpec($inner);
}
Expand All @@ -159,12 +148,6 @@ function vector<Tv>(TypeSpec<Tv> $inner): TypeSpec<Vector<Tv>> {
return new __Private\VectorSpec(Vector::class, $inner);
}

function varray_or_darray<Tv>(
TypeSpec<Tv> $inner,
): TypeSpec<varray_or_darray<Tv>> {
return new __Private\VArrayOrDArraySpec($inner);
}

function of<reify T>(): TypeSpec<T> {
return __Private\from_type_structure(
\HH\ReifiedGenerics\get_type_structure<T>(),
Expand Down
8 changes: 5 additions & 3 deletions src/TypeSpec/__Private/ClassnameSpec.hack
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ final class ClassnameSpec<Tinner, T as classname<Tinner>> extends TypeSpec<T> {

<<__Override>>
public function assertType(mixed $value): T {
if (($value is string) && \is_a($value, $this->what, /* strings = */ true)) {
/* HH_IGNORE_ERROR[4110] is_a is not understood by Hack */
return $value;
if ($value is string && \is_a($value, $this->what, /* strings = */ true)) {
return \HH\FIXME\UNSAFE_CAST<mixed, T>(
$value,
'is_a is not understood by Hack',
);
}
throw
IncorrectTypeException::withValue($this->getTrace(), $this->what, $value);
Expand Down
76 changes: 0 additions & 76 deletions src/TypeSpec/__Private/DarraySpec.hack

This file was deleted.

6 changes: 4 additions & 2 deletions src/TypeSpec/__Private/InstanceOfSpec.hack
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ final class InstanceOfSpec<T> extends TypeSpec<T> {
<<__Override>>
public function assertType(mixed $value): T {
if (\is_a($value, $this->what)) {
/* HH_IGNORE_ERROR[4110] unsafe for generics */
return $value;
return \HH\FIXME\UNSAFE_CAST<mixed, T>(
$value,
'unsafe for classname<GenericClass<_>>; the `<_>` goes unchecked.',
);
}
throw
IncorrectTypeException::withValue($this->getTrace(), $this->what, $value);
Expand Down
2 changes: 1 addition & 1 deletion src/TypeSpec/__Private/KeyedTraversableSpec.hack
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ final class KeyedTraversableSpec<Tk, Tv, T as KeyedTraversable<Tk, Tv>>
$tsk->assertType($k);
$tsv->assertType($v);
}
return /* HH_IGNORE_ERROR[4110] */ $value;
return \HH\FIXME\UNSAFE_CAST<KeyedContainer<arraykey, mixed>, T>($value);
}

<<__Override>>
Expand Down
31 changes: 17 additions & 14 deletions src/TypeSpec/__Private/MapSpec.hack
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,14 @@ final class MapSpec<Tk as arraykey, Tv, T as \ConstMap<Tk, Tv>>
$changed === false &&
\is_a($value, $this->what, /* allow_string = */ true)
) {
/* HH_IGNORE_ERROR[4110] */
return $value;
return \HH\FIXME\UNSAFE_CAST<KeyedTraversable<mixed, mixed>, T>($value);
}

if ($this->what === Map::class) {
/* HH_IGNORE_ERROR[4110] */
return $out;
return \HH\FIXME\UNSAFE_CAST<Map<arraykey, mixed>, T>($out);
}

/* HH_IGNORE_ERROR[4110] */
return $out->immutable();
return \HH\FIXME\UNSAFE_CAST<ImmMap<arraykey, mixed>, T>($out->immutable());
}

<<__Override>>
Expand Down Expand Up @@ -99,19 +96,25 @@ final class MapSpec<Tk as arraykey, Tv, T as \ConstMap<Tk, Tv>>
}

if (!$changed) {
/* HH_IGNORE_ERROR[4110] is_a() ensure the collection type
and $spec->assertType() ensures the inner type. */
return $value;
// $value has an undenotable type Tk#1, so mixed.
return \HH\FIXME\UNSAFE_CAST<mixed, T>(
$value,
'is_a() ensures the collection type and $spec->assertType() ensures the inner type.',
);
}

if ($this->what === Map::class) {
/* HH_IGNORE_ERROR[4110] $out is a Map and $this->what is also Map. */
return $out;
return \HH\FIXME\UNSAFE_CAST<Map<Tk, Tv>, T>(
$out,
'$out is a Map and $this->what is also Map.',
);
}

/* HH_IGNORE_ERROR[4110] Return ImmMap when the user asks for ConstMap or ImmMap.
This immutability for ConstMap is not needed, but kept for backwards compatibility. */
return $out->immutable();
return \HH\FIXME\UNSAFE_CAST<ImmMap<Tk, Tv>, T>(
$out->immutable(),
'Return ImmMap when the user asks for ConstMap or ImmMap.
This immutability for ConstMap is not needed, but kept for backwards compatibility.',
);
}

<<__Override>>
Expand Down
30 changes: 16 additions & 14 deletions src/TypeSpec/__Private/SetSpec.hack
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,13 @@ final class SetSpec<Tv as arraykey, T as \ConstSet<Tv>> extends TypeSpec<T> {
}

if ($changed === false && \is_a($value, $this->what)) {
/* HH_IGNORE_ERROR[4110] */
return $value;
return \HH\FIXME\UNSAFE_CAST<Traversable<mixed>, T>($value);
}

if ($this->what === Set::class) {
/* HH_IGNORE_ERROR[4110] */
return $out;
return \HH\FIXME\UNSAFE_CAST<Set<Tv>, T>($out);
}
/* HH_IGNORE_ERROR[4110] */
return $out->immutable();
return \HH\FIXME\UNSAFE_CAST<ImmSet<Tv>, T>($out->immutable());
}

<<__Override>>
Expand Down Expand Up @@ -85,19 +82,24 @@ final class SetSpec<Tv as arraykey, T as \ConstSet<Tv>> extends TypeSpec<T> {
}

if (!$changed) {
/* HH_IGNORE_ERROR[4110] is_a() ensures the collection type
and $spec->assertType() ensures the inner type. */
return $value;
return \HH\FIXME\UNSAFE_CAST<\ConstSet<arraykey>, T>(
$value,
'is_a() ensures the collection type and $spec->assertType() ensures the inner type.',
);
}

if ($this->what === Set::class) {
/* HH_IGNORE_ERROR[4110] $out is a Set and $this->what is also Set. */
return $out;
return \HH\FIXME\UNSAFE_CAST<\ConstSet<Tv>, T>(
$out,
'$out is a Set and $this->what is also Set.',
);
}

/* HH_IGNORE_ERROR[4110] Return ImmSet when the user asks for ConstSet or ImmSet.
This immutability for ConstSet is not needed, but kept for consistency with MapSpec and VectorSpec. */
return $out->immutable();
return \HH\FIXME\UNSAFE_CAST<ImmSet<Tv>, T>(
$out->immutable(),
'Return ImmSet when the user asks for ConstSet or ImmSet.
This immutability for ConstSet is not needed, but kept for consistency with MapSpec and VectorSpec.',
);
}

<<__Override>>
Expand Down
44 changes: 19 additions & 25 deletions src/TypeSpec/__Private/ShapeSpec.hack
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ final class ShapeSpec extends TypeSpec<shape()> {
<<__Override>>
public function coerceType(mixed $value): shape() {
if (!$value is KeyedTraversable<_, _>) {
throw TypeCoercionException::withValue(
$this->getTrace(),
'shape',
$value,
);
throw
TypeCoercionException::withValue($this->getTrace(), 'shape', $value);
}

$value = dict(/* HH_IGNORE_ERROR[4323] */$value);
$value = dict(\HH\FIXME\UNSAFE_CAST<
KeyedTraversable<mixed, mixed>,
KeyedTraversable<arraykey, mixed>,
>($value));
$out = dict[];
foreach ($this->inners as $key => $spec) {
$trace = $this->getTrace()->withFrame('shape['.$key.']');
Expand All @@ -62,23 +62,24 @@ final class ShapeSpec extends TypeSpec<shape()> {
}
}

return self::dictToShapeUNSAFE($out);
return \HH\FIXME\UNSAFE_CAST<dict<arraykey, mixed>, shape()>(
$out,
'Can not use $out as shape(...), because the generic is closed.',
);
}

<<__Override>>
public function assertType(mixed $value): shape() {
if (!\HH\is_dict_or_darray($value)) {
throw IncorrectTypeException::withValue(
$this->getTrace(),
'shape',
$value,
);
if (!$value is dict<_, _>) {
throw
IncorrectTypeException::withValue($this->getTrace(), 'shape', $value);
}
$out = dict[];
foreach ($this->inners as $key => $spec) {
$trace = $this->getTrace()->withFrame('shape['.$key.']');
if (C\contains_key($value, $key)) {
$out[$key as arraykey] = $spec->withTrace($trace)->assertType($value[$key as dynamic] ?? null);
$out[$key as arraykey] =
$spec->withTrace($trace)->assertType($value[$key as dynamic] ?? null);
continue;
}

Expand Down Expand Up @@ -106,17 +107,10 @@ final class ShapeSpec extends TypeSpec<shape()> {
}
}

return self::dictToShapeUNSAFE($out);
}

private static function dictToShapeUNSAFE(
dict<arraykey, mixed> $shape,
): shape() {
if (shape() is dict<_, _>) {
/* HH_IGNORE_ERROR[4110] */
return $shape;
}
return /* HH_IGNORE_ERROR[4110] */ darray($shape);
return \HH\FIXME\UNSAFE_CAST<dict<arraykey, mixed>, shape()>(
$out,
'Can not use $out as shape(...), because the generic is closed.',
);
}

<<__Override>>
Expand Down
2 changes: 1 addition & 1 deletion src/TypeSpec/__Private/TraversableSpec.hack
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ final class TraversableSpec<Tinner, T as Traversable<Tinner>>
foreach ($value as $v) {
$this->inner->withTrace($trace)->assertType($v);
}
return /* HH_IGNORE_ERROR[4110] */ $value;
return \HH\FIXME\UNSAFE_CAST<Container<mixed>, T>($value);
}

<<__Override>>
Expand Down
Loading