File 0001-Do-not-crash-when-handling-ambiguity-errors-closes-1.patch of Package elixir
From 616e0d8ccce41401cb0fcebd46094e32cc629052 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jos=C3=A9=20Valim?= <jose.valim@dashbit.co>
Date: Tue, 13 Jul 2021 10:35:36 +0200
Subject: [PATCH 03/10] Do not crash when handling ambiguity errors, closes
#11111
---
lib/elixir/src/elixir_expand.erl | 16 ++++++----------
lib/elixir/test/elixir/kernel/expansion_test.exs | 14 +++++++++++++-
2 files changed, 19 insertions(+), 11 deletions(-)
diff --git a/lib/elixir/src/elixir_expand.erl b/lib/elixir/src/elixir_expand.erl
index d9755fe38..76c56a5e6 100644
--- a/lib/elixir/src/elixir_expand.erl
+++ b/lib/elixir/src/elixir_expand.erl
@@ -1161,23 +1161,19 @@ format_error({invalid_alias, Expr}) ->
"it at runtime. If instead you wanted to invoke a function or access a field, "
"wrap the function or field name in double quotes",
io_lib:format(Message, ['Elixir.Macro':to_string(Expr)]);
-format_error({op_ambiguity, Name, {Op, _, [Arg]}}) ->
+format_error({op_ambiguity, Name, Arg}) ->
NameString = atom_to_binary(Name, utf8),
- OpString = atom_to_binary(Op, utf8),
ArgString = 'Elixir.Macro':to_string(Arg),
Message =
- "\"~ts ~ts~ts\" looks like a function call but there is a variable named \"~ts\".\n"
+ "\"~ts ~ts\" looks like a function call but there is a variable named \"~ts\". "
"If you want to perform a function call, use parentheses:\n"
"\n"
- " ~ts(~ts~ts)\n"
+ " ~ts(~ts)\n"
"\n"
- "If you want to perform an operation on the variable ~ts, use even spaces instead:\n"
- "\n"
- " ~ts ~ts ~ts",
- io_lib:format(Message, [NameString, OpString, ArgString, NameString,
- NameString, OpString, ArgString, NameString,
- NameString, OpString, ArgString]);
+ "If you want to perform an operation on the variable ~ts, use spaces "
+ "around the unary operator",
+ io_lib:format(Message, [NameString, ArgString, NameString, NameString, ArgString, NameString]);
format_error({invalid_clauses, Name}) ->
Message =
"the function \"~ts\" cannot handle clauses with the -> operator because it is not a macro. "
diff --git a/lib/elixir/test/elixir/kernel/expansion_test.exs b/lib/elixir/test/elixir/kernel/expansion_test.exs
index 0acba1d2a..24b83b471 100644
--- a/lib/elixir/test/elixir/kernel/expansion_test.exs
+++ b/lib/elixir/test/elixir/kernel/expansion_test.exs
@@ -2538,10 +2538,10 @@ test "raises on binary fields with size in matches" do
describe "op ambiguity" do
test "raises when a call is ambiguous" do
+ # We use string_to_quoted! here to avoid the formatter adding parentheses
message = ~r["a -1" looks like a function call but there is a variable named "a"]
assert_raise CompileError, message, fn ->
- # We use string_to_quoted! here to avoid the formatter adding parentheses to "a -1".
code =
Code.string_to_quoted!("""
a = 1
@@ -2550,6 +2550,18 @@ test "raises when a call is ambiguous" do
expand(code)
end
+
+ message = ~r["a -1..a+1" looks like a function call but there is a variable named "a"]
+
+ assert_raise CompileError, message, fn ->
+ code =
+ Code.string_to_quoted!("""
+ a = 1
+ a -1 .. a + 1
+ """)
+
+ expand(code)
+ end
end
end
--
2.26.2