Skip to content

Constraints on parameter types allow ref struct #727

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 14 commits into from
May 8, 2025
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
Binary file added external/Test/AllowsRefStructDemo.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion mdoc/Consts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Mono.Documentation
{
public static class Consts
{
public static string MonoVersion = "5.9.3.8";
public static string MonoVersion = "5.9.4";
public const string DocId = "DocId";
public const string CppCli = "C++ CLI";
public const string CppCx = "C++ CX";
Expand Down
3 changes: 3 additions & 0 deletions mdoc/Mono.Documentation/MDocUpdater.Member.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ internal static void MakeTypeParameterConstraints(XmlElement root, XmlElement e,
AppendElementText(ce, "ParameterAttribute", "NotNullableValueTypeConstraint");
if ((attrs & GenericParameterAttributes.ReferenceTypeConstraint) != 0)
AppendElementText(ce, "ParameterAttribute", "ReferenceTypeConstraint");
// Check for 'allows ref struct' constraint
if ((attrs & (GenericParameterAttributes)0x0020) != 0) // Assuming 0x0020 is the flag for 'allows ref struct'
AppendElementText(ce, "ParameterAttribute", "AllowByRefLike");

#if NEW_CECIL
foreach (GenericParameterConstraint c in constraints)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,10 @@ private StringBuilder AppendConstraints (StringBuilder buf, IList<GenericParamet
bool isref = (attrs & GenericParameterAttributes.ReferenceTypeConstraint) != 0;
bool isvt = (attrs & GenericParameterAttributes.NotNullableValueTypeConstraint) != 0;
bool isnew = (attrs & GenericParameterAttributes.DefaultConstructorConstraint) != 0;
bool isAllowsRefStruct = (attrs & (GenericParameterAttributes)0x0020) != 0; // Assuming 0x0020 is the flag for 'allows ref struct'
bool comma = false;

if (!isref && !isvt && !isnew && constraints.Count == 0)
if (!isref && !isvt && !isAllowsRefStruct && !isnew && constraints.Count == 0)
continue;
buf.Append (" where ").Append (genArg.Name).Append (" : ");
if (isref)
Expand Down Expand Up @@ -350,6 +351,14 @@ private StringBuilder AppendConstraints (StringBuilder buf, IList<GenericParamet
buf.Append (", ");
buf.Append ("new()");
}

// Handle 'allows ref struct' constraint
if (isAllowsRefStruct)
{
if (comma || constraints.Count > 0 || isnew)
buf.Append(", ");
buf.Append("allows ref struct");
}
}
return buf;
}
Expand Down
18 changes: 18 additions & 0 deletions mdoc/mdoc.Test/FormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,24 @@ public void CSharpStaticEventImplementation(string typeFullName, string eventNam
TestEventSignature(staticVirtualMemberDllPath, typeFullName, eventName, expectedSignature);
}

[TestCase("AllowsRefStructDemo.IRefStructProcessor`1",
"public interface IRefStructProcessor<T> where T : allows ref struct")]
public void CSharpAllowsRefStructForTypeTest(string typeFullName, string expectedSignature)
{
var allowsRefStructDllPath = "../../../../external/Test/AllowsRefStructDemo.dll";
TestTypeSignature(allowsRefStructDllPath, typeFullName, expectedSignature);
}

[TestCase("AllowsRefStructDemo.Immutable", "Update",
"public bool Update<TArg> (TArg transformerArgument) where TArg : new(), allows ref struct;")]
[TestCase("AllowsRefStructDemo.RefStructHandler", "Handle",
"public void Handle<T> (ref T item) where T : new(), allows ref struct;")]
public void CSharpAllowsRefStructForMemberTest(string typeFullName, string methodName, string expectedSignature)
{
var allowsRefStructDllPath = "../../../../external/Test/AllowsRefStructDemo.dll";
TestMethodSignature(allowsRefStructDllPath, typeFullName, methodName, expectedSignature);
}

#region Helper Methods
string RealTypeName(string name){
switch (name) {
Expand Down
2 changes: 1 addition & 1 deletion mdoc/mdoc.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>mdoc</id>
<version>5.9.3.8</version>
<version>5.9.4</version>
<title>mdoc</title>
<authors>Microsoft</authors>
<owners>Microsoft</owners>
Expand Down