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

Improve handling of indexed array access in flow analysis #6776

Merged
merged 3 commits into from
Jul 18, 2023

Conversation

mavasani
Copy link
Contributor

Fixes #6616

We now ensure that setting an abstract value for an indexed access entity with any non-constant index clears out the existing value for entities that can overlap the indexed location

Fixes dotnet#6616

We now ensure that setting an abstract value for an indexed access entity with any non-constant index clears out the existing value for entities that can overlap the indexed location
@mavasani mavasani requested a review from a team as a code owner July 17, 2023 12:41
@@ -159,6 +163,46 @@ public PredicateValueKind ApplyPredicatedDataForEntity(AnalysisEntity predicated

public void AddTrackedEntities(HashSet<AnalysisEntity> builder) => builder.UnionWith(CoreAnalysisData.Keys);

private void ClearOverlappingAnalysisDataForIndexedEntity(AnalysisEntity analysisEntity, TValue value)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the core fix

@mavasani
Copy link
Contributor Author

@Youssef1313

return false;
}

// Now perform slow check that compares individual hash code parts sequences.
Copy link
Member

Choose a reason for hiding this comment

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

Why are we comparing hash codes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Youssef1313 We were seeing extremely large allocations and CPU stacks for dictionary operations keyed on AnalysisEntity during flow analysis, hence we had switched to using a CacheBasedEquatable approach. It's possible that this is no longer required in the current state of the flow analysis framework, but we would need to invest a few cycles in cleanup and performance measurements on real world code to confirm.

@codecov
Copy link

codecov bot commented Jul 18, 2023

Codecov Report

Merging #6776 (2a58a22) into main (40a3ec1) will increase coverage by 0.00%.
The diff coverage is 95.55%.

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #6776    +/-   ##
========================================
  Coverage   96.34%   96.34%            
========================================
  Files        1388     1386     -2     
  Lines      325732   325934   +202     
  Branches    10735    10722    -13     
========================================
+ Hits       313811   314008   +197     
- Misses       9210     9215     +5     
  Partials     2711     2711            

@mavasani
Copy link
Contributor Author

@CollinAlpert I am seeing intermittent test failures for CA1001 in CI after #6741

Test:
Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.UnitTests.TypesThatOwnDisposableFieldsShouldBeDisposableAnalyzerTests.CA1001_ExcludedSymbolNamesAsync(editorConfigText: "")

Expected diagnostic message to be "Type 'SomeClass' owns disposable field(s) '_fs', '_ms' but is not disposable" was "Type 'SomeClass' owns disposable field(s) '_fs' but is not disposable"


Expected diagnostic:
// /0/Test0.cs(4,14,4,23): hidden CA1001: Type 'SomeClass' owns disposable field(s) '_fs', '_ms' but is not disposable
VerifyCS.Diagnostic().WithSpan(4, 14, 4, 23).WithArguments("SomeClass", "_fs', '_ms"),


Actual diagnostic:
// /0/Test0.cs(4,14): hidden CA1001: Type 'SomeClass' owns disposable field(s) '_fs' but is not disposable
VerifyCS.Diagnostic().WithSpan(4, 14, 4, 23).WithArguments("SomeClass", "_fs"),


Assert.Equal() Failure
Expected: Type 'SomeClass' owns disposable field(s) '_fs', '_ms' but is not disposable
Actual:   Type 'SomeClass' owns disposable field(s) '_fs' but is not disposable



Stack trace
   at Microsoft.CodeAnalysis.Testing.Verifiers.XUnitVerifier.Equal[T](T expected, T actual, String message) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Testing.Verifiers.XUnit/XUnitVerifier.cs:line 49
   at Microsoft.CodeAnalysis.Testing.AnalyzerTest`1.VerifyDiagnosticResults(IEnumerable`1 actualResults, ImmutableArray`1 analyzers, DiagnosticResult[] expectedResults, IVerifier verifier) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/AnalyzerTest`1.cs:line 603
   at Microsoft.CodeAnalysis.Testing.AnalyzerTest`1.VerifyDiagnosticsAsync(EvaluatedProjectState primaryProject, ImmutableArray`1 additionalProjects, DiagnosticResult[] expected, IVerifier verifier, CancellationToken cancellationToken) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/AnalyzerTest`1.cs:line 465
   at Microsoft.CodeAnalysis.Testing.CodeFixTest`1.RunImplAsync(CancellationToken cancellationToken) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.CodeFix.Testing/CodeFixTest`1.cs:line 305
   at Microsoft.CodeAnalysis.Testing.AnalyzerTest`1.RunAsync(CancellationToken cancellationToken) in /_/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/AnalyzerTest`1.cs:line 188
   at Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.UnitTests.TypesThatOwnDisposableFieldsShouldBeDisposableAnalyzerTests.CA1001_ExcludedSymbolNamesAsync(String editorConfigText) in /_/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/TypesThatOwnDisposableFieldsShouldBeDisposableTests.cs:line 875
--- End of stack trace from previous location ---

@mavasani mavasani merged commit 20690b4 into dotnet:main Jul 18, 2023
@mavasani mavasani deleted the ArrayAccess branch July 18, 2023 05:38
amiru3f pushed a commit to amiru3f/roslyn-analyzers that referenced this pull request Jul 26, 2023
Improve handling of indexed array access in flow analysis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CA1508 False Positive after array values set in a for loop
2 participants