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

Handle C# ref return when converting VB->C# #1116

Merged
merged 9 commits into from
Jul 22, 2024
Merged
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
15 changes: 13 additions & 2 deletions CodeConverter/CSharp/ExpressionNodeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1833,7 +1833,9 @@ private RefConversion NeedsVariableForArgument(VBasic.Syntax.ArgumentSyntax node
RefConversion GetRefConversion(VBSyntax.ExpressionSyntax expression)
{
var symbolInfo = GetSymbolInfoInDocument<ISymbol>(expression);
if (symbolInfo is IPropertySymbol propertySymbol) {
if (symbolInfo is IPropertySymbol propertySymbol
// a property in VB.NET code can be ReturnsByRef if it's defined in a C# assembly the VB.NET code references
&& !propertySymbol.ReturnsByRef && !propertySymbol.ReturnsByRefReadonly) {
return propertySymbol.IsReadOnly ? RefConversion.PreAssigment : RefConversion.PreAndPostAssignment;
}

Expand All @@ -1858,7 +1860,16 @@ RefConversion GetRefConversion(VBSyntax.ExpressionSyntax expression)
bool IsRefArrayAcces(VBSyntax.ExpressionSyntax expression)
{
if (!(expression is VBSyntax.InvocationExpressionSyntax ies)) return false;
return _semanticModel.GetOperation(ies).IsArrayElementAccess() && GetRefConversion(ies.Expression) == RefConversion.Inline;
var op = _semanticModel.GetOperation(ies);
return (op.IsArrayElementAccess() || IsReturnsByRefPropertyElementAccess(op))
&& GetRefConversion(ies.Expression) == RefConversion.Inline;

static bool IsReturnsByRefPropertyElementAccess(IOperation op)
{
return op.IsPropertyElementAccess()
&& op is IPropertyReferenceOperation { Property: { } prop }
&& (prop.ReturnsByRef || prop.ReturnsByRefReadonly);
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions CodeConverter/CSharp/OperationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ public static bool IsAssignableExpression(this IOperation operation)
case OperationKind.DynamicMemberReference:
return true;

//Just documenting since it's the only one mentioning reference that can't necessarily be assigned to AFAIK
case OperationKind.PropertyReference:
return false;
//a property might be RefReturn, if it's defined in a referenced C# assembly
var prop = ((IPropertyReferenceOperation)operation).Property;
return prop.ReturnsByRef || prop.ReturnsByRefReadonly;
}

return false;
Expand Down
10 changes: 8 additions & 2 deletions Tests/CSharp/MultiFileSolutionAndProjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ public MultiFileSolutionAndProjectTests(MultiFileTestFixture multiFileTestFixtur
_multiFileTestFixture = multiFileTestFixture;
}

[Fact] /* enable for executing locally */
[Fact]
public async Task ConvertWholeSolutionAsync()
{

await _multiFileTestFixture.ConvertProjectsWhereAsync(p => true, Language.CS);
}

[Fact] /* enable for executing locally */
[Fact]
public async Task ConvertVbLibraryOnlyAsync()
{
await _multiFileTestFixture.ConvertProjectsWhereAsync(p => p.Name == "VbLibrary", Language.CS);
}

[Fact(Skip= "Roslyn bugs mean we can't run this test: https://github.com/icsharpcode/CodeConverter/pull/1116#issuecomment-2242645546")]
public async Task ConvertVbUsingCSharpRefReturnOnlyAsync()
{
await _multiFileTestFixture.ConvertProjectsWhereAsync(p => p.Name == "VisualBasicUsingCSharpRefReturn", Language.CS);
}
}
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>{8B843547-F49D-40A2-8C4E-1B81D8C5D589}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CSharpRefReturn</RootNamespace>
<AssemblyName>CSharpRefReturn</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="RefReturnList.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("CSharpRefReturn")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CSharpRefReturn")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[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("8b843547-f49d-40a2-8c4e-1b81d8c5d589")]

// 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,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpRefReturn
{
public class RefReturnList<T>
{
private T dummy;
public ref T this[int i] {
get {
return ref dummy;
}
}

public ref T RefProperty
{
get
{
return ref dummy;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Public Class ByRefArgument
Sub UseArr()
Dim arrObj() As Object
Modify(arrObj(0))

Dim arrInt() As Integer
Modify(arrInt(0))
End Sub

Sub UseRefReturn()
Dim lstObj As CSharpRefReturn.RefReturnList(Of Object)
Modify(lstObj(0))
Modify(lstObj.RefProperty)

Dim lstInt As CSharpRefReturn.RefReturnList(Of Integer)
Modify(lstInt(0))
Modify(lstInt.RefProperty)
End Sub

Sub Modify(ByRef o As Object)
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("VisualBasicUsesCSharpRefReturn")>
<Assembly: AssemblyDescription("")>
<Assembly: AssemblyCompany("")>
<Assembly: AssemblyProduct("VisualBasicUsesCSharpRefReturn")>
<Assembly: AssemblyCopyright("Copyright © 2024")>
<Assembly: AssemblyTrademark("")>

<Assembly: ComVisible(False)>

'The following GUID is for the ID of the typelib if this project is exposed to COM
<Assembly: Guid("47520bc8-6837-4f63-9aef-0a252d368f05")>

' 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