From 42460a6b2cbed0d9c09e1c28fd3bb753bdc3f99f Mon Sep 17 00:00:00 2001 From: Vitek Karas <10670590+vitek-karas@users.noreply.github.com> Date: Wed, 1 Dec 2021 12:11:49 +0100 Subject: Add some tests for by ref type data flow (#2407) --- .../DataFlow/MethodByRefParameterDataFlow.cs | 65 ++++++++++++++++++++++ .../DataFlow/MethodByRefReturnDataFlow.cs | 49 ++++++++++++++++ .../DataFlow/MethodParametersDataFlow.cs | 12 ++++ 3 files changed, 126 insertions(+) create mode 100644 test/Mono.Linker.Tests.Cases/DataFlow/MethodByRefParameterDataFlow.cs create mode 100644 test/Mono.Linker.Tests.Cases/DataFlow/MethodByRefReturnDataFlow.cs (limited to 'test/Mono.Linker.Tests.Cases/DataFlow') diff --git a/test/Mono.Linker.Tests.Cases/DataFlow/MethodByRefParameterDataFlow.cs b/test/Mono.Linker.Tests.Cases/DataFlow/MethodByRefParameterDataFlow.cs new file mode 100644 index 000000000..974947e79 --- /dev/null +++ b/test/Mono.Linker.Tests.Cases/DataFlow/MethodByRefParameterDataFlow.cs @@ -0,0 +1,65 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Diagnostics.CodeAnalysis; +using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Helpers; +using Mono.Linker.Tests.Cases.Expectations.Metadata; + +namespace Mono.Linker.Tests.Cases.DataFlow +{ + [SkipKeptItemsValidation] + [ExpectedNoWarnings] + class MethodByRefParameterDataFlow + { + public static void Main () + { + Type typeWithMethods = _fieldWithMethods; + + TestAssignStaticToAnnotatedRefParameter (ref typeWithMethods); + TestAssignParameterToAnnotatedRefParameter (ref typeWithMethods, typeof (TestType)); + } + + [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] + static Type _fieldWithMethods = null; + + [ExpectedWarning ("IL2026", "Message for --TestType.Requires--")] + + // https://github.com/dotnet/linker/issues/2158 + // The type.GetMethods call generates a warning because we're not able to correctly track the value of the "this". + // (there's a ldind.ref insruction here which we currently don't handle and the "this" becomes unknown) + [ExpectedWarning ("IL2065")] + static void TestAssignStaticToAnnotatedRefParameter ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] ref Type type) + { + type = typeof (TestTypeWithRequires); + type.GetMethods (); // Should not warn + } + + // The warning message is REALLY confusing (basically wrong) since it talks about "calling the method with wrong argument" + // which is definitely not the case here. + [ExpectedWarning ("IL2067", "typeWithFields")] + + // https://github.com/dotnet/linker/issues/2158 + // The type.GetMethods call generates a warning because we're not able to correctly track the value of the "this". + // (there's a ldind.ref insruction here which we currently don't handle and the "this" becomes unknown) + [ExpectedWarning ("IL2065")] + static void TestAssignParameterToAnnotatedRefParameter ( + [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] ref Type type, + [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] Type typeWithFields) + { + type = typeWithFields; // Should warn + type.GetMethods (); // Should not warn + } + + class TestTypeWithRequires + { + [RequiresUnreferencedCode ("Message for --TestType.Requires--")] + public static void Requires () { } + } + + class TestType + { + } + } +} \ No newline at end of file diff --git a/test/Mono.Linker.Tests.Cases/DataFlow/MethodByRefReturnDataFlow.cs b/test/Mono.Linker.Tests.Cases/DataFlow/MethodByRefReturnDataFlow.cs new file mode 100644 index 000000000..04fa56f26 --- /dev/null +++ b/test/Mono.Linker.Tests.Cases/DataFlow/MethodByRefReturnDataFlow.cs @@ -0,0 +1,49 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Diagnostics.CodeAnalysis; +using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Helpers; +using Mono.Linker.Tests.Cases.Expectations.Metadata; + +namespace Mono.Linker.Tests.Cases.DataFlow +{ + [SkipKeptItemsValidation] + [ExpectedNoWarnings] + public class MethodByRefReturnDataFlow + { + public static void Main () + { + ReturnAnnotatedTypeReferenceAsUnannotated (); + AssignToAnnotatedTypeReference (); + } + + [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] + static Type _annotatedField; + + // This should warn, as assiging to the return ref Type will assign value to the annotated field + // but the annotation is not propagated + // https://github.com/dotnet/linker/issues/2158 + // [ExpectedWarning("IL????")] + static ref Type ReturnAnnotatedTypeReferenceAsUnannotated () { return ref _annotatedField; } + + [return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] + static ref Type ReturnAnnotatedTypeReferenceAsAnnotated () { return ref _annotatedField; } + + // https://github.com/dotnet/linker/issues/2158 + // [ExpectedWarning("IL2026", "Message for --TestType.Requires--")] + static void AssignToAnnotatedTypeReference () + { + ref Type typeShouldHaveAllMethods = ref ReturnAnnotatedTypeReferenceAsAnnotated (); + typeShouldHaveAllMethods = typeof (TestTypeWithRequires); // This should apply the annotation -> cause IL2026 due to RUC method + _annotatedField.GetMethods (); // Doesn't warn, but now contains typeof(TestType) - no warning here is correct + } + + public class TestTypeWithRequires + { + [RequiresUnreferencedCode ("Message for --TestType.Requires--")] + public static void Requires () { } + } + } +} \ No newline at end of file diff --git a/test/Mono.Linker.Tests.Cases/DataFlow/MethodParametersDataFlow.cs b/test/Mono.Linker.Tests.Cases/DataFlow/MethodParametersDataFlow.cs index f3c3486a5..df6e2cd1e 100644 --- a/test/Mono.Linker.Tests.Cases/DataFlow/MethodParametersDataFlow.cs +++ b/test/Mono.Linker.Tests.Cases/DataFlow/MethodParametersDataFlow.cs @@ -36,6 +36,8 @@ namespace Mono.Linker.Tests.Cases.DataFlow instance.WriteToParameterOnInstanceMethod (null); instance.LongWriteToParameterOnInstanceMethod (0, 0, 0, 0, null); instance.UnsupportedParameterType (null); + + TestParameterOverwrite (typeof (TestType)); } // Validate the error message when annotated parameter is passed to another annotated parameter @@ -223,6 +225,16 @@ namespace Mono.Linker.Tests.Cases.DataFlow { } + [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] + static Type _fieldWithMethods; + + [ExpectedWarning ("IL2077", nameof (_fieldWithMethods))] + static void TestParameterOverwrite ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] Type type) + { + type = _fieldWithMethods; + type.GetFields (); + } + class TestType { public TestType () { } -- cgit v1.2.3