Skip to content

Commit

Permalink
[core] migrate more uses of Set and Map
Browse files Browse the repository at this point in the history
Summary: Migrating to OCaml 5.2 requires upgrading Core and that breaks many uses of `Set` and `Map`. This diff fixes uses in Erlang, Topl and Lineage by converting to `Stdlib` sets and maps where necessary.

Reviewed By: geralt-encore

Differential Revision:
D66357472

Privacy Context Container: L1208441

fbshipit-source-id: ce499f0ffe9b4501ac376f7ebf9d697ce9eba256
  • Loading branch information
ngorogiannis authored and facebook-github-bot committed Nov 22, 2024
1 parent 0b40efe commit 6da548c
Show file tree
Hide file tree
Showing 20 changed files with 179 additions and 156 deletions.
2 changes: 1 addition & 1 deletion infer/src/base/ToplAst.ml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type label =
; pattern: label_pattern }
[@@deriving show]

type vertex = string [@@deriving compare, hash, sexp, show]
type vertex = string [@@deriving compare, hash, sexp, show, equal]

type transition = {source: vertex; target: vertex; label: label option} [@@deriving show]

Expand Down
19 changes: 10 additions & 9 deletions infer/src/checkers/Lineage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ module Local = struct
| ConstantString of string
| Cell of (Cell.t[@sexp.opaque])
[@@deriving compare, equal, sexp, hash]

let pp _ _ = assert false
end

include T
include Comparable.Make (T)

let pp fmt local =
match local with
Expand All @@ -88,10 +89,10 @@ module Local = struct


module Set = struct
include Set
include PrettyPrintable.MakeHashSexpPPSet (T)

(** Shortcut for adding a Cell-variant local *)
let add_cell set cell = add set (Cell cell)
let add_cell set cell = add (Cell cell) set
end
end

Expand Down Expand Up @@ -433,13 +434,13 @@ end = struct
module FieldPathSet = struct
(* Sets of field paths, that shall be associated to an argument index *)

module M = Set.Make_tree (FieldPath)
module M = PrettyPrintable.MakeHashSexpPPSet (FieldPath)
include M

let pp ~arg_index =
(* Prints: $argN#foo#bar $argN#other#field *)
let pp_field_path = Fmt.using (fun path -> arg_index & path) F.arg_path in
IFmt.Labelled.iter ~sep:Fmt.sp M.iter pp_field_path
IFmt.Labelled.iter ~sep:Fmt.sp (fun a ~f -> M.iter f a) pp_field_path
end

(* Marshallable maps from integer indices. Note: using an array instead of Map could improve the
Expand Down Expand Up @@ -469,15 +470,15 @@ end = struct
| None ->
FieldPathSet.singleton arg_field_path
| Some field_path_set ->
FieldPathSet.add field_path_set arg_field_path )
FieldPathSet.add arg_field_path field_path_set )


let fold t ~f ~init =
IntMap.fold
~f:(fun ~key:arg_index ~data:field_path_set acc ->
FieldPathSet.fold
~f:(fun acc arg_field_path -> f ~arg_index ~arg_field_path acc)
~init:acc field_path_set )
(fun arg_field_path acc -> f ~arg_index ~arg_field_path acc)
field_path_set acc )
~init t
end

Expand Down Expand Up @@ -1514,7 +1515,7 @@ end = struct

let local_set local_set : unit t =
fun shapes node f astate ->
Set.fold ~f:(fun acc one_local -> local one_local shapes node f acc) ~init:astate local_set
Local.Set.fold (fun one_local acc -> local one_local shapes node f acc) local_set astate


let atom atom_name : unit t = local (ConstantAtom atom_name)
Expand Down
16 changes: 7 additions & 9 deletions infer/src/checkers/LineageShape.ml
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ end
returns some field [foo] of [X]: we want to know that we should create the Lineage node
[$argN#foo] at the same time as the node [$argN]. *)

module StringSet = Set.Make_tree (String)
module StringSet = IString.Set
module FieldLabelMap = Map.Make_tree (FieldLabel)

module Structure : sig
Expand Down Expand Up @@ -352,7 +352,7 @@ end = struct
Fmt.pf fmt "?"
| Variant constructors ->
Fmt.pf fmt "[@[%a@]]"
(IFmt.Labelled.iter ~sep:(Fmt.any "@ |@ ") StringSet.iter Fmt.string)
(IFmt.Labelled.iter ~sep:(Fmt.any "@ |@ ") (fun a ~f -> StringSet.iter f a) Fmt.string)
constructors
| Vector {is_fully_abstract; fields; all_map_value} ->
(* Example: ('foo' : <1>, 'bar' : <1>, baz : <2>) {* : <1>} *)
Expand Down Expand Up @@ -389,7 +389,7 @@ end = struct
let local_abstract = LocalAbstract

let variant constructors =
if StringSet.length constructors <= ShapeConfig.variant_width then Variant constructors
if StringSet.cardinal constructors <= ShapeConfig.variant_width then Variant constructors
else scalar


Expand Down Expand Up @@ -653,7 +653,7 @@ end = struct
let id = Union_find.get shape in
match Hashtbl.find shape_structures id with
| Some (Variant constructors) ->
Some (StringSet.to_list constructors)
Some (StringSet.elements constructors)
| _ ->
None

Expand Down Expand Up @@ -1457,9 +1457,7 @@ end = struct
let fold_field_labels_actual summary (var, field_path) ~init ~f ~fallback =
match find_var_path_structure summary (var, field_path) with
| Variant set ->
StringSet.fold ~init
~f:(fun acc constructor -> f acc (FieldLabel.map_key constructor))
set
StringSet.fold (fun constructor acc -> f acc (FieldLabel.map_key constructor)) set init
| _ ->
fallback init

Expand All @@ -1477,8 +1475,8 @@ end = struct
let* summary = summary_option in
match find_var_path_structure summary var_path with
| Variant set ->
if Int.O.(StringSet.length set = 1) then
let+ constructor = StringSet.choose set in
if Int.O.(StringSet.cardinal set = 1) then
let+ constructor = StringSet.choose_opt set in
FieldLabel.map_key constructor
else None
| _ ->
Expand Down
12 changes: 6 additions & 6 deletions infer/src/checkers/annotationReachability.ml
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ let parse_custom_models () =
match Config.annotation_reachability_custom_models with
(* The default value for JSON options is an empty list and not an empty object *)
| `List [] ->
String.Map.empty
IString.Map.empty
| json ->
json |> Yojson.Safe.Util.to_assoc
|> List.map ~f:(fun (key, val_arr) ->
( key
, val_arr |> Yojson.Safe.Util.to_list
|> List.map ~f:Yojson.Safe.Util.to_string
|> List.map ~f:Str.regexp ) )
|> String.Map.of_alist_exn
|> Stdlib.List.to_seq |> IString.Map.of_seq


let check_attributes check tenv pname =
Expand All @@ -111,7 +111,7 @@ let check_modeled_annotation models annot pname =
let method_name =
Procname.to_string ~verbosity:(if Procname.is_erlang pname then Verbose else FullNameOnly) pname
in
Option.exists (String.Map.find models annot.Annot.class_name) ~f:(fun methods ->
Option.exists (IString.Map.find_opt annot.Annot.class_name models) ~f:(fun methods ->
List.exists methods ~f:(fun r -> Str.string_match r method_name 0) )


Expand Down Expand Up @@ -166,7 +166,7 @@ module AnnotationSpec = struct
; name: string (** Short name to be added at the beginning of the report *)
; description: string (** Extra description to be added to the issue report *)
; issue_type: IssueType.t
; models: Str.regexp list IStd.String.Map.t (** model functions as if they were annotated *)
; models: Str.regexp list IString.Map.t (** model functions as if they were annotated *)
; pre_check: Domain.t InterproceduralAnalysis.t -> unit
(** additional check before reporting *) }
end
Expand Down Expand Up @@ -386,7 +386,7 @@ module NoAllocationAnnotationSpec = struct
; name= ""
; description= ""
; issue_type= IssueType.checkers_allocates_memory
; models= String.Map.empty
; models= IString.Map.empty
; pre_check= (fun _ -> ()) }
end

Expand Down Expand Up @@ -427,7 +427,7 @@ module ExpensiveAnnotationSpec = struct
; name= ""
; description= ""
; issue_type= IssueType.checkers_calls_expensive_method
; models= String.Map.empty
; models= IString.Map.empty
; pre_check=
(fun ({InterproceduralAnalysis.proc_desc; tenv} as analysis_data) ->
let proc_name = Procdesc.get_proc_name proc_desc in
Expand Down
2 changes: 1 addition & 1 deletion infer/src/erlang/ErlangAst.ml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ and tuple_type = AnySize | FixedSize of type_ list [@@deriving sexp_of]

(* Function specs can be overloaded, forming disjunctions.
Currently the only kind of constraints are "subtype of" which we track in a map. *)
type spec_disjunct = {arguments: type_ list; return: type_; constraints: type_ String.Map.t}
type spec_disjunct = {arguments: type_ list; return: type_; constraints: type_ IString.Map.t}
[@@deriving sexp_of]

type spec = spec_disjunct list [@@deriving sexp_of]
Expand Down
8 changes: 4 additions & 4 deletions infer/src/erlang/ErlangAstValidator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ let pp_location (loc : Ast.location) =


let validate_record_name (env : (_, _) Env.t) name =
match String.Map.find env.records name with
match IString.Map.find_opt name env.records with
| None ->
L.debug Capture Verbose "Record definition not found for '%s'@." name ;
false
Expand All @@ -31,12 +31,12 @@ let validate_record_name (env : (_, _) Env.t) name =


let validate_record_field (env : (_, _) Env.t) name field =
match String.Map.find env.records name with
match IString.Map.find_opt name env.records with
| None ->
L.debug Capture Verbose "Record definition not found for '%s'@." name ;
false
| Some record_info -> (
match String.Map.find record_info.field_info field with
match IString.Map.find_opt field record_info.field_info with
| None ->
L.debug Capture Verbose "Record field '%s' not found in definition of '%s'@." field name ;
false
Expand Down Expand Up @@ -357,7 +357,7 @@ let rec validate_type (type_ : Ast.type_) =
let validate_spec_disjunct ({arguments; return; constraints} : Ast.spec_disjunct) =
List.for_all ~f:validate_type arguments
&& validate_type return
&& String.Map.for_all ~f:validate_type constraints
&& IString.Map.for_all (fun _k v -> validate_type v) constraints


let validate_spec_arities (func_arity : int) (spec : Ast.spec) =
Expand Down
Loading

0 comments on commit 6da548c

Please sign in to comment.