Skip to content

Fix HashSet enumerator-invalidation test flags - #137165

Merged
eiriktsarpalis merged 1 commit into
dotnet:mainfrom
su-senka:fix/hashset-enumerator-test-flags
Nov 9, 2026
Merged

eiriktsarpalis merged 1 commit into
dotnet:mainfrom
su-senka:fix/hashset-enumerator-test-flags

Conversation

@su-senka

@su-senka su-senka commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Both HashSet test suites set ModifyEnumeratorThrows from base.ModifyEnumeratorAllowed, which is None. None & ~(...) is always None, so the modify-during-enumeration tests have had nothing to check for HashSet since #37180. You can delete _version++ from HashSet<T>.AddIfNotPresent and System.Collections.Tests still passes.

This switches both suites to base.ModifyEnumeratorThrows. Two more changes were needed once the tests actually ran:

  • An empty HashSet enumerated through the interface returns the shared empty enumerator, which has no version check. So Enumerator_Empty_ModifiedDuringEnumeration_ThrowsInvalidOperationException is now false, same as in the LinkedList and OrderedDictionary tests.
  • The non-generic suite only generated a Clear operation, and HashSet.Clear doesn't invalidate enumerators. I moved Clear to the allowed set and added an Add operation so the suite tests something.

With this change System.Collections.Tests passes on osx-arm64, and removing that _version++ fails 60 tests.

Overwrite ends up in both the throws and allowed sets of the generic suite. It has no effect, since nothing generates an Overwrite operation for sets, but I can exclude it.

ModifyEnumeratorThrows was derived from base.ModifyEnumeratorAllowed
(None), so None & ~(...) left the modify-during-enumeration tests with
nothing to check. Derive it from base.ModifyEnumeratorThrows instead,
mark the empty-set singleton enumerator as not throwing, exclude Clear
in the non-generic suite (HashSet.Clear does not invalidate), and give
that suite an Add operation so its throw tests are not vacuous.

Removing _version++ from HashSet.AddIfNotPresent previously passed all
System.Collections tests; with this change 60 tests fail.
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 8, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.

@eiriktsarpalis eiriktsarpalis 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

@eiriktsarpalis
eiriktsarpalis merged commit c96730e into dotnet:main Nov 9, 2026
94 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Collections community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants