for loop bound still differs from .NET for reassigned mutable arrays

オープン
#5,285 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
36/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
dart, erlang, fsharp, javascript, python, rust, typescript
領域
backend, compilers

調査の方向性

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 - 1 and xs.RemoveAt 0 in the body, .NET gives 4 and both give 2. Beam also evaluates a bound like let x = f 1 in x + f 1 on every iteration.
  • PHP prints the bound in the for condition, so it probably re-reads too, and it prints <= for downto. That's from reading PhpPrinter.fs only, 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 defined and Rust fails to compile with E0586.

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

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

  • Dockerfile・Docker Compose ファイルなし
  • プルリクエストのテンプレートなし
  • コントリビューションガイドなし

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

fable-compiler/Fable のほかの issue

fable-compiler/Fable の issue をすべて見る

似ている issue

Backend & API Design の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。