Skip to content

Commit b3747a2

Browse files
author
José Valim
committed
Raise an exception if var!(x) does not expand to a proper var
1 parent f380d0c commit b3747a2

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

lib/elixir/src/elixir_scope.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
%% Convenience functions used to manipulate scope
22
%% and its variables.
33
-module(elixir_scope).
4-
-export([translate_var/4,
4+
-export([translate_var/5,
55
build_erl_var/2, build_ex_var/2,
66
build_erl_var/3, build_ex_var/3,
77
build_erl_var/4, build_ex_var/4,
@@ -13,7 +13,7 @@
1313
-include("elixir.hrl").
1414
-compile({parse_transform, elixir_transform}).
1515

16-
translate_var(Meta, Name, Kind, S) ->
16+
translate_var(Meta, Name, Kind, S, Callback) ->
1717
Line = ?line(Meta),
1818
Vars = S#elixir_scope.vars,
1919

@@ -48,7 +48,7 @@ translate_var(Meta, Name, Kind, S) ->
4848
_ ->
4949
case orddict:find({ Name, Kind }, Vars) of
5050
{ ok, VarName } -> { { var, Line, VarName }, S };
51-
error -> elixir_translator:translate_each({ Name, Meta, [] }, S)
51+
error -> Callback()
5252
end
5353
end
5454
end.

lib/elixir/src/elixir_translator.erl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,13 @@ translate_each({ 'var!', Meta, [Arg] }, S) ->
329329
translate_each({ 'var!', Meta, [Arg, nil] }, S);
330330

331331
translate_each({ 'var!', Meta, [{Name, _, Atom}, Kind] }, S) when is_atom(Name), is_atom(Atom) ->
332-
Expanded = expand_var_context(Meta, Kind, "invalid second argument for var!", S),
333-
elixir_scope:translate_var(Meta, Name, Expanded, S);
332+
translate_each({ 'var!', Meta, [Name, Kind] }, S);
334333

335334
translate_each({ 'var!', Meta, [Name, Kind] }, S) when is_atom(Name) ->
336335
Expanded = expand_var_context(Meta, Kind, "invalid second argument for var!", S),
337-
elixir_scope:translate_var(Meta, Name, Expanded, S);
336+
elixir_scope:translate_var(Meta, Name, Expanded, S, fun() ->
337+
syntax_error(Meta, S#elixir_scope.file, "expected var!(~ts) to expand to an existing variable or be a part of a match", [Name])
338+
end);
338339

339340
translate_each({ 'var!', Meta, [_, _] }, S) ->
340341
syntax_error(Meta, S#elixir_scope.file, "invalid first argument for var!, expected a var or an atom");
@@ -400,7 +401,9 @@ translate_each({ '^', Meta, [ { Name, _, Kind } ] }, S) when is_atom(Kind) ->
400401
"cannot access variable ^~s outside of assignment", [Name]);
401402

402403
translate_each({ Name, Meta, Kind }, S) when is_atom(Name), is_atom(Kind) ->
403-
elixir_scope:translate_var(Meta, Name, Kind, S);
404+
elixir_scope:translate_var(Meta, Name, Kind, S, fun() ->
405+
translate_each({ Name, Meta, [] }, S)
406+
end);
404407

405408
%% Local calls
406409

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ defmodule Kernel.ErrorsTest do
184184
assert "nofile:1: unit in bitstring expects an integer as argument" == format_rescue '<<1 :: unit(x)>>'
185185
end
186186

187+
test :invalid_var! do
188+
assert "nofile:1: expected var!(y) to expand to an existing variable or be a part of a match",
189+
format_rescue 'var!(x)'
190+
end
191+
187192
test :invalid_alias do
188193
assert "nofile:1: invalid args for alias, cannot create nested alias Sample.Lists" ==
189194
format_rescue 'alias :lists, as: Sample.Lists'

0 commit comments

Comments
 (0)