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

Fix #1049 by adding a workaround (until Roslyn issue is fixed) #1059

Closed
Closed
Show file tree
Hide file tree
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
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
21 changes: 21 additions & 0 deletions CodeConverter/CSharp/DataFlowAnalysisExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{E6A02631-FF88-4FDE-A356-1EC4CA397445}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CSharpDllWithRefReturnIndexer</RootNamespace>
<AssemblyName>CSharpDllWithRefReturnIndexer</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="SpecialListWithRefReturnIndexer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("CSharpDllWithRefReturnIndexer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CSharpDllWithRefReturnIndexer")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("e6a02631-ff88-4fde-a356-1ec4ca397445")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpDllWithRefReturnIndexer
{
public class SpecialListWithRefReturnIndexer<T>
{
private T dummy;
public ref T this[int i] {
get {
return ref dummy;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "Prefix.VbLibrary", "Prefix.
EndProject
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "VisualBasicConsoleApp", "ConsoleApp1\VisualBasicConsoleApp.vbproj", "{C69D27C1-FF4E-43CB-BB0F-9BF811BF0F81}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpDllWithRefReturnIndexer", "CSharpDllWithRefReturnIndexer\CSharpDllWithRefReturnIndexer.csproj", "{E6A02631-FF88-4FDE-A356-1EC4CA397445}"
EndProject
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "VbNetUsingCSharpRefReturnIndexer", "VbNetUsingCSharpRefReturnIndexer\VbNetUsingCSharpRefReturnIndexer.vbproj", "{FAA9F1FC-C0BC-41DC-8CC7-D8D1DCBB6FEA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -68,6 +72,14 @@ Global
{C69D27C1-FF4E-43CB-BB0F-9BF811BF0F81}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C69D27C1-FF4E-43CB-BB0F-9BF811BF0F81}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C69D27C1-FF4E-43CB-BB0F-9BF811BF0F81}.Release|Any CPU.Build.0 = Release|Any CPU
{E6A02631-FF88-4FDE-A356-1EC4CA397445}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E6A02631-FF88-4FDE-A356-1EC4CA397445}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E6A02631-FF88-4FDE-A356-1EC4CA397445}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E6A02631-FF88-4FDE-A356-1EC4CA397445}.Release|Any CPU.Build.0 = Release|Any CPU
{FAA9F1FC-C0BC-41DC-8CC7-D8D1DCBB6FEA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FAA9F1FC-C0BC-41DC-8CC7-D8D1DCBB6FEA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FAA9F1FC-C0BC-41DC-8CC7-D8D1DCBB6FEA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FAA9F1FC-C0BC-41DC-8CC7-D8D1DCBB6FEA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Public Class Class1
Private Structure SomeStruct
Dim SomeField As Integer
Dim Text As String
End Structure

Dim lst As New CSharpDllWithRefReturnIndexer.SpecialListWithRefReturnIndexer(Of SomeStruct)

Sub S()
Dim i As Integer
Dim s As String

With lst(i)
.SomeField = 5
s = .Text
End With
End Sub
End Class

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<MyApplicationData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MySubMain>false</MySubMain>
<SingleInstance>false</SingleInstance>
<ShutdownMode>0</ShutdownMode>
<EnableVisualStyles>true</EnableVisualStyles>
<AuthenticationMode>0</AuthenticationMode>
<ApplicationType>1</ApplicationType>
<SaveMySettingsOnExit>true</SaveMySettingsOnExit>
</MyApplicationData>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Imports System
Imports System.Reflection
Imports System.Runtime.InteropServices

' General Information about an assembly is controlled through the following
' set of attributes. Change these attribute values to modify the information
' associated with an assembly.

' Review the values of the assembly attributes

<Assembly: AssemblyTitle("VbNetUsingCSharpRefReturnIndexer")>
<Assembly: AssemblyDescription("")>
<Assembly: AssemblyCompany("")>
<Assembly: AssemblyProduct("VbNetUsingCSharpRefReturnIndexer")>
<Assembly: AssemblyCopyright("Copyright © 2023")>
<Assembly: AssemblyTrademark("")>

<Assembly: ComVisible(False)>

'The following GUID is for the ID of the typelib if this project is exposed to COM
<Assembly: Guid("be11cb47-e713-43a7-82df-453d58c71116")>

' Version information for an assembly consists of the following four values:
'
' Major Version
' Minor Version
' Build Number
' Revision
'
' You can specify all the values or you can default the Build and Revision Numbers
' by using the '*' as shown below:
' <Assembly: AssemblyVersion("1.0.*")>

<Assembly: AssemblyVersion("1.0.0.0")>
<Assembly: AssemblyFileVersion("1.0.0.0")>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading