Skip to content

Commit eb73369

Browse files
authored
Fix origin for warnings due to event methods (#100500)
This fixes the warning origin to prevent warnings from being reported on events for annotated event methods. Instead the warning is shown at the location that introduces the dependency on the event. Doesn't fix the multiple warnings reported in some cases. This is due to MarkEvent also marking event methods, unlike MarkProperty. We might consider fixing this too, but it was looking like a larger change than I wanted to make here. This removes some spurious warnings that were showing up when bubbling up RUC in winforms.
1 parent e21bdfe commit eb73369

File tree

4 files changed

+14
-20
lines changed

4 files changed

+14
-20
lines changed

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ internal void MarkEntireType (TypeDefinition type, in DependencyInfo reason)
405405

406406
if (type.HasEvents) {
407407
foreach (var ev in type.Events) {
408-
MarkEventVisibleToReflection (ev, new DependencyInfo (DependencyKind.MemberOfType, type), ScopeStack.CurrentScope.Origin);
408+
MarkEventVisibleToReflection (ev, new DependencyInfo (DependencyKind.MemberOfType, ScopeStack.CurrentScope.Origin), ScopeStack.CurrentScope.Origin);
409409
}
410410
}
411411
}
@@ -3325,6 +3325,7 @@ static DependencyKind PropagateDependencyKindToAccessors (DependencyKind parentD
33253325
case DependencyKind.AlreadyMarked:
33263326
case DependencyKind.TypePreserve:
33273327
case DependencyKind.PreservedMethod:
3328+
case DependencyKind.DynamicallyAccessedMemberOnType:
33283329
return parentDependencyKind;
33293330

33303331
default:
@@ -3583,15 +3584,15 @@ protected internal virtual void MarkEvent (EventDefinition evt, in DependencyInf
35833584
if (!Annotations.MarkProcessed (evt, reason))
35843585
return;
35853586

3587+
var origin = reason.Source is IMemberDefinition member ? new MessageOrigin (member) : ScopeStack.CurrentScope.Origin;
3588+
DependencyKind dependencyKind = PropagateDependencyKindToAccessors (reason.Kind, DependencyKind.EventMethod);
3589+
MarkMethodIfNotNull (evt.AddMethod, new DependencyInfo (dependencyKind, evt), origin);
3590+
MarkMethodIfNotNull (evt.InvokeMethod, new DependencyInfo (dependencyKind, evt), origin);
3591+
MarkMethodIfNotNull (evt.RemoveMethod, new DependencyInfo (dependencyKind, evt), origin);
3592+
35863593
using var eventScope = ScopeStack.PushLocalScope (new MessageOrigin (evt));
35873594

35883595
MarkCustomAttributes (evt, new DependencyInfo (DependencyKind.CustomAttribute, evt));
3589-
3590-
DependencyKind dependencyKind = PropagateDependencyKindToAccessors (reason.Kind, DependencyKind.EventMethod);
3591-
MarkMethodIfNotNull (evt.AddMethod, new DependencyInfo (dependencyKind, evt), ScopeStack.CurrentScope.Origin);
3592-
MarkMethodIfNotNull (evt.InvokeMethod, new DependencyInfo (dependencyKind, evt), ScopeStack.CurrentScope.Origin);
3593-
MarkMethodIfNotNull (evt.RemoveMethod, new DependencyInfo (dependencyKind, evt), ScopeStack.CurrentScope.Origin);
3594-
35953596
DoAdditionalEventProcessing (evt);
35963597
}
35973598

src/tools/illink/test/Mono.Linker.Tests.Cases/Reflection/TypeHierarchyReflectionWarnings.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,11 @@ class AnnotatedPublicEvents
246246
public delegate void MyEventHandler (object sender, int i);
247247

248248
[Kept]
249-
[ExpectedWarning ("IL2026", "--RUC on add_RUCEvent--", ProducedBy = Tool.Trimmer)]
250249
public event MyEventHandler RUCEvent {
251250
[Kept]
252251
[ExpectedWarning ("IL2112", nameof (AnnotatedPublicEvents), "--RUC on add_RUCEvent--")]
252+
// https://github.com/dotnet/runtime/issues/100499
253+
[ExpectedWarning ("IL2112", nameof (AnnotatedPublicEvents), "--RUC on add_RUCEvent--", ProducedBy = Tool.Trimmer)]
253254
[KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute))]
254255
[RequiresUnreferencedCode ("--RUC on add_RUCEvent--")]
255256
add { }

src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/BasicRequires.cs

-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ static void TestRequiresFromNameOf ()
137137

138138
class OnEventMethod
139139
{
140-
[ExpectedWarning ("IL2026", "--EventToTestRemove.remove--", ProducedBy = Tool.Trimmer)]
141140
static event EventHandler EventToTestRemove {
142141
add { }
143142
[RequiresUnreferencedCode ("Message for --EventToTestRemove.remove--")]
@@ -146,7 +145,6 @@ static event EventHandler EventToTestRemove {
146145
remove { }
147146
}
148147

149-
[ExpectedWarning ("IL2026", "--EventToTestAdd.add--", ProducedBy = Tool.Trimmer)]
150148
static event EventHandler EventToTestAdd {
151149
[RequiresUnreferencedCode ("Message for --EventToTestAdd.add--")]
152150
[RequiresAssemblyFiles ("Message for --EventToTestAdd.add--")]

src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresOnClass.cs

+4-10
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,6 @@ class MemberTypesWithRequires
492492
public static int field;
493493
public static int Property { get; set; }
494494

495-
// These should not be reported https://github.com/mono/linker/issues/2218
496-
[ExpectedWarning ("IL2026", "MemberTypesWithRequires.Event.add", ProducedBy = Tool.Trimmer)]
497-
[ExpectedWarning ("IL2026", "MemberTypesWithRequires.Event.remove", ProducedBy = Tool.Trimmer)]
498495
public static event EventHandler Event;
499496
}
500497

@@ -838,24 +835,21 @@ public static void Test ()
838835

839836
class ReflectionAccessOnEvents
840837
{
841-
// Most of the tests in this run into https://github.com/dotnet/linker/issues/2218
838+
// Most of the tests in this run into https://github.com/dotnet/runtime/issues/100499
842839
// So for now keeping just a very simple test
843840

844841
[RequiresUnreferencedCode ("--WithRequires--")]
845842
[RequiresDynamicCode ("--WithRequires--")]
846843
class WithRequires
847844
{
848-
// These should be reported only in TestDirectReflectionAccess
849-
// https://github.com/mono/linker/issues/2218
850-
[ExpectedWarning ("IL2026", "StaticEvent.add", ProducedBy = Tool.Trimmer)]
851-
[ExpectedWarning ("IL2026", "StaticEvent.remove", ProducedBy = Tool.Trimmer)]
852845
public static event EventHandler StaticEvent;
853846
}
854847

855848
[ExpectedWarning ("IL2026", "StaticEvent.add")]
856849
[ExpectedWarning ("IL3050", "StaticEvent.add", ProducedBy = Tool.NativeAot)]
857-
// https://github.com/mono/linker/issues/2218
858-
[ExpectedWarning ("IL2026", "StaticEvent.remove", ProducedBy = Tool.Analyzer | Tool.NativeAot)]
850+
// https://github.com/dotnet/runtime/issues/100499
851+
[ExpectedWarning ("IL2026", "StaticEvent.add", ProducedBy = Tool.Trimmer)]
852+
[ExpectedWarning ("IL2026", "StaticEvent.remove")]
859853
[ExpectedWarning ("IL3050", "StaticEvent.remove", ProducedBy = Tool.NativeAot)]
860854
static void TestDirectReflectionAccess ()
861855
{

0 commit comments

Comments
 (0)