-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Validate generic constraints in ILLink tests #103452
Conversation
And add test for byref-like types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you!
...illink/test/Mono.Linker.Tests.Cases/Attributes/OnlyKeepUsed/MethodWithUnmanagedConstraint.cs
Outdated
Show resolved
Hide resolved
.../illink/test/Mono.Linker.Tests.Cases/Attributes/OnlyKeepUsed/NullableOnConstraintsRemoved.cs
Outdated
Show resolved
Hide resolved
...ests.Cases/Inheritance.Interfaces/OnValueType/StructWithNestedStructImplementingInterface.cs
Outdated
Show resolved
Hide resolved
...Cases/Inheritance.Interfaces/StaticInterfaceMethods/StaticAbstractInterfaceMethodsLibrary.cs
Outdated
Show resolved
Hide resolved
.../test/Mono.Linker.Tests.Cases/Inheritance.Interfaces/StaticInterfaceMethods/VarianceBasic.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Jackson Schuster <36744439+jtschuster@users.noreply.github.com>
...illink/test/Mono.Linker.Tests.Cases/Attributes/OnlyKeepUsed/MethodWithUnmanagedConstraint.cs
Outdated
Show resolved
Hide resolved
…eepUsed/MethodWithUnmanagedConstraint.cs
@gooroodev can you review this please? |
@oleksandr-vonahi, thanks for summoning me!
Summary of Changes
Identified Issues
Code Snippet for Fixing Identified IssuesIssue 1: Simplify File: IEnumerable<string> VerifyGenericParameterConstraints (GenericParameter src, GenericParameter linked)
{
if (src.HasConstraints != linked.HasConstraints) {
yield return $"Mismatch in generic parameter constraints on {src} of {src.Owner}. Input has constraints?: {src.HasConstraints}, Output has constraints?: {linked.HasConstraints}";
yield break;
}
if (!src.HasConstraints)
yield break;
if (src.Constraints.Count != linked.Constraints.Count) {
yield return $"Mismatch in generic parameter constraint count on {src} of {src.Owner}. Input has {src.Constraints.Count} constraints, Output has {linked.Constraints.Count} constraints";
yield break;
}
for (int i = 0; i < src.Constraints.Count; i++) {
var srcConstraint = src.Constraints[i];
var linkedConstraint = linked.Constraints[i];
if (srcConstraint.ConstraintType.FullName != linkedConstraint.ConstraintType.FullName) {
yield return $"Mismatch in generic parameter constraint type. {src} constraint {i} is {srcConstraint.ConstraintType.FullName}, {linked} constraint {i} is {linkedConstraint.ConstraintType.FullName}";
}
}
var expectedConstraintAttributes = src.CustomAttributes
.Where (a => IsKeptAttributeOnConstraint (a))
.Select (a => (a.ConstructorArguments[0].Value.ToString (), a.ConstructorArguments[1].Value.ToString ()))
.ToHashSet ();
var linkedConstraintAttributes = linked.Constraints
.Where (c => c.HasCustomAttributes)
.SelectMany (c => c.CustomAttributes.Select (a => (c.ConstraintType.FullName, a.AttributeType.FullName)))
.ToHashSet ();
if (!expectedConstraintAttributes.SetEquals (linkedConstraintAttributes)) {
var missing = $"Missing: {string.Join (", ", expectedConstraintAttributes.Except (linkedConstraintAttributes).Select (c => $"{c.AttributeType} on {c.ConstraintType}"))}";
var extra = $"Extra: {string.join (", ", linkedConstraintAttributes.Except (expectedConstraintAttributes).Select (c => $"{c.AttributeType} on {c.ConstraintType}"))}";
yield return string.Join (Environment.NewLine, $"Custom attributes on `{src}' generic parameter constraints are not matching:", missing, extra);
}
static bool IsKeptAttributeOnConstraint (CustomAttribute attr) {
if (attr.AttributeType.Name != nameof (KeptAttributeOnConstraintAttribute))
return false;
if (attr.ConstructorArguments.Count != 2)
throw new NotImplementedException ("Unexpected KeptCustomAttributeOnConstraintAttribute ctor variant");
return true;
}
} Explanation:
General ReviewThe code quality and style are generally good, focusing on improving the validation of generic constraints. The added attributes enhance the clarity of the constraints on generic parameters. The main issue identified is the complexity of the -- |
My first AI code review, interesting! Looks like the suggested diff is: +1195,6 @@ IEnumerable<string> VerifyGenericParameterConstraints (GenericParameter src, Gen
yield break;
}
- // ILLink doesn't rewrite generic parameter constraint types, so just check they are identical to inputs.
for (int i = 0; i < src.Constraints.Count; i++) {
var srcConstraint = src.Constraints[i];
var linkedConstraint = linked.Constraints[i];
@@ -1204,21 +1203,19 @@ IEnumerable<string> VerifyGenericParameterConstraints (GenericParameter src, Gen
}
}
- // C# doesn't have syntax for annotating generic parameter constraints with arbitrary attributes,
- // so expected attributes on generic parameter constraints are specified on the generic parameter itself.
- HashSet<(string ConstraintType, string AttributeType)> expectedConstraintAttributes = src.CustomAttributes
+ var expectedConstraintAttributes = src.CustomAttributes
.Where (a => IsKeptAttributeOnConstraint (a))
.Select (a => (a.ConstructorArguments[0].Value.ToString (), a.ConstructorArguments[1].Value.ToString ()))
.ToHashSet ();
- HashSet<(string ConstraintType, string AttributeType)> linkedConstraintAttributes = linked.Constraints
+ var linkedConstraintAttributes = linked.Constraints
.Where (c => c.HasCustomAttributes)
.SelectMany (c => c.CustomAttributes.Select (a => (c.ConstraintType.FullName, a.AttributeType.FullName)))
.ToHashSet ();
if (!expectedConstraintAttributes.SetEquals (linkedConstraintAttributes)) {
var missing = $"Missing: {string.Join (", ", expectedConstraintAttributes.Except (linkedConstraintAttributes).Select (c => $"{c.AttributeType} on {c.ConstraintType}"))}";
- var extra = $"Extra: {string.Join (", ", linkedConstraintAttributes.Except (expectedConstraintAttributes).Select (c => $"{c.Attribute
Type} on {c.ConstraintType}"))}";
+ var extra = $"Extra: {string.join (", ", linkedConstraintAttributes.Except (expectedConstraintAttributes).Select (c => $"{c.Attribute
Type} on {c.ConstraintType}"))}";
yield return string.Join (Environment.NewLine, $"Custom attributes on `{src}' generic parameter constraints are not matching:", missi
ng, extra);
} It removed comments despite saying
And introduced a typo ( I prefer to keep the explicit types since I think it makes it clearer what is being compared. |
And add a test for byref-like types.
Closes #98519