for loop bound still differs from .NET for reassigned mutable arrays
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 36/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- dart, erlang, fsharp, javascript, python, rust, typescript
調査の方向性
Start in FSharp2Fable.fs around line 939, where both explicit and implicit step forms reach FastIntegerForLoop, and compare the generated loops across the listed targets. Reproduce the mutable-array cases and the integer overflow and division cases from the issue, then trace how bounds are represented and emitted. Done means the reported .NET behavior matches across affected targets, while preserving the stated exception for upward .Length - 1 loops over reassigned mutable arrays.
索引モデルが issue の本文から書いたものです。
説明
After #5282, JS and TS evaluate a for loop bound once, like .NET, except in the cases below. Each one is also wrong on main. Outputs are from dotnet fsi and from Fable on the #5282 branch.
Array.length a - 1 on a mutable array reassigned in the body
let mutable a = [| 1; 2; 3 |]
let mutable n = 0
for _ = 0 to Array.length a - 1 do
a <- [| 1 |]
n <- n + 1
.NET: 3. Fable: 1.
.NET reads this bound once, but re-reads 0 to a.Length - 1 in an upward loop. #5282 keeps a.Length - 1 inline to match that, and a.Length - 1 and Array.length a - 1 become the same Fable AST, so Fable2Babel can't treat them differently. FSharp2Fable still sees them as different calls. The .NET behaviour looks like it comes from the compiler's OptimizeFastIntegerForLoop, but I haven't checked the compiler source.
Explicit step of 1 over a mutable array reassigned in the body
let mutable a = [| 1; 2; 3 |]
let mutable n = 0
for _ in 0 .. 1 .. a.Length - 1 do
a <- [| 1 |]
n <- n + 1
.NET: 3. Fable: 1. Without the step, for _ in 0 .. a.Length - 1 gives 1 on both. Both forms reach Fable as the same FastIntegerForLoop (FSharp2Fable.fs:939) and print the same JS, i <= (a.length - 1). A guess I haven't checked: the compiler stores the bound of the step form in a temporary, and Fable inlines that single-use temporary back into the loop. The same thing could explain for x in a below.
for x in a when the body reassigns a
let mutable a = [| 1; 2; 3 |]
let mutable sum = 0
for x in a do
a <- [| 100 |]
sum <- sum + x
.NET: 6. Fable: 1. .NET iterates the array a held when the loop started, while Fable reads a again on every iteration.
Bounds that should wrap or throw
| Bound | .NET | Fable |
|---|---|---|
0 to xs.Count + Int32.MaxValue |
wraps to a negative bound, 0 iterations | about 2^31 iterations |
0 to 1 / z with z = 0 |
DivideByZeroException |
1 iteration |
These two come from int arithmetic on JS, not from the loop.
Other targets
- Dart and Beam re-read every bound. With
for _ in 0 .. xs.Count - 1andxs.RemoveAt 0in the body, .NET gives 4 and both give 2. Beam also evaluates a bound likelet x = f 1 in x + f 1on every iteration. - PHP prints the bound in the
forcondition, so it probably re-reads too, and it prints<=fordownto. That's from readingPhpPrinter.fsonly, not run. - Python and Rust evaluate the bound once. If the bound compiles to statements (for example a local function Fable inlines), Python fails with
NameError: name '_expr0' is not definedand Rust fails to compile withE0586.
If this is fixed in FSharp2Fable, it would cover all targets at once: bind the bound with a let before the loop, except for an upward .Length - 1 on a mutable array, which .NET re-reads.
- 主要言語
- F#
- スター
- 3.2k
- フォーク
- 335
- 平均マージ
- 19時間 39分
- マージ済み PR(30日)
- 158
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドなし
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
fable-compiler/Fable のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
fable-compiler/Fable#5280 ·
メンテナーはふだん 1 日以内に返信
-
agentic-workflows automation repo-assist
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
fable-compiler/Fable#4531 ·
メンテナーはふだん 1 日以内に返信
-
agentic-workflows automation repo-assist
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
fable-compiler/Fable#4522 ·
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
fable-compiler/Fable#5286 ·
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 55/100
fable-compiler/Fable#5284 ·
メンテナーはふだん 1 日以内に返信
fable-compiler/Fable の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 73/100
メンテナーはふだん 4 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
NVIDIA/auto-ontology#463 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
RustCrypto/utils#1597 ·
メンテナーはふだん 1 日以内に返信
-
/shutdown/ready can fail to return an HTTP response when readiness takes longer than the server write timeout対応中かも @floriankoch が今日担当しました。 オープンtriage
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
envoyproxy/gateway#10562 ·
メンテナーはふだん 1 日以内に返信