Skip to content

Commit aaa775a

Browse files
committed
Fix @macrocallback spec translation
Signed-off-by: James Fish <[email protected]>
1 parent 6647b00 commit aaa775a

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

lib/elixir/lib/kernel/typespec.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ defmodule Kernel.Typespec do
448448
defp translate_spec(kind, meta, name, args, return, guard, caller) when is_atom(args),
449449
do: translate_spec(kind, meta, name, [], return, guard, caller)
450450
defp translate_spec(:macrocallback, meta, name, args, return, guard, caller),
451-
do: translate_spec(:callback, meta, :"MACRO-#{name}", [quote(do: env :: Macro.Env.t) | args], return, guard, caller)
451+
do: translate_spec(:callback, meta, :"MACRO-#{name}", macro_args(args), return, guard, caller)
452452
defp translate_spec(kind, meta, name, args, return, guard, caller) do
453453
ensure_no_defaults!(args)
454454

@@ -470,6 +470,10 @@ defmodule Kernel.Typespec do
470470
{{kind, {name, arity}, spec}, caller.line}
471471
end
472472

473+
defp macro_args(args) do
474+
[quote(do: {line :: Macro.Env.line, env :: Macro.Env.t}) | args]
475+
end
476+
473477
defp ensure_no_defaults!(args) do
474478
:lists.foreach fn
475479
{:::, _, [left, right]} ->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
defmodule Dialyzer.Macrocallback do
2+
@macrocallback required(atom) :: Macro.t
3+
@macrocallback optional(atom) :: Macro.t
4+
@optional_callbacks [optional: 1]
5+
end
6+
7+
defmodule Dialyzer.Macrocallback.Impl do
8+
@behaviour Dialyzer.Macrocallback
9+
defmacro required(var), do: Macro.expand(var, __CALLER__)
10+
defmacro optional(var), do: Macro.expand(var, __CALLER__)
11+
end

lib/elixir/test/elixir/kernel/dialyzer_test.exs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ defmodule Kernel.DialyzerTest do
1616
|> Path.join("base_plt")
1717
|> String.to_charlist()
1818

19-
# Add a few key elixir modules for types
20-
files = Enum.map([Kernel, String, Keyword, Exception], &:code.which/1)
19+
# Some OSs (like Windows) do not provide the HOME environment variable.
20+
unless System.get_env("HOME") do
21+
System.put_env("HOME", System.user_home())
22+
end
23+
24+
# Add a few key elixir modules for types and macro functions
25+
mods = [Kernel, String, Keyword, Exception, Macro, Macro.Env, :elixir_env]
26+
files = Enum.map(mods, &:code.which/1)
2127
:dialyzer.run([analysis_type: :plt_build, output_plt: plt,
2228
apps: [:erts], files: files])
2329

@@ -64,6 +70,12 @@ defmodule Kernel.DialyzerTest do
6470
assert_dialyze_no_warnings! context
6571
end
6672

73+
test "no warnings on macrocallback", context do
74+
copy_beam! context, Dialyzer.Macrocallback
75+
copy_beam! context, Dialyzer.Macrocallback.Impl
76+
assert_dialyze_no_warnings! context
77+
end
78+
6779
defp copy_beam!(context, module) do
6880
name = "#{module}.beam"
6981
File.cp! Path.join(context[:base_dir], name),

0 commit comments

Comments
 (0)