Skip to content

Fix InvalidProgramException from Seq.collect over a struct seq materialised with List.ofSeq/Seq.toList/Seq.toArray - #20227

Open
T-Gro wants to merge 4 commits into
mainfrom
fix/issue-20203
Open

Fix InvalidProgramException from Seq.collect over a struct seq materialised with List.ofSeq/Seq.toList/Seq.toArray#20227
T-Gro wants to merge 4 commits into
mainfrom
fix/issue-20203

Conversation

@T-Gro

@T-Gro T-Gro commented Aug 6, 2026

Copy link
Copy Markdown
Member

Fixes #20203

open System.Collections.Immutable
// ImmutableArray<_> is a struct that implements seq<'T>
let xs = ImmutableArray.Create(1, 2, 3)
let result = [ yield! xs ]   // or List.ofSeq (Seq.collect id ...)
// before (Release only): System.InvalidProgramException at runtime
// after: works

The comprehension lowering fed a struct (value-type) sub-collection straight into ListCollector/ArrayCollector.AddMany, which expects a reference seq<'T>, and typed the generated try/finally with the original struct body type. That produced invalid IL — a value type where a reference IEnumerable was expected, plus a spurious ldnull stored into a struct local. The struct sub-collection is now coerced (boxed) to seq<'T> and the try/finally result type is unit.

@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

❗ Release notes required

You can open this PR in browser to add release notes: open in github.dev


✅ Found changes and release notes in following paths:

Warning

No PR link found in some release notes, please consider adding it.

Change path Release notes path Description
`src/Compiler` docs/release-notes/.FSharp.Compiler.Service/11.0.100.md No current pull request URL (#20227) found, please consider adding it

@github-actions github-actions Bot added the AI-Tooling-Check-Bypassed Tooling check: non-fork PR, not diff-analyzed label Aug 6, 2026
Copilot and others added 4 commits August 7, 2026 09:40
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…e in list/array collector lowering (#20203)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
… IL (#20203)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Consolidate the Issue20203 execution tests into a [<Theory>]/[<InlineData>]
covering the struct seq<'T> materialisation matrix (List.ofSeq/Seq.toList/
Seq.toArray, yield! list+array, non-identity collect, multiple ordered
sub-collections, reference-collection negatives, Array.ofSeq, Seq.map, empty,
single, 1000-element boundary), plus two dedicated [<Fact>] tests for generic
element types and a disposable-enumerator use path. Redundant RED facts removed.

Add a ### Fixed release-notes entry for the #20203 Release-only
InvalidProgramException fix.

Update the ListExpressionStepping05/06 EmittedIL baselines that the codegen fix
legitimately changed (spurious ldnull into the try/finally result local removed,
unit try/finally result type); these were missed in the baseline sprint.

Validation: build.cmd -c Release and -c Debug succeed; EmittedIL suite green
(1537 passed); RegressionTests green (30 passed). Full --testcoreclr not run in
this environment (requires -msbuildEngine dotnet and full-suite runtime).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@T-Gro
T-Gro force-pushed the fix/issue-20203 branch from 9e8690d to 3083358 Compare August 7, 2026 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-Tooling-Check-Bypassed Tooling check: non-fork PR, not diff-analyzed

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

Release-only InvalidProgramException: Seq.collect over a struct collection (ImmutableArray) materialised with List.ofSeq

2 participants