Skip to content

Commit

Permalink
Fix type error in Vec\fill
Browse files Browse the repository at this point in the history
Summary:
array_fill can return false if $size<0. This results in a cast from bool to vec, leading to a InvalidOperationException with a confusing message.

Instead of invoking array_fill with invalid arguments, throw an InvariantException.

Reviewed By: kmeht

Differential Revision: D5652216

fbshipit-source-id: 16995500392e07f74601cd5e99c8ca7763268055
  • Loading branch information
Matt Glazar authored and facebook-github-bot committed Aug 18, 2017
1 parent d8a048c commit 1cde7b3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/vec/transform.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function fill<Tv>(
int $size,
Tv $value,
): vec<Tv> {
invariant($size >= 0, 'Expected non-negative fill size, got %d.', $size);
return vec(\array_fill(0, $size, $value));
}

Expand Down
5 changes: 5 additions & 0 deletions tests/vec/VecTransformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use namespace HH\Lib\Vec;
use function \Facebook\FBExpect\expect;
// @oss-disable: use InvariantViolationException as InvariantException;

/**
* @emails oncall+hack_prod_infra
Expand Down Expand Up @@ -87,6 +88,10 @@ public function testFill<Tv>(
expect(Vec\fill($size, $value))->toBeSame($expected);
}

public function testFillExceptions(): void {
expect(() ==> Vec\fill(-1, true))->toThrow(InvariantException::class);
}

public static function provideTestFlatten(): array<mixed> {
return array(
tuple(
Expand Down

0 comments on commit 1cde7b3

Please sign in to comment.