Skip to content

Commit

Permalink
Merge pull request #169 from dwango/feature/issue-168/auto_import_to_…
Browse files Browse the repository at this point in the history
…erlang

Feature/issue 168/auto import to erlang
  • Loading branch information
yoshihiro503 authored Feb 1, 2019
2 parents 06763a4 + 59fda9e commit 3bce88e
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
2 changes: 2 additions & 0 deletions lib/bif.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ let type_sigs = [
Type.(of_elem (TyFun ([Type.bool; Type.bool], Type.bool))));
({module_name="erlang"; function_name="not"; arity=1},
Type.(of_elem (TyFun ([Type.bool], Type.bool))));
({module_name="erlang"; function_name="abs"; arity=1},
Type.(of_elem (TyFun ([of_elem TyNumber], of_elem TyNumber))));
]
17 changes: 12 additions & 5 deletions lib/context.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
open Base
open Polymorphic_compare
module Format = Caml.Format

module Key = struct
Expand All @@ -20,11 +21,17 @@ let add key data t =
Log.debug [%here] "variable '%s' shadowed" (Key.show key);
t

let add_bif_signatures ctx0 : t =
let add_bif_signatures imports ctx0 : t =
let update ctx (mfa, ty) =
if List.mem imports mfa.Mfa.module_name ~equal:(=) then
add (Key.MFA mfa) ty ctx
|> add (Key.LocalFun {function_name=mfa.Mfa.function_name; arity=mfa.Mfa.arity}) ty
else
add (Key.MFA mfa) ty ctx
in
Bif.type_sigs
|> List.fold_left ~f:(fun ctx (mfa, ty) ->
add (Key.MFA mfa) ty ctx) ~init:ctx0
|> List.fold_left ~f:update ~init:ctx0

let init () =
let create ~import_modules =
empty
|> add_bif_signatures
|> add_bif_signatures import_modules
2 changes: 1 addition & 1 deletion lib/context.mli
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ type t
val empty : t
val find : t -> Key.t -> Type.t option
val add : Key.t -> Type.t -> t -> t
val init : unit -> t
val create : import_modules : string list -> t
5 changes: 3 additions & 2 deletions lib/type_check.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ let check_module plt ctx m =
let open Result in
Log.debug [%here] "Checking module: %s" (m.Ast.name);
From_erlang.module_to_expr m >>= fun expr ->
Derivation.derive (Context.init ()) expr >>= fun (ty, c) ->
Derivation.derive ctx expr >>= fun (ty, c) ->
Log.debug [%here] "Constraints:\n%s" (Type.show_constraint c);
Solver.solve Solver.init c >>= fun sol ->
Log.debug [%here] "Types:\n%s" (Solver.lookup_type sol ty |> Type.pp);
Ok ()

let check_modules plt modules =
let ctx = Context.init () in (*TODO: make type context from specs of the modules *)
let import_modules = ["erlang"] in (*TODO: https://github.com/dwango/fialyzer/issues/166 *)
let ctx = Context.create ~import_modules in (*TODO: https://github.com/dwango/fialyzer/issues/167 *)
let open Result in
result_map_m ~f:(check_module plt ctx) modules >>= fun _ ->
Result.return ()
7 changes: 7 additions & 0 deletions test/blackbox-test/test-cases/auto_import.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-module(auto_import).

-export([foo/1]).

-spec foo(number()) -> number().
foo(X) ->
abs(X).
1 change: 1 addition & 0 deletions test/blackbox-test/test-cases/auto_import.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
done (passed successfully)

0 comments on commit 3bce88e

Please sign in to comment.