Skip to content

Commit fa6c733

Browse files
jwatzmansgolemon
authored andcommitted
Fix assertion failure with wrong type param arity
We wanted to assert that, when using a class, its methods have the right arity. However, that check currently happens during the typing phase, and so depending on ordering there's a chance that some callsite might consume the return type with a wrong arity before we can check that it's correct. Furthermore this check can't be moved up into declaration since we need to get the typeclass of the target class in order to determine what the arity should be in the first place. (I suppose we could use the nast for this, but that sounds annoying.) So we could define a new phase where this is checked and where we don't go into typing if it fails... or we could just remove the asserts and let typing deal with spewing the type errors later. We already are at least intended to do something reasonable on arity mismatch since we omit this check in silent mode anyways. Reviewed By: @gabelevi Differential Revision: D1246487
1 parent 0d609f7 commit fa6c733

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

hphp/hack/src/typing/typing.ml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,11 +1870,6 @@ and obj_get_ is_method env ty1 (p, s as id) k k_lhs =
18701870
| _ -> assert false)
18711871
| Some { ce_visibility = vis; ce_type = method_ } ->
18721872
check_visibility p env (Reason.to_pos (fst method_), vis) None;
1873-
let arity_match =
1874-
List.length class_.tc_tparams = List.length paraml
1875-
in
1876-
assert (!is_silent_mode || arity_match);
1877-
18781873
let new_name = "alpha_varied_this" in
18791874

18801875
(* Since a param might include a "this" type, let's alpha vary

hphp/hack/src/typing/typing_instantiate.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ let rec make_subst tparams tyl =
3535
(* We tolerate missing types in silent_mode. When that happens, we bind
3636
* all the paremeters we can, and bind the remaining ones to "Tany".
3737
*)
38-
assert (!is_silent_mode || List.length tparams = List.length tyl);
3938
let subst = ref SMap.empty in
4039
let tyl = ref tyl in
4140
List.iter (make_subst_tparam subst tyl) tparams;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?hh
2+
3+
class D<T> {
4+
public function f(): C<T, T> {}
5+
}
6+
7+
class C<T> {
8+
public function f(): T {}
9+
}
10+
11+
function f(): void {
12+
$c = (new D())->f();
13+
$c->f();
14+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
File "incorrect_tparam_arity.php", line 4, characters 24-24:
2+
The type C expects 1 type parameter(s)

0 commit comments

Comments
 (0)