From 33c3b2c60d0a2006162a6326db853fe5415439bd Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Mon, 15 Aug 2022 11:21:45 -0700 Subject: [PATCH 1/5] Add MakeGenericMethod tests for static interface methods (#2962) --- .../DataFlow/MakeGenericDataFlow.cs | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/test/Mono.Linker.Tests.Cases/DataFlow/MakeGenericDataFlow.cs b/test/Mono.Linker.Tests.Cases/DataFlow/MakeGenericDataFlow.cs index 262e9b3f72b4..9e4df4cbcc51 100644 --- a/test/Mono.Linker.Tests.Cases/DataFlow/MakeGenericDataFlow.cs +++ b/test/Mono.Linker.Tests.Cases/DataFlow/MakeGenericDataFlow.cs @@ -351,6 +351,7 @@ public static void Test () TestWithMultipleArgumentsWithRequirements (); + StaticInterfaceMethods.Test (); NewConstraint.Test (); StructConstraint.Test (); UnmanagedConstraint.Test (); @@ -631,7 +632,6 @@ static void TestWithRequirementsFromGenericParam< .MakeGenericMethod (typeof (T)); } - static void TestWithRequirementsViaRuntimeMethod () { typeof (MakeGenericMethod).GetRuntimeMethod (nameof (GenericWithRequirements), Type.EmptyTypes) @@ -750,6 +750,36 @@ static void GenericWithMultipleArgumentsWithRequirements< { } + class StaticInterfaceMethods + { + public static void Test () + { + KnownType (); + UnannotatedGenericParam (); + AnnotatedGenericParam (); + } + + static MethodInfo KnownType () + => typeof (IFoo).GetMethod ("Method") + .MakeGenericMethod (new Type[] { typeof (int) }); + + [ExpectedWarning ("IL2090", "T", "PublicMethods")] + static MethodInfo UnannotatedGenericParam () + => typeof (IFoo).GetMethod ("Method") + .MakeGenericMethod (new Type[] { typeof (T) }); + + static MethodInfo AnnotatedGenericParam< + [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] T> () + => typeof (IFoo).GetMethod ("Method") + .MakeGenericMethod (new Type[] { typeof (T) }); + + interface IFoo + { + static abstract T Method< + [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] T> (); + } + } + class NewConstraint { static void GenericWithNewConstraint () where T : new() From c710e8af4224a3d0d2975ba5c2b09f0398ee2383 Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Mon, 15 Aug 2022 12:07:47 -0700 Subject: [PATCH 2/5] Add IL Verification to tests (#2960) Adds an ILVerifier to check that the IL produced by the linker is valid. Unsafe C# produced unverifiable code, so we skip verification when we pass that flag to the compiler. Also, there are a few warnings that are produced by valid C# with new features like static abstract interface methods and ref fields and ref returns. In the future, it may be nice to add better error messages with the type, method name, and IL offset that produced the error, and perhaps an [ExpectedILVerifyError] attribute instead of filtering all of a type of error, but those are non-trivial to implement and don't occur in many tests (<10), so I haven't done that yet. --- eng/Versions.props | 1 + .../Mono.Linker.Tests.csproj | 3 +- .../TestCasesRunner/ILVerifier.cs | 132 ++++++++++++++++++ .../TestCasesRunner/ResultChecker.cs | 62 +++++++- 4 files changed, 194 insertions(+), 4 deletions(-) create mode 100644 test/Mono.Linker.Tests/TestCasesRunner/ILVerifier.cs diff --git a/eng/Versions.props b/eng/Versions.props index ea29c2eae231..cef1af03cc2b 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -25,6 +25,7 @@ $(MicrosoftCodeAnalysisVersion) 1.0.1-beta1.* 3.3.2 + 7.0.0-preview.7.22375.6 diff --git a/test/Mono.Linker.Tests/Mono.Linker.Tests.csproj b/test/Mono.Linker.Tests/Mono.Linker.Tests.csproj index 4a9ea7e7f2ce..448601a8a643 100644 --- a/test/Mono.Linker.Tests/Mono.Linker.Tests.csproj +++ b/test/Mono.Linker.Tests/Mono.Linker.Tests.csproj @@ -1,4 +1,4 @@ - + true @@ -32,6 +32,7 @@ +