From e67f230f5f318b9a661ad3d42367172d4dd8c081 Mon Sep 17 00:00:00 2001 From: Sven Boemer Date: Fri, 4 Oct 2024 09:36:10 -0700 Subject: [PATCH] ILLink: Avoid warning for RUC field rooted as part of a root assembly (#108498) Similar to https://github.com/dotnet/runtime/issues/81864, fields in a RUC class are producing trim analysis warnings in the OOB trim step (the mentioned issue was specific to library mode, but it's also a problem for the normal root assembly mode). This extends the fix from https://github.com/dotnet/runtime/pull/84620 to avoid producing warnings for fields that are marked just because an assembly was rooted. Arguably rooting an assembly _should_ warn about rooted RUC members, but that's not the case today for RUC methods, so this makes the behavior consistent for fields. --------- Co-authored-by: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> --- .../src/linker/Linker.Steps/MarkStep.cs | 1 + .../RequiresInRootAllAssembly.cs | 59 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresInRootAllAssembly.cs diff --git a/src/tools/illink/src/linker/Linker.Steps/MarkStep.cs b/src/tools/illink/src/linker/Linker.Steps/MarkStep.cs index 27859afd6df75..d025204b1315d 100644 --- a/src/tools/illink/src/linker/Linker.Steps/MarkStep.cs +++ b/src/tools/illink/src/linker/Linker.Steps/MarkStep.cs @@ -1728,6 +1728,7 @@ void ProcessAnalysisAnnotationsForField (FieldDefinition field, DependencyKind d case DependencyKind.AlreadyMarked: case DependencyKind.TypePreserve: case DependencyKind.PreservedMethod: + case DependencyKind.MemberOfType: return; case DependencyKind.DynamicallyAccessedMemberOnType: diff --git a/src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresInRootAllAssembly.cs b/src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresInRootAllAssembly.cs new file mode 100644 index 0000000000000..b8fa63c186c1c --- /dev/null +++ b/src/tools/illink/test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresInRootAllAssembly.cs @@ -0,0 +1,59 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Diagnostics.CodeAnalysis; +using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Metadata; + +namespace Mono.Linker.Tests.Cases.RequiresCapability +{ + [SetupLinkerArgument ("-a", "test.exe", "all")] + + [SkipKeptItemsValidation] + [ExpectedNoWarnings] + public class RequiresInRootAllAssembly + { + public static void Main () + { + } + + [RequiresDynamicCode ("--MethodWhichRequires--")] + public static void MethodWhichRequires () { } + + [RequiresDynamicCode ("--InstanceMethodWhichRequires--")] + public void InstanceMethodWhichRequires () { } + + public sealed class ClassWithDAMAnnotatedMembers + { + public static void Method ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] Type type) { } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] + public static Type Field; + } + + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] + public sealed class ClassWithDAMAnnotation + { + public void Method () { } + } + + [RequiresUnreferencedCode ("--ClassWithRequires--")] + public sealed class ClassWithRequires + { + public static int Field; + + internal static int InternalField; + + private static int PrivateField; + + public static void Method () { } + + public void InstanceMethod () { } + + public static int Property { get; set; } + + public static event EventHandler PropertyChanged; + } + } +}