Skip to content

Use ThrowHelper.ThrowInvalidOperationException in ImmutableCollection - #129942

Merged
jkotas merged 1 commit into
dotnet:mainfrom
prozolic:immutablecollection
Jun 28, 2026
Merged

jkotas merged 1 commit into
dotnet:mainfrom
prozolic:immutablecollection

Conversation

@prozolic

Copy link
Copy Markdown
Contributor

This PR replaces inline throw new InvalidOperationException statements with ThrowHelper.ThrowInvalidOperationException calls, following the pattern established in FrozenSet/FrozenDictionary.

Replace inline throw new InvalidOperationException statements with
ThrowHelper.ThrowInvalidOperationException calls, following the
pattern established in FrozenSet/FrozenDictionary.
Copilot AI review requested due to automatic review settings June 28, 2026 07:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors several System.Collections.Immutable implementation sites to use the library’s System.Collections.ThrowHelper.ThrowInvalidOperationException() helper instead of inline throw new InvalidOperationException() in common “invalid state” paths (e.g., empty builder, invalid enumerator Current).

Changes:

  • Switched a few InvalidOperationException throw sites to ThrowHelper.ThrowInvalidOperationException() across builder helpers and enumerators.
  • Added using System.Collections; in the System.Linq extensions file to access ThrowHelper.
  • Kept enumerator semantics equivalent while slightly reshaping conditionals (e.g., invert the null/bounds check and throw via helper).

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/libraries/System.Collections.Immutable/src/System/Linq/ImmutableArrayExtensions.cs Uses ThrowHelper.ThrowInvalidOperationException() for empty ImmutableArray<T>.Builder First/Last helpers (adds using System.Collections; for access).
src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/SortedInt32KeyNode.Enumerator.cs Uses ThrowHelper.ThrowInvalidOperationException() for invalid Current state.
src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableStack_1.Enumerator.cs Uses ThrowHelper.ThrowInvalidOperationException() for invalid Current state in both struct and class enumerators.
src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet_1.Enumerator.cs Uses ThrowHelper.ThrowInvalidOperationException() for invalid Current state.
src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary_2.Enumerator.cs Uses ThrowHelper.ThrowInvalidOperationException() for invalid Current state.
src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.Enumerator.cs Uses ThrowHelper.ThrowInvalidOperationException() for invalid Current state.
src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary_2.HashBucket.cs Uses ThrowHelper.ThrowInvalidOperationException() for FirstValue when bucket is empty.
src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Enumerator.cs Uses ThrowHelper.ThrowInvalidOperationException() for invalid Current state and preserves the (uint) bounds-check pattern.

@prozolic
prozolic marked this pull request as ready for review June 28, 2026 07:23

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@jkotas
jkotas merged commit 19a803d into dotnet:main Jun 28, 2026
90 checks passed
@prozolic
prozolic deleted the immutablecollection branch June 28, 2026 14:23
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jun 29, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
…#129942)

This PR replaces inline `throw new InvalidOperationException` statements
with `ThrowHelper.ThrowInvalidOperationException` calls, following the
pattern established in `FrozenSet`/`FrozenDictionary`.
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants