Skip to content

Commit b78b480

Browse files
committed
Raise on missing struct for module conflict, closes #12113
1 parent 62ec71a commit b78b480

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/elixir/src/elixir_map.erl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,19 @@ load_struct(Meta, Name, Args, Keys, E) ->
138138
wrapped_load_struct(Meta, Name, Args, Keys, E) ->
139139
%% We also include the current module because it won't be present
140140
%% in context module in case the module name is defined dynamically.
141-
InContext = lists:member(Name, [?key(E, module) | ?key(E, context_modules)]),
141+
Module = ?key(E, module),
142+
InContext = lists:member(Name, [Module | ?key(E, context_modules)]),
142143

143144
Arity = length(Args),
144145
External = InContext orelse (not(ensure_loaded(Name)) andalso wait_for_struct(Name)),
145146

146147
try
147148
case External andalso elixir_def:external_for(Meta, Name, '__struct__', Arity, [def]) of
149+
%% If I am accessing myself and there is no __struct__ function,
150+
%% don't invoke the fallback to avoid calling loaded code.
151+
false when Module == Name ->
152+
error(undef);
153+
148154
false ->
149155
apply(Name, '__struct__', Args);
150156

lib/elixir/test/elixir/kernel/errors_test.exs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,23 @@ defmodule Kernel.ErrorsTest do
204204
end
205205
end
206206

207+
test "bad struct on module conflict" do
208+
Code.put_compiler_option(:ignore_module_conflict, true)
209+
210+
assert_eval_raise CompileError, ~r'MissingStructOnReload\.__struct__/1 is undefined', ~c'''
211+
defmodule MissingStructOnReload do
212+
defstruct [:title]
213+
def d(), do: %MissingStructOnReload{}
214+
end
215+
216+
defmodule MissingStructOnReload do
217+
def d(), do: %MissingStructOnReload{}
218+
end
219+
'''
220+
after
221+
Code.put_compiler_option(:ignore_module_conflict, false)
222+
end
223+
207224
test "missing struct key" do
208225
missing_struct_key_error =
209226
~r"expected Kernel.ErrorsTest.MissingStructKey.__struct__/(0|1) to return a map.*, got: %\{\}"

0 commit comments

Comments
 (0)