Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TypeHandleDataFlow.cs « DataFlow « Mono.Linker.Tests.Cases « test - github.com/mono/linker.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8c57eb4f4f4bd5fa55764c993e078e87df6ff195 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// 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 System.Reflection;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Helpers;

namespace Mono.Linker.Tests.Cases.DataFlow
{
	[SkipKeptItemsValidation]
	[ExpectedNoWarnings]
	class TypeHandleDataFlow
	{
		public static void Main ()
		{
			TestTypeOf ();
			TestTypeOfFromGeneric<TestType> ();
			TestGetTypeHandle ();
			TestGetTypeHandleFromGeneric<TestType> ();
			TestUnsupportedPatterns (typeof (TestType));
			TestNull ();
			TestNoValue ();
		}

		[ExpectedWarning ("IL2026", "--" + nameof (TestTypeWithRUCOnMembers.PublicMethodWithRUC) + "--")]
		static void TestTypeOf ()
		{
			// In IL this is a type handle (ldtoken) followed by call to GetTypeFromHandle
			// In analyzer this is a special operation node - so also interesting but doesn't involve type handle

			// Validate that we propagate type - without the propagation this should warn
			typeof (TestType).RequiresNonPublicMethods ();

			// Validate that we correctly propagate the type as a known type, so that we can actually mark
			// specific methods on it.
			typeof (TestTypeWithRUCOnMembers).RequiresPublicMethods ();
		}

		[ExpectedWarning ("IL2087", nameof (DataFlowTypeExtensions.RequiresPublicFields))]
		static void TestTypeOfFromGeneric<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] T> ()
		{
			typeof (T).RequiresPublicMethods ();
			typeof (T).RequiresPublicFields ();
		}

		[ExpectedWarning ("IL2026", "--" + nameof (TestTypeWithRUCOnMembers.PublicMethodWithRUC) + "--")]
		static void TestGetTypeHandle ()
		{
			Type.GetTypeFromHandle (typeof (TestType).TypeHandle).RequiresNonPublicMethods ();
			Type.GetTypeFromHandle (typeof (TestTypeWithRUCOnMembers).TypeHandle).RequiresPublicMethods ();
		}

		[ExpectedWarning ("IL2087", nameof (DataFlowTypeExtensions.RequiresPublicFields))]
		static void TestGetTypeHandleFromGeneric<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] T> ()
		{
			Type.GetTypeFromHandle (typeof (T).TypeHandle).RequiresPublicMethods ();
			Type.GetTypeFromHandle (typeof (T).TypeHandle).RequiresPublicFields ();
		}

		[ExpectedWarning ("IL2072", nameof (Type.GetTypeFromHandle), nameof (DataFlowTypeExtensions.RequiresPublicMethods))]
		static void TestUnsupportedPatterns ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] Type typeWithMethods)
		{
			Type.GetTypeFromHandle (typeWithMethods.TypeHandle).RequiresPublicMethods ();
		}

		static void TestNull ()
		{
			Type type = null;
			// This should not warn - we know the type is null. Null is ignored since we know it will fail at runtime
			// and thus there are no actual requirements on the rest of the app to make it fail that way.
			Type.GetTypeFromHandle (type.TypeHandle).RequiresPublicMethods ();
		}

		static void TestNoValue ()
		{
			Type t = null;
			Type noValue = Type.GetTypeFromHandle (t.TypeHandle);
			// t.TypeHandle throws at runtime so don't warn here.
			Type.GetTypeFromHandle (noValue.TypeHandle).RequiresPublicMethods ();
		}

		class TestType { }

		class TestTypeWithRUCOnMembers
		{
			[RequiresUnreferencedCode ("--" + nameof (PublicMethodWithRUC) + "--")]
			public static void PublicMethodWithRUC () { }

			[RequiresUnreferencedCode ("--" + nameof (PrivateMethodWithRUC) + "--")]
			void PrivateMethodWithRUC () { }
		}
	}
}