From b0a64e65d6c53b525a65b46ce9fce084b5ce5da7 Mon Sep 17 00:00:00 2001 From: Timur Kelman Date: Fri, 8 Dec 2023 12:06:01 +0100 Subject: [PATCH] add a workaround It's only required until https://github.com/dotnet/roslyn/issues/71115 is fixed. --- CodeConverter/CSharp/CommonConversions.cs | 2 +- .../CSharp/DataFlowAnalysisExtensions.cs | 24 +++++++++++++++++++ .../CSharp/DefiniteAssignmentAnalyzer.cs | 2 +- 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 CodeConverter/CSharp/DataFlowAnalysisExtensions.cs diff --git a/CodeConverter/CSharp/CommonConversions.cs b/CodeConverter/CSharp/CommonConversions.cs index 7c5c9a2cf..f1e7229ff 100644 --- a/CodeConverter/CSharp/CommonConversions.cs +++ b/CodeConverter/CSharp/CommonConversions.cs @@ -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); } } diff --git a/CodeConverter/CSharp/DataFlowAnalysisExtensions.cs b/CodeConverter/CSharp/DataFlowAnalysisExtensions.cs new file mode 100644 index 000000000..5557ff8fa --- /dev/null +++ b/CodeConverter/CSharp/DataFlowAnalysisExtensions.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ICSharpCode.CodeConverter.CSharp; +internal static class DataFlowAnalysisExtensions +{ + /// + /// Accesses the 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) + /// + public static System.Collections.Immutable.ImmutableArray ReadInsideSafe(this DataFlowAnalysis dataFlow) + { + try + { + return dataFlow.ReadInside; + } + catch + { + return dataFlow.ReadInside; + } + } +} diff --git a/CodeConverter/CSharp/DefiniteAssignmentAnalyzer.cs b/CodeConverter/CSharp/DefiniteAssignmentAnalyzer.cs index 931a68ac4..66def1f57 100644 --- a/CodeConverter/CSharp/DefiniteAssignmentAnalyzer.cs +++ b/CodeConverter/CSharp/DefiniteAssignmentAnalyzer.cs @@ -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); }