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

Open
wants to merge 13 commits 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
Binary file added external/Test/AllowsRefStructDemo.dll
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@huangmin-ms @v-fuquanli Is it not required that the code used for the test cases be compiled as part of the project build? #712 and #713 are more or less hung up on assuming that's a requirement.

Binary file not shown.
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