facebookarchive / hack-langspec

The official Hack Language specification.
http://hacklang.org
Other
172 stars 48 forks source link

Default variance for type parameters #110

Open periodic1236 opened 7 years ago

periodic1236 commented 7 years ago

The section on Type Parameters in Ch. 14 states the following (emphasis mine).

generic-type-parameter-variance indicates the variance for that parameter: + for covariance, -for contravariance. If generic-type-parameter-variance is omitted, covariance is assumed.

However, the default behavior for type parameters without a variance annotation appears to be invariant. If covariance were assumed, the following file should be error-free.

<?hh // strict

class A<T> {}

function foo(A<num> $x): void {}

function test(): void {
  $x = new A<int>();
  foo($x);
}

Nevertheless, the type checker reports:

foo.php:9:7,8: Invalid argument (Typing[4110])
  foo.php:5:16,18: This is a num (int/float)
  foo.php:8:14,16: It is incompatible with an int
  foo.php:5:16,18: Considering that this type argument is invariant with respect to A