Skip to content

Commit

Permalink
#945509 Support ref readonly parameters
Browse files Browse the repository at this point in the history
Add support for correct rendering of "ref readonly" parameter in C#
method signature documentation.
  • Loading branch information
nickwalkmsft committed Sep 11, 2024
1 parent 97940aa commit b8d8963
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
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

0 comments on commit b8d8963

Please sign in to comment.