Skip to content

Commit d566697

Browse files
committed
Process fields even when structs are unknown, closes #15156
1 parent 92e1830 commit d566697

4 files changed

Lines changed: 28 additions & 0 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,12 @@ defmodule Module.Types.Expr do
243243
end
244244
end)
245245
else
246+
context =
247+
Enum.reduce(pairs, context, fn {_key, value}, context ->
248+
{_type, context} = of_expr(value, term(), expr, stack, context)
249+
context
250+
end)
251+
246252
{error_type(), context}
247253
end
248254
end

lib/elixir/lib/module/types/pattern.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,12 @@ defmodule Module.Types.Pattern do
751751
{{:closed_map, static, dynamic}, precise?, context}
752752
end
753753
else
754+
context =
755+
Enum.reduce(args, context, fn {key, value}, context ->
756+
{_, _, context} = of_pattern(value, [{:key, key} | path], stack, context)
757+
context
758+
end)
759+
754760
{error_type(), false, context}
755761
end
756762
end

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,17 @@ defmodule Module.Types.ExprTest do
12531253
"struct Enumerable is undefined (there is such module but it does not define a struct)"
12541254
end
12551255

1256+
test "updating field in unknown struct" do
1257+
assert typeerror!(
1258+
[x],
1259+
(
1260+
%UNKNOWN.URI{x | foo: y = 123}
1261+
y
1262+
)
1263+
) =~
1264+
"struct UNKNOWN.URI is undefined (module UNKNOWN.URI is not available or is yet to be defined)"
1265+
end
1266+
12561267
test "updating unknown field" do
12571268
{_, [diagnostic]} = typediag!([%URI{} = x], %URI{x | unknown: 123})
12581269
assert diagnostic.severity == :warning

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ defmodule Module.Types.PatternTest do
193193
"struct Enumerable is undefined (there is such module but it does not define a struct)"
194194
end
195195

196+
test "fields access on unknown struct" do
197+
assert typeerror!([%UNKNOWN.URI{reason: reason}], reason) =~
198+
"struct UNKNOWN.URI is undefined (module UNKNOWN.URI is not available or is yet to be defined)"
199+
end
200+
196201
test "unknown field" do
197202
{_, [diagnostic]} = typediag!([%URI{unknown: _} = x], x)
198203
assert diagnostic.severity == :error

0 commit comments

Comments
 (0)