Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ILLink] Fix origin for warnings due to event methods #100500

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/tools/illink/src/linker/Linker.Steps/MarkStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ internal void MarkEntireType (TypeDefinition type, in DependencyInfo reason)

if (type.HasEvents) {
foreach (var ev in type.Events) {
MarkEventVisibleToReflection (ev, new DependencyInfo (DependencyKind.MemberOfType, type), ScopeStack.CurrentScope.Origin);
MarkEventVisibleToReflection (ev, new DependencyInfo (DependencyKind.MemberOfType, ScopeStack.CurrentScope.Origin), ScopeStack.CurrentScope.Origin);
}
}
}
Expand Down Expand Up @@ -3325,6 +3325,7 @@ static DependencyKind PropagateDependencyKindToAccessors (DependencyKind parentD
case DependencyKind.AlreadyMarked:
case DependencyKind.TypePreserve:
case DependencyKind.PreservedMethod:
case DependencyKind.DynamicallyAccessedMemberOnType:
return parentDependencyKind;

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

var origin = reason.Source is IMemberDefinition member ? new MessageOrigin (member) : ScopeStack.CurrentScope.Origin;
DependencyKind dependencyKind = PropagateDependencyKindToAccessors (reason.Kind, DependencyKind.EventMethod);
MarkMethodIfNotNull (evt.AddMethod, new DependencyInfo (dependencyKind, evt), origin);
MarkMethodIfNotNull (evt.InvokeMethod, new DependencyInfo (dependencyKind, evt), origin);
MarkMethodIfNotNull (evt.RemoveMethod, new DependencyInfo (dependencyKind, evt), origin);

using var eventScope = ScopeStack.PushLocalScope (new MessageOrigin (evt));

MarkCustomAttributes (evt, new DependencyInfo (DependencyKind.CustomAttribute, evt));

DependencyKind dependencyKind = PropagateDependencyKindToAccessors (reason.Kind, DependencyKind.EventMethod);
MarkMethodIfNotNull (evt.AddMethod, new DependencyInfo (dependencyKind, evt), ScopeStack.CurrentScope.Origin);
MarkMethodIfNotNull (evt.InvokeMethod, new DependencyInfo (dependencyKind, evt), ScopeStack.CurrentScope.Origin);
MarkMethodIfNotNull (evt.RemoveMethod, new DependencyInfo (dependencyKind, evt), ScopeStack.CurrentScope.Origin);

DoAdditionalEventProcessing (evt);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,11 @@ class AnnotatedPublicEvents
public delegate void MyEventHandler (object sender, int i);

[Kept]
[ExpectedWarning ("IL2026", "--RUC on add_RUCEvent--", ProducedBy = Tool.Trimmer)]
public event MyEventHandler RUCEvent {
[Kept]
[ExpectedWarning ("IL2112", nameof (AnnotatedPublicEvents), "--RUC on add_RUCEvent--")]
// https://github.com/dotnet/runtime/issues/100499
[ExpectedWarning ("IL2112", nameof (AnnotatedPublicEvents), "--RUC on add_RUCEvent--", ProducedBy = Tool.Trimmer)]
[KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute))]
[RequiresUnreferencedCode ("--RUC on add_RUCEvent--")]
add { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ static void TestRequiresFromNameOf ()

class OnEventMethod
{
[ExpectedWarning ("IL2026", "--EventToTestRemove.remove--", ProducedBy = Tool.Trimmer)]
static event EventHandler EventToTestRemove {
add { }
[RequiresUnreferencedCode ("Message for --EventToTestRemove.remove--")]
Expand All @@ -146,7 +145,6 @@ static event EventHandler EventToTestRemove {
remove { }
}

[ExpectedWarning ("IL2026", "--EventToTestAdd.add--", ProducedBy = Tool.Trimmer)]
static event EventHandler EventToTestAdd {
[RequiresUnreferencedCode ("Message for --EventToTestAdd.add--")]
[RequiresAssemblyFiles ("Message for --EventToTestAdd.add--")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,6 @@ class MemberTypesWithRequires
public static int field;
public static int Property { get; set; }

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

Expand Down Expand Up @@ -838,24 +835,21 @@ public static void Test ()

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

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

[ExpectedWarning ("IL2026", "StaticEvent.add")]
[ExpectedWarning ("IL3050", "StaticEvent.add", ProducedBy = Tool.NativeAot)]
// https://github.com/mono/linker/issues/2218
[ExpectedWarning ("IL2026", "StaticEvent.remove", ProducedBy = Tool.Analyzer | Tool.NativeAot)]
// https://github.com/dotnet/runtime/issues/100499
[ExpectedWarning ("IL2026", "StaticEvent.add", ProducedBy = Tool.Trimmer)]
[ExpectedWarning ("IL2026", "StaticEvent.remove")]
[ExpectedWarning ("IL3050", "StaticEvent.remove", ProducedBy = Tool.NativeAot)]
static void TestDirectReflectionAccess ()
{
Expand Down
Loading