Repository navigation
Allow mixing ranges and values in collection expressions - #20759
xperiandri wants to merge 6 commits into
Conversation
✅ Release notes checked
|
There was a problem hiding this comment.
🟡 Changes recommended
Computation-expression recovery can emit cascading diagnostics, and the duplicated fixture suite should be consolidated.
2 open findings
What changed in this PR
Implements RFC FS-1031 behind --langversion:preview, allowing ranges to splice into list, array, sequence, and computation expressions.
Changes:
- Adds feature-gated range-splice checking and FS3925 diagnostics.
- Optimizes integral list/array splices into counted loops.
- Adds localization, release notes, and extensive component coverage.
| File | Description |
|---|---|
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions20.fs |
Tests empty ranges. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions19.fs |
Tests bigint ranges. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions18.fs |
Tests character ranges. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions17.fs |
Tests nested control-flow positions. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions16.fs |
Tests explicit yield! compatibility. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions15.fs |
Tests range yield! forms. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions14.fs |
Tests existing yield behavior. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions13.fs |
Tests mixed explicit and implicit splices. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions12.fs |
Tests equivalent collection forms. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions11.fs |
Tests stepped mixed ranges. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions10.fs |
Tests multiple mixed ranges. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions08.fs |
Tests the primary RFC examples. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions07.fs |
Tests custom computation expressions. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions06.fs |
Tests type inference. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions05.fs |
Tests sequence expression combinations. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions04.fs |
Tests sequence equivalence. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions03.fs |
Tests array ranges. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions02d.fs |
Tests stepped list ranges. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions02c.fs |
Tests multiple list ranges. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions02b.fs |
Tests surrounded list ranges. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/SequenceExpressions02a.fs |
Tests trailing list ranges. |
tests/FSharp.Compiler.ComponentTests/Language/SequenceExpressions/MixedSequenceExpressionTests.fs |
Defines comprehensive feature tests. |
tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj |
Includes the new test module. |
src/Compiler/xlf/FSComp.txt.zh-Hant.xlf |
Adds Traditional Chinese localization entries. |
src/Compiler/xlf/FSComp.txt.zh-Hans.xlf |
Adds Simplified Chinese localization entries. |
src/Compiler/xlf/FSComp.txt.tr.xlf |
Adds Turkish localization entries. |
src/Compiler/xlf/FSComp.txt.ru.xlf |
Adds Russian localization entries. |
src/Compiler/xlf/FSComp.txt.pt-BR.xlf |
Adds Portuguese localization entries. |
src/Compiler/xlf/FSComp.txt.pl.xlf |
Adds Polish localization entries. |
src/Compiler/xlf/FSComp.txt.ko.xlf |
Adds Korean localization entries. |
src/Compiler/xlf/FSComp.txt.ja.xlf |
Adds Japanese localization entries. |
src/Compiler/xlf/FSComp.txt.it.xlf |
Adds Italian localization entries. |
src/Compiler/xlf/FSComp.txt.fr.xlf |
Adds French localization entries. |
src/Compiler/xlf/FSComp.txt.es.xlf |
Adds Spanish localization entries. |
src/Compiler/xlf/FSComp.txt.de.xlf |
Adds German localization entries. |
src/Compiler/xlf/FSComp.txt.cs.xlf |
Adds Czech localization entries. |
src/Compiler/Optimize/LowerComputedCollections.fs |
Lowers integral splices to counted loops. |
src/Compiler/FSComp.txt |
Defines feature and FS3925 messages. |
src/Compiler/Facilities/LanguageFeatures.fsi |
Exposes the feature case internally. |
src/Compiler/Facilities/LanguageFeatures.fs |
Registers the preview feature. |
src/Compiler/Checking/Expressions/CheckSequenceExpressions.fs |
Handles range splices in sequences. |
src/Compiler/Checking/Expressions/CheckExpressionsOps.fs |
Adds range recognition and rewriting helpers. |
src/Compiler/Checking/Expressions/CheckComputationExpressions.fs |
Translates computation-expression splices. |
src/Compiler/Checking/Expressions/CheckArrayOrListComputedExpressions.fs |
Preserves flat literal element semantics. |
docs/release-notes/.Language/preview.md |
Documents the preview language feature. |
docs/release-notes/.FSharp.Compiler.Service/11.0.200.md |
Records the FCS change. |
🧠 Review effort: Balanced
T-Gro
left a comment
There was a problem hiding this comment.
🤖 🕵️ AI review — verify independently.
T-Gro
left a comment
There was a problem hiding this comment.
The diff is still way too big - apply the NoBloat instructions and CodeCompaction skill.
Especially the massive code comments would benefit not just with drastic reduction, but ideally with well named functions/arguments/bindings/types making the intention clear.
If a well named piece of code just tells what the comment says - delete the comment.
- This diff adds 13 new code comment lines and 1154 new code comment chars.
- Comments bypass the type system and become stale maintenance state. Keep only durable public contracts; express implementation intent through names, functions, active patterns, discriminated unions, and types.
d9ef791 to
d2f0165
Compare
|
Reworked and rebased on the latest main (daacf37). The change now adds 471 lines instead of 828 (222 of them tests, 130 generated xlf) and no code comments (13 before). Size and comments
Fixed on the way (each has a line in the tests)
Notes
Tests run locally: the four new tests, |
d2f0165 to
3377652
Compare
T-Gro
left a comment
There was a problem hiding this comment.
🤖🕵️ AI review — verify independently.
| TryGetSimpleSemicolonSequenceOfComprehension cexpr [] cenv acceptDeprecated | ||
|
|
||
| [<return: Struct>] | ||
| let (|SimpleSemicolonSequenceWithRanges|_|) cenv cexpr = |
There was a problem hiding this comment.
🤖🕵️⏱️🔥
[<Params(1_000, 2_500, 5_000)>]
member val Count = 0 with get, set
[<Benchmark>]
member this.CheckMixedRange() =
check $"[ 0; 1..{this.Count}; 9 ]"Add mixed-range, explicit-yield!, and flat-value compiler benchmarks before merge. The bounded 5k computed forms exceeded 180s while flat values stayed ~0.8s; track the scaling instead of relying on correctness-only tests.
There was a problem hiding this comment.
Added in a4bd607: MixedRangeBenchmarks in the compiler benchmarks. Each benchmark checks a list of Count elements for Count 250, 500 and 1000: the flat literal is the baseline, the other two are mixed ranges and the explicit yield! form, with a range at every tenth element. Local Release build, --inProcess:
| Count | flat | mixed ranges | explicit yield! |
|---|---|---|---|
| 250 | 128 ms | 201 ms | 216 ms |
| 500 | 121 ms | 467 ms | 546 ms |
| 1000 | 92 ms | 1,723 ms | 1,944 ms |
The flat literal stays constant. Mixed ranges and explicit yield! grow about 4x per doubling, and so do their allocations (105, 196, 543 MB against a flat 70 MB), and mixed is never slower than explicit: both are checked by the sequence-expression checker. So the PR adds no new cliff, but the cost of these forms is not linear. The SDK compiler already overflows its stack on the explicit form at 1,500 elements, while the flat literal is fine. I did not change the checker's complexity here.
There was a problem hiding this comment.
Follow-up after the rebase onto main (the benchmark commit is c89137e now, not a4bd607). On current main the stack guard from #20480 keeps the explicit and the mixed form from crashing, but they are slow instead: the Typecheck phase takes 5 s for 2000 yield statements and 40 to 60 s for 3000, against 1.5 s for the flat literal at any size, and much more when the machine is busy, so the more than 180 s you saw at 5k fits. I am filing an issue for the checker, and I suggested a softer Scaling sentence in fsharp/fslang-design#860 (the RFC says checking MUST stay linear).
Implements RFC FS-1031 under --langversion:preview: an unparenthesized range in element position of a list, array, sequence or computation expression splices its elements, as yield! does. Builds on Edgar Gonzalez's prototype in dotnet#18670, reworked to follow the amended RFC. Co-authored-by: Edgar Gonzalez <edgargonzalez.info@gmail.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After the error, recovery passed the range to 'Yield' or 'Return' as a sequence, so a builder whose method takes an element also reported a type mismatch. 'yield e1..e2' now recovers as 'yield! e1..e2' when the builder has 'YieldFrom'. Otherwise, and for 'return', the method gets a placeholder argument of any type. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The 21 standalone fixtures each compiled their own program. Their input and output pairs now run as named checks in two programs, and the two files that differed only in two ranges are one theory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review asked for a smaller diff without code comments. The intent now sits in
names (BareRangeExpr, RangeAsSingleValue, mkRangeSplice, rewriteYieldFromRange,
rejectRangeOperand, SimpleSemicolonSequenceWithRanges); one rejectRangeOperand
replaces the recovery arms for yield and return of a range; the tests are four
compilations instead of a fixture per scenario.
Fixes found on the way:
- a unativeint range with a single element was treated as empty (EmptyRange),
which also dropped the element of [ 2un..2un ] and skipped for _ in 2un..2un on main
- convertSimpleReturnToExpr declines BindReturn for a returned range and stays
free of diagnostics, so FS3925 is reported once by the return arm
- the counted loop reads a mutable range operand before the later operands run
and no longer counts extra elements for a nativeint range with the smallest step
- a then sequence is not split into the elements of a flat literal
- query { return 1..3 } reports FS3144 only
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…terals Each benchmark checks a list of Count elements, a range at every tenth one in the mixed and the explicit form, with Count 250, 500 and 1000. The flat literal is the baseline. Mixed ranges are checked by the sequence-expression checker like the explicit form, so the two scale alike and the flat literal does not. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
a4bd607 to
c89137e
Compare


Implements RFC FS-1031 under
--langversion:preview: a range can be an element of a list, array, sequence or computation expression, as in[ -3; 1..10; 19 ], and splices its elements likeyield!. Builds on @edgarfgp's prototype #18670, reworked to follow the amended RFC.Details
if,match,for,while,try,letandusebodies;yield! 1..10is allowed too. Likeyield!, it is not an explicityieldfor the FS-1069 rule.let f c = [ 1; 2..3; c ]infersc: int. Elsewhere plain values keep the FS-1069 rule.yield e1..e2,-> e1..e2and, in a computation expression,return e1..e2report the new error FS3926;returnin a list, array or sequence keeps FS0635, and(e1..e2)stays FS0751.YieldFrom(YieldFromFinalin tail position) and report FS0708 without it.yield!of a range in a list or array comprehension compiles to a counted loop instead ofAddManyover the range enumerator. It is gated byLowerIntegralRangesToFastLoops(F# 9), so it also applies to the explicityield! (..) a bunder stable language versions.unativeintrange with a single element was empty, a mutable bound was read after the later bounds had run, and anativeintrange with the smallest step gave extra elements.🤖 Generated with Claude Code