Skip to content

Commit f7cad1c

Browse files
author
José Valim
committed
Improve error messages
1 parent dc0897e commit f7cad1c

25 files changed

+99
-97
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2969,7 +2969,8 @@ defmodule Kernel do
29692969
Record.access(atom, fields, args, caller)
29702970
false ->
29712971
case caller.in_match? do
2972-
true -> raise "invalid usage of access protocol in signature"
2972+
true -> raise << "the access protocol cannot be used inside match clauses ",
2973+
"(for example, on the left hand side of a match or in function signatures)" >>
29732974
false -> quote do: Access.access(unquote(element), unquote(args))
29742975
end
29752976
end

lib/elixir/lib/macro/env.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ defmodule Macro.Env do
2525
@type file :: binary
2626
@type line :: non_neg_integer
2727
@type aliases :: [{ module, module }]
28-
@type context :: :assign | :guard | nil
28+
@type context :: :match | :guard | nil
2929
@type requires :: [module]
3030
@type functions :: [{ module, [name_arity] }]
3131
@type macros :: [{ module, [name_arity] }]
@@ -58,7 +58,7 @@ defmodule Macro.Env do
5858
Returns wether the compilation environment is currently
5959
inside a match clause.
6060
"""
61-
def in_match?(record), do: context(record) == :assign
61+
def in_match?(record), do: context(record) == :match
6262

6363
@doc """
6464
Returns the environment stacktrace.

lib/elixir/src/elixir_aliases.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ lookup(Else, Dict) ->
145145
%% Errors
146146

147147
format_error({unloaded_module, Module}) ->
148-
io_lib:format("module ~s is not loaded and could not be found", [elixir_errors:inspect(Module)]);
148+
io_lib:format("module ~ts is not loaded and could not be found", [elixir_errors:inspect(Module)]);
149149

150150
format_error({scheduled_module, Module}) ->
151-
io_lib:format("module ~s is not loaded but was defined. This happens because you are trying to use a module in the same context it is defined. Try defining the module outside the context that requires it.",
151+
io_lib:format("module ~ts is not loaded but was defined. This happens because you are trying to use a module in the same context it is defined. Try defining the module outside the context that requires it.",
152152
[elixir_errors:inspect(Module)]).

lib/elixir/src/elixir_clauses.erl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ get_pairs(Meta, Key, Clauses, S, AllowNil) ->
1919
{ Key, nil } when AllowNil ->
2020
[];
2121
{ Key, _ } ->
22-
elixir_errors:syntax_error(Meta, S#elixir_scope.file, "expected pairs with -> for key ~s", [Key]);
22+
elixir_errors:syntax_error(Meta, S#elixir_scope.file, "expected pairs with -> for key ~ts", [Key]);
2323
_ ->
2424
[]
2525
end.
2626

2727
% Function for translating assigns.
2828

29-
assigns(Fun, Args, #elixir_scope{context=Context} = S) when Context /= assign ->
30-
{ Result, NewS } = assigns(Fun, Args, S#elixir_scope{context=assign, temp_vars=orddict:new()}),
29+
assigns(Fun, Args, #elixir_scope{context=Context} = S) when Context /= match ->
30+
{ Result, NewS } = assigns(Fun, Args, S#elixir_scope{context=match, temp_vars=orddict:new()}),
3131
{ Result, NewS#elixir_scope{context=Context} };
3232

3333
assigns(Fun, Args, S) -> Fun(Args, S).
@@ -176,10 +176,10 @@ each_clause(Meta, { 'after', [Condition], Expr }, S) ->
176176
{ { clause, ?line(Meta), [TCondition], [], TBody }, SB };
177177

178178
each_clause(Meta, { Key, [_|_], _ }, S) when Key == do; Key == 'after' ->
179-
elixir_errors:syntax_error(Meta, S#elixir_scope.file, "too many arguments given for ~s", [Key]);
179+
elixir_errors:syntax_error(Meta, S#elixir_scope.file, "too many arguments given for ~ts", [Key]);
180180

181181
each_clause(Meta, { Key, _, _ }, S) ->
182-
elixir_errors:syntax_error(Meta, S#elixir_scope.file, "invalid key ~s", [Key]).
182+
elixir_errors:syntax_error(Meta, S#elixir_scope.file, "invalid key ~ts", [Key]).
183183

184184
% Check if the given expression is a match tuple.
185185
% This is a small optimization to allow us to change

lib/elixir/src/elixir_compiler.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ env_default_opts() ->
137137
{ok,List} when is_list(List) -> List;
138138
{ok,Term} -> [Term];
139139
{error,_Reason} ->
140-
io:format("Ignoring bad term in ~s\n", [Key]),
140+
io:format("Ignoring bad term in ~ts\n", [Key]),
141141
[]
142142
end;
143143
{error, {_,_,_Reason}, _} ->
144-
io:format("Ignoring bad term in ~s\n", [Key]),
144+
io:format("Ignoring bad term in ~ts\n", [Key]),
145145
[]
146146
end
147147
end.
@@ -195,7 +195,7 @@ core_file(File) ->
195195
try
196196
Lists = file(list_to_binary(File)),
197197
[binary_to_path(X, "lib/elixir/ebin") || X <- Lists],
198-
io:format("Compiled ~s~n", [File])
198+
io:format("Compiled ~ts~n", [File])
199199
catch
200200
Kind:Reason ->
201201
io:format("~p: ~p~nstacktrace: ~p~n", [Kind, Reason, erlang:get_stacktrace()]),
@@ -235,7 +235,7 @@ core_main() ->
235235
%% ERROR HANDLING
236236

237237
format_error({ skip_native, Module }) ->
238-
io_lib:format("skipping native compilation for ~s because it contains on_load attribute",
238+
io_lib:format("skipping native compilation for ~ts because it contains on_load attribute",
239239
[elixir_errors:inspect(Module)]).
240240

241241
format_errors(_File, []) ->

lib/elixir/src/elixir_def.erl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ store_definition(Kind, Line, Module, Call, Body, RawS) ->
7575
{ Name, Args } = case elixir_clauses:extract_args(NameAndArgs) of
7676
error ->
7777
Format = [Kind, 'Elixir.Macro':to_binary(NameAndArgs)],
78-
elixir_errors:syntax_error(Line, S#elixir_scope.file, "invalid syntax in ~s ~s", Format);
78+
elixir_errors:syntax_error(Line, S#elixir_scope.file, "invalid syntax in ~ts ~ts", Format);
7979
Tuple ->
8080
Tuple
8181
end,
@@ -85,7 +85,7 @@ store_definition(Kind, Line, Module, Call, Body, RawS) ->
8585

8686
store_definition(Kind, Line, nil, _Name, _Args, _Guards, _Body, RawS) ->
8787
S = elixir_scope:deserialize(RawS),
88-
elixir_errors:syntax_error(Line, S#elixir_scope.file, "cannot define function outside module, invalid scope for ~s", [Kind]);
88+
elixir_errors:syntax_error(Line, S#elixir_scope.file, "cannot define function outside module, invalid scope for ~ts", [Kind]);
8989

9090
store_definition(Kind, Line, Module, Name, Args, Guards, Body, RawS) ->
9191
do_store_definition(Kind, Line, Module, Name, Args, Guards, Body, elixir_scope:deserialize(RawS)).
@@ -312,7 +312,7 @@ check_valid_defaults(Line, File, Name, Arity, _) ->
312312
elixir_errors:handle_file_warning(File, { Line, ?MODULE, { clauses_with_docs, { Name, Arity } } }).
313313

314314
assert_no_aliases_name(Line, '__aliases__', [Atom], #elixir_scope{file=File}) when is_atom(Atom) ->
315-
Message = "function names should start with lowercase characters or underscore, invalid name ~s",
315+
Message = "function names should start with lowercase characters or underscore, invalid name ~ts",
316316
elixir_errors:syntax_error(Line, File, Message, [atom_to_binary(Atom, utf8)]);
317317

318318
assert_no_aliases_name(_Meta, _Aliases, _Args, _S) ->
@@ -321,10 +321,10 @@ assert_no_aliases_name(_Meta, _Aliases, _Args, _S) ->
321321
%% Format errors
322322

323323
format_error({clauses_with_docs,{Name,Arity}}) ->
324-
io_lib:format("function ~s/~B has default values and multiple clauses, use a separate clause for declaring defaults", [Name, Arity]);
324+
io_lib:format("function ~ts/~B has default values and multiple clauses, use a separate clause for declaring defaults", [Name, Arity]);
325325

326326
format_error({changed_clause,{{Name,Arity},{ElseName,ElseArity}}}) ->
327-
io_lib:format("function ~s/~B does not match previous clause ~s/~B", [Name, Arity, ElseName, ElseArity]);
327+
io_lib:format("function ~ts/~B does not match previous clause ~ts/~B", [Name, Arity, ElseName, ElseArity]);
328328

329329
format_error({changed_kind,{Name,Arity,Previous,Current}}) ->
330-
io_lib:format("~s ~s/~B already defined as ~s", [Current, Name, Arity, Previous]).
330+
io_lib:format("~ts ~ts/~B already defined as ~ts", [Current, Name, Arity, Previous]).

lib/elixir/src/elixir_def_local.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,4 @@ check_unused_local_macros(File, Recorded, Defmacrop) ->
7575
Check, not lists:member(Fun, Recorded)].
7676

7777
format_error({unused_macro,{Name, Arity}}) ->
78-
io_lib:format("macro ~s/~B is unused", [Name, Arity]).
78+
io_lib:format("macro ~ts/~B is unused", [Name, Arity]).

lib/elixir/src/elixir_def_overridable.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ store_pending(Module) ->
8888
format_error({ no_super, Module, { Name, Arity } }) ->
8989
Bins = [ format_fa(X) || { X, { _, _, _ } } <- overridable(Module)],
9090
Joined = 'Elixir.Enum':join(Bins, <<", ">>),
91-
io_lib:format("no super defined for ~s/~B in module ~s. Overridable functions available are: ~s",
91+
io_lib:format("no super defined for ~ts/~B in module ~ts. Overridable functions available are: ~ts",
9292
[Name, Arity, elixir_errors:inspect(Module), Joined]).
9393

9494
format_fa({ Name, Arity }) ->

lib/elixir/src/elixir_dispatch.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ munge_stacktrace(_, [], _) ->
223223

224224
format_error({ unrequired_module,{Receiver, Name, Arity, Required }}) ->
225225
String = string:join([elixir_errors:inspect(R) || R <- Required], ", "),
226-
io_lib:format("tried to invoke macro ~s.~s/~B but module was not required. Required: ~s",
226+
io_lib:format("tried to invoke macro ~ts.~ts/~B but module was not required. Required: ~ts",
227227
[elixir_errors:inspect(Receiver), Name, Arity, String]).
228228

229229
%% INTROSPECTION

lib/elixir/src/elixir_errors.erl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
-export([syntax_error/3, syntax_error/4, inspect/1,
55
form_error/4, parse_error/4, assert_module_scope/3,
66
assert_no_function_scope/3, assert_function_scope/3,
7-
assert_no_assign_scope/3, assert_no_guard_scope/3,
8-
assert_no_assign_or_guard_scope/3,
7+
assert_no_match_scope/3, assert_no_guard_scope/3,
8+
assert_no_match_or_guard_scope/3,
99
handle_file_warning/2, handle_file_warning/3, handle_file_error/2,
1010
deprecation/3, deprecation/4, file_format/3]).
1111
-include("elixir.hrl").
@@ -97,15 +97,15 @@ handle_file_warning(_, _File, { _Line, v3_kernel, bad_call }) -> [];
9797
%% Rewrite
9898
handle_file_warning(_, File, {Line,erl_lint,{undefined_behaviour_func,{Fun,Arity},Module}}) ->
9999
Kind = protocol_or_behaviour(Module),
100-
Raw = "undefined ~s function ~s/~B (for ~s ~s)",
100+
Raw = "undefined ~ts function ~ts/~B (for ~ts ~ts)",
101101
Message = io_lib:format(Raw, [Kind, Fun, Arity, Kind, inspect(Module)]),
102102
io:format(file_format(Line, File, Message));
103103

104104
handle_file_warning(_, File, {Line,erl_lint,{undefined_behaviour,Module}}) ->
105105
case elixir_compiler:get_opt(internal) of
106106
true -> [];
107107
false ->
108-
Message = io_lib:format("behaviour ~s undefined", [inspect(Module)]),
108+
Message = io_lib:format("behaviour ~ts undefined", [inspect(Module)]),
109109
io:format(file_format(Line, File, Message))
110110
end;
111111

@@ -137,26 +137,26 @@ handle_file_error(File, {Line,Module,Desc}) ->
137137

138138
assert_no_function_scope(_Meta, _Kind, #elixir_scope{function=nil}) -> [];
139139
assert_no_function_scope(Meta, Kind, S) ->
140-
syntax_error(Meta, S#elixir_scope.file, "cannot invoke ~s inside a function", [Kind]).
140+
syntax_error(Meta, S#elixir_scope.file, "cannot invoke ~ts inside a function", [Kind]).
141141

142-
assert_no_assign_or_guard_scope(Meta, Kind, S) ->
143-
assert_no_assign_scope(Meta, Kind, S),
142+
assert_no_match_or_guard_scope(Meta, Kind, S) ->
143+
assert_no_match_scope(Meta, Kind, S),
144144
assert_no_guard_scope(Meta, Kind, S).
145145

146-
assert_no_assign_scope(Meta, Kind, #elixir_scope{context=assign} = S) ->
147-
syntax_error(Meta, S#elixir_scope.file, "cannot invoke ~s inside assign", [Kind]);
148-
assert_no_assign_scope(_Meta, _Kind, _S) -> [].
146+
assert_no_match_scope(Meta, Kind, #elixir_scope{context=match} = S) ->
147+
syntax_error(Meta, S#elixir_scope.file, "cannot invoke ~ts inside match clause", [Kind]);
148+
assert_no_match_scope(_Meta, _Kind, _S) -> [].
149149

150150
assert_no_guard_scope(Meta, Kind, #elixir_scope{context=guard} = S) ->
151-
syntax_error(Meta, S#elixir_scope.file, "cannot invoke ~s inside guard", [Kind]);
151+
syntax_error(Meta, S#elixir_scope.file, "cannot invoke ~ts inside guard", [Kind]);
152152
assert_no_guard_scope(_Meta, _Kind, _S) -> [].
153153

154154
assert_module_scope(Meta, Kind, #elixir_scope{module=nil,file=File}) ->
155-
syntax_error(Meta, File, "cannot invoke ~s outside module", [Kind]);
155+
syntax_error(Meta, File, "cannot invoke ~ts outside module", [Kind]);
156156
assert_module_scope(_Meta, _Kind, #elixir_scope{module=Module}) -> Module.
157157

158158
assert_function_scope(Meta, Kind, #elixir_scope{function=nil,file=File}) ->
159-
syntax_error(Meta, File, "cannot invoke ~s outside function", [Kind]);
159+
syntax_error(Meta, File, "cannot invoke ~ts outside function", [Kind]);
160160
assert_function_scope(_Meta, _Kind, #elixir_scope{function=Function}) -> Function.
161161

162162
%% Helpers

0 commit comments

Comments
 (0)