Skip to content
Permalink
Browse files

Have invalid arraykey check not report errors for untyped arrays

Summary: Intuitively, this makes sense: if you're using `ArrayAccess` in untyped code, you don't want Hack to tell you you're doing something wrong.

Reviewed By: kmeht

Differential Revision: D13678407

fbshipit-source-id: f11a865b544e9229e1b31932c56bfe5cc621656f
  • Loading branch information...
hgoldstein authored and hhvm-bot committed Jan 23, 2019
1 parent 4825b90 commit 01ef485c652d8e068d385fb5732e5e65e242254a
@@ -14,28 +14,30 @@ open Typing_defs

module Env = Tast_env
module TCO = TypecheckerOptions
module Subtype = Typing_subtype

let should_enforce env =
TCO.disallow_invalid_arraykey (Env.get_tcopt env)
let should_enforce env = TCO.disallow_invalid_arraykey (Env.get_tcopt env)

let info_of_type (reason, typ): Pos.t * string =
(Reason.to_pos reason, Typing_print.error typ)

let is_valid_arraykey env tcontainer tkey =
let is_vector_container env e =
let is_maplike_container env e =
List.exists
[SN.Collections.cVector;
SN.Collections.cImmVector;
SN.Collections.cVec;
SN.Collections.cConstVector]
[SN.Collections.cMap;
SN.Collections.cImmMap;
SN.Collections.cConstMap;
SN.Collections.cDict]
~f:begin fun cls ->
Env.can_subtype env e
(Reason.Rnone,
Tclass ((Pos.none, cls), Nonexact, [Reason.Rnone, Tany]))
end
in
is_vector_container env tcontainer ||
Tclass ((Pos.none, cls),
Nonexact,
[Reason.Rnone, Tany; Reason.Rnone, Tany;]))
end ||
Env.can_subtype env tcontainer (Reason.Rnone, Tarraykind AKany)
in
Env.is_untyped env tcontainer ||
(not @@ is_maplike_container env tcontainer) ||
Env.can_subtype env tkey (Reason.Rnone, Tprim Tarraykey)

let handler = object
@@ -87,6 +87,9 @@ let get_upper_bounds = Typing_env.get_upper_bounds

let is_fresh_generic_parameter = Typing_env.is_fresh_generic_parameter

let is_untyped env ty =
Typing_utils.is_any env ty || Typing_utils.is_dynamic env ty

let subtype env ty_sub ty_super =
Errors.ignore_ (fun () ->
Errors.try_
@@ -153,6 +153,10 @@ val is_fresh_generic_parameter: string -> bool
as part of an `instanceof`, `is`, or `as` expression (instead of being
explicitly declared in code by the user). *)

val is_untyped: env -> Tast.ty -> bool
(** Return {true} when the given type is {Tany}, {Tdynamic}, etc. See:
{Typing_utils.is_any} and {Typing_utils.is_dynamic}. *)

val subtype: env -> Tast.ty -> Tast.ty -> env * bool
(** Return {true} when the first type can be considered a subtype of the second
type after resolving unbound type variables in both types (if any), and an
@@ -1,6 +1,6 @@
<?hh
function main($untyped) {
function main($untyped, $arr) {
$x = array();
$x['foo'] = 0;
$x[null] = 0;
@@ -26,4 +26,8 @@ function main($untyped) {
$x[null] = 0;
$x[4.2] = 0;
$x[$untyped] = 0;
$arr[0] = 0;
$arr[null] = 0;
$arr[4.2] = 0;
$arr[$untyped] = 0;
}

0 comments on commit 01ef485

Please sign in to comment.
You can’t perform that action at this time.