From 87539d443b197b63b99fabf603e20811bad632c5 Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Wed, 25 May 2022 12:01:21 -0500 Subject: Warn on mismatch for ref params (#2769) --- .../DataFlow/ArrayDataFlow.cs | 8 +++--- .../DataFlow/ByRefDataflow.cs | 8 ++++-- .../DataFlow/MethodByRefParameterDataFlow.cs | 13 ++++------ .../DataFlow/MethodByRefReturnDataFlow.cs | 6 ++--- .../DataFlow/MethodOutParameterDataFlow.cs | 30 +++++++++++----------- 5 files changed, 33 insertions(+), 32 deletions(-) (limited to 'test/Mono.Linker.Tests.Cases') diff --git a/test/Mono.Linker.Tests.Cases/DataFlow/ArrayDataFlow.cs b/test/Mono.Linker.Tests.Cases/DataFlow/ArrayDataFlow.cs index 7331f81ac..2caf1fc83 100644 --- a/test/Mono.Linker.Tests.Cases/DataFlow/ArrayDataFlow.cs +++ b/test/Mono.Linker.Tests.Cases/DataFlow/ArrayDataFlow.cs @@ -226,15 +226,15 @@ namespace Mono.Linker.Tests.Cases.DataFlow // https://github.com/dotnet/linker/issues/2680 - analyzer doesn't reset array in this case [ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicFields), ProducedBy = ProducedBy.Trimmer)] - // https://github.com/dotnet/linker/issues/2632 - Ref params don't reset or track. - // [ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods))] + // https://github.com/dotnet/linker/issues/2680 - analyzer doesn't reset array in this case + [ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods), ProducedBy = ProducedBy.Trimmer)] static void TestArrayResetGetElementOnByRefArray (int i = 0) { Type[] arr = new Type[] { typeof (TestType), typeof (TestType) }; arr[0].RequiresPublicProperties (); - TakesTypeByRef (ref arr[0]); // Should reset index 0 - linker doesn't - arr[0].RequiresPublicMethods (); // Should warn -- linker doesn't + TakesTypeByRef (ref arr[0]); // Should reset index 0 - analyzer doesn't + arr[0].RequiresPublicMethods (); // Should warn - analyzer doesn't arr[1].RequiresPublicMethods (); // Shouldn't warn TakesTypeByRef (ref arr[i]); // Reset - unknown index diff --git a/test/Mono.Linker.Tests.Cases/DataFlow/ByRefDataflow.cs b/test/Mono.Linker.Tests.Cases/DataFlow/ByRefDataflow.cs index efc270ce1..3d47170d6 100644 --- a/test/Mono.Linker.Tests.Cases/DataFlow/ByRefDataflow.cs +++ b/test/Mono.Linker.Tests.Cases/DataFlow/ByRefDataflow.cs @@ -2,9 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Text; using Mono.Linker.Tests.Cases.Expectations.Assertions; using Mono.Linker.Tests.Cases.Expectations.Metadata; @@ -44,9 +42,15 @@ namespace Mono.Linker.Tests.Cases.DataFlow // Trimmer and analyzer use different formats for ref parameters: https://github.com/dotnet/linker/issues/2406 [ExpectedWarning ("IL2077", nameof (ByRefDataflow) + "." + nameof (MethodWithRefParameter) + "(Type&)", ProducedBy = ProducedBy.Trimmer)] [ExpectedWarning ("IL2077", nameof (ByRefDataflow) + "." + nameof (MethodWithRefParameter) + "(ref Type)", ProducedBy = ProducedBy.Analyzer)] + [ExpectedWarning ("IL2069", nameof (s_typeWithPublicParameterlessConstructor), "parameter 'type'", nameof (MethodWithRefParameter), ProducedBy = ProducedBy.Trimmer)] + // MethodWithRefParameter (ref x) + [ExpectedWarning ("IL2077", nameof (ByRefDataflow) + "." + nameof (MethodWithRefParameter) + "(Type&)", ProducedBy = ProducedBy.Trimmer)] + [ExpectedWarning ("IL2077", nameof (ByRefDataflow) + "." + nameof (MethodWithRefParameter) + "(ref Type)", ProducedBy = ProducedBy.Analyzer)] public static void PassRefToField () { MethodWithRefParameter (ref s_typeWithPublicParameterlessConstructor); + var x = s_typeWithPublicParameterlessConstructor; + MethodWithRefParameter (ref x); } [Kept] diff --git a/test/Mono.Linker.Tests.Cases/DataFlow/MethodByRefParameterDataFlow.cs b/test/Mono.Linker.Tests.Cases/DataFlow/MethodByRefParameterDataFlow.cs index 16e3dca37..2b2726541 100644 --- a/test/Mono.Linker.Tests.Cases/DataFlow/MethodByRefParameterDataFlow.cs +++ b/test/Mono.Linker.Tests.Cases/DataFlow/MethodByRefParameterDataFlow.cs @@ -87,9 +87,7 @@ namespace Mono.Linker.Tests.Cases.DataFlow typeWithMethods.RequiresPublicMethods (); } - // https://github.com/dotnet/linker/issues/2632 - // This test should generate a warning since there's mismatch on annotations - // [ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicFields))] + [ExpectedWarning ("IL2067", nameof (TryGetAnnotatedValue), "RequiresPublicFields")] static void TestReadFromRefParameter_MismatchOnOutput () { Type typeWithMethods = null; @@ -97,9 +95,7 @@ namespace Mono.Linker.Tests.Cases.DataFlow typeWithMethods.RequiresPublicFields (); } - // https://github.com/dotnet/linker/issues/2632 - // This test should generate a warning since there's mismatch on annotations - // [ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicFields))] + [ExpectedWarning ("IL2067", nameof (TryGetAnnotatedValue), "RequiresPublicFields")] static void TestReadFromRefParameter_MismatchOnOutput_PassedTwice () { Type typeWithMethods = null; @@ -111,7 +107,7 @@ namespace Mono.Linker.Tests.Cases.DataFlow // https://github.com/dotnet/linker/issues/2632 // This second warning should not be generated, the value of typeWithMethods should have PublicMethods // after the call with out parameter. - [ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresPublicMethods))] + [ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresPublicMethods), ProducedBy = ProducedBy.Analyzer)] static void TestReadFromRefParameter_MismatchOnInput () { Type typeWithMethods = GetTypeWithFields (); @@ -124,7 +120,7 @@ namespace Mono.Linker.Tests.Cases.DataFlow // https://github.com/dotnet/linker/issues/2632 // This third warning should not be generated, the value of typeWithMethods should have PublicMethods // after the call with ref parameter. - [ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresPublicMethods))] + [ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresPublicMethods), ProducedBy = ProducedBy.Analyzer)] static void TestReadFromRefParameter_MismatchOnInput_PassedTwice () { Type typeWithMethods = GetTypeWithFields (); @@ -137,6 +133,7 @@ namespace Mono.Linker.Tests.Cases.DataFlow TryGetAnnotatedValue (ref typeWithMethods); } + [ExpectedWarning ("IL2067", "typeWithMethods", nameof (TryGetAnnotatedValue))] [ExpectedWarning ("IL2067", "typeWithMethods", nameof (TryGetAnnotatedValue))] static void TestPassingRefParameter_Mismatch ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] ref Type typeWithMethods) { diff --git a/test/Mono.Linker.Tests.Cases/DataFlow/MethodByRefReturnDataFlow.cs b/test/Mono.Linker.Tests.Cases/DataFlow/MethodByRefReturnDataFlow.cs index d47bf1c5e..dc1783482 100644 --- a/test/Mono.Linker.Tests.Cases/DataFlow/MethodByRefReturnDataFlow.cs +++ b/test/Mono.Linker.Tests.Cases/DataFlow/MethodByRefReturnDataFlow.cs @@ -25,14 +25,14 @@ namespace Mono.Linker.Tests.Cases.DataFlow // 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--")] + // Correct behavior in the linker, but needs to be added in analyzer + // Bug link: https://github.com/dotnet/linker/issues/2158 + [ExpectedWarning ("IL2026", "Message for --TestType.Requires--", ProducedBy = ProducedBy.Trimmer)] static void AssignToAnnotatedTypeReference () { ref Type typeShouldHaveAllMethods = ref ReturnAnnotatedTypeReferenceAsAnnotated (); diff --git a/test/Mono.Linker.Tests.Cases/DataFlow/MethodOutParameterDataFlow.cs b/test/Mono.Linker.Tests.Cases/DataFlow/MethodOutParameterDataFlow.cs index cd9a2c301..86f2f2feb 100644 --- a/test/Mono.Linker.Tests.Cases/DataFlow/MethodOutParameterDataFlow.cs +++ b/test/Mono.Linker.Tests.Cases/DataFlow/MethodOutParameterDataFlow.cs @@ -23,9 +23,15 @@ namespace Mono.Linker.Tests.Cases.DataFlow TestInitializedReadFromOutParameter_MismatchOnOutput_PassedTwice (); TestInitializedReadFromOutParameter_MismatchOnInput (); TestInitializedReadFromOutParameter_MismatchOnInput_PassedTwice (); - TestPassingOutParameter (out t); + // Gets Fields TestPassingOutParameter_Mismatch (out t); + t.RequiresPublicFields (); + // Gets Methods + TestPassingOutParameter (out t); + // Needs Methods and gets Methods TestAssigningToOutParameter (t, out t); + t = typeof (int); + // Needs Fields and gets Methods TestAssigningToOutParameter_Mismatch (t, out t); } @@ -43,10 +49,6 @@ namespace Mono.Linker.Tests.Cases.DataFlow typeWithMethods.RequiresPublicMethods (); } - // https://github.com/dotnet/linker/issues/2632 - // These two warnings should not be generated, the annotations are all correct - [ExpectedWarning ("IL2062", nameof (TryGetAnnotatedValue))] - [ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicMethods))] static void TestUninitializedReadFromOutParameter () { Type typeWithMethods; @@ -54,9 +56,7 @@ namespace Mono.Linker.Tests.Cases.DataFlow typeWithMethods.RequiresPublicMethods (); } - // https://github.com/dotnet/linker/issues/2632 - // This test should generate a warning since there's mismatch on annotations - // [ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicFields))] + [ExpectedWarning ("IL2067", nameof (DataFlowTypeExtensions.RequiresPublicFields))] static void TestInitializedReadFromOutParameter_MismatchOnOutput () { Type typeWithMethods = null; @@ -66,7 +66,7 @@ namespace Mono.Linker.Tests.Cases.DataFlow // https://github.com/dotnet/linker/issues/2632 // This test should generate a warning since there's mismatch on annotations - // [ExpectedWarning ("IL2062", nameof (DataFlowTypeExtensions.RequiresPublicFields))] + [ExpectedWarning ("IL2067", nameof (DataFlowTypeExtensions.RequiresPublicFields))] static void TestInitializedReadFromOutParameter_MismatchOnOutput_PassedTwice () { Type typeWithMethods = null; @@ -74,27 +74,27 @@ namespace Mono.Linker.Tests.Cases.DataFlow typeWithMethods.RequiresPublicFields (); } - [ExpectedWarning ("IL2072", nameof (TryGetAnnotatedValue))] // https://github.com/dotnet/linker/issues/2632 - // This second warning should not be generated, the value of typeWithMethods should have PublicMethods + // This warning should not be generated, the value of typeWithMethods should have PublicMethods // after the call with out parameter. - [ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresPublicMethods))] + [ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresPublicMethods), ProducedBy = ProducedBy.Analyzer)] static void TestInitializedReadFromOutParameter_MismatchOnInput () { Type typeWithMethods = GetTypeWithFields (); + // No warning on out parameter TryGetAnnotatedValue (out typeWithMethods); typeWithMethods.RequiresPublicMethods (); } - [ExpectedWarning ("IL2072", nameof (TryGetAnnotatedValueFromValue))] [ExpectedWarning ("IL2072", nameof (TryGetAnnotatedValueFromValue))] // https://github.com/dotnet/linker/issues/2632 - // This third warning should not be generated, the value of typeWithMethods should have PublicMethods + // This warning should not be generated, the value of typeWithMethods should have PublicMethods // after the call with out parameter. - [ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresPublicMethods))] + [ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresPublicMethods), ProducedBy = ProducedBy.Analyzer)] static void TestInitializedReadFromOutParameter_MismatchOnInput_PassedTwice () { Type typeWithMethods = GetTypeWithFields (); + // Warn on first parameter only, not on out parameter TryGetAnnotatedValueFromValue (typeWithMethods, out typeWithMethods); typeWithMethods.RequiresPublicMethods (); } -- cgit v1.2.3