From 46ae3580c2e9aa705cf398c43c0e2ee16654cdcc Mon Sep 17 00:00:00 2001 From: Mitya Lyubarskiy Date: Mon, 21 Oct 2019 04:40:20 -0700 Subject: [PATCH] [nullsafe] Simplify signature of InferredNullability.join Summary: This function can return `None` if the result is equal to the first argument of join (why first?). It is unclear if it was an optimization attempt of over-complicated logic. Reviewed By: artempyanykh Differential Revision: D17876561 fbshipit-source-id: 9628fb86e --- infer/src/nullsafe/InferredNullability.ml | 6 +----- infer/src/nullsafe/InferredNullability.mli | 2 +- infer/src/nullsafe/typeState.ml | 22 ++++++++++------------ 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/infer/src/nullsafe/InferredNullability.ml b/infer/src/nullsafe/InferredNullability.ml index 6ab1c54c8..bd6eedfc7 100644 --- a/infer/src/nullsafe/InferredNullability.ml +++ b/infer/src/nullsafe/InferredNullability.ml @@ -9,8 +9,6 @@ open! IStd type t = {nullability: Nullability.t; origin: TypeOrigin.t} [@@deriving compare] -let equal = [%compare.equal: t] - let get_nullability {nullability} = nullability let is_nonnull {nullability} = match nullability with Nullable -> false | Nonnull -> true @@ -50,9 +48,7 @@ let join t1 t2 = *) TypeOrigin.join t1.origin t2.origin in - let result = {nullability= joined_nullability; origin= joined_origin} in - (* TODO: make the result return non optional value *) - if equal result t1 then None else Some result + {nullability= joined_nullability; origin= joined_origin} let get_origin t = t.origin diff --git a/infer/src/nullsafe/InferredNullability.mli b/infer/src/nullsafe/InferredNullability.mli index 77a1f41f4..3a84f15ec 100644 --- a/infer/src/nullsafe/InferredNullability.mli +++ b/infer/src/nullsafe/InferredNullability.mli @@ -43,7 +43,7 @@ val of_annotated_nullability : AnnotatedNullability.t -> TypeOrigin.t -> t val get_origin : t -> TypeOrigin.t (** The simple explanation of how was nullability inferred. *) -val join : t -> t -> t option +val join : t -> t -> t (** This is what happens with nullability when we join two flows in CFG, e.g. {[ diff --git a/infer/src/nullsafe/typeState.ml b/infer/src/nullsafe/typeState.ml index db715fc11..631d71031 100644 --- a/infer/src/nullsafe/typeState.ml +++ b/infer/src/nullsafe/typeState.ml @@ -39,18 +39,16 @@ let pp fmt typestate = let map_join m1 m2 = let range_join _exp range1_opt range2_opt = Option.both range1_opt range2_opt - |> Option.map - ~f:(fun (((typ1, inferred_nullability1) as range1), (_, inferred_nullability2)) -> - InferredNullability.join inferred_nullability1 inferred_nullability2 - |> Option.value_map ~default:range1 ~f:(fun ta' -> - ( (* Java does not support local type inference (for codebases and Java version nullsafe is currently aimed for). - The real type does not depend on types being joined; it is determined by the corresponding type declaration instead. - We don't really use type information for reasons not related to things like diagnostic, and using one of types - serves as a good proxy. - Let's take the left one. - *) - typ1 - , ta' ) ) ) + |> Option.map ~f:(fun ((typ1, inferred_nullability1), (_, inferred_nullability2)) -> + (* Unlike nullability that Nullsafe infers, Java does not support local type inference + (for codebases and Java version nullsafe is currently aimed for). + The real type does not depend on types being joined; it is determined by the corresponding type declaration instead. + We don't really use type information for reasons not related to things like diagnostic, and using one of types + serves as a good proxy. + Let's take the left one. + *) + let joined_type = typ1 in + (joined_type, InferredNullability.join inferred_nullability1 inferred_nullability2) ) in if phys_equal m1 m2 then m1 else M.merge range_join m1 m2