Skip to content

Commit 1d47983

Browse files
committed
Tests
1 parent de2462f commit 1d47983

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

lib/elixir/lib/module/types/expr.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ defmodule Module.Types.Expr do
148148
type_fun = fn pattern_type, context ->
149149
# See if we can use the expected type to further refine the pattern type,
150150
# if we cannot, use the pattern type as that will fail later on.
151-
{_ok_or_error, type} = compatible_intersection(pattern_type, expected)
151+
{_ok_or_error, type} = compatible_intersection(dynamic(pattern_type), expected)
152152
of_expr(right_expr, type, expr, stack, context)
153153
end
154154

lib/elixir/test/elixir/module/types/integration_test.exs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,77 @@ defmodule Module.Types.IntegrationTest do
8080
]
8181
end
8282

83+
test "writes exports with inferred map types" do
84+
files = %{
85+
"a.ex" => """
86+
defmodule A do
87+
defstruct [:x, :y, :z]
88+
89+
def struct_create_with_atom_keys(x) do
90+
infer(y = %A{x: x})
91+
{x, y}
92+
end
93+
94+
def map_create_with_atom_keys(x) do
95+
infer(%{__struct__: A, x: x, y: nil, z: nil})
96+
x
97+
end
98+
99+
def map_update_with_atom_keys(x) do
100+
infer(%{x | y: nil})
101+
x
102+
end
103+
104+
def map_update_with_unknown_keys(x, y) do
105+
infer(%{x | y => 123})
106+
x
107+
end
108+
109+
defp infer(%A{x: <<_::binary>>, y: nil}) do
110+
:ok
111+
end
112+
end
113+
"""
114+
}
115+
116+
modules = compile_modules(files)
117+
exports = read_chunk(modules[A]).exports |> Map.new()
118+
119+
return = fn name, arity ->
120+
pair = {name, arity}
121+
%{^pair => %{sig: {:infer, nil, [{_, return}]}}} = exports
122+
return
123+
end
124+
125+
assert return.(:struct_create_with_atom_keys, 1) ==
126+
dynamic(
127+
tuple([
128+
binary(),
129+
closed_map(
130+
__struct__: atom([A]),
131+
x: binary(),
132+
y: atom([nil]),
133+
z: atom([nil])
134+
)
135+
])
136+
)
137+
138+
assert return.(:map_create_with_atom_keys, 1) == dynamic(binary())
139+
140+
assert return.(:map_update_with_atom_keys, 1) ==
141+
dynamic(
142+
closed_map(
143+
__struct__: atom([A]),
144+
x: binary(),
145+
y: term(),
146+
z: term()
147+
)
148+
)
149+
150+
assert return.(:map_update_with_unknown_keys, 2) ==
151+
dynamic(open_map())
152+
end
153+
83154
test "writes exports for implementations" do
84155
files = %{
85156
"pi.ex" => """

0 commit comments

Comments
 (0)