Skip to content

Commit aa93d65

Browse files
committed
Propagate diagnostics from inner compiler process
1 parent f5b71a7 commit aa93d65

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

lib/elixir/src/elixir_erl_compiler.erl

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,42 @@
55
spawn(Fun) ->
66
CompilerInfo = get(elixir_compiler_info),
77

8+
CodeDiagnostics =
9+
case get(elixir_code_diagnostics) of
10+
undefined -> undefined;
11+
{_Tail, Log} -> {[], Log}
12+
end,
13+
814
{_, Ref} =
915
spawn_monitor(fun() ->
1016
put(elixir_compiler_info, CompilerInfo),
17+
put(elixir_code_diagnostics, CodeDiagnostics),
1118

1219
try Fun() of
13-
Result -> exit({ok, Result})
20+
Result -> exit({ok, Result, get(elixir_code_diagnostics)})
1421
catch
1522
Kind:Reason:Stack ->
16-
exit({Kind, Reason, Stack})
23+
exit({Kind, Reason, Stack, get(elixir_code_diagnostics)})
1724
end
1825
end),
1926

2027
receive
21-
{'DOWN', Ref, process, _, {ok, Result}} ->
28+
{'DOWN', Ref, process, _, {ok, Result, Diagnostics}} ->
29+
copy_diagnostics(Diagnostics),
2230
Result;
23-
{'DOWN', Ref, process, _, {Kind, Reason, Stack}} ->
31+
{'DOWN', Ref, process, _, {Kind, Reason, Stack, Diagnostics}} ->
32+
copy_diagnostics(Diagnostics),
2433
erlang:raise(Kind, Reason, Stack)
2534
end.
2635

36+
copy_diagnostics(undefined) ->
37+
ok;
38+
copy_diagnostics({Head, _}) ->
39+
case get(elixir_code_diagnostics) of
40+
undefined -> ok;
41+
{Tail, Log} -> put(elixir_code_diagnostics, {Head ++ Tail, Log})
42+
end.
43+
2744
forms(Forms, File, Opts) ->
2845
compile(Forms, File, Opts ++ compile:env_compiler_options()).
2946

lib/elixir/src/elixir_module.erl

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -508,19 +508,17 @@ beam_location(ModuleAsCharlist) ->
508508
checker_info() ->
509509
case get(elixir_checker_info) of
510510
undefined -> undefined;
511-
_ ->
512-
Log =
513-
case erlang:get(elixir_code_diagnostics) of
514-
{_, false} -> false;
515-
_ -> true
516-
end,
517-
518-
{'Elixir.Module.ParallelChecker':get(), Log}
511+
_ -> 'Elixir.Module.ParallelChecker':get()
519512
end.
520513

521514
spawn_parallel_checker(undefined, _Module, _ModuleMap) ->
522-
nil;
523-
spawn_parallel_checker({CheckerInfo, Log}, Module, ModuleMap) ->
515+
ok;
516+
spawn_parallel_checker(CheckerInfo, Module, ModuleMap) ->
517+
Log =
518+
case erlang:get(elixir_code_diagnostics) of
519+
{_, false} -> false;
520+
_ -> true
521+
end,
524522
'Elixir.Module.ParallelChecker':spawn(CheckerInfo, Module, ModuleMap, Log).
525523

526524
make_module_available(Module, Binary) ->

lib/elixir/test/elixir/code_test.exs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,28 @@ defmodule CodeTest do
9090
Code.eval_quoted(quoted, [])
9191
end)
9292
end
93+
94+
test "captures unknown local calls" do
95+
sample = """
96+
defmodule CodeTest.UnknownLocalCall do
97+
def perform do
98+
foo()
99+
end
100+
end
101+
"""
102+
103+
assert {:rescued, [%{message: message}]} =
104+
Code.with_diagnostics(fn ->
105+
try do
106+
quoted = Code.string_to_quoted!(sample, columns: true)
107+
Code.eval_quoted(quoted, [])
108+
rescue
109+
_ -> :rescued
110+
end
111+
end)
112+
113+
assert message =~ "undefined function foo/0"
114+
end
93115
end
94116

95117
describe "eval_string/1,2,3" do

0 commit comments

Comments
 (0)