Skip to content

Commit 616e0d8

Browse files
committed
Do not crash when handling ambiguity errors, closes #11111
1 parent 13ba96b commit 616e0d8

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

lib/elixir/src/elixir_expand.erl

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,23 +1161,19 @@ format_error({invalid_alias, Expr}) ->
11611161
"it at runtime. If instead you wanted to invoke a function or access a field, "
11621162
"wrap the function or field name in double quotes",
11631163
io_lib:format(Message, ['Elixir.Macro':to_string(Expr)]);
1164-
format_error({op_ambiguity, Name, {Op, _, [Arg]}}) ->
1164+
format_error({op_ambiguity, Name, Arg}) ->
11651165
NameString = atom_to_binary(Name, utf8),
1166-
OpString = atom_to_binary(Op, utf8),
11671166
ArgString = 'Elixir.Macro':to_string(Arg),
11681167

11691168
Message =
1170-
"\"~ts ~ts~ts\" looks like a function call but there is a variable named \"~ts\".\n"
1169+
"\"~ts ~ts\" looks like a function call but there is a variable named \"~ts\". "
11711170
"If you want to perform a function call, use parentheses:\n"
11721171
"\n"
1173-
" ~ts(~ts~ts)\n"
1172+
" ~ts(~ts)\n"
11741173
"\n"
1175-
"If you want to perform an operation on the variable ~ts, use even spaces instead:\n"
1176-
"\n"
1177-
" ~ts ~ts ~ts",
1178-
io_lib:format(Message, [NameString, OpString, ArgString, NameString,
1179-
NameString, OpString, ArgString, NameString,
1180-
NameString, OpString, ArgString]);
1174+
"If you want to perform an operation on the variable ~ts, use spaces "
1175+
"around the unary operator",
1176+
io_lib:format(Message, [NameString, ArgString, NameString, NameString, ArgString, NameString]);
11811177
format_error({invalid_clauses, Name}) ->
11821178
Message =
11831179
"the function \"~ts\" cannot handle clauses with the -> operator because it is not a macro. "

lib/elixir/test/elixir/kernel/expansion_test.exs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2538,10 +2538,10 @@ defmodule Kernel.ExpansionTest do
25382538

25392539
describe "op ambiguity" do
25402540
test "raises when a call is ambiguous" do
2541+
# We use string_to_quoted! here to avoid the formatter adding parentheses
25412542
message = ~r["a -1" looks like a function call but there is a variable named "a"]
25422543

25432544
assert_raise CompileError, message, fn ->
2544-
# We use string_to_quoted! here to avoid the formatter adding parentheses to "a -1".
25452545
code =
25462546
Code.string_to_quoted!("""
25472547
a = 1
@@ -2550,6 +2550,18 @@ defmodule Kernel.ExpansionTest do
25502550

25512551
expand(code)
25522552
end
2553+
2554+
message = ~r["a -1..a+1" looks like a function call but there is a variable named "a"]
2555+
2556+
assert_raise CompileError, message, fn ->
2557+
code =
2558+
Code.string_to_quoted!("""
2559+
a = 1
2560+
a -1 .. a + 1
2561+
""")
2562+
2563+
expand(code)
2564+
end
25532565
end
25542566
end
25552567

0 commit comments

Comments
 (0)