Skip to content

Commit 42460a6

Browse files
authored
Add some tests for by ref type data flow (#2407)
1 parent 7f2fd06 commit 42460a6

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Diagnostics.CodeAnalysis;
6+
using Mono.Linker.Tests.Cases.Expectations.Assertions;
7+
using Mono.Linker.Tests.Cases.Expectations.Helpers;
8+
using Mono.Linker.Tests.Cases.Expectations.Metadata;
9+
10+
namespace Mono.Linker.Tests.Cases.DataFlow
11+
{
12+
[SkipKeptItemsValidation]
13+
[ExpectedNoWarnings]
14+
class MethodByRefParameterDataFlow
15+
{
16+
public static void Main ()
17+
{
18+
Type typeWithMethods = _fieldWithMethods;
19+
20+
TestAssignStaticToAnnotatedRefParameter (ref typeWithMethods);
21+
TestAssignParameterToAnnotatedRefParameter (ref typeWithMethods, typeof (TestType));
22+
}
23+
24+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
25+
static Type _fieldWithMethods = null;
26+
27+
[ExpectedWarning ("IL2026", "Message for --TestType.Requires--")]
28+
29+
// https://github.com/dotnet/linker/issues/2158
30+
// The type.GetMethods call generates a warning because we're not able to correctly track the value of the "this".
31+
// (there's a ldind.ref insruction here which we currently don't handle and the "this" becomes unknown)
32+
[ExpectedWarning ("IL2065")]
33+
static void TestAssignStaticToAnnotatedRefParameter ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] ref Type type)
34+
{
35+
type = typeof (TestTypeWithRequires);
36+
type.GetMethods (); // Should not warn
37+
}
38+
39+
// The warning message is REALLY confusing (basically wrong) since it talks about "calling the method with wrong argument"
40+
// which is definitely not the case here.
41+
[ExpectedWarning ("IL2067", "typeWithFields")]
42+
43+
// https://github.com/dotnet/linker/issues/2158
44+
// The type.GetMethods call generates a warning because we're not able to correctly track the value of the "this".
45+
// (there's a ldind.ref insruction here which we currently don't handle and the "this" becomes unknown)
46+
[ExpectedWarning ("IL2065")]
47+
static void TestAssignParameterToAnnotatedRefParameter (
48+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] ref Type type,
49+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] Type typeWithFields)
50+
{
51+
type = typeWithFields; // Should warn
52+
type.GetMethods (); // Should not warn
53+
}
54+
55+
class TestTypeWithRequires
56+
{
57+
[RequiresUnreferencedCode ("Message for --TestType.Requires--")]
58+
public static void Requires () { }
59+
}
60+
61+
class TestType
62+
{
63+
}
64+
}
65+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Diagnostics.CodeAnalysis;
6+
using Mono.Linker.Tests.Cases.Expectations.Assertions;
7+
using Mono.Linker.Tests.Cases.Expectations.Helpers;
8+
using Mono.Linker.Tests.Cases.Expectations.Metadata;
9+
10+
namespace Mono.Linker.Tests.Cases.DataFlow
11+
{
12+
[SkipKeptItemsValidation]
13+
[ExpectedNoWarnings]
14+
public class MethodByRefReturnDataFlow
15+
{
16+
public static void Main ()
17+
{
18+
ReturnAnnotatedTypeReferenceAsUnannotated ();
19+
AssignToAnnotatedTypeReference ();
20+
}
21+
22+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
23+
static Type _annotatedField;
24+
25+
// This should warn, as assiging to the return ref Type will assign value to the annotated field
26+
// but the annotation is not propagated
27+
// https://github.com/dotnet/linker/issues/2158
28+
// [ExpectedWarning("IL????")]
29+
static ref Type ReturnAnnotatedTypeReferenceAsUnannotated () { return ref _annotatedField; }
30+
31+
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
32+
static ref Type ReturnAnnotatedTypeReferenceAsAnnotated () { return ref _annotatedField; }
33+
34+
// https://github.com/dotnet/linker/issues/2158
35+
// [ExpectedWarning("IL2026", "Message for --TestType.Requires--")]
36+
static void AssignToAnnotatedTypeReference ()
37+
{
38+
ref Type typeShouldHaveAllMethods = ref ReturnAnnotatedTypeReferenceAsAnnotated ();
39+
typeShouldHaveAllMethods = typeof (TestTypeWithRequires); // This should apply the annotation -> cause IL2026 due to RUC method
40+
_annotatedField.GetMethods (); // Doesn't warn, but now contains typeof(TestType) - no warning here is correct
41+
}
42+
43+
public class TestTypeWithRequires
44+
{
45+
[RequiresUnreferencedCode ("Message for --TestType.Requires--")]
46+
public static void Requires () { }
47+
}
48+
}
49+
}

test/Mono.Linker.Tests.Cases/DataFlow/MethodParametersDataFlow.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public static void Main ()
3636
instance.WriteToParameterOnInstanceMethod (null);
3737
instance.LongWriteToParameterOnInstanceMethod (0, 0, 0, 0, null);
3838
instance.UnsupportedParameterType (null);
39+
40+
TestParameterOverwrite (typeof (TestType));
3941
}
4042

4143
// Validate the error message when annotated parameter is passed to another annotated parameter
@@ -223,6 +225,16 @@ private static void RequirePublicParameterlessConstructorAndNothing (
223225
{
224226
}
225227

228+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
229+
static Type _fieldWithMethods;
230+
231+
[ExpectedWarning ("IL2077", nameof (_fieldWithMethods))]
232+
static void TestParameterOverwrite ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] Type type)
233+
{
234+
type = _fieldWithMethods;
235+
type.GetFields ();
236+
}
237+
226238
class TestType
227239
{
228240
public TestType () { }

0 commit comments

Comments
 (0)