Skip to content

Commit

Permalink
add a workaround
Browse files Browse the repository at this point in the history
It's only required until dotnet/roslyn#71115 is fixed.
  • Loading branch information
Timur Kelman authored and Timur Kelman committed Dec 8, 2023
1 parent 28fcc08 commit b0a64e6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CodeConverter/CSharp/CommonConversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ public CSSyntax.IdentifierNameSyntax GetRetVariableNameOrNull(VBSyntax.MethodBlo
var flow = SemanticModel.AnalyzeDataFlow(node.Statements.First(), node.Statements.Last());

if (flow.Succeeded) {
assignsToMethodNameVariable = flow.ReadInside.Any(equalsMethodName) || flow.WrittenInside.Any(equalsMethodName);
assignsToMethodNameVariable = flow.ReadInsideSafe().Any(equalsMethodName) || flow.WrittenInside.Any(equalsMethodName);
}
}

Expand Down
24 changes: 24 additions & 0 deletions CodeConverter/CSharp/DataFlowAnalysisExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace ICSharpCode.CodeConverter.CSharp;
internal static class DataFlowAnalysisExtensions
{
/// <summary>
/// Accesses the <see cref="DataFlowAnalysis.ReadInside" /> second time in case of exception.
/// This is a workaround for a bug present in Roslyn up to version 4.8.0
/// (https://github.com/dotnet/roslyn/issues/71115)
/// </summary>
public static System.Collections.Immutable.ImmutableArray<ISymbol> ReadInsideSafe(this DataFlowAnalysis dataFlow)
{
try
{
return dataFlow.ReadInside;
}
catch
{
return dataFlow.ReadInside;
}
}
}
2 changes: 1 addition & 1 deletion CodeConverter/CSharp/DefiniteAssignmentAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ internal static class DefiniteAssignmentAnalyzer

public static bool IsDefinitelyAssignedBeforeRead(ISymbol localSymbol, DataFlowAnalysis methodFlow)
{
if (!methodFlow.ReadInside.Contains(localSymbol)) return true;
if (!methodFlow.ReadInsideSafe().Contains(localSymbol)) return true;
var unassignedVariables = methodFlow.GetVbUnassignedVariables();
return unassignedVariables != null && !unassignedVariables.Contains(localSymbol, SymbolEqualityComparer.IncludeNullability);
}
Expand Down

0 comments on commit b0a64e6

Please sign in to comment.