[nullsafe] `get_modelled_annotated_signature` respects models in NullsafeType

Summary:
This function is the main entry point for getting annotated signature
for nullsafe.

We will modify it and its callees in follow up diffs to migrate other
features of Annot.items to specialized types.

Reviewed By: ngorogiannis

Differential Revision: D17448082

fbshipit-source-id: be00b4737
master
Mitya Lyubarskiy 5 years ago committed by Facebook Github Bot
parent 47f314c00e
commit ebf9e84e3c

@ -111,34 +111,54 @@ let mk_ia_nullable ia =
let mark_ia_nullability ia x = if x then mk_ia_nullable ia else ia let mark_ia_nullability ia x = if x then mk_ia_nullable ia else ia
(* TODO(T54088319) Make this update NullsafeType.t as well*) (* Override existing information about nullability for a given type and
let mark_nullability proc_name asig (b, bs) = set it to either nullable or nonnull *)
let ia, t = asig.ret in let set_modelled_nullability_for_nullsafe_type nullsafe_type should_set_nullable =
let ret' = (mark_ia_nullability ia b, t) in let nullability =
let mark_param (s, ia, t) x = if should_set_nullable then NullsafeType.Nullable ModelledNullable
let ia' = if x then mk_ia_nullable ia else ia in else NullsafeType.Nonnull ModelledNonnull
(s, ia', t)
in in
let params' = NullsafeType.{nullsafe_type with nullability}
let set_modelled_nullability proc_name asig (nullability_for_ret, params_nullability) =
let set_modelled_nullability_for_param (mangled, original_annotation, original_nullsafe_type)
should_set_nullable =
let final_annotation =
if should_set_nullable then mk_ia_nullable original_annotation else original_annotation
in
( mangled
, final_annotation
, set_modelled_nullability_for_nullsafe_type original_nullsafe_type should_set_nullable )
in
let final_params =
let fail () = let fail () =
L.die InternalError L.die InternalError
"Annotation for procedure %s has wrong number of arguments.@\n Annotated signature: %a" "Annotation for procedure %s has wrong number of arguments.@\n Annotated signature: %a"
(Typ.Procname.to_unique_id proc_name) (Typ.Procname.to_unique_id proc_name)
(pp proc_name) asig (pp proc_name) asig
in in
let rec combine l1 l2 = let rec model_param_nullability original_params params_nullability =
match (l1, l2) with match (original_params, params_nullability) with
| (p, ia, t) :: l1', l2' when Mangled.is_this p -> | (mangled, annotation, nullsafe_type) :: params_tail, nullability_tail
(p, ia, t) :: combine l1' l2' when Mangled.is_this mangled ->
| (s, ia, t) :: l1', x :: l2' -> (* Skip "this" param - there is no notion of "nullable this" *)
mark_param (s, ia, t) x :: combine l1' l2' (mangled, annotation, nullsafe_type)
| [], _ :: _ -> :: model_param_nullability params_tail nullability_tail
fail () | param :: params_tail, should_set_nullable :: nullability_tail ->
| _ :: _, [] -> set_modelled_nullability_for_param param should_set_nullable
:: model_param_nullability params_tail nullability_tail
| [], _ :: _ | _ :: _, [] ->
(* One list extausted before the other one *)
fail () fail ()
| [], [] -> | [], [] ->
[] []
in in
combine asig.params bs model_param_nullability asig.params params_nullability
in
let original_ret_annotations, original_ret_nullsafe_type = asig.ret in
let final_ret_nullsafe_type =
set_modelled_nullability_for_nullsafe_type original_ret_nullsafe_type nullability_for_ret
in in
{ret= ret'; params= params'} let final_ret_annotation = mark_ia_nullability original_ret_annotations nullability_for_ret in
{ret= (final_ret_annotation, final_ret_nullsafe_type); params= final_params}

@ -17,8 +17,8 @@ type t =
val param_has_annot : (Annot.Item.t -> bool) -> Pvar.t -> t -> bool val param_has_annot : (Annot.Item.t -> bool) -> Pvar.t -> t -> bool
(** Check if the given parameter has an annotation in the given signature *) (** Check if the given parameter has an annotation in the given signature *)
val mark_nullability : Typ.Procname.t -> t -> bool * bool list -> t val set_modelled_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 ). *) (** Override nullability for a function signature given its modelled nullability (for ret value and params) *)
val get : ProcAttributes.t -> t val get : ProcAttributes.t -> t
(** Get a method signature with annotations from a proc_attributes. *) (** Get a method signature with annotations from a proc_attributes. *)

@ -34,8 +34,8 @@ let get_modelled_annotated_signature proc_attributes =
let proc_id = Typ.Procname.to_unique_id proc_name in let proc_id = Typ.Procname.to_unique_id proc_name in
let lookup_models_nullable ann_sig = let lookup_models_nullable ann_sig =
try try
let mark = Hashtbl.find annotated_table_nullable proc_id in let modelled_nullability = Hashtbl.find annotated_table_nullable proc_id in
AnnotatedSignature.mark_nullability proc_name ann_sig mark AnnotatedSignature.set_modelled_nullability proc_name ann_sig modelled_nullability
with Caml.Not_found -> ann_sig with Caml.Not_found -> ann_sig
in in
annotated_signature |> lookup_models_nullable annotated_signature |> lookup_models_nullable

Loading…
Cancel
Save