Skip to content

Commit ee1dc07

Browse files
committed
Ensure parallel matches in assert propagate type information, closes #14921
1 parent 32e724c commit ee1dc07

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

lib/ex_unit/lib/ex_unit/assertions.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ defmodule ExUnit.Assertions do
160160
end
161161
end
162162

163+
{left, right} = move_match(left, right)
163164
__match__(left, right, code, check, __CALLER__)
164165
end
165166

@@ -380,6 +381,12 @@ defmodule ExUnit.Assertions do
380381
{ExUnit.AssertionError.no_value(), expr}
381382
end
382383

384+
defp move_match(left, {:=, meta, [middle, right]}),
385+
do: move_match({:=, meta, [left, middle]}, right)
386+
387+
defp move_match(left, right),
388+
do: {left, right}
389+
383390
@doc false
384391
def __match__({:when, _, _} = left, right, _, _, _) do
385392
suggestion =
@@ -409,7 +416,7 @@ defmodule ExUnit.Assertions do
409416
case right do
410417
unquote(left) ->
411418
unquote(check)
412-
unquote(mark_as_generated(vars))
419+
{unquote_splicing(mark_as_generated(vars))}
413420

414421
_ ->
415422
left = unquote(Macro.escape(left))
@@ -427,7 +434,7 @@ defmodule ExUnit.Assertions do
427434
quote do
428435
right = unquote(right)
429436
expr = unquote(code)
430-
unquote(vars) = unquote(match_expr)
437+
{unquote_splicing(vars)} = unquote(match_expr)
431438
right
432439
end
433440
end

lib/ex_unit/lib/ex_unit/doc_test.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,14 @@ defmodule ExUnit.DocTest do
964964

965965
@doc false
966966
defmacro __assert__({:=, _, [left, right]} = assertion) do
967+
{left, right} = move_match(left, right)
967968
code = Macro.escape(assertion, prune_metadata: true)
968969
ExUnit.Assertions.__match__(left, right, code, :ok, __CALLER__)
969970
end
971+
972+
defp move_match(left, {:=, meta, [middle, right]}),
973+
do: move_match({:=, meta, [left, middle]}, right)
974+
975+
defp move_match(left, right),
976+
do: {left, right}
970977
end

lib/ex_unit/test/ex_unit/assertions_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,16 @@ defmodule ExUnit.AssertionsTest do
246246
end
247247
end
248248

249+
test "assert parallel match" do
250+
assert %URI{} = uri = apply(URI, :parse, ["/foo"])
251+
# This should not warn
252+
assert %URI{uri | path: "/bar"}.path == "/bar"
253+
254+
assert uri = %URI{} = apply(URI, :parse, ["/foo"])
255+
# This should not warn
256+
assert %URI{uri | path: "/bar"}.path == "/bar"
257+
end
258+
249259
test "assert match with pinned variable" do
250260
a = 1
251261
{2, 1} = assert {2, ^a} = Value.tuple()

0 commit comments

Comments
 (0)