Skip to content

Commit a4bac65

Browse files
committed
#945509 Support ref readonly parameters
Add support for correct rendering of "ref readonly" parameter in C# method signature documentation.
1 parent 3944f3b commit a4bac65

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

mdoc/Consts.cs

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public static class Consts
4949
public const string CompilerGeneratedAttribute = "System.Runtime.CompilerServices.CompilerGeneratedAttribute";
5050
public const string IsByRefLikeAttribute = "System.Runtime.CompilerServices.IsByRefLikeAttribute";
5151
public const string IsReadOnlyAttribute = "System.Runtime.CompilerServices.IsReadOnlyAttribute";
52+
public const string RequiresLocationAttribute = "System.Runtime.CompilerServices.RequiresLocationAttribute";
5253
public const string InAttribute = "System.Runtime.InteropServices.InAttribute";
5354
public const string OutAttribute = "System.Runtime.InteropServices.OutAttribute";
5455
public const string TupleElementNamesAttribute = "System.Runtime.CompilerServices.TupleElementNamesAttribute";

mdoc/Mono.Documentation/Updater/Formatters/CSharpFullMemberFormatter.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ private StringBuilder AppendParameters (StringBuilder buf, MethodDefinition meth
610610
protected override StringBuilder AppendParameter(StringBuilder buf, ParameterDefinition parameter)
611611
{
612612
TypeReference parameterType = parameter.ParameterType;
613-
var refType = new BitArray(3);
613+
var refType = new BitArray(4);
614614

615615
if (parameter.HasCustomAttributes)
616616
{
@@ -641,14 +641,18 @@ protected override StringBuilder AppendParameter(StringBuilder buf, ParameterDef
641641
{
642642
refType.Set(0, true);
643643
}
644+
else if (parameter.IsIn && DocUtils.HasCustomAttribute(parameter, Consts.RequiresLocationAttribute))
645+
{
646+
refType.Set(3, true);
647+
}
644648
else
645649
{
646650
refType.Set(2, true);
647651
}
648652
parameterType = byReferenceType.ElementType;
649653
}
650654

651-
buf.Append(refType.Get(0) ? "in " : (refType.Get(1) ? "out " : (refType.Get(2) ? "ref ": "")));
655+
buf.Append(refType.Get(0) ? "in " : (refType.Get(1) ? "out " : (refType.Get(2) ? "ref ": (refType.Get(3) ? "ref readonly " : ""))));
652656

653657
if (parameter.HasCustomAttributes)
654658
{

mdoc/mdoc.Test/FormatterTests.cs

+8
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,14 @@ public void CSharpStaticEventImplementation(string typeFullName, string eventNam
601601
TestEventSignature(staticVirtualMemberDllPath, typeFullName, eventName, expectedSignature);
602602
}
603603

604+
[TestCase("MethodWithRefReadonlyParam", "public void MethodWithRefReadonlyParam (ref readonly int i);")]
605+
public void CSharpRefReadonlyTest(string methodName, string expectedSignature)
606+
{
607+
var method = GetMethod(typeof(SampleClasses.SomeClass), m => m.Name == methodName);
608+
var methodSignature = formatter.GetDeclaration(method);
609+
Assert.AreEqual(expectedSignature, methodSignature);
610+
}
611+
604612
#region Helper Methods
605613
string RealTypeName(string name){
606614
switch (name) {

mdoc/mdoc.Test/SampleClasses/SomeClass.cs

+4
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ public ref T TestScopedParams<T>(scoped in T p1, scoped out T p2, scoped ref T p
104104
throw new PlatformNotSupportedException();
105105
}
106106

107+
public void MethodWithRefReadonlyParam(ref readonly int i)
108+
{
109+
}
110+
107111
public event EventHandler<object> AppMemoryUsageIncreased;
108112
public static event EventHandler<object> StaticEvent;
109113
private static event EventHandler<object> PrivateEvent;

0 commit comments

Comments
 (0)