Skip to content

Commit 0c5fe38

Browse files
authored
Merge pull request #6935 from mavasani/Issue6929
Avoid 'Collection was modified' InvalidOperationException
2 parents d29b797 + 20f7bea commit 0c5fe38

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/Utilities/FlowAnalysis/FlowAnalysis/Framework/DataFlow/AnalysisEntityBasedPredicateAnalysisData.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Diagnostics;
77
using System.Diagnostics.CodeAnalysis;
88
using System.Linq;
9+
using Analyzer.Utilities.PooledObjects;
910

1011
namespace Microsoft.CodeAnalysis.FlowAnalysis.DataFlow
1112
{
@@ -170,6 +171,11 @@ private void ClearOverlappingAnalysisDataForIndexedEntity(AnalysisEntity analysi
170171
return;
171172
}
172173

174+
// Collect all the overlapping indexed entities whose value needs to be updated into a builder.
175+
// Ensure that we perform these state updates after the foreach loop to avoid modifying the
176+
// underlying CoreAnalysisData within the loop.
177+
// See https://github.com/dotnet/roslyn-analyzers/issues/6929 for more details.
178+
using var builder = ArrayBuilder<(AnalysisEntity, TValue)>.GetInstance(CoreAnalysisData.Count);
173179
foreach (var entity in CoreAnalysisData.Keys)
174180
{
175181
if (entity.Indices.Length != analysisEntity.Indices.Length ||
@@ -197,10 +203,15 @@ private void ClearOverlappingAnalysisDataForIndexedEntity(AnalysisEntity analysi
197203
var mergedValue = ValueDomain.Merge(value, existingValue);
198204
if (!existingValue!.Equals(mergedValue))
199205
{
200-
SetAbstractValue(entity, mergedValue);
206+
builder.Add((entity, mergedValue));
201207
}
202208
}
203209
}
210+
211+
foreach (var (entity, newValue) in builder.AsEnumerable())
212+
{
213+
SetAbstractValue(entity, newValue);
214+
}
204215
}
205216

206217
protected override void Dispose(bool disposing)

0 commit comments

Comments
 (0)