Skip to content

Commit 9754f05

Browse files
author
José Valim
committed
Improve error messages for in-erlang macros
1 parent e19dbf0 commit 9754f05

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

lib/elixir/src/elixir_macros.erl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ translate({ function, MetaFA, [{ '/', _, [{F, Meta, C}, A]}] }, S) when is_atom(
7070
end,
7171

7272
case elixir_dispatch:import_function(WrappedMeta, F, A, S) of
73-
false -> syntax_error(WrappedMeta, S#elixir_scope.file, "expected ~ts/~B to be a function, but it is a macro", [F, A]);
73+
false -> syntax_error(WrappedMeta, S#elixir_scope.file,
74+
"expected ~ts/~B to be a function, but it is a macro", [F, A]);
7475
Else -> Else
7576
end;
7677

@@ -132,7 +133,7 @@ translate({'@', Meta, [{ Name, _, Args }]}, S) ->
132133

133134
%% Case
134135

135-
translate({'case', Meta, [Expr, KV]}, S) ->
136+
translate({'case', Meta, [Expr, KV]}, S) when is_list(KV) ->
136137
assert_no_match_or_guard_scope(Meta, 'case', S),
137138
Clauses = elixir_clauses:get_pairs(Meta, do, KV, S),
138139
{ TExpr, NS } = translate_each(Expr, S),
@@ -147,7 +148,7 @@ translate({'case', Meta, [Expr, KV]}, S) ->
147148

148149
%% Try
149150

150-
translate({'try', Meta, [Clauses]}, S) ->
151+
translate({'try', Meta, [Clauses]}, S) when is_list(Clauses) ->
151152
assert_no_match_or_guard_scope(Meta, 'try', S),
152153

153154
Do = proplists:get_value('do', Clauses, nil),
@@ -167,7 +168,7 @@ translate({'try', Meta, [Clauses]}, S) ->
167168

168169
%% Receive
169170

170-
translate({'receive', Meta, [KV] }, S) ->
171+
translate({'receive', Meta, [KV] }, S) when is_list(KV) ->
171172
assert_no_match_or_guard_scope(Meta, 'receive', S),
172173
Do = elixir_clauses:get_pairs(Meta, do, KV, S, true),
173174

@@ -185,7 +186,7 @@ translate({'receive', Meta, [KV] }, S) ->
185186

186187
%% Definitions
187188

188-
translate({defmodule, Meta, [Ref, KV]}, S) ->
189+
translate({defmodule, Meta, [Ref, KV]}, S) when is_list(KV) ->
189190
{ TRef, _ } = translate_each(Ref, S),
190191

191192
Block = case lists:keyfind(do, 1, KV) of
@@ -243,7 +244,11 @@ translate({ apply, Meta, [Left, Right, Args] }, S) when is_list(Args) ->
243244

244245
translate({ apply, Meta, Args }, S) ->
245246
{ TArgs, NS } = translate_args(Args, S),
246-
{ ?wrap_call(?line(Meta), erlang, apply, TArgs), NS }.
247+
{ ?wrap_call(?line(Meta), erlang, apply, TArgs), NS };
248+
249+
translate({ Name, Meta, Args }, S) ->
250+
syntax_error(Meta, S#elixir_scope.file,
251+
"invalid arguments for macro ~ts/~B", [Name, length(Args)]).
247252

248253
%% Helpers
249254

@@ -302,7 +307,8 @@ translate_in(Meta, Left, Right, S) ->
302307
true ->
303308
{ false, ?wrap_call(Line, 'Elixir.Enum', 'member?', [TRight, TLeft]) };
304309
false ->
305-
syntax_error(Meta, S#elixir_scope.file, "invalid args for operator in, it expects an explicit array or an explicit range on the right side when used in guard expressions")
310+
syntax_error(Meta, S#elixir_scope.file, "invalid args for operator in, it expects an explicit list "
311+
" or an explicit range on the right side when used in guard expressions")
306312
end
307313
end,
308314

lib/elixir/src/elixir_translator.erl

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,8 @@ translate_each({ quote, Meta, [KV, Do] }, S) when is_list(Do) ->
236236
false -> { nil, nil }
237237
end,
238238

239-
Line = case lists:keyfind(line, 1, TKV) of
240-
{ line, LineValue } -> LineValue;
241-
false -> DefaultLine
242-
end,
243-
244-
File = case lists:keyfind(file, 1, TKV) of
245-
{ file, FileValue } -> FileValue;
246-
false -> DefaultFile
247-
end,
239+
Line = proplists:get_value(line, TKV, DefaultLine),
240+
File = proplists:get_value(file, TKV, DefaultFile),
248241

249242
Scope = case File of
250243
keep -> S#elixir_scope.file;

0 commit comments

Comments
 (0)