Skip to content

Commit 21c3c91

Browse files
authored
Fix warning suppressions on types for type hierarchy (dotnet/linker#2148)
* Fix warning suppressions on types for type hierarchy * Add non-library mode tests Commit migrated from dotnet/linker@9ecf5bd
1 parent 054c183 commit 21c3c91

File tree

3 files changed

+222
-2
lines changed

3 files changed

+222
-2
lines changed

src/tools/illink/src/linker/Linker.Steps/MarkStep.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,14 +1756,16 @@ protected internal virtual TypeDefinition MarkType (TypeReference reference, Dep
17561756

17571757
MarkType (type.BaseType, new DependencyInfo (DependencyKind.BaseType, type));
17581758

1759+
MarkCustomAttributes (type, new DependencyInfo (DependencyKind.CustomAttribute, type));
1760+
17591761
// The DynamicallyAccessedMembers hiearchy processing must be done after the base type was marked
1760-
// (to avoid inconsistencies in the cache), but before anything else as work done below
1762+
// (to avoid inconsistencies in the cache), and after marking custom attributes (in case the attributes have
1763+
// warning suppressions for the type hierarchy marking) but before anything else as work done below
17611764
// might need the results of the processing here.
17621765
_dynamicallyAccessedMembersTypeHierarchy.ProcessMarkedTypeForDynamicallyAccessedMembersHierarchy (type);
17631766

17641767
if (type.DeclaringType != null)
17651768
MarkType (type.DeclaringType, new DependencyInfo (DependencyKind.DeclaringType, type));
1766-
MarkCustomAttributes (type, new DependencyInfo (DependencyKind.CustomAttribute, type));
17671769
MarkSecurityDeclarations (type, new DependencyInfo (DependencyKind.CustomAttribute, type));
17681770

17691771
if (type.IsMulticastDelegate ()) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
[SetupLinkerArgument ("-a", "test.exe", "library")]
16+
[ExpectedNoWarnings]
17+
[KeptMember (".ctor()")]
18+
public class TypeHierarchyLibraryModeSuppressions
19+
{
20+
public static void Main ()
21+
{
22+
var t1 = typeof (Unsuppressed);
23+
var t2 = typeof (Suppressed);
24+
}
25+
26+
[Kept]
27+
[KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))]
28+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
29+
[ExpectedWarning ("IL2026", nameof (Unsuppressed))]
30+
class Unsuppressed
31+
{
32+
[Kept]
33+
[KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute))]
34+
[RequiresUnreferencedCode ("--RUC on Unsuppressed--")]
35+
public void RUCMethod () { }
36+
}
37+
38+
[Kept]
39+
[KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))]
40+
[KeptAttributeAttribute (typeof (UnconditionalSuppressMessageAttribute))]
41+
[UnconditionalSuppressMessage ("TrimAnalysis", "IL2026")]
42+
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
43+
class Suppressed
44+
{
45+
[Kept]
46+
[KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute))]
47+
[RequiresUnreferencedCode ("--RUC on Suppressed--")]
48+
public void RUCMethod () { }
49+
}
50+
}
51+
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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

Comments
 (0)