|
| 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 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Diagnostics.CodeAnalysis; |
| 8 | +using System.Text; |
| 9 | +using Mono.Linker.Tests.Cases.Expectations.Assertions; |
| 10 | +using Mono.Linker.Tests.Cases.Expectations.Helpers; |
| 11 | +using Mono.Linker.Tests.Cases.Expectations.Metadata; |
| 12 | + |
| 13 | +namespace Mono.Linker.Tests.Cases.Reflection |
| 14 | +{ |
| 15 | + [ExpectedNoWarnings] |
| 16 | + public class TypeHierarchySuppressions |
| 17 | + { |
| 18 | + // https://github.com/mono/linker/issues/2136 |
| 19 | + // Warnings should originate from the types (or rather their members, with the |
| 20 | + // proposed behavior), and the type suppressions should silence the relevant |
| 21 | + // warnings. |
| 22 | + |
| 23 | + // Should originate from types instead |
| 24 | + [ExpectedWarning ("IL2026", "--RUC on Unsuppressed--")] |
| 25 | + [ExpectedWarning ("IL2026", "--RUC on DerivedFromUnsuppressed1--")] |
| 26 | + |
| 27 | + // Should be suppressed by type-level suppression |
| 28 | + [ExpectedWarning ("IL2026", "--RUC on Suppressed--")] |
| 29 | + [ExpectedWarning ("IL2026", "--RUC on SuppressedOnDerived1--")] |
| 30 | + [ExpectedWarning ("IL2026", "--RUC on DerivedFromSuppressed1--")] |
| 31 | + public static void Main () |
| 32 | + { |
| 33 | + RequireMethods (unsuppressed.GetType ()); |
| 34 | + RequireMethods (suppressed.GetType ()); |
| 35 | + |
| 36 | + var t = typeof (DerivedFromSuppressed1); |
| 37 | + var t2 = typeof (DerivedFromUnsuppressed1); |
| 38 | + var t3 = typeof (SuppressedOnDerived1); |
| 39 | + |
| 40 | + UseDerivedTypes (); |
| 41 | + } |
| 42 | + |
| 43 | + // Referencing these types in a separate method ensures that they get |
| 44 | + // marked after applying annotations on the base type. |
| 45 | + [Kept] |
| 46 | + static void UseDerivedTypes () |
| 47 | + { |
| 48 | + var t = typeof (DerivedFromUnsuppressed2); |
| 49 | + var t2 = typeof (DerivedFromSuppressed2); |
| 50 | + var t3 = typeof (SuppressedOnDerived2); |
| 51 | + } |
| 52 | + |
| 53 | + [Kept] |
| 54 | + static Unsuppressed unsuppressed; |
| 55 | + |
| 56 | + [Kept] |
| 57 | + static Suppressed suppressed; |
| 58 | + |
| 59 | + [Kept] |
| 60 | + static void RequireMethods ( |
| 61 | + [KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))] |
| 62 | + [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] |
| 63 | + Type type) |
| 64 | + { } |
| 65 | + |
| 66 | + [Kept] |
| 67 | + [KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))] |
| 68 | + [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] |
| 69 | + // https://github.com/mono/linker/issues/2136 |
| 70 | + // [ExpectedWarning ("IL2026", "--RUC on Unsuppressed--")] |
| 71 | + class Unsuppressed |
| 72 | + { |
| 73 | + [Kept] |
| 74 | + [KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute))] |
| 75 | + [RequiresUnreferencedCode ("--RUC on Unsuppressed--")] |
| 76 | + public void RUCMethod () { } |
| 77 | + } |
| 78 | + |
| 79 | + [Kept] |
| 80 | + [KeptBaseType (typeof (Unsuppressed))] |
| 81 | + // https://github.com/mono/linker/issues/2136 |
| 82 | + // [ExpectedWarning ("IL2026", "--RUC on DerivedFromUnsuppressed1--")] |
| 83 | + class DerivedFromUnsuppressed1 : Unsuppressed |
| 84 | + { |
| 85 | + [Kept] |
| 86 | + [KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute))] |
| 87 | + [RequiresUnreferencedCode ("--RUC on DerivedFromUnsuppressed1--")] |
| 88 | + public void DerivedRUCMethod () { } |
| 89 | + } |
| 90 | + |
| 91 | + [Kept] |
| 92 | + [KeptBaseType (typeof (Unsuppressed))] |
| 93 | + [ExpectedWarning ("IL2026", "--RUC on DerivedFromUnsuppressed2")] |
| 94 | + // https://github.com/mono/linker/issues/2136 |
| 95 | + // Should originate from the base type instead |
| 96 | + [ExpectedWarning ("IL2026", "--RUC on Unsuppressed--")] |
| 97 | + class DerivedFromUnsuppressed2 : Unsuppressed |
| 98 | + { |
| 99 | + [Kept] |
| 100 | + [KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute))] |
| 101 | + [RequiresUnreferencedCode ("--RUC on DerivedFromUnsuppressed2--")] |
| 102 | + public void DerivedRUCMethod () { } |
| 103 | + } |
| 104 | + |
| 105 | + [Kept] |
| 106 | + [KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))] |
| 107 | + [KeptAttributeAttribute (typeof (UnconditionalSuppressMessageAttribute))] |
| 108 | + [UnconditionalSuppressMessage ("TrimAnalysis", "IL2026")] |
| 109 | + [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] |
| 110 | + class Suppressed |
| 111 | + { |
| 112 | + [Kept] |
| 113 | + [KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute))] |
| 114 | + [RequiresUnreferencedCode ("--RUC on Suppressed--")] |
| 115 | + public void RUCMethod () { } |
| 116 | + } |
| 117 | + |
| 118 | + [Kept] |
| 119 | + [KeptBaseType (typeof (Suppressed))] |
| 120 | + // https://github.com/mono/linker/issues/2136 |
| 121 | + // [ExpectedWarning ("IL2026", "--RUC on DerivedFromSuppressed1--")] |
| 122 | + class DerivedFromSuppressed1 : Suppressed |
| 123 | + { |
| 124 | + [Kept] |
| 125 | + [KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute))] |
| 126 | + [RequiresUnreferencedCode ("--RUC on DerivedFromSuppressed1--")] |
| 127 | + public void RUCDerivedMethod () { } |
| 128 | + } |
| 129 | + |
| 130 | + [Kept] |
| 131 | + [KeptBaseType (typeof (Suppressed))] |
| 132 | + [ExpectedWarning ("IL2026", "--RUC on DerivedFromSuppressed2--")] |
| 133 | + // https://github.com/mono/linker/issues/2136 |
| 134 | + [ExpectedWarning ("IL2026", "--RUC on Suppressed--")] |
| 135 | + class DerivedFromSuppressed2 : Suppressed |
| 136 | + { |
| 137 | + [Kept] |
| 138 | + [KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute))] |
| 139 | + [RequiresUnreferencedCode ("--RUC on DerivedFromSuppressed2--")] |
| 140 | + public void RUCDerivedMethod () { } |
| 141 | + } |
| 142 | + |
| 143 | + [Kept] |
| 144 | + [KeptBaseType (typeof (Unsuppressed))] |
| 145 | + [KeptAttributeAttribute (typeof (UnconditionalSuppressMessageAttribute))] |
| 146 | + [UnconditionalSuppressMessage ("TrimAnalysis", "IL2026")] |
| 147 | + class SuppressedOnDerived1 : Unsuppressed |
| 148 | + { |
| 149 | + [Kept] |
| 150 | + [KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute))] |
| 151 | + [RequiresUnreferencedCode ("--RUC on SuppressedOnDerived1--")] |
| 152 | + public void DerivedRUCMethod () { } |
| 153 | + } |
| 154 | + |
| 155 | + [Kept] |
| 156 | + [KeptBaseType (typeof (Unsuppressed))] |
| 157 | + [KeptAttributeAttribute (typeof (UnconditionalSuppressMessageAttribute))] |
| 158 | + [UnconditionalSuppressMessage ("TrimAnalysis", "IL2026")] |
| 159 | + class SuppressedOnDerived2 : Unsuppressed |
| 160 | + { |
| 161 | + [Kept] |
| 162 | + [KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute))] |
| 163 | + [RequiresUnreferencedCode ("--RUC on SuppressedOnDerived2--")] |
| 164 | + public void DerivedRUCMethod () { } |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments