From 58fe80fdf5df2737052cddd6e2f3d9a89094fd8f Mon Sep 17 00:00:00 2001 From: Mitya Lyubarskiy Date: Tue, 3 Sep 2019 03:31:20 -0700 Subject: [PATCH] [nullsafe] remove 'annotation' abstraction. Summary: This abstraction was not always used consistently. Its usage made more sense when it supported both present annotations and optional annotation (which got removed in previous diff). The rought semantic of that was "what is the inferred type for such and such value (variable or expression) in typestate". So it is not really _annotation_ in first place, it is more like "what we inferred about nullability given annotations, known special cases, and current sybmolic execition state". Let's explicitly rename `map` to `is_nullable`. If/when we need to enhance this further (and we likely will), we will do it accordingly. Reviewed By: jvillard Differential Revision: D17153434 fbshipit-source-id: 3c85b56df --- infer/src/nullsafe/AnnotatedSignature.ml | 16 ++++++---------- infer/src/nullsafe/AnnotatedSignature.mli | 10 ++++------ infer/src/nullsafe/eradicateChecks.ml | 3 +-- infer/src/nullsafe/models.ml | 2 +- infer/src/nullsafe/typeAnnotation.ml | 17 ++++------------- 5 files changed, 16 insertions(+), 32 deletions(-) diff --git a/infer/src/nullsafe/AnnotatedSignature.ml b/infer/src/nullsafe/AnnotatedSignature.ml index 34f07bad8..7485329eb 100644 --- a/infer/src/nullsafe/AnnotatedSignature.ml +++ b/infer/src/nullsafe/AnnotatedSignature.ml @@ -11,10 +11,6 @@ module L = Logging type t = {ret: Annot.Item.t * Typ.t; params: (Mangled.t * Annot.Item.t * Typ.t) list} [@@deriving compare] -type annotation = Nullable [@@deriving compare] - -let ia_is ann ia = match ann with Nullable -> Annotations.ia_is_nullable ia - let get proc_attributes : t = let method_annotation = proc_attributes.ProcAttributes.method_annotation in let formals = proc_attributes.ProcAttributes.formals in @@ -58,17 +54,17 @@ let pp proc_name fmt annotated_signature = let mk_ann_str s = {Annot.class_name= s; parameters= []} -let mk_ann = function Nullable -> mk_ann_str Annotations.nullable +let mk_ia_nullable ia = + if Annotations.ia_is_nullable ia then ia else (mk_ann_str Annotations.nullable, true) :: ia -let mk_ia ann ia = if ia_is ann ia then ia else (mk_ann ann, true) :: ia -let mark_ia ann ia x = if x then mk_ia ann ia else ia +let mark_ia_nullability ia x = if x then mk_ia_nullable ia else ia -let mark proc_name ann asig (b, bs) = +let mark_nullability proc_name asig (b, bs) = let ia, t = asig.ret in - let ret' = (mark_ia ann ia b, t) in + let ret' = (mark_ia_nullability ia b, t) in let mark_param (s, ia, t) x = - let ia' = if x then mk_ia ann ia else ia in + let ia' = if x then mk_ia_nullable ia else ia in (s, ia', t) in let params' = diff --git a/infer/src/nullsafe/AnnotatedSignature.mli b/infer/src/nullsafe/AnnotatedSignature.mli index 7c71aa6dc..62e87eb49 100644 --- a/infer/src/nullsafe/AnnotatedSignature.mli +++ b/infer/src/nullsafe/AnnotatedSignature.mli @@ -14,19 +14,17 @@ type t = ; params: (Mangled.t * Annot.Item.t * Typ.t) list (** Annotated parameters. *) } [@@deriving compare] -type annotation = Nullable [@@deriving compare] - val param_has_annot : (Annot.Item.t -> bool) -> Pvar.t -> t -> bool (** Check if the given parameter has an annotation in the given signature *) -val mark : Typ.Procname.t -> annotation -> t -> bool * bool list -> t -(** Mark the annotated signature with the given annotation map. *) +val mark_nullability : Typ.Procname.t -> t -> bool * bool list -> t +(** Mark the annotated signature with the given nullability of the ret value and given nullability of the params ). *) val get : ProcAttributes.t -> t (** Get a method signature with annotations from a proc_attributes. *) -val mk_ia : annotation -> Annot.Item.t -> Annot.Item.t -(** Add the annotation to the item_annotation. *) +val mk_ia_nullable : Annot.Item.t -> Annot.Item.t +(** Add the nullable annotation to the item_annotation, if not already present. *) val pp : Typ.Procname.t -> Format.formatter -> t -> unit (** Pretty print a method signature with annotations. *) diff --git a/infer/src/nullsafe/eradicateChecks.ml b/infer/src/nullsafe/eradicateChecks.ml index 419757500..6eb83c03c 100644 --- a/infer/src/nullsafe/eradicateChecks.ml +++ b/infer/src/nullsafe/eradicateChecks.ml @@ -18,8 +18,7 @@ let get_field_annotation tenv fn typ = let ia' = (* TODO (t4968422) eliminate not !Config.eradicate check by marking fields as nullified *) (* outside of Eradicate in some other way *) - if not Config.eradicate then AnnotatedSignature.mk_ia AnnotatedSignature.Nullable ia - else ia + if not Config.eradicate then AnnotatedSignature.mk_ia_nullable ia else ia in Some (t, ia') diff --git a/infer/src/nullsafe/models.ml b/infer/src/nullsafe/models.ml index 13535fa87..b36ac93a6 100644 --- a/infer/src/nullsafe/models.ml +++ b/infer/src/nullsafe/models.ml @@ -35,7 +35,7 @@ let get_modelled_annotated_signature proc_attributes = let lookup_models_nullable ann_sig = try let mark = Hashtbl.find annotated_table_nullable proc_id in - AnnotatedSignature.mark proc_name AnnotatedSignature.Nullable ann_sig mark + AnnotatedSignature.mark_nullability proc_name ann_sig mark with Caml.Not_found -> ann_sig in annotated_signature |> lookup_models_nullable diff --git a/infer/src/nullsafe/typeAnnotation.ml b/infer/src/nullsafe/typeAnnotation.ml index d9c04e075..7e8ce98ed 100644 --- a/infer/src/nullsafe/typeAnnotation.ml +++ b/infer/src/nullsafe/typeAnnotation.ml @@ -9,19 +9,13 @@ open! IStd (** Module to represent annotations on types. *) -module AnnotationsMap = Caml.Map.Make (struct - type t = AnnotatedSignature.annotation [@@deriving compare] -end) - -type t = {map: bool AnnotationsMap.t; origin: TypeOrigin.t} [@@deriving compare] +type t = {is_nullable: bool; origin: TypeOrigin.t} [@@deriving compare] let equal = [%compare.equal: t] -let is_nullable ta = try AnnotationsMap.find Nullable ta.map with Caml.Not_found -> false - -let set_nullable b ta = - if Bool.equal (is_nullable ta) b then ta else {ta with map= AnnotationsMap.add Nullable b ta.map} +let is_nullable ta = ta.is_nullable +let set_nullable b ta = if Bool.equal (is_nullable ta) b then ta else {ta with is_nullable= b} let descr_origin ta = let descr_opt = TypeOrigin.get_description ta.origin in @@ -56,10 +50,7 @@ let origin_is_fun_library ta = false -let const_nullable b origin = - let ta = {origin; map= AnnotationsMap.empty} in - set_nullable b ta - +let const_nullable is_nullable origin = {origin; is_nullable} let with_origin ta o = {ta with origin= o}