Incorrect type narrowing of `for` comprehension initial accumulator
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 38/100
Hướng nghiên cứu
Start by running both scripts from the issue as reduce_repro.exs and reduce_repro_1.exs to reproduce the false-positive type warnings. Trace the type handling for reduce: comprehensions in the compiler/type checker, using the gradual typing reference only to assess the documented alternative. Done means the initializer retains its input type and both examples execute without warnings while preserving their outputs.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Existing issue
- I have searched existing issues and could not find a duplicate.
Elixir and Erlang/OTP versions
Erlang/OTP 28 [erts-16.4.0.1] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit]
Elixir 1.21.0-dev (7a40579)
Operating system
any
Current behavior
The current implementation types the initializer with the expected type of the comprehension result. An initializer is always evaluated but its value may never reach the final reduction result.
Repro:
save this as reduce_repro.exs and execute
defmodule ReduceRepro do
def calculate(initial) do
(for _ <- [1], reduce: initial do
_ -> 1
end) + 1
end
def run, do: calculate(:old)
end
IO.inspect(ReduceRepro.run())
Result:
the code works fine and prints 2 but emits a false positive warning:
warning: incompatible types given to calculate/1:
calculate(:old)
given types:
:old
but expected one of:
float() or integer()
type warning found at:
│
8 │ def run, do: calculate(:old)
│ ~
│
└─ reduce_repro.exs:8:20: ReduceRepro.run/0
another example:
defmodule ReduceRepro1 do
def original(initial) do
:ok =
for _ <- [1], reduce: initial do
_ -> :ok
end
case initial do
:old -> :kept
_ -> :other
end
end
end
IO.inspect(ReduceRepro1.original(:old))
result: code prints kept and warns
warning: the following clause will never match:
:old ->
because it attempts to match on the result of:
initial
which has type:
dynamic(:ok)
type warning found at:
│
17 │ :old -> :kept
│ ~~~~~~~~~~~~~
│
└─ reduce_repro_1.exs:17: ReduceRepro1.original/1
initial is incorrectly narrowed to dynamic(:ok)
I believe this is a separate problem from https://github.com/elixir-lang/elixir/issues/15451 - covers refinements inside conditional code branches
Expected behavior
Initializer type should not be narrowed. Precise typing is probably not possible as the reducer callback is not guaranteed to execute (as in #15451).
Alternatively, if the behaviour is intended it should be documented in https://github.com/elixir-lang/elixir/blob/main/lib/elixir/pages/references/gradual-set-theoretic-types.md#false-positives
- Ngôn ngữ chính
- Elixir
- Star
- 26.7k
- Fork
- 3.7k
- Merge trung bình
- 8 giờ 7 phút
- Pull request đã merge (30 ngày)
- 60
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của elixir-lang/elixir
-
Mixed type comprehension target narrows list elements to bitstringsCó thể làm lại được Pull request cho issue này đã bị đóng mà không được merge. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 55/100
elixir-lang/elixir#15950 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
map_(get|put|update) ignore atom fields with static term() keyCó thể đã có người làm @lukaszsamson đã nhận 19 ngày trước. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 45/100
elixir-lang/elixir#15932 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 55/100
elixir-lang/elixir#15909 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 64/100
elixir-lang/elixir#15900 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Gradual map domain with `none` looses static part when computing lower boundCó thể đã có người làm @lukaszsamson đã nhận 37 ngày trước. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
elixir-lang/elixir#15854 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của elixir-lang/elixir
Issue tương tự
-
github_actions
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 94/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
team-alembic/clarity#146 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
etalab/schema-irve#83 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
ash-project/ash_ai#249 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
marketing v9
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
revelrylabs/harmonium#715 ·
Maintainer thường phản hồi trong vòng 1 ngày