Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid 'Collection was modified' InvalidOperationException #6935

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Analyzer.Utilities.PooledObjects;

namespace Microsoft.CodeAnalysis.FlowAnalysis.DataFlow
{
Expand Down Expand Up @@ -170,6 +171,11 @@ private void ClearOverlappingAnalysisDataForIndexedEntity(AnalysisEntity analysi
return;
}

// Collect all the overlapping indexed entities whose value needs to be updated into a builder.
// Ensure that we perform these state updates after the foreach loop to avoid modifying the
// underlying CoreAnalysisData within the loop.
// See https://github.com/dotnet/roslyn-analyzers/issues/6929 for more details.
using var builder = ArrayBuilder<(AnalysisEntity, TValue)>.GetInstance(CoreAnalysisData.Count);
foreach (var entity in CoreAnalysisData.Keys)
{
if (entity.Indices.Length != analysisEntity.Indices.Length ||
Expand Down Expand Up @@ -197,10 +203,15 @@ private void ClearOverlappingAnalysisDataForIndexedEntity(AnalysisEntity analysi
var mergedValue = ValueDomain.Merge(value, existingValue);
if (!existingValue!.Equals(mergedValue))
{
SetAbstractValue(entity, mergedValue);
builder.Add((entity, mergedValue));
}
}
}

foreach (var (entity, newValue) in builder.AsEnumerable())
{
SetAbstractValue(entity, newValue);
}
}

protected override void Dispose(bool disposing)
Expand Down