Skip to content

Commit e5e17fa

Browse files
MarisaKirisamefacebook-github-bot
authored andcommitted
Implement can_index solving for simple tuple
Summary: Implement can_index solving for simple tuple Differential Revision: D77865850 fbshipit-source-id: e0a9fa1f795b1d62054777cd4594e5a91bcee5ac
1 parent 4514948 commit e5e17fa

30 files changed

+201
-40
lines changed

hphp/hack/src/oxidized/copy_types.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ type_counter::Category
109109
type_counter::LoggedType
110110
typing_defs::ClassConstKind
111111
typing_defs::VisibilityBehavior
112+
typing_defs_core::CanIndexShape
112113
typing_defs_core::ConsistentKind
113114
typing_defs_core::DestructureKind
114115
typing_defs_core::Enforcement

hphp/hack/src/oxidized/gen/typing_defs_core.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// This source code is licensed under the MIT license found in the
44
// LICENSE file in the "hack" directory of this source tree.
55
//
6-
// @generated SignedSource<<c6b3687da54a645cd9a50f16647cec27>>
6+
// @generated SignedSource<<061a1baad55781ab7cbf6d30a6d839fd>>
77
//
88
// To regenerate this file, run:
99
// hphp/hack/src/oxidized_regen.sh
@@ -1122,6 +1122,30 @@ pub struct HasMember {
11221122
pub explicit_targs: Option<Vec<nast::Targ>>,
11231123
}
11241124

1125+
#[derive(
1126+
Clone,
1127+
Copy,
1128+
Debug,
1129+
Deserialize,
1130+
Eq,
1131+
EqModuloPos,
1132+
FromOcamlRep,
1133+
FromOcamlRepIn,
1134+
Hash,
1135+
NoPosHash,
1136+
Ord,
1137+
PartialEq,
1138+
PartialOrd,
1139+
Serialize,
1140+
ToOcamlRep
1141+
)]
1142+
#[rust_to_ocaml(attr = "deriving show")]
1143+
#[repr(C, u8)]
1144+
pub enum CanIndexShape {
1145+
IntLit(isize),
1146+
Generic,
1147+
}
1148+
11251149
#[derive(
11261150
Clone,
11271151
Debug,
@@ -1142,7 +1166,7 @@ pub struct HasMember {
11421166
#[repr(C)]
11431167
pub struct CanIndex {
11441168
pub key: Ty,
1145-
pub shape: Option<TshapeFieldName>,
1169+
pub shape: CanIndexShape,
11461170
pub val: Ty,
11471171
pub expr_pos: pos::Pos,
11481172
pub index_pos: pos::Pos,

hphp/hack/src/oxidized_by_ref/copy_types.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ t_shape_map::TShapeMap
182182
tast::CheckStatus
183183
typing_cont_key::TypingContKey
184184
typing_defs::VisibilityBehavior
185+
typing_defs_core::CanIndexShape
185186
typing_defs_core::Capability
186187
typing_defs_core::CeVisibility
187188
typing_defs_core::ClassRefinement

hphp/hack/src/oxidized_by_ref/gen/typing_defs_core.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// This source code is licensed under the MIT license found in the
44
// LICENSE file in the "hack" directory of this source tree.
55
//
6-
// @generated SignedSource<<a3f5dd3a3e94f23bb77802ece593e841>>
6+
// @generated SignedSource<<ac83f45f6e5208cf1fc74d07be0e3f73>>
77
//
88
// To regenerate this file, run:
99
// hphp/hack/src/oxidized_regen.sh
@@ -1102,3 +1102,28 @@ pub enum TupleExtra<'a> {
11021102
}
11031103
impl<'a> TrivialDrop for TupleExtra<'a> {}
11041104
arena_deserializer::impl_deserialize_in_arena!(TupleExtra<'arena>);
1105+
1106+
#[derive(
1107+
Clone,
1108+
Copy,
1109+
Debug,
1110+
Deserialize,
1111+
Eq,
1112+
EqModuloPos,
1113+
FromOcamlRepIn,
1114+
Hash,
1115+
NoPosHash,
1116+
Ord,
1117+
PartialEq,
1118+
PartialOrd,
1119+
Serialize,
1120+
ToOcamlRep
1121+
)]
1122+
#[rust_to_ocaml(attr = "deriving show")]
1123+
#[repr(C, u8)]
1124+
pub enum CanIndexShape {
1125+
IntLit(isize),
1126+
Generic,
1127+
}
1128+
impl TrivialDrop for CanIndexShape {}
1129+
arena_deserializer::impl_deserialize_in_arena!(CanIndexShape);

hphp/hack/src/oxidized_by_ref/owned_types.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ tast::CheckStatus
2222
tast::FunTastInfo
2323
typing_cont_key::TypingContKey
2424
typing_defs::VisibilityBehavior
25+
typing_defs_core::CanIndexShape
2526
typing_defs_core::CollectionStyle
2627
typing_defs_core::ConsistentKind
2728
typing_defs_core::DependentType

hphp/hack/src/typing/typing.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3962,7 +3962,10 @@ end = struct
39623962
Tcan_index
39633963
{
39643964
ci_key = ty2;
3965-
ci_shape = None;
3965+
ci_shape =
3966+
(match e2 with
3967+
| (_, _, Int i) -> IntLit (int_of_string i)
3968+
| _ -> Generic);
39663969
ci_val = res_ty;
39673970
ci_expr_pos = p;
39683971
ci_index_pos = snd3 e2;

hphp/hack/src/typing/typing_defs_core.ml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,9 +1443,14 @@ type has_member = {
14431443
}
14441444
[@@deriving show]
14451445

1446+
type can_index_shape =
1447+
| IntLit of int
1448+
| Generic
1449+
[@@deriving show]
1450+
14461451
type can_index = {
14471452
ci_key: locl_ty;
1448-
ci_shape: tshape_field_name option;
1453+
ci_shape: can_index_shape;
14491454
ci_val: locl_ty;
14501455
ci_expr_pos: Pos.t;
14511456
ci_index_pos: Pos.t;
@@ -1573,11 +1578,18 @@ let has_member_compare ~normalize_lists hm1 hm2 =
15731578
| comp -> comp)
15741579
| comp -> comp
15751580

1581+
let can_index_shape_compare cis1 cis2 =
1582+
match (cis1, cis2) with
1583+
| (IntLit i1, IntLit i2) -> Int.compare i1 i2
1584+
| (IntLit _, _) -> -1
1585+
| (_, IntLit _) -> 1
1586+
| (Generic, Generic) -> 0
1587+
15761588
let can_index_compare ~normalize_lists ci1 ci2 =
15771589
match ty_compare ~normalize_lists ci1.ci_key ci2.ci_key with
15781590
| 0 ->
15791591
(match ty_compare ~normalize_lists ci1.ci_val ci2.ci_val with
1580-
| 0 -> Option.compare compare_tshape_field_name ci1.ci_shape ci2.ci_shape
1592+
| 0 -> can_index_shape_compare ci1.ci_shape ci2.ci_shape
15811593
| comp -> comp)
15821594
| comp -> comp
15831595

hphp/hack/src/typing/typing_defs_core.mli

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,9 +645,14 @@ type destructure = {
645645
* indexed with certain literals, and so in those cases it is not sufficient to
646646
* record simply the type of the index.
647647
*)
648+
type can_index_shape =
649+
| IntLit of int
650+
| Generic
651+
[@@deriving show]
652+
648653
type can_index = {
649654
ci_key: locl_ty;
650-
ci_shape: tshape_field_name option;
655+
ci_shape: can_index_shape;
651656
ci_val: locl_ty;
652657
ci_expr_pos: Pos.t;
653658
ci_index_pos: Pos.t;

hphp/hack/src/typing/typing_print.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,10 +652,10 @@ module Full = struct
652652
let (fuel, val_doc) = k ~fuel ci.ci_val in
653653
( fuel,
654654
match ci.ci_shape with
655-
| None ->
655+
| Generic ->
656656
Concat
657657
[text "can_index"; text "("; key_doc; comma_sep; val_doc; text ")"]
658-
| Some _ft ->
658+
| _ ->
659659
Concat
660660
[
661661
text "can_index";

hphp/hack/src/typing/typing_subtype.ml

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6178,23 +6178,17 @@ end = struct
61786178
let mk_prop ~subtype_env ~this_ty ~lhs ~rhs =
61796179
simplify ~subtype_env ~this_ty ~lhs ~rhs
61806180
in
6181-
let is_container tk tv =
6181+
let simplify_default ~subtype_env lhs rhs env =
61826182
Subtype.simplify
61836183
~subtype_env
61846184
~this_ty
6185-
~lhs:{ sub_supportdyn; ty_sub = can_index.ci_key }
6186-
~rhs:{ super_like = false; super_supportdyn = false; ty_super = tk }
6185+
~lhs:{ sub_supportdyn; ty_sub = lhs }
6186+
~rhs:{ super_like = false; super_supportdyn = false; ty_super = rhs }
61876187
env
6188-
&&& Subtype.simplify
6189-
~subtype_env
6190-
~this_ty
6191-
~lhs:{ sub_supportdyn; ty_sub = tv }
6192-
~rhs:
6193-
{
6194-
super_like = false;
6195-
super_supportdyn = false;
6196-
ty_super = can_index.ci_val;
6197-
}
6188+
in
6189+
let is_container tk tv =
6190+
simplify_default ~subtype_env can_index.ci_key tk env
6191+
&&& simplify_default ~subtype_env tv can_index.ci_val
61986192
in
61996193
match deref ty_sub with
62006194
| (_, Tvar _) ->
@@ -6206,16 +6200,10 @@ end = struct
62066200
(ConstraintType
62076201
(mk_constraint_type (reason_super, Tcan_index can_index)))
62086202
| (_, Tdynamic) ->
6209-
Subtype.simplify
6203+
simplify_default
62106204
~subtype_env
6211-
~this_ty
6212-
~lhs:{ sub_supportdyn; ty_sub = MakeType.dynamic reason_super }
6213-
~rhs:
6214-
{
6215-
super_like = false;
6216-
super_supportdyn = false;
6217-
ty_super = can_index.ci_val;
6218-
}
6205+
(MakeType.dynamic reason_super)
6206+
can_index.ci_val
62196207
env
62206208
&&&
62216209
if
@@ -6225,16 +6213,10 @@ end = struct
62256213
let subtype_env =
62266214
{ subtype_env with coerce = Some Typing_logic.CoerceToDynamic }
62276215
in
6228-
Subtype.simplify
6216+
simplify_default
62296217
~subtype_env
6230-
~this_ty
6231-
~lhs:{ sub_supportdyn; ty_sub = can_index.ci_key }
6232-
~rhs:
6233-
{
6234-
super_like = false;
6235-
super_supportdyn = false;
6236-
ty_super = MakeType.dynamic (get_reason ty_sub);
6237-
}
6218+
can_index.ci_key
6219+
(MakeType.dynamic (get_reason ty_sub))
62386220
else
62396221
valid
62406222
| (_, Tclass ((_, n), _, [tv]))
@@ -6264,6 +6246,35 @@ end = struct
62646246
(sub_supportdyn, ty_subs)
62656247
rhs
62666248
env
6249+
| (_, Ttuple tup) ->
6250+
(match can_index.ci_shape with
6251+
| IntLit i ->
6252+
(match List.nth tup.t_required i with
6253+
| Some ty -> simplify_default ~subtype_env ty can_index.ci_val env
6254+
| None ->
6255+
invalid
6256+
~fail:
6257+
(Some
6258+
Typing_error.(
6259+
primary
6260+
@@ Primary.Generic_unify
6261+
{
6262+
pos = can_index.ci_index_pos;
6263+
msg = Reason.string_of_ureason Reason.index_tuple;
6264+
}))
6265+
env)
6266+
| _ ->
6267+
invalid
6268+
~fail:
6269+
(Some
6270+
Typing_error.(
6271+
primary
6272+
@@ Primary.Generic_unify
6273+
{
6274+
pos = can_index.ci_index_pos;
6275+
msg = Reason.string_of_ureason Reason.URtuple_access;
6276+
}))
6277+
env)
62676278
| _ -> invalid ~fail env
62686279
end
62696280

0 commit comments

Comments
 (0)