Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue 168/auto import to erlang #169

Merged
merged 2 commits into from
Feb 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

abs is not in PLT, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Thank you for the links!

LGTM.

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 =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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)