Skip to content

Commit 14272c9

Browse files
author
José Valim
committed
Improve invalid match and guard errors, closes #1673
1 parent 42ffd31 commit 14272c9

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

lib/elixir/src/elixir_errors.erl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,6 @@ assert_no_function_scope(_Meta, _Kind, #elixir_scope{function=nil}) -> [];
178178
assert_no_function_scope(Meta, Kind, S) ->
179179
syntax_error(Meta, S#elixir_scope.file, "cannot invoke ~ts inside a function", [Kind]).
180180

181-
assert_no_match_or_guard_scope(Meta, Kind, S) ->
182-
assert_no_match_scope(Meta, Kind, S),
183-
assert_no_guard_scope(Meta, Kind, S).
184-
185-
assert_no_match_scope(Meta, Kind, #elixir_scope{context=match} = S) ->
186-
syntax_error(Meta, S#elixir_scope.file, "cannot invoke ~ts inside match clause", [Kind]);
187-
assert_no_match_scope(_Meta, _Kind, _S) -> [].
188-
189-
assert_no_guard_scope(Meta, Kind, #elixir_scope{context=guard} = S) ->
190-
syntax_error(Meta, S#elixir_scope.file, "cannot invoke ~ts inside guard", [Kind]);
191-
assert_no_guard_scope(_Meta, _Kind, _S) -> [].
192-
193181
assert_module_scope(Meta, Kind, #elixir_scope{module=nil,file=File}) ->
194182
syntax_error(Meta, File, "cannot invoke ~ts outside module", [Kind]);
195183
assert_module_scope(_Meta, _Kind, #elixir_scope{module=Module}) -> Module.
@@ -198,6 +186,18 @@ assert_function_scope(Meta, Kind, #elixir_scope{function=nil,file=File}) ->
198186
syntax_error(Meta, File, "cannot invoke ~ts outside function", [Kind]);
199187
assert_function_scope(_Meta, _Kind, #elixir_scope{function=Function}) -> Function.
200188

189+
assert_no_match_or_guard_scope(Meta, Kind, S) ->
190+
assert_no_match_scope(Meta, Kind, S),
191+
assert_no_guard_scope(Meta, Kind, S).
192+
193+
assert_no_match_scope(Meta, _Kind, #elixir_scope{context=match} = S) ->
194+
compile_error(Meta, S#elixir_scope.file, "invalid pattern in match clause", []);
195+
assert_no_match_scope(_Meta, _Kind, _S) -> [].
196+
197+
assert_no_guard_scope(Meta, _Kind, #elixir_scope{context=guard} = S) ->
198+
compile_error(Meta, S#elixir_scope.file, "invalid pattern in guard", []);
199+
assert_no_guard_scope(_Meta, _Kind, _S) -> [].
200+
201201
%% Helpers
202202

203203
raise(Meta, File, Kind, Message) when is_list(Meta) ->

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ defmodule Kernel.ErrorsTest do
113113
'''
114114
end
115115

116+
test :invalid_match_pattern do
117+
assert_compile_fail CompileError,
118+
"nofile:2: invalid pattern in match clause",
119+
'''
120+
case true do
121+
true && true -> true
122+
end
123+
'''
124+
end
125+
116126
test :different_defs_with_defaults do
117127
assert_compile_fail CompileError,
118128
"nofile:3: def hello/3 defaults conflicts with def hello/2",

0 commit comments

Comments
 (0)