Skip to content

Commit e6cfc20

Browse files
oneemanJosé Valim
authored andcommitted
Produce clearer messages for syntax errors before a sigil
Closes #3130 Signed-off-by: José Valim <[email protected]>
1 parent 906f031 commit e6cfc20

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/elixir/src/elixir_errors.erl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ parse_error(Line, File, Error, <<>>) ->
5555
parse_error(Line, File, <<"syntax error before: ">>, <<"'end'">>) ->
5656
do_raise(Line, File, 'Elixir.SyntaxError', <<"unexpected token: end">>);
5757

58+
%% Produce a human-readable message for errors before a sigil
59+
parse_error(Line, File, <<"syntax error before: ">>, <<"{sigil,", _Rest/binary>> = Full) ->
60+
{ok, Tokens, _} = erl_scan:string(binary_to_list(Full)),
61+
{ok, {sigil, _, Sigil, [Content], _}} = erl_parse:parse_term(Tokens ++ [{dot, 1}]),
62+
Message = <<"syntax error before: sigil ~", Sigil," with content '", Content/binary, "'">>,
63+
do_raise(Line, File, 'Elixir.SyntaxError', Message);
64+
5865
%% Aliases are wrapped in ['']
5966
parse_error(Line, File, Error, <<"['", Token/binary>>) when is_binary(Error) ->
6067
Rest =

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ defmodule Kernel.ErrorsTest do
8989
'+.foo'
9090
end
9191

92+
test :syntax_error_before_sigil do
93+
assert_compile_fail SyntaxError,
94+
"nofile:1: syntax error before: sigil ~s with content 'bar baz'",
95+
'~s(foo) ~s(bar baz)'
96+
end
97+
9298
test :compile_error_on_op_ambiguity do
9399
msg = "nofile:1: \"a -1\" looks like a function call but there is a variable named \"a\", " <>
94100
"please use explicit parenthesis or even spaces"

0 commit comments

Comments
 (0)