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

Further improve compilation performance and reduce compiled module size #132

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
10 changes: 7 additions & 3 deletions lib/type_check.ex
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ defmodule TypeCheck do
case unquote(check) do
{:ok, bindings, altered_value} -> {:ok, altered_value}
{:error, problem} ->
exception = TypeCheck.TypeError.exception({problem, unquote(Macro.Env.location(__CALLER__))})
exception = TypeCheck.TypeError.exception({unquote(TypeCheck.Internals.Escaper.escape(type)), {problem, unquote(Macro.Env.location(__CALLER__))}})
{:error, exception}
end
end
Expand Down Expand Up @@ -230,7 +230,11 @@ defmodule TypeCheck do
res = quote generated: true, location: :keep do
case unquote(check) do
{:ok, _bindings, altered_value} -> altered_value
{:error, other} -> raise TypeCheck.TypeError, other
{:error, other} ->
type = unquote(TypeCheck.Internals.Escaper.escape(type))
IO.inspect(type, label: :conforms_type)
exception = TypeCheck.TypeError.exception({type, {other, []}})
raise exception
end
end

Expand Down Expand Up @@ -281,7 +285,7 @@ defmodule TypeCheck do
{:current_stacktrace, [_ , _, caller | _]} = Process.info(self(), :current_stacktrace)
location = elem(caller, 3)
location = update_in(location[:file], &to_string/1)
exception = TypeCheck.TypeError.exception({problem, location})
exception = TypeCheck.TypeError.exception({type, {problem, location}})
{:error, exception}
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/type_check/builtin/atom.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule TypeCheck.Builtin.Atom do

use TypeCheck
@type! t :: %__MODULE__{}
@type! problem_tuple :: {t(), :no_match, %{}, any()}
@type! problem_tuple :: {:no_match, %{}, any()}

defimpl TypeCheck.Protocols.ToCheck do
def to_check(s, param) do
Expand All @@ -19,7 +19,7 @@ defmodule TypeCheck.Builtin.Atom do
{:ok, [], x}

_ ->
{:error, {unquote(Macro.escape(s)), :no_match, %{}, unquote(param)}}
{:error, {:no_match, %{}, unquote(param)}}
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/type_check/builtin/binary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule TypeCheck.Builtin.Binary do

use TypeCheck
@type! t :: %__MODULE__{}
@type! problem_tuple :: {t(), :no_match, %{}, any()}
@type! problem_tuple :: {:no_match, %{}, any()}

defimpl TypeCheck.Protocols.ToCheck do
def to_check(s, param) do
Expand All @@ -13,7 +13,7 @@ defmodule TypeCheck.Builtin.Binary do
{:ok, [], x}

_ ->
{:error, {unquote(Macro.escape(s)), :no_match, %{}, unquote(param)}}
{:error, {:no_match, %{}, unquote(param)}}
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/type_check/builtin/bitstring.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule TypeCheck.Builtin.Bitstring do

use TypeCheck
@type! t :: %__MODULE__{}
@type! problem_tuple :: {t(), :no_match, %{}, any()}
@type! problem_tuple :: {:no_match, %{}, any()}

defimpl TypeCheck.Protocols.ToCheck do
def to_check(s, param) do
Expand All @@ -13,7 +13,7 @@ defmodule TypeCheck.Builtin.Bitstring do
{:ok, [], x}

_ ->
{:error, {unquote(Macro.escape(s)), :no_match, %{}, unquote(param)}}
{:error, {:no_match, %{}, unquote(param)}}
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/type_check/builtin/boolean.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule TypeCheck.Builtin.Boolean do

use TypeCheck
@type! t :: %__MODULE__{}
@type! problem_tuple :: {t(), :no_match, %{}, any()}
@type! problem_tuple :: {:no_match, %{}, any()}

defimpl TypeCheck.Protocols.ToCheck do
def to_check(s, param) do
Expand All @@ -13,7 +13,7 @@ defmodule TypeCheck.Builtin.Boolean do
{:ok, [], x}

_ ->
{:error, {unquote(Macro.escape(s)), :no_match, %{}, unquote(param)}}
{:error, {:no_match, %{}, unquote(param)}}
end
end
end
Expand Down
18 changes: 9 additions & 9 deletions lib/type_check/builtin/compound_fixed_map.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ defmodule TypeCheck.Builtin.CompoundFixedMap do
@type! t :: %__MODULE__{fixed: TypeCheck.Builtin.FixedMap.t(), flexible: TypeCheck.Builtin.Map.t()}

@type! problem_tuple ::
{t(), :not_a_map, %{}, any()}
| {t(), :missing_keys, %{keys: list(atom())}, map()}
| {t(), :superfluous_keys, %{keys: list(atom())}, map()}
| {t(), :value_error,
{:not_a_map, %{}, any()}
| {:missing_keys, %{keys: list(atom())}, map()}
| {:superfluous_keys, %{keys: list(atom())}, map()}
| {:value_error,
%{problem: lazy(TypeCheck.TypeError.Formatter.problem_tuple()), key: any()}, map()}
| {t(), :key_error,
| {:key_error,
%{problem: lazy(TypeCheck.TypeError.Formatter.problem_tuple()), key: any()}, map()}

defimpl TypeCheck.Protocols.ToCheck do
Expand All @@ -36,21 +36,21 @@ defmodule TypeCheck.Builtin.CompoundFixedMap do
do
{:ok, bindings1 ++ bindings2, Map.merge(fixed_part, flexible_part)}
else
{:error, {_, reason, info, _val}} ->
{:error, {unquote(TypeCheck.Internals.Escaper.escape(s)), reason, info, unquote(param)}}
{:error, {reason, info, _val}} ->
{:error, {reason, info, unquote(param)}}
end
end

res
end

defp map_check(param, s) do
defp map_check(param, _s) do
quote generated: true, location: :keep do
case unquote(param) do
val when is_map(val) ->
{:ok, [], val}
other ->
{:error, {unquote(TypeCheck.Internals.Escaper.escape(s)), :not_a_map, %{}, other}}
{:error, {:not_a_map, %{}, other}}
end
end
end
Expand Down
14 changes: 7 additions & 7 deletions lib/type_check/builtin/fixed_list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ defmodule TypeCheck.Builtin.FixedList do
@type! t :: %__MODULE__{element_types: list(TypeCheck.Type.t())}

@type! problem_tuple ::
{t(), :not_a_list, %{}, any()}
| {t(), :different_length, %{expected_length: non_neg_integer()}, list()}
| {t(), :element_error,
{:not_a_list, %{}, any()}
| {:different_length, %{expected_length: non_neg_integer()}, list()}
| {:element_error,
%{problem: lazy(TypeCheck.TypeError.Formatter.problem_tuple()), index: non_neg_integer()},
list()}

Expand All @@ -28,11 +28,11 @@ defmodule TypeCheck.Builtin.FixedList do
quote generated: :true, location: :keep do
case unquote(param) do
x when not is_list(x) ->
{:error, {unquote(Macro.escape(s)), :not_a_list, %{}, x}}
{:error, {:not_a_list, %{}, x}}

x when length(x) != unquote(expected_length) ->
{:error,
{unquote(Macro.escape(s)), :different_length,
{:different_length,
%{expected_length: unquote(expected_length)}, x}}

_ ->
Expand All @@ -41,7 +41,7 @@ defmodule TypeCheck.Builtin.FixedList do
end
end

def build_element_checks_ast(element_types, param, s) do
def build_element_checks_ast(element_types, param, _s) do
element_checks =
element_types
|> Enum.with_index()
Expand Down Expand Up @@ -75,7 +75,7 @@ defmodule TypeCheck.Builtin.FixedList do
else
{{:error, error}, index, _rest} ->
{:error,
{unquote(Macro.escape(s)), :element_error, %{problem: error, index: index},
{:element_error, %{problem: error, index: index},
unquote(param)}}
end
end
Expand Down
16 changes: 8 additions & 8 deletions lib/type_check/builtin/fixed_map.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ defmodule TypeCheck.Builtin.FixedMap do
@type! t :: %__MODULE__{keypairs: list({term(), TypeCheck.Type.t()})}

@type! problem_tuple ::
{t(), :not_a_map, %{}, any()}
| {t(), :missing_keys, %{keys: list(atom())}, map()}
| {t(), :superfluous_keys, %{keys: list(atom())}, map()}
| {t(), :value_error,
{:not_a_map, %{}, any()}
| {:missing_keys, %{keys: list(atom())}, map()}
| {:superfluous_keys, %{keys: list(atom())}, map()}
| {:value_error,
%{problem: lazy(TypeCheck.TypeError.Formatter.problem_tuple()), key: any()}, map()}


Expand Down Expand Up @@ -55,7 +55,7 @@ defmodule TypeCheck.Builtin.FixedMap do
val when is_map(val) ->
{:ok, [], val}
other ->
{:error, {unquote(TypeCheck.Internals.Escaper.escape(s)), :not_a_map, %{}, other}}
{:error, {:not_a_map, %{}, other}}
end
end
end
Expand All @@ -76,7 +76,7 @@ defmodule TypeCheck.Builtin.FixedMap do

missing_keys ->
{:error,
{unquote(TypeCheck.Internals.Escaper.escape(s)), :missing_keys, %{keys: missing_keys}, unquote(param)}}
{:missing_keys, %{keys: missing_keys}, unquote(param)}}
end
end
end
Expand All @@ -96,7 +96,7 @@ defmodule TypeCheck.Builtin.FixedMap do

superfluous_keys ->
{:error,
{unquote(TypeCheck.Internals.Escaper.escape(s)), :superfluous_keys, %{keys: superfluous_keys},
{:superfluous_keys, %{keys: superfluous_keys},
unquote(param)}}
end
end
Expand Down Expand Up @@ -133,7 +133,7 @@ defmodule TypeCheck.Builtin.FixedMap do
else
{{:error, error}, key} ->
{:error,
{unquote(TypeCheck.Internals.Escaper.escape(s)), :value_error, %{problem: error, key: key}, unquote(param)}}
{:value_error, %{problem: error, key: key}, unquote(param)}}
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/type_check/builtin/fixed_tuple.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ defmodule TypeCheck.Builtin.FixedTuple do
@type! t :: %__MODULE__{element_types: list(TypeCheck.Type.t())}

@type! problem_tuple ::
{t(), :not_a_tuple, %{}, any()}
| {t(), :different_size, %{expected_size: integer()}, tuple()}
| {t(), :element_error,
{:not_a_tuple, %{}, any()}
| {:different_size, %{expected_size: integer()}, tuple()}
| {:element_error,
%{problem: lazy(TypeCheck.TypeError.Formatter.problem_tuple()), index: integer()},
tuple()}

Expand All @@ -25,11 +25,11 @@ defmodule TypeCheck.Builtin.FixedTuple do
quote generated: true, location: :keep do
case unquote(param) do
x when not is_tuple(x) ->
{:error, {unquote(Macro.escape(s)), :not_a_tuple, %{}, x}}
{:error, {:not_a_tuple, %{}, x}}

x when tuple_size(x) != unquote(expected_size) ->
{:error,
{unquote(TypeCheck.Internals.Escaper.escape(s)), :different_size, %{expected_size: unquote(expected_size)},
{:different_size, %{expected_size: unquote(expected_size)},
x}}

_ ->
Expand Down Expand Up @@ -69,7 +69,7 @@ defmodule TypeCheck.Builtin.FixedTuple do
else
{{:error, error}, index} ->
{:error,
{unquote(Macro.escape(s)), :element_error, %{problem: error, index: index},
{:element_error, %{problem: error, index: index},
unquote(param)}}
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/type_check/builtin/float.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule TypeCheck.Builtin.Float do

use TypeCheck
@type! t :: %__MODULE__{}
@type! problem_tuple :: {t(), :no_match, %{}, any()}
@type! problem_tuple :: {:no_match, %{}, any()}

defimpl TypeCheck.Protocols.ToCheck do
def to_check(s, param) do
Expand All @@ -13,7 +13,7 @@ defmodule TypeCheck.Builtin.Float do
{:ok, [], x}

_ ->
{:error, {unquote(Macro.escape(s)), :no_match, %{}, unquote(param)}}
{:error, {:no_match, %{}, unquote(param)}}
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/type_check/builtin/function.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule TypeCheck.Builtin.Function do
param_types: list(TypeCheck.Type.t()) | nil,
return_type: TypeCheck.Type.t()
}
@type! problem_tuple :: {t(), :no_match, %{}, any()}
@type! problem_tuple :: {:no_match, %{}, any()}

defimpl TypeCheck.Protocols.Escape do
def escape(s) do
Expand All @@ -31,7 +31,7 @@ defmodule TypeCheck.Builtin.Function do
{:ok, [], wrapped_fun}

_ ->
{:error, {unquote(Macro.escape(s)), :no_match, %{}, unquote(param)}}
{:error, {:no_match, %{}, unquote(param)}}
end
end
end
Expand Down Expand Up @@ -84,7 +84,7 @@ defmodule TypeCheck.Builtin.Function do

{:error, problem} ->
raise TypeCheck.TypeError,
{unquote(Macro.escape(s)), :return_error,
{:return_error,
%{problem: problem, arguments: unquote(clean_params)},
var!(result, nil)}
end
Expand Down
6 changes: 4 additions & 2 deletions lib/type_check/builtin/guarded.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ defmodule TypeCheck.Builtin.Guarded do

@type! t() :: %TypeCheck.Builtin.Guarded{type: TypeCheck.Type.t(), guard: ast()}

# TODO problem_tuple type


defimpl TypeCheck.Protocols.Escape do
def escape(s) do
Expand Down Expand Up @@ -95,13 +97,13 @@ defmodule TypeCheck.Builtin.Guarded do
{:ok, bindings, altered_param}
else
{:error,
{unquote(Macro.escape(s)), :guard_failed, %{bindings: bindings_map},
{:guard_failed, %{bindings: bindings_map},
unquote(param)}}
end

{:error, problem} ->
{:error,
{unquote(Macro.escape(s)), :type_failed, %{problem: problem}, unquote(param)}}
{:type_failed, %{problem: problem}, unquote(param)}}
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/type_check/builtin/implements_protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ defmodule TypeCheck.Builtin.ImplementsProtocol do

use TypeCheck
@type! t :: %__MODULE__{protocol: module()}
@type! problem_tuple :: {t(), :no_match, %{}, any()}
@type! problem_tuple :: {:no_match, %{}, any()}

defimpl TypeCheck.Protocols.ToCheck do
def to_check(s, param) do
quote generated: true, location: :keep do
x = unquote(param)
case unquote(s.protocol).impl_for(x) do
nil ->
{:error, {unquote(Macro.escape(s)), :no_match, %{}, x}}
{:error, {:no_match, %{}, x}}
_ ->
{:ok, [], x}
end
Expand Down
4 changes: 2 additions & 2 deletions lib/type_check/builtin/integer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule TypeCheck.Builtin.Integer do

use TypeCheck
@type! t :: %__MODULE__{}
@type! problem_tuple :: {t(), :no_match, %{}, any()}
@type! problem_tuple :: {:no_match, %{}, any()}

defimpl TypeCheck.Protocols.ToCheck do
def to_check(s, param) do
Expand All @@ -13,7 +13,7 @@ defmodule TypeCheck.Builtin.Integer do
{:ok, [], x}

_ ->
{:error, {unquote(Macro.escape(s)), :no_match, %{}, unquote(param)}}
{:error, {:no_match, %{}, unquote(param)}}
end
end
end
Expand Down
Loading