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

#945509 Support ref readonly parameters #710

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions mdoc/Consts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static class Consts
public const string CompilerGeneratedAttribute = "System.Runtime.CompilerServices.CompilerGeneratedAttribute";
public const string IsByRefLikeAttribute = "System.Runtime.CompilerServices.IsByRefLikeAttribute";
public const string IsReadOnlyAttribute = "System.Runtime.CompilerServices.IsReadOnlyAttribute";
public const string RequiresLocationAttribute = "System.Runtime.CompilerServices.RequiresLocationAttribute";
public const string InAttribute = "System.Runtime.InteropServices.InAttribute";
public const string OutAttribute = "System.Runtime.InteropServices.OutAttribute";
public const string TupleElementNamesAttribute = "System.Runtime.CompilerServices.TupleElementNamesAttribute";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ private StringBuilder AppendParameters (StringBuilder buf, MethodDefinition meth
protected override StringBuilder AppendParameter(StringBuilder buf, ParameterDefinition parameter)
{
TypeReference parameterType = parameter.ParameterType;
var refType = new BitArray(3);
var refType = new BitArray(4);

if (parameter.HasCustomAttributes)
{
Expand Down Expand Up @@ -641,14 +641,18 @@ protected override StringBuilder AppendParameter(StringBuilder buf, ParameterDef
{
refType.Set(0, true);
}
else if (parameter.IsIn && DocUtils.HasCustomAttribute(parameter, Consts.RequiresLocationAttribute))
{
refType.Set(3, true);
}
else
{
refType.Set(2, true);
}
parameterType = byReferenceType.ElementType;
}

buf.Append(refType.Get(0) ? "in " : (refType.Get(1) ? "out " : (refType.Get(2) ? "ref ": "")));
buf.Append(refType.Get(0) ? "in " : (refType.Get(1) ? "out " : (refType.Get(2) ? "ref ": (refType.Get(3) ? "ref readonly " : ""))));

if (parameter.HasCustomAttributes)
{
Expand Down
8 changes: 8 additions & 0 deletions mdoc/mdoc.Test/FormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,14 @@ public void CSharpStaticEventImplementation(string typeFullName, string eventNam
TestEventSignature(staticVirtualMemberDllPath, typeFullName, eventName, expectedSignature);
}

[TestCase("MethodWithRefReadonlyParam", "public void MethodWithRefReadonlyParam (ref readonly int i);")]
public void CSharpRefReadonlyTest(string methodName, string expectedSignature)
{
var method = GetMethod(typeof(SampleClasses.SomeClass), m => m.Name == methodName);
var methodSignature = formatter.GetDeclaration(method);
Assert.AreEqual(expectedSignature, methodSignature);
}

#region Helper Methods
string RealTypeName(string name){
switch (name) {
Expand Down
4 changes: 4 additions & 0 deletions mdoc/mdoc.Test/SampleClasses/SomeClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ public ref T TestScopedParams<T>(scoped in T p1, scoped out T p2, scoped ref T p
throw new PlatformNotSupportedException();
}

public void MethodWithRefReadonlyParam(ref readonly int i)
{
}

public event EventHandler<object> AppMemoryUsageIncreased;
public static event EventHandler<object> StaticEvent;
private static event EventHandler<object> PrivateEvent;
Expand Down