Skip to content

Commit

Permalink
Run MethodOutParameterDataFlow tests in analyzer (dotnet#101735)
Browse files Browse the repository at this point in the history
And add a testcase to cover IL2068.

This just ensures that this particular testcase runs without
allowMissingWarnings for the analyzer, and adds one more testcase
to track the specific missing warning that was encountered in
dotnet#101203 (tracked in
dotnet#101734).
  • Loading branch information
sbomer authored and michaelgsharp committed May 8, 2024
1 parent ae8d844 commit ace22a7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ public Task MemberTypesRelationships ()
return RunTest (nameof (MemberTypesRelationships));
}

[Fact]
public Task MethodOutParameterDataFlow ()
{
return RunTest ();
}

[Fact]
public Task MethodParametersDataFlow ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ public Task MethodByRefParameterDataFlow ()
return RunTest (allowMissingWarnings: true);
}

[Fact]
public Task MethodOutParameterDataFlow ()
{
return RunTest (allowMissingWarnings: true);
}

[Fact]
public Task StaticInterfaceMethodDataflow ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static void Main ()
TestInitializedReadFromOutParameter_PassedTwice ();
TestUninitializedReadFromOutParameter ();
TestInitializedReadFromOutParameter_MismatchOnOutput ();
TestInitializedReturnOutParameter_MismatchOnOutput ();
TestInitializedReadFromOutParameter_MismatchOnOutput_PassedTwice ();
TestInitializedReadFromOutParameter_MismatchOnInput ();
TestInitializedReadFromOutParameter_MismatchOnInput_PassedTwice ();
Expand Down Expand Up @@ -55,28 +56,36 @@ static void TestUninitializedReadFromOutParameter ()
typeWithMethods.RequiresPublicMethods ();
}

[ExpectedWarning ("IL2067", nameof (DataFlowTypeExtensions.RequiresPublicFields))]
[ExpectedWarning ("IL2067", nameof (DataFlowTypeExtensions.RequiresPublicFields), Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/linker/issues/2632")]
static void TestInitializedReadFromOutParameter_MismatchOnOutput ()
{
Type typeWithMethods = null;
TryGetAnnotatedValue (out typeWithMethods);
typeWithMethods.RequiresPublicFields ();
}

// https://github.com/dotnet/linker/issues/2632
// This test should generate a warning since there's mismatch on annotations
[ExpectedWarning ("IL2067", nameof (DataFlowTypeExtensions.RequiresPublicFields))]
[ExpectedWarning ("IL2068", nameof (TryGetAnnotatedValue), nameof (DynamicallyAccessedMemberTypes.PublicFields), Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/linker/issues/2632")]
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)]
static Type TestInitializedReturnOutParameter_MismatchOnOutput ()
{
Type typeWithMethods = null;
TryGetAnnotatedValue (out typeWithMethods);
return typeWithMethods;
}

// This test should generate a warning since there's mismatch on annotations
[ExpectedWarning ("IL2067", nameof (DataFlowTypeExtensions.RequiresPublicFields), Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/linker/issues/2632")]
static void TestInitializedReadFromOutParameter_MismatchOnOutput_PassedTwice ()
{
Type typeWithMethods = null;
TryGetAnnotatedValueFromValue (typeWithMethods, out typeWithMethods);
typeWithMethods.RequiresPublicFields ();
}

// https://github.com/dotnet/linker/issues/2632
// This warning should not be generated, the value of typeWithMethods should have PublicMethods
// after the call with out parameter.
[ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresPublicMethods), Tool.Analyzer, "")]
[ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresPublicMethods), Tool.Analyzer, "https://github.com/dotnet/linker/issues/2632")]
static void TestInitializedReadFromOutParameter_MismatchOnInput ()
{
Type typeWithMethods = GetTypeWithFields ();
Expand All @@ -86,10 +95,9 @@ static void TestInitializedReadFromOutParameter_MismatchOnInput ()
}

[ExpectedWarning ("IL2072", nameof (TryGetAnnotatedValueFromValue))]
// https://github.com/dotnet/linker/issues/2632
// This warning should not be generated, the value of typeWithMethods should have PublicMethods
// after the call with out parameter.
[ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresPublicMethods), Tool.Analyzer, "")]
[ExpectedWarning ("IL2072", nameof (DataFlowTypeExtensions.RequiresPublicMethods), Tool.Analyzer, "https://github.com/dotnet/linker/issues/2632")]
static void TestInitializedReadFromOutParameter_MismatchOnInput_PassedTwice ()
{
Type typeWithMethods = GetTypeWithFields ();
Expand All @@ -103,7 +111,7 @@ static void TestPassingOutParameter ([DynamicallyAccessedMembers (DynamicallyAcc
TryGetAnnotatedValue (out typeWithMethods);
}

[ExpectedWarning ("IL2067", "typeWithFields", nameof (TryGetAnnotatedValue))]
[ExpectedWarning ("IL2067", "typeWithFields", nameof (TryGetAnnotatedValue), Tool.Trimmer | Tool.NativeAot, "https://github.com/dotnet/linker/issues/2632")]
static void TestPassingOutParameter_Mismatch ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] out Type typeWithFields)
{
TryGetAnnotatedValue (out typeWithFields);
Expand Down

0 comments on commit ace22a7

Please sign in to comment.