Skip to content

fix(all): read more for loop bounds once like .NET - #5287

Merged
MangelMaxime merged 1 commit into
mainfrom
fix/for-loop-bound-follow-up
Nov 19, 2026
Merged

MangelMaxime merged 1 commit into
mainfrom
fix/for-loop-bound-follow-up

Conversation

@MangelMaxime

@MangelMaxime MangelMaxime commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Part of #5285. Follow-up to #5282, which fixed the JS/TS loop bound for most shapes.

This PR fixes the shapes that #5282 could not fix in Fable2Babel, and it applies the same rule to Dart and Beam.

Case .NET Before This PR
0 to Array.length a - 1 on a mutable array reassigned in the body 3 1 3
0 .. 1 .. a.Length - 1 on a mutable array reassigned in the body 3 1 3
for x in a with a reassigned in the body 6 1 6
Dart and Beam: 0 .. xs.Count - 1 with xs.RemoveAt 0 in the body 4 2 4
Beam: bound let x = f 1 in x + f 1 over 3 iterations, calls to f 2 8 2

.NET reads 0 to a.Length - 1 on a mutable array again on each iteration. JS/TS, Dart and Beam keep this behavior.

Changes:

  • FSharp2Fable sees the F# calls, so it can tell Array.length a from a.Length. For Array.length a - 1 and for a step of 1, it binds the bound with a let before the loop.
  • for x in a on a mutable array binds a with a let before the loop.
  • isStableForLoopLimit, isReReadForLoopLimit and the new makeForLoopWithBoundLimit move from Fable2Babel.fs to Transforms.Util.fs. Dart and Beam use them.

Not fixed:

  • Python and Rust read 0 to a.Length - 1 on a mutable array only once. .NET reads it again on each iteration.
  • Python and Rust: a bound that compiles to statements fails (NameError on Python, E0586 on Rust).

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Python Type Checking Results (Pyright)

Metric Value
Total errors 33
Files with errors 4
Excluded files 4
New errors ✅ No
Excluded files with errors (4 files)

These files have known type errors and are excluded from CI. Remove from pyrightconfig.ci.json as errors are fixed.

File Errors Status
temp/tests/Python/test_hash_set.py 18 Excluded
temp/tests/Python/test_applicative.py 11 Excluded
temp/tests/Python/test_nested_and_recursive_pattern.py 2 Excluded
temp/tests/Python/fable_modules/thoth_json_python/encode.py 2 Excluded

@MangelMaxime
MangelMaxime force-pushed the fix/for-loop-bound-follow-up branch from 49d1256 to a1ec839 Compare November 19, 2026 17:11
@MangelMaxime MangelMaxime changed the title fix(all): read more for loop bounds once like .NET fix(all): read more for loop bounds once like .NET Nov 19, 2026
@MangelMaxime
MangelMaxime marked this pull request as ready for review November 19, 2026 20:37
@MangelMaxime
MangelMaxime merged commit 1d9fe05 into main Nov 19, 2026
44 checks passed
@MangelMaxime
MangelMaxime deleted the fix/for-loop-bound-follow-up branch November 19, 2026 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant