Skip to content

Fix "ref" parameter #61

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

Merged
merged 1 commit into from
Mar 2, 2023
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
2 changes: 2 additions & 0 deletions src-examples/ProxyInterfaceConsumer/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ public void AddAddresses(params Address[] addresses)
public void In_Out_Ref1(in int a, out int b, ref int c)
{
b = 1;
c++;
}

public int In_Out_Ref2(in Address a, out Address b, ref Address c)
{
b = new Address();
c.HouseNumber = 11;
return 404;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static string Build(IParameterSymbol parameterSymbol, string? type)
{
stringBuilder.Append(parameterSymbol.GetAttributesPrefix()); // "" or [NotNullWhen(true)]
stringBuilder.Append(parameterSymbol.GetParamsPrefix()); // "" or "params "
stringBuilder.Append(parameterSymbol.GetRefPrefix()); // "" or "out "
stringBuilder.Append(parameterSymbol.GetRefKindPrefix()); // "" or "out "
stringBuilder.AppendFormat("{0} ", type); // string or another type
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ internal static class ParameterSymbolExtensions
{
private const string ParameterValueNull = "null";

public static string GetRefPrefix(this IParameterSymbol ps)
public static bool IsRef(this IParameterSymbol ps)
{
return ps.RefKind is RefKind.Ref or RefKind.RefReadOnly;
}

public static string GetRefKindPrefix(this IParameterSymbol ps)
{
return ps.RefKind switch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ namespace ProxyInterfaceSourceGenerator.Extensions;

internal static class StringExtensions
{
public static string IIf(this bool value, string valueTrue, string valueFalse = "")
{
return value ? valueTrue : valueFalse;
}

// See https://andrewlock.net/why-is-string-gethashcode-different-each-time-i-run-my-program-in-net-core/
public static string GetDeterministicHashCodeAsString(this string str)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ private string GeneratePublicMethods(ClassSymbol targetClassSymbol, bool proxyBa
var type = GetParameterType(parameterSymbol, out _);

methodParameters.Add(MethodParameterBuilder.Build(parameterSymbol, type));
invokeParameters.Add($"{parameterSymbol.GetRefPrefix()}{parameterSymbol.GetSanitizedName()}_");

// Do not add the '_' for a 'ref' parameter.
invokeParameters.Add($"{parameterSymbol.GetRefKindPrefix()}{parameterSymbol.GetSanitizedName()}{(!parameterSymbol.IsRef()).IIf("_")}");
}

string overrideOrVirtual = string.Empty;
Expand All @@ -235,8 +237,9 @@ private string GeneratePublicMethods(ClassSymbol targetClassSymbol, bool proxyBa
}

str.AppendLine($" public {overrideOrVirtual}{returnTypeAsString} {method.GetMethodNameWithOptionalTypeParameters()}({string.Join(", ", methodParameters)}){whereStatement}");
str.AppendLine(" {");
foreach (var ps in method.Parameters)
str.AppendLine(@" {");

foreach (var ps in method.Parameters.Where(p => !p.IsRef()))
{
var type = FixType(ps.Type.ToString());
string normalOrMap = $" = {ps.GetSanitizedName()}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ public int DefaultValue(int x = 100)

public void In_Out_Ref1(in int a, out int b, ref int c)
{
int a_ = a;
int b_;
int c_ = c;
_Instance.In_Out_Ref1(in a_, out b_, ref c_);
_Instance.In_Out_Ref1(in a, out b_, ref c);
b = b_;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,8 @@ public int DefaultValue(int x = 100)

public void In_Out_Ref1(in int a, out int b, ref int c)
{
int a_ = a;
int b_;
int c_ = c;
_Instance.In_Out_Ref1(in a_, out b_, ref c_);
_Instance.In_Out_Ref1(in a, out b_, ref c);
b = b_;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
HintName: ProxyInterfaceGenerator.ProxyAttribute.g.cs,
HintName: ProxyInterfaceGenerator.Extra.g.cs,
Source:
//----------------------------------------------------------------------------------------
// <auto-generated>
Expand All @@ -16,7 +16,7 @@ using System;
namespace ProxyInterfaceGenerator
{
[AttributeUsage(AttributeTargets.Interface)]
public class ProxyAttribute : Attribute
internal sealed class ProxyAttribute : Attribute
{
public Type Type { get; }
public bool ProxyBaseClasses { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public void GenerateFiles_When_MixedVisibility_Should_GenerateCorrectFiles()
public void GenerateFiles_ForSingleClass_Should_GenerateCorrectFiles()
{
// Arrange
var attributeFilename = "ProxyInterfaceGenerator.ProxyAttribute.g.cs";
var attributeFilename = "ProxyInterfaceGenerator.Extra.g.cs";
var interfaceFilename = "ProxyInterfaceSourceGeneratorTests.Source.IPersonExtends.g.cs";
var proxyClassFilename = "ProxyInterfaceSourceGeneratorTests.Source.PersonExtendsProxy.g.cs";

Expand Down Expand Up @@ -364,7 +364,7 @@ public void GenerateFiles_ForSingleClass_AsInternal_Should_GenerateCorrectFiles(
public void GenerateFiles_ForTwoClasses_Should_GenerateCorrectFiles()
{
// Arrange
var attributeFilename = "ProxyInterfaceGenerator.ProxyAttribute.g.cs";
var attributeFilename = "ProxyInterfaceGenerator.Extra.g.cs";
var interfaceHumanFilename = "ProxyInterfaceSourceGeneratorTests.Source.IHuman.g.cs";
var proxyClassHumanFilename = "ProxyInterfaceSourceGeneratorTests.Source.HumanProxy.g.cs";
var interfacePersonFilename = "ProxyInterfaceSourceGeneratorTests.Source.IPerson.g.cs";
Expand Down Expand Up @@ -440,5 +440,12 @@ public void GenerateFiles_ForTwoClasses_Should_GenerateCorrectFiles()
var proxyCode = proxyClassPerson.ToString();
if (Write) File.WriteAllText($"../../../Destination/{proxyClassPersonFilename}", proxyCode);
proxyCode.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{proxyClassPersonFilename}"));

var personProxy = new PersonProxy(new Person());

int c = 100;
personProxy.In_Out_Ref1(1, out var b, ref c);

c.Should().Be(101);
}
}
1 change: 1 addition & 0 deletions tests/ProxyInterfaceSourceGeneratorTests/Source/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public int DefaultValue(int x = 100)
public void In_Out_Ref1(in int a, out int b, ref int c)
{
b = 1;
c++;
}

public double[,] Out_MultiDimensionIssue54(out double[,] x)
Expand Down