Skip to content

Commit 6c9602b

Browse files
committed
Get rid of elixir_locals too
1 parent 108f975 commit 6c9602b

File tree

9 files changed

+74
-91
lines changed

9 files changed

+74
-91
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3646,7 +3646,7 @@ defmodule Kernel do
36463646
:ok
36473647

36483648
true ->
3649-
pos = :elixir_locals.cache_env(__CALLER__)
3649+
pos = :elixir_module.cache_env(__CALLER__)
36503650
%{line: line, file: file, module: module} = __CALLER__
36513651

36523652
quote do
@@ -5323,7 +5323,7 @@ defmodule Kernel do
53235323
key
53245324
end
53255325

5326-
pos = :elixir_locals.cache_env(env)
5326+
pos = :elixir_module.cache_env(env)
53275327

53285328
quote do
53295329
:elixir_def.store_definition(unquote(kind), unquote(store), unquote(pos))

lib/elixir/lib/kernel/cli.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ defmodule Kernel.CLI do
196196
end
197197

198198
@elixir_internals [:elixir, :elixir_aliases, :elixir_expand, :elixir_compiler, :elixir_module] ++
199-
[:elixir_clauses, :elixir_lexical, :elixir_def, :elixir_map, :elixir_locals] ++
199+
[:elixir_clauses, :elixir_lexical, :elixir_def, :elixir_map] ++
200200
[:elixir_erl, :elixir_erl_clauses, :elixir_erl_compiler, :elixir_erl_pass] ++
201201
[Kernel.ErrorHandler, Module.ParallelChecker]
202202

lib/elixir/lib/kernel/typespec.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ defmodule Kernel.Typespec do
138138
case type_to_signature(expr) do
139139
{name, arity} = signature when signature in @reserved_signatures ->
140140
compile_error(
141-
:elixir_locals.get_cached_env(pos),
141+
:elixir_module.get_cached_env(pos),
142142
"type #{name}/#{arity} is a reserved type and it cannot be defined"
143143
)
144144

@@ -247,7 +247,7 @@ defmodule Kernel.Typespec do
247247

248248
defp collect_defined_type_pairs(type_typespecs) do
249249
fun = fn {_kind, expr, pos}, type_pairs ->
250-
%{file: file, line: line} = env = :elixir_locals.get_cached_env(pos)
250+
%{file: file, line: line} = env = :elixir_module.get_cached_env(pos)
251251

252252
case type_to_signature(expr) do
253253
{name, arity} = type_pair ->
@@ -292,7 +292,7 @@ defmodule Kernel.Typespec do
292292

293293
defp translate_type({kind, {:"::", _, [{name, meta, args}, definition]}, pos}, state)
294294
when is_list(meta) do
295-
caller = :elixir_locals.get_cached_env(pos)
295+
caller = :elixir_module.get_cached_env(pos)
296296
state = clean_local_state(state)
297297

298298
args =
@@ -349,12 +349,12 @@ defmodule Kernel.Typespec do
349349
defp underspecified?(_kind, _arity, _spec), do: false
350350

351351
defp translate_spec({kind, {:when, _meta, [spec, guard]}, pos}, state) do
352-
caller = :elixir_locals.get_cached_env(pos)
352+
caller = :elixir_module.get_cached_env(pos)
353353
translate_spec(kind, spec, guard, caller, state)
354354
end
355355

356356
defp translate_spec({kind, spec, pos}, state) do
357-
caller = :elixir_locals.get_cached_env(pos)
357+
caller = :elixir_module.get_cached_env(pos)
358358
translate_spec(kind, spec, [], caller, state)
359359
end
360360

lib/elixir/src/elixir_bootstrap.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ define({Line, _S, #{module := Module} = E}, Kind, Call, Expr) ->
5050
Key
5151
end,
5252

53-
Args = [Kind, Store, elixir_locals:cache_env(E#{line := Line})],
53+
Args = [Kind, Store, elixir_module:cache_env(E#{line := Line})],
5454
{{'.', [], [elixir_def, store_definition]}, [], Args}.
5555

5656
unless_loaded(Fun, Args, Callback) ->

lib/elixir/src/elixir_def.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ head_and_definition_meta(_, _Meta, _HeadDefaults, [{_, _, HeadMeta, _} | _]) ->
135135
%% Section for storing definitions
136136

137137
store_definition(Kind, {Call, Body}, Pos) ->
138-
E = elixir_locals:get_cached_env(Pos),
138+
E = elixir_module:get_cached_env(Pos),
139139
store_definition(Kind, false, Call, Body, E);
140140
store_definition(Kind, Key, Pos) ->
141-
#{module := Module} = E = elixir_locals:get_cached_env(Pos),
141+
#{module := Module} = E = elixir_module:get_cached_env(Pos),
142142
{Call, Body} = elixir_module:read_cache(Module, Key),
143143
store_definition(Kind, true, Call, Body, E).
144144

lib/elixir/src/elixir_dispatch.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import_function(Meta, Name, Arity, E) ->
5555
case find_import_by_name_arity(Meta, Tuple, [], E) of
5656
{function, Receiver} ->
5757
elixir_env:trace({imported_function, Meta, Receiver, Name, Arity}, E),
58-
elixir_locals:record_import(Tuple, Receiver, ?key(E, module), ?key(E, function)),
58+
elixir_import:record(Tuple, Receiver, ?key(E, module), ?key(E, function)),
5959
remote_function(Meta, Receiver, Name, Arity, E);
6060
{macro, _Receiver} ->
6161
false;
@@ -192,13 +192,13 @@ do_expand_import(Result, Meta, Name, Arity, Module, E, Trace) ->
192192
{function, Receiver} ->
193193
Trace andalso begin
194194
elixir_env:trace({imported_function, Meta, Receiver, Name, Arity}, E),
195-
elixir_locals:record_import({Name, Arity}, Receiver, Module, ?key(E, function))
195+
elixir_import:record({Name, Arity}, Receiver, Module, ?key(E, function))
196196
end,
197197
{function, Receiver, Name};
198198
{macro, Receiver} ->
199199
Trace andalso begin
200200
elixir_env:trace({imported_macro, Meta, Receiver, Name, Arity}, E),
201-
elixir_locals:record_import({Name, Arity}, Receiver, Module, ?key(E, function))
201+
elixir_import:record({Name, Arity}, Receiver, Module, ?key(E, function))
202202
end,
203203
{macro, Receiver, expander_macro_named(Meta, Receiver, Name, Arity, E)};
204204
{import, Receiver} ->

lib/elixir/src/elixir_import.erl

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
%% between local functions and imports.
33
%% For imports dispatch, please check elixir_dispatch.
44
-module(elixir_import).
5-
-export([import/6, import/7, special_form/2, format_error/1]).
5+
-export([import/6, import/7, special_form/2,
6+
record/4, ensure_no_local_conflict/3,
7+
format_error/1]).
68
-compile(inline_list_funcs).
79
-include("elixir.hrl").
810

@@ -135,6 +137,31 @@ calculate_key(Meta, Key, Old, New, E, Warn) ->
135137
{true, FinalSet, [{Key, FinalSet} | keydelete(Key, Old)]}
136138
end.
137139

140+
%% Record function calls for local conflicts
141+
142+
record(_Tuple, Receiver, Module, Function)
143+
when Function == nil; Module == Receiver -> false;
144+
record(Tuple, Receiver, Module, _Function) ->
145+
try
146+
{Set, _Bag} = elixir_module:data_tables(Module),
147+
ets:insert(Set, {{import, Tuple}, Receiver}),
148+
true
149+
catch
150+
error:badarg -> false
151+
end.
152+
153+
ensure_no_local_conflict('Elixir.Kernel', _All, _E) ->
154+
ok;
155+
ensure_no_local_conflict(Module, AllDefinitions, E) ->
156+
{Set, _} = elixir_module:data_tables(Module),
157+
158+
[try
159+
Receiver = ets:lookup_element(Set, {import, Pair}, 2),
160+
elixir_errors:module_error(Meta, E, ?MODULE, {import_conflict, Receiver, Pair})
161+
catch
162+
error:badarg -> false
163+
end || {Pair, _, Meta, _} <- AllDefinitions].
164+
138165
%% Retrieve functions and macros from modules
139166

140167
get_functions(Module, InfoCallback) ->
@@ -229,7 +256,11 @@ format_error({special_form_conflict, {Receiver, Name, Arity}}) ->
229256
[elixir_aliases:inspect(Receiver), Name, Arity]);
230257

231258
format_error({no_macros, Module}) ->
232-
io_lib:format("could not load macros from module ~ts", [elixir_aliases:inspect(Module)]).
259+
io_lib:format("could not load macros from module ~ts", [elixir_aliases:inspect(Module)]);
260+
261+
format_error({import_conflict, Receiver, {Name, Arity}}) ->
262+
io_lib:format("imported ~ts.~ts/~B conflicts with local function",
263+
[elixir_aliases:inspect(Receiver), Name, Arity]).
233264

234265
%% LIST HELPERS
235266

lib/elixir/src/elixir_locals.erl

Lines changed: 0 additions & 69 deletions
This file was deleted.

lib/elixir/src/elixir_module.erl

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
-module(elixir_module).
22
-export([file/1, data_tables/1, is_open/1, mode/1, delete_definition_attributes/6,
33
compile/6, expand_callback/6, format_error/1, compiler_modules/0,
4-
write_cache/3, read_cache/2, next_counter/1, taint/1]).
4+
write_cache/3, read_cache/2, next_counter/1, taint/1, cache_env/1, get_cached_env/1]).
55
-include("elixir.hrl").
66
-define(counter_attr, {elixir, counter}).
7+
-define(cache_key, {elixir, cache_env}).
78

89
%% Stores modules currently being defined by the compiler
910

@@ -71,6 +72,29 @@ taint(Module) ->
7172
_:_ -> false
7273
end.
7374

75+
cache_env(#{line := Line, module := Module} = E) ->
76+
{Set, _} = data_tables(Module),
77+
Cache = elixir_env:reset_vars(E#{line := nil}),
78+
PrevKey = ets:lookup_element(Set, ?cache_key, 2),
79+
80+
Pos =
81+
case ets:lookup(Set, {cache_env, PrevKey}) of
82+
[{_, Cache}] ->
83+
PrevKey;
84+
_ ->
85+
NewKey = PrevKey + 1,
86+
ets:insert(Set, [{{cache_env, NewKey}, Cache}, {?cache_key, NewKey}]),
87+
NewKey
88+
end,
89+
90+
{Module, {Line, Pos}}.
91+
92+
get_cached_env({Module, {Line, Pos}}) ->
93+
{Set, _} = data_tables(Module),
94+
(ets:lookup_element(Set, {cache_env, Pos}, 2))#{line := Line};
95+
get_cached_env(Env) ->
96+
Env.
97+
7498
%% Compilation hook
7599

76100
compile(Meta, Module, Block, Vars, Prune, Env) ->
@@ -146,7 +170,7 @@ compile(Meta, Module, ModuleAsCharlist, Block, Vars, Prune, E) ->
146170

147171
NifsAttribute = lists:keyfind(nifs, 1, Attributes),
148172
validate_nifs_attribute(NifsAttribute, AllDefinitions, Line, E),
149-
elixir_locals:ensure_no_import_conflict(Module, AllDefinitions, E),
173+
elixir_import:ensure_no_local_conflict(Module, AllDefinitions, E),
150174

151175
make_readonly(Module),
152176

@@ -330,7 +354,6 @@ build(Module, Line, File, E) ->
330354
%% * {{type, Tuple}, ...}, {{opaque, Tuple}, ...}
331355
%% * {{callback, Tuple}, ...}, {{macrocallback, Tuple}, ...}
332356
%% * {{def, Tuple}, ...} (from elixir_def)
333-
%% * {{import, Tuple}, ...} (from elixir_locals)
334357
%% * {{overridable, Tuple}, ...} (from elixir_overridable)
335358
%%
336359
DataSet = ets:new(Module, [set, public]),
@@ -346,8 +369,6 @@ build(Module, Line, File, E) ->
346369
%% * {overridables, ...} (from elixir_overridable)
347370
%% * {{default, Name}, ...} (from elixir_def)
348371
%% * {{clauses, Tuple}, ...} (from elixir_def)
349-
%% * {reattach, ...} (from elixir_locals)
350-
%% * {{local, Tuple}, ...} (from elixir_locals)
351372
%%
352373
DataBag = ets:new(Module, [duplicate_bag, public]),
353374

@@ -374,6 +395,7 @@ build(Module, Line, File, E) ->
374395
{optional_callbacks, [], accumulate, []},
375396

376397
% Others
398+
{?cache_key, 0},
377399
{?counter_attr, 0}
378400
]),
379401

@@ -390,7 +412,6 @@ build(Module, Line, File, E) ->
390412
%% Setup definition related modules
391413
Tables = {DataSet, DataBag},
392414
elixir_def:setup(Tables),
393-
elixir_locals:setup(Tables),
394415
Tuple = {Module, Tables, Line, File, all},
395416

396417
Ref =

0 commit comments

Comments
 (0)