Skip to content

Commit 74ad0a4

Browse files
committed
Don't emit the warning for RUC on the assembly attributes from the trimmer. Only emit it from the analyzer
1 parent e54d597 commit 74ad0a4

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,8 +1947,11 @@ internal void MarkStaticConstructorVisibleToReflection (TypeDefinition type, in
19471947
// or because of a copy assembly with a reference and so on) then we should not spam the warnings due to the type itself.
19481948
// Also don't warn when the type is marked due to an assembly being rooted.
19491949
if (!(reason.Source is IMemberDefinition sourceMemberDefinition && sourceMemberDefinition.DeclaringType == type) &&
1950-
reason.Kind is not DependencyKind.TypeInAssembly)
1951-
Context.LogWarning (origin, DiagnosticId.AttributeIsReferencedButTrimmerRemoveAllInstances, type.GetDisplayName ());
1950+
reason.Kind is not DependencyKind.TypeInAssembly) {
1951+
// Don't warn for type map attribute types. They're marked as "remove attributes" but we explicitly keep the ones needed.
1952+
if (type is not { Namespace: "System.Runtime.InteropServices", Name: "TypeMapAttribute`1" or "TypeMapAssociationAttribute`1" or "TypeMapAssemblyTargetAttribute`1"})
1953+
Context.LogWarning (origin, DiagnosticId.AttributeIsReferencedButTrimmerRemoveAllInstances, type.GetDisplayName ());
1954+
}
19521955
}
19531956

19541957
if (CheckProcessed (type))

src/tools/illink/src/linker/Linker/LinkContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,11 @@ public bool IsWarningSuppressed (int warningCode, string subcategory, MessageOri
742742
if (Suppressions == null)
743743
return false;
744744

745+
// Don't warn for RUC on assembly attributes.
746+
// There's no way to suppress it.
747+
if (warningCode == 2026 && origin.Provider is AssemblyDefinition)
748+
return true;
749+
745750
return Suppressions.IsSuppressed (warningCode, origin, out _);
746751
}
747752

src/tools/illink/src/linker/Linker/TypeMapHandler.cs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,24 @@
1010
using Mono.CompilerServices.SymbolWriter;
1111
using Mono.Linker.Steps;
1212

13+
using CustomAttributeWithOrigin = (Mono.Cecil.CustomAttribute Attribute, Mono.Cecil.AssemblyDefinition Origin);
14+
1315
namespace Mono.Linker
1416
{
1517
sealed class TypeMapHandler
1618
{
1719
readonly TypeMapResolver _lazyTypeMapResolver;
1820

19-
// [trim target: [type map group: custom attributes]]
20-
readonly Dictionary<TypeDefinition, Dictionary<TypeReference, List<CustomAttribute>>> _unmarkedExternalTypeMapEntries = [];
21+
// [trim target: [type map group: custom attributes with assembly origin]]
22+
readonly Dictionary<TypeDefinition, Dictionary<TypeReference, List<CustomAttributeWithOrigin>>> _unmarkedExternalTypeMapEntries = [];
2123

2224
// [source type: [type map group: custom attributes]]
23-
readonly Dictionary<TypeDefinition, Dictionary<TypeReference, List<CustomAttribute>>> _unmarkedProxyTypeMapEntries = [];
25+
readonly Dictionary<TypeDefinition, Dictionary<TypeReference, List<CustomAttributeWithOrigin>>> _unmarkedProxyTypeMapEntries = [];
2426

2527
// CustomAttributes that we want to mark when the type mapping APIs are used.
2628
// [type map group: custom attributes]
27-
Dictionary<TypeReference, List<CustomAttribute>> _pendingExternalTypeMapEntries = [];
28-
Dictionary<TypeReference, List<CustomAttribute>> _pendingProxyTypeMapEntries = [];
29+
Dictionary<TypeReference, List<CustomAttributeWithOrigin>> _pendingExternalTypeMapEntries = [];
30+
Dictionary<TypeReference, List<CustomAttributeWithOrigin>> _pendingProxyTypeMapEntries = [];
2931
HashSet<TypeReference> _referencedExternalTypeMaps = [];
3032
HashSet<TypeReference> _referencedProxyTypeMaps = [];
3133

@@ -70,34 +72,34 @@ public void Initialize (LinkContext context, MarkStep markStep)
7072

7173
public void ProcessExternalTypeMapGroupSeen (MethodDefinition callingMethod, TypeReference typeMapGroup)
7274
{
73-
_referencedExternalTypeMaps.Add (typeMapGroup);
74-
if (!_pendingExternalTypeMapEntries.Remove (typeMapGroup, out List<CustomAttribute>? pendingEntries)) {
75+
_referencedExternalTypeMaps.Add(typeMapGroup);
76+
if (!_pendingExternalTypeMapEntries.Remove (typeMapGroup, out List<CustomAttributeWithOrigin>? pendingEntries)) {
7577
return;
7678
}
7779

7880
foreach (var entry in pendingEntries) {
79-
MarkTypeMapAttribute (entry, new DependencyInfo (DependencyKind.TypeMapEntry, callingMethod), new MessageOrigin (callingMethod));
81+
MarkTypeMapAttribute (entry, new DependencyInfo (DependencyKind.TypeMapEntry, callingMethod));
8082
}
8183
}
8284

8385
public void ProcessProxyTypeMapGroupSeen (MethodDefinition callingMethod, TypeReference typeMapGroup)
8486
{
8587
_referencedProxyTypeMaps.Add (typeMapGroup);
86-
if (!_pendingProxyTypeMapEntries.Remove (typeMapGroup, out List<CustomAttribute>? pendingEntries)) {
88+
if (!_pendingProxyTypeMapEntries.Remove (typeMapGroup, out List<CustomAttributeWithOrigin>? pendingEntries)) {
8789
return;
8890
}
8991

9092
foreach (var entry in pendingEntries) {
91-
MarkTypeMapAttribute (entry, new DependencyInfo (DependencyKind.TypeMapEntry, callingMethod), new MessageOrigin (callingMethod));
93+
MarkTypeMapAttribute (entry, new DependencyInfo (DependencyKind.TypeMapEntry, callingMethod));
9294
}
9395
}
9496

95-
void MarkTypeMapAttribute (CustomAttribute entry, DependencyInfo info, MessageOrigin origin)
97+
void MarkTypeMapAttribute (CustomAttributeWithOrigin entry, DependencyInfo info)
9698
{
97-
_markStep.MarkCustomAttribute (entry, info, new MessageOrigin (origin));
99+
_markStep.MarkCustomAttribute (entry.Attribute, info, new MessageOrigin (entry.Origin));
98100

99101
// Mark the target type as instantiated
100-
TypeReference targetType = (TypeReference) entry.ConstructorArguments[1].Value;
102+
TypeReference targetType = (TypeReference) entry.Attribute.ConstructorArguments[1].Value;
101103
if (targetType is not null && _context.Resolve (targetType) is TypeDefinition targetTypeDef)
102104
_context.Annotations.MarkInstantiated (targetTypeDef);
103105
}
@@ -107,18 +109,18 @@ public void ProcessType (TypeDefinition definition)
107109
RecordTargetTypeSeen (definition, _unmarkedExternalTypeMapEntries, _referencedExternalTypeMaps, _pendingExternalTypeMapEntries);
108110
}
109111

110-
void RecordTargetTypeSeen (TypeDefinition definition, Dictionary<TypeDefinition, Dictionary<TypeReference, List<CustomAttribute>>> unmarked, HashSet<TypeReference> referenced, Dictionary<TypeReference, List<CustomAttribute>> pending)
112+
void RecordTargetTypeSeen (TypeDefinition definition, Dictionary<TypeDefinition, Dictionary<TypeReference, List<CustomAttributeWithOrigin>>> unmarked, HashSet<TypeReference> referenced, Dictionary<TypeReference, List<CustomAttributeWithOrigin>> pending)
111113
{
112-
if (unmarked.Remove (definition, out Dictionary<TypeReference, List<CustomAttribute>>? entries)) {
114+
if (unmarked.Remove (definition, out Dictionary<TypeReference, List<CustomAttributeWithOrigin>>? entries)) {
113115
foreach (var (key, attributes) in entries) {
114116

115117
if (referenced.Contains (key)) {
116118
foreach (var attr in attributes) {
117-
MarkTypeMapAttribute (attr, new DependencyInfo (DependencyKind.TypeMapEntry, definition), new MessageOrigin (definition));
119+
MarkTypeMapAttribute (attr, new DependencyInfo (DependencyKind.TypeMapEntry, definition));
118120
}
119121
}
120122

121-
if (!pending.TryGetValue (key, out List<CustomAttribute>? value)) {
123+
if (!pending.TryGetValue (key, out List<CustomAttributeWithOrigin>? value)) {
122124
pending[key] = [.. attributes];
123125
} else {
124126
value.AddRange (attributes);
@@ -133,13 +135,13 @@ public void ProcessInstantiated (TypeDefinition definition)
133135
RecordTargetTypeSeen (definition, _unmarkedProxyTypeMapEntries, _referencedProxyTypeMaps, _pendingProxyTypeMapEntries);
134136
}
135137

136-
void AddExternalTypeMapEntry (TypeReference group, CustomAttribute attr)
138+
void AddExternalTypeMapEntry (TypeReference group, CustomAttributeWithOrigin attr)
137139
{
138-
if (attr.ConstructorArguments is [_, _, { Value: TypeReference trimTarget }]) {
140+
if (attr.Attribute.ConstructorArguments is [_, _, { Value: TypeReference trimTarget }]) {
139141
RecordTypeMapEntry (attr, group, trimTarget, _unmarkedExternalTypeMapEntries);
140142
return;
141143
}
142-
if (attr.ConstructorArguments is [_, { Value: TypeReference target }]) {
144+
if (attr.Attribute.ConstructorArguments is [_, { Value: TypeReference target }]) {
143145
// This is a TypeMapAssemblyTargetAttribute, which has a single type argument.
144146
RecordTypeMapEntry (attr, group, target, _unmarkedExternalTypeMapEntries);
145147
return;
@@ -148,9 +150,9 @@ void AddExternalTypeMapEntry (TypeReference group, CustomAttribute attr)
148150
// Let the runtime handle the failure.
149151
}
150152

151-
void AddProxyTypeMapEntry (TypeReference group, CustomAttribute attr)
153+
void AddProxyTypeMapEntry (TypeReference group, CustomAttributeWithOrigin attr)
152154
{
153-
if (attr.ConstructorArguments is [{ Value: TypeReference sourceType }, _]) {
155+
if (attr.Attribute.ConstructorArguments is [{ Value: TypeReference sourceType }, _]) {
154156
// This is a TypeMapAssociationAttribute, which has a single type argument.
155157
RecordTypeMapEntry (attr, group, sourceType, _unmarkedProxyTypeMapEntries);
156158
return;
@@ -159,25 +161,25 @@ void AddProxyTypeMapEntry (TypeReference group, CustomAttribute attr)
159161
// Let the runtime handle the failure.
160162
}
161163

162-
void RecordTypeMapEntry (CustomAttribute attr, TypeReference group, TypeReference trimTarget, Dictionary<TypeDefinition, Dictionary<TypeReference, List<CustomAttribute>>> unmarkedEntryList)
164+
void RecordTypeMapEntry (CustomAttributeWithOrigin attr, TypeReference group, TypeReference trimTarget, Dictionary<TypeDefinition, Dictionary<TypeReference, List<CustomAttributeWithOrigin>>> unmarkedEntryList)
163165
{
164166
TypeDefinition? typeDef = _context.Resolve (trimTarget);
165167
if (typeDef is null) {
166168
return; // Couldn't find the type we were asked about.
167169
}
168170

169171
if (_context.Annotations.IsMarked (typeDef)) {
170-
MarkTypeMapAttribute (attr, new DependencyInfo (DependencyKind.TypeMapEntry, trimTarget), new MessageOrigin(typeDef));
172+
MarkTypeMapAttribute (attr, new DependencyInfo (DependencyKind.TypeMapEntry, trimTarget));
171173
} else {
172174

173-
if (!unmarkedEntryList.TryGetValue (typeDef, out Dictionary<TypeReference, List<CustomAttribute>>? entries)) {
175+
if (!unmarkedEntryList.TryGetValue (typeDef, out Dictionary<TypeReference, List<CustomAttributeWithOrigin>>? entries)) {
174176
entries = new () {
175177
{ group, [] }
176178
};
177179
unmarkedEntryList[typeDef] = entries;
178180
}
179181

180-
if (!entries.TryGetValue(group, out List<CustomAttribute>? attrs)) {
182+
if (!entries.TryGetValue(group, out List<CustomAttributeWithOrigin>? attrs)) {
181183
entries[group] = [attr];
182184
} else {
183185
attrs.Add (attr);
@@ -204,9 +206,9 @@ public void Resolve (LinkContext context, TypeMapHandler manager)
204206
}
205207

206208
if (attr.AttributeType.Name is "TypeMapAttribute`1") {
207-
manager.AddExternalTypeMapEntry (typeMapGroup, attr);
209+
manager.AddExternalTypeMapEntry (typeMapGroup, (attr, assembly));
208210
} else if (attr.AttributeType.Name is "TypeMapAssociationAttribute`1") {
209-
manager.AddProxyTypeMapEntry (typeMapGroup, attr);
211+
manager.AddProxyTypeMapEntry (typeMapGroup, (attr, assembly));
210212
}
211213
}
212214
}

0 commit comments

Comments
 (0)