Skip to content

Commit 2cafd8c

Browse files
author
José Valim
committed
Ensure we don't look up inexisting variables for default args
1 parent 7271dfe commit 2cafd8c

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

lib/elixir/src/elixir_def.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ translate_definition(Kind, Line, Module, Name, Args, Guards, Body, E) when is_in
176176
Arity = length(Args),
177177

178178
{EArgs, EGuards, EBody, _} = elixir_exp_clauses:def(fun elixir_def_defaults:expand/2,
179-
Args, Guards, expr_from_body(Line, Body), E),
179+
Args, Guards, expr_from_body(Line, Body), E),
180180

181181
Body == nil andalso check_args_for_bodyless_clause(Line, EArgs, E),
182182

lib/elixir/src/elixir_def_defaults.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
-include("elixir.hrl").
55

66
expand(Args, E) ->
7+
NoContext = E#{context := nil},
78
lists:mapfoldl(fun
89
({'\\\\', Meta, [Left, Right]}, Acc) ->
910
{ELeft, EL} = elixir_exp:expand(Left, Acc),
10-
{ERight, _} = elixir_exp:expand(Right, Acc#{context := nil}),
11+
{ERight, _} = elixir_exp:expand(Right, NoContext),
1112
{{'\\\\', Meta, [ELeft, ERight]}, EL};
1213
(Left, Acc) ->
1314
elixir_exp:expand(Left, Acc)

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ defmodule Kernel.ErrorsTest do
150150
def hello(arg), do: arg
151151
end
152152
'''
153+
154+
assert_compile_fail CompileError,
155+
"nofile:2: function foo/0 undefined",
156+
~C'''
157+
defmodule ErrorsTest do
158+
def hello(foo, bar \\ foo)
159+
def hello(foo, bar), do: foo + bar
160+
end
161+
'''
153162
end
154163

155164
test :invalid_match_pattern do

0 commit comments

Comments
 (0)