Skip to content

Commit

Permalink
Merge pull request #68 from Qqwy/66-__MODULE__
Browse files Browse the repository at this point in the history
__MODULE__ is expanded correctly
  • Loading branch information
Qqwy committed Oct 13, 2021
2 parents 4103411 + fab9e9f commit 1ca2bc7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ Details:

### Changelog
- master -
- Fixes:
- Wrapping private functions no longer make the function public. (c.f. #64)
- Wrapping macros now works correctly. (also related to #64)
- Using `__MODULE__` inside a struct inside a type now expands correctly. (c.f. #66)
- 0.9.0 -
- Support for bitstring type syntax: `<<>>`, `<<_ :: size>>`, `<<_ :: _ * unit>>`, `<<_ :: size, _ :: _ * unit>>` (both as types and as generators)
- 0.8.2 -
Expand Down
2 changes: 1 addition & 1 deletion lib/type_check/internals/pre_expander.ex
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ defmodule TypeCheck.Internals.PreExpander do
# TODO wrap in struct-checker
quote generated: true, location: :keep do
TypeCheck.Builtin.fixed_map(
[__struct__: TypeCheck.Builtin.literal(unquote(struct_name))] ++ unquote(field_types)
[__struct__: unquote(rewrite(struct_name, env, options))] ++ unquote(field_types)
)
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/type_check/macros.ex
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,18 @@ defmodule TypeCheck.Macros do
end
end

def define_type(other, kind, caller) do
raise TypeCheck.CompileError, """
Compilation error encountered while attempting to create a `#{to_string(kind)}`
in `#{to_string(caller.module)}`.
Cannot parse syntax:
#{Macro.to_string(other)}
Maybe you forgot to give the type a name?
"""
end

defp append_typedoc(caller, extra_doc) do
{_line, old_doc} = Module.get_attribute(caller.module, :typedoc) || {0, ""}
newdoc = old_doc <> extra_doc
Expand Down
19 changes: 19 additions & 0 deletions test/type_check/macros_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,23 @@ defmodule TypeCheck.MacrosTest do
end
end)
end

test "__MODULE__ is expanded correctly" do
defmodule ModuleExpansion do
use TypeCheck

@type! t :: %__MODULE__{name: String.t()}
defstruct [:name]

@spec! build(String.t()) :: t()
def build(name) do
%__MODULE__{name: name}
end
end

assert ModuleExpansion.build("hello") == %{__struct__: ModuleExpansion, name: "hello"}
assert_raise TypeCheck.TypeError, fn ->
ModuleExpansion.build(:not_a_string)
end
end
end

0 comments on commit 1ca2bc7

Please sign in to comment.