From f8ff337187f86861aec68803e037eef3e5f6b00f Mon Sep 17 00:00:00 2001 From: Max Heiber Date: Thu, 27 Oct 2022 09:20:29 -0700 Subject: [PATCH] Handle self, static, super Summary: In the shape-like dict analysis, handle constant accesses that go through `self`, `static`, and `parent`. Reviewed By: madgen Differential Revision: D40429969 fbshipit-source-id: e045c2e4657e15fb1d7c783d96ddd490d115f858 --- .../src/shape_analysis/shape_analysis_walker.ml | 23 +++++++++++++++++++--- .../{const_parent_wip.php => const_parent.php} | 0 ...nst_parent_wip.php.exp => const_parent.php.exp} | 7 ++----- .../{const_self_wip.php => const_self.php} | 0 .../{const_self_wip.php.exp => const_self.php.exp} | 7 ++----- .../{const_static_wip.php => const_static.php} | 0 ...nst_static_wip.php.exp => const_static.php.exp} | 7 ++----- 7 files changed, 26 insertions(+), 18 deletions(-) rename hphp/hack/test/shape_analysis/solve_constraints/{const_parent_wip.php => const_parent.php} (100%) rename hphp/hack/test/shape_analysis/solve_constraints/{const_parent_wip.php.exp => const_parent.php.exp} (64%) rename hphp/hack/test/shape_analysis/solve_constraints/{const_self_wip.php => const_self.php} (100%) rename hphp/hack/test/shape_analysis/solve_constraints/{const_self_wip.php.exp => const_self.php.exp} (57%) rename hphp/hack/test/shape_analysis/solve_constraints/{const_static_wip.php => const_static.php} (100%) rename hphp/hack/test/shape_analysis/solve_constraints/{const_static_wip.php.exp => const_static.php.exp} (57%) diff --git a/hphp/hack/src/shape_analysis/shape_analysis_walker.ml b/hphp/hack/src/shape_analysis/shape_analysis_walker.ml index 871b1292f63..05bda331a5b 100644 --- a/hphp/hack/src/shape_analysis/shape_analysis_walker.ml +++ b/hphp/hack/src/shape_analysis/shape_analysis_walker.ml @@ -116,6 +116,22 @@ let any_shape_can_flow tast_env ty = in Tast_env.is_sub_type tast_env shape_top ty +let class_name_of_class_id pos tenv class_id = + let open Aast in + match class_id with + | CIparent -> Tast_env.get_parent_id tenv + | CIself -> Tast_env.get_self_id tenv + | CIstatic -> Tast_env.get_self_id tenv + | CIexpr (_, _, Lvar (_, _)) -> + (* TODO(T135268910): handle `classname` / `new $c` *) + None + | CIexpr (_, _, e) -> + failwithpos pos + @@ Printf.sprintf + "Unexpected class name expression: %s" + (Aast_names_utils.expr_name e) + | CI (_, id) -> Some id + let add_key_constraint ~(pos : Pos.t) ~(origin : int) @@ -509,10 +525,11 @@ and expr_ (env : env) ((ty, pos, e) : T.expr) : env * entity = in let env = Env.add_inter_constraint env constr_ in (env, Some entity_) - | A.Class_const ((_, ident_pos, A.CI class_id), (_, const_name)) -> - let entity__ = - { HT.ident_pos; HT.class_name_opt = Some (snd class_id); HT.const_name } + | A.Class_const ((_, ident_pos, class_id), (_, const_name)) -> + let class_name_opt = + class_name_of_class_id ident_pos env.tast_env class_id in + let entity__ = { HT.ident_pos; HT.class_name_opt; HT.const_name } in let entity_ = Inter (HT.ConstantIdentifier entity__) in let env = dynamic_when_local ~origin:__LINE__ env entity_ in let constr_ = diff --git a/hphp/hack/test/shape_analysis/solve_constraints/const_parent_wip.php b/hphp/hack/test/shape_analysis/solve_constraints/const_parent.php similarity index 100% rename from hphp/hack/test/shape_analysis/solve_constraints/const_parent_wip.php rename to hphp/hack/test/shape_analysis/solve_constraints/const_parent.php diff --git a/hphp/hack/test/shape_analysis/solve_constraints/const_parent_wip.php.exp b/hphp/hack/test/shape_analysis/solve_constraints/const_parent.php.exp similarity index 64% rename from hphp/hack/test/shape_analysis/solve_constraints/const_parent_wip.php.exp rename to hphp/hack/test/shape_analysis/solve_constraints/const_parent.php.exp index 07ff21862ca..4b4703736e4 100644 --- a/hphp/hack/test/shape_analysis/solve_constraints/const_parent_wip.php.exp +++ b/hphp/hack/test/shape_analysis/solve_constraints/const_parent.php.exp @@ -1,11 +1,8 @@ -=== Shape analysis results for const_parent_wip.php +=== Shape analysis results for const_parent.php Summary after closing and simplifying for \TheChild: Summary after closing and simplifying for \TheChild::foo: Summary after closing and simplifying for \TheParent::DICT: [4:11-31] [Constant]: - shape('a' => string) + shape('a' => string, ?'b' => string) [4:39-55] [Allocation]: shape('a' => string) - -Errors: -"[9:7-19]: Unsupported expression: Class_const" diff --git a/hphp/hack/test/shape_analysis/solve_constraints/const_self_wip.php b/hphp/hack/test/shape_analysis/solve_constraints/const_self.php similarity index 100% rename from hphp/hack/test/shape_analysis/solve_constraints/const_self_wip.php rename to hphp/hack/test/shape_analysis/solve_constraints/const_self.php diff --git a/hphp/hack/test/shape_analysis/solve_constraints/const_self_wip.php.exp b/hphp/hack/test/shape_analysis/solve_constraints/const_self.php.exp similarity index 57% rename from hphp/hack/test/shape_analysis/solve_constraints/const_self_wip.php.exp rename to hphp/hack/test/shape_analysis/solve_constraints/const_self.php.exp index e7e0092ac43..e15dfd1a1d3 100644 --- a/hphp/hack/test/shape_analysis/solve_constraints/const_self_wip.php.exp +++ b/hphp/hack/test/shape_analysis/solve_constraints/const_self.php.exp @@ -1,10 +1,7 @@ -=== Shape analysis results for const_self_wip.php +=== Shape analysis results for const_self.php Summary after closing and simplifying for \Foo::DICT: [4:11-31] [Constant]: - shape('a' => string) + shape('a' => string, ?'b' => string) [4:39-55] [Allocation]: shape('a' => string) Summary after closing and simplifying for \Foo::foo: - -Errors: -"[6:7-17]: Unsupported expression: Class_const" diff --git a/hphp/hack/test/shape_analysis/solve_constraints/const_static_wip.php b/hphp/hack/test/shape_analysis/solve_constraints/const_static.php similarity index 100% rename from hphp/hack/test/shape_analysis/solve_constraints/const_static_wip.php rename to hphp/hack/test/shape_analysis/solve_constraints/const_static.php diff --git a/hphp/hack/test/shape_analysis/solve_constraints/const_static_wip.php.exp b/hphp/hack/test/shape_analysis/solve_constraints/const_static.php.exp similarity index 57% rename from hphp/hack/test/shape_analysis/solve_constraints/const_static_wip.php.exp rename to hphp/hack/test/shape_analysis/solve_constraints/const_static.php.exp index e32f5a27731..1bb45653d47 100644 --- a/hphp/hack/test/shape_analysis/solve_constraints/const_static_wip.php.exp +++ b/hphp/hack/test/shape_analysis/solve_constraints/const_static.php.exp @@ -1,10 +1,7 @@ -=== Shape analysis results for const_static_wip.php +=== Shape analysis results for const_static.php Summary after closing and simplifying for \Foo::DICT: [4:11-31] [Constant]: - shape('a' => string) + shape('a' => string, ?'b' => string) [4:39-55] [Allocation]: shape('a' => string) Summary after closing and simplifying for \Foo::foo: - -Errors: -"[6:7-19]: Unsupported expression: Class_const" -- 2.11.4.GIT