From e4d85757ab8a6841efef85b8ab678dfb412faec6 Mon Sep 17 00:00:00 2001 From: Vitek Karas <10670590+vitek-karas@users.noreply.github.com> Date: Fri, 1 Jul 2022 02:05:31 -0700 Subject: Small updates to help with linker->aot port (#2873) * Move GetDiagnosticCategory into shared code * Describe the intent around usage of DiagnosticContext in a comment * Minor fix in MessageOrigin GetHasCode * Add tests for patterns which NativeAOT had trouble with Linker changes made for https://github.com/dotnet/runtime/pull/71485 --- .../DataFlow/AttributeConstructorDataflow.cs | 12 +++++++++ .../DataFlow/AttributeFieldDataflow.cs | 27 ++++++++++++++++---- .../DataFlow/AttributePropertyDataflow.cs | 29 ++++++++++++++++++---- .../DataFlow/ByRefDataflow.cs | 18 ++++++++++++++ 4 files changed, 76 insertions(+), 10 deletions(-) (limited to 'test/Mono.Linker.Tests.Cases/DataFlow') diff --git a/test/Mono.Linker.Tests.Cases/DataFlow/AttributeConstructorDataflow.cs b/test/Mono.Linker.Tests.Cases/DataFlow/AttributeConstructorDataflow.cs index 7f73071d4..46120679c 100644 --- a/test/Mono.Linker.Tests.Cases/DataFlow/AttributeConstructorDataflow.cs +++ b/test/Mono.Linker.Tests.Cases/DataFlow/AttributeConstructorDataflow.cs @@ -16,8 +16,10 @@ namespace Mono.Linker.Tests.Cases.DataFlow { [KeptAttributeAttribute (typeof (KeepsPublicConstructorAttribute))] [KeptAttributeAttribute (typeof (KeepsPublicMethodsAttribute))] + [KeptAttributeAttribute (typeof (TypeArrayAttribute))] [KeepsPublicConstructor (typeof (ClassWithKeptPublicConstructor))] [KeepsPublicMethods ("Mono.Linker.Tests.Cases.DataFlow.AttributeConstructorDataflow+ClassWithKeptPublicMethods")] + [TypeArray (new Type[] { typeof (AttributeConstructorDataflow) })] // Trimmer only for now - https://github.com/dotnet/linker/issues/2273 [ExpectedWarning ("IL2026", "--ClassWithKeptPublicMethods--", ProducedBy = ProducedBy.Trimmer)] public static void Main () @@ -109,5 +111,15 @@ namespace Mono.Linker.Tests.Cases.DataFlow public int Field; } } + + [Kept] + [KeptBaseType (typeof (Attribute))] + class TypeArrayAttribute : Attribute + { + [Kept] + public TypeArrayAttribute (Type[] types) // This should not trigger data flow analysis of the parameter + { + } + } } } diff --git a/test/Mono.Linker.Tests.Cases/DataFlow/AttributeFieldDataflow.cs b/test/Mono.Linker.Tests.Cases/DataFlow/AttributeFieldDataflow.cs index 44c857b74..2cac037c5 100644 --- a/test/Mono.Linker.Tests.Cases/DataFlow/AttributeFieldDataflow.cs +++ b/test/Mono.Linker.Tests.Cases/DataFlow/AttributeFieldDataflow.cs @@ -16,8 +16,10 @@ namespace Mono.Linker.Tests.Cases.DataFlow { [KeptAttributeAttribute (typeof (KeepsPublicConstructorsAttribute))] [KeptAttributeAttribute (typeof (KeepsPublicMethodsAttribute))] + [KeptAttributeAttribute (typeof (TypeArrayAttribute))] [KeepsPublicConstructors (Type = typeof (ClassWithKeptPublicConstructor))] - [KeepsPublicMethods ("Mono.Linker.Tests.Cases.DataFlow.AttributeFieldDataflow+ClassWithKeptPublicMethods")] + [KeepsPublicMethods (Type = "Mono.Linker.Tests.Cases.DataFlow.AttributeFieldDataflow+ClassWithKeptPublicMethods")] + [TypeArray (Types = new Type[] { typeof (AttributeFieldDataflow) })] // Trimmer only for now - https://github.com/dotnet/linker/issues/2273 [ExpectedWarning ("IL2026", "--ClassWithKeptPublicMethods--", ProducedBy = ProducedBy.Trimmer)] public static void Main () @@ -46,12 +48,14 @@ namespace Mono.Linker.Tests.Cases.DataFlow class KeepsPublicMethodsAttribute : Attribute { [Kept] - public KeepsPublicMethodsAttribute ( - [KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))] - [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] - string type) + public KeepsPublicMethodsAttribute () { } + + [Kept] + [KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))] + [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] + public string Type; } [Kept] @@ -74,5 +78,18 @@ namespace Mono.Linker.Tests.Cases.DataFlow public static void KeptMethod () { } static void Method () { } } + + [Kept] + [KeptBaseType (typeof (Attribute))] + class TypeArrayAttribute : Attribute + { + [Kept] + public TypeArrayAttribute () + { + } + + [Kept] + public Type[] Types; + } } } diff --git a/test/Mono.Linker.Tests.Cases/DataFlow/AttributePropertyDataflow.cs b/test/Mono.Linker.Tests.Cases/DataFlow/AttributePropertyDataflow.cs index e6848fa5d..1b10390ea 100644 --- a/test/Mono.Linker.Tests.Cases/DataFlow/AttributePropertyDataflow.cs +++ b/test/Mono.Linker.Tests.Cases/DataFlow/AttributePropertyDataflow.cs @@ -16,8 +16,10 @@ namespace Mono.Linker.Tests.Cases.DataFlow { [KeptAttributeAttribute (typeof (KeepsPublicConstructorsAttribute))] [KeptAttributeAttribute (typeof (KeepsPublicMethodsAttribute))] + [KeptAttributeAttribute (typeof (TypeArrayAttribute))] [KeepsPublicConstructors (Type = typeof (ClassWithKeptPublicConstructor))] - [KeepsPublicMethods ("Mono.Linker.Tests.Cases.DataFlow.AttributePropertyDataflow+ClassWithKeptPublicMethods")] + [KeepsPublicMethods (Type = "Mono.Linker.Tests.Cases.DataFlow.AttributePropertyDataflow+ClassWithKeptPublicMethods")] + [TypeArray (Types = new Type[] { typeof (AttributePropertyDataflow) })] // Trimmer only for now - https://github.com/dotnet/linker/issues/2273 [ExpectedWarning ("IL2026", "--ClassWithKeptPublicMethods--", ProducedBy = ProducedBy.Trimmer)] public static void Main () @@ -47,12 +49,15 @@ namespace Mono.Linker.Tests.Cases.DataFlow class KeepsPublicMethodsAttribute : Attribute { [Kept] - public KeepsPublicMethodsAttribute ( - [KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))] - [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] - string type) + public KeepsPublicMethodsAttribute () { } + + [field: Kept] + [Kept] + [KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))] + [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] + public string Type { get; [Kept] set; } } [Kept] @@ -75,5 +80,19 @@ namespace Mono.Linker.Tests.Cases.DataFlow public static void KeptMethod () { } static void Method () { } } + + [Kept] + [KeptBaseType (typeof (Attribute))] + class TypeArrayAttribute : Attribute + { + [Kept] + public TypeArrayAttribute () + { + } + + [field: Kept] + [Kept] + public Type[] Types { get; [Kept] set; } + } } } diff --git a/test/Mono.Linker.Tests.Cases/DataFlow/ByRefDataflow.cs b/test/Mono.Linker.Tests.Cases/DataFlow/ByRefDataflow.cs index 0b05ededc..a16c455e8 100644 --- a/test/Mono.Linker.Tests.Cases/DataFlow/ByRefDataflow.cs +++ b/test/Mono.Linker.Tests.Cases/DataFlow/ByRefDataflow.cs @@ -125,11 +125,29 @@ namespace Mono.Linker.Tests.Cases.DataFlow [RequiresUnreferencedCode ("")] static IntPtr GetDangerous () { return IntPtr.Zero; } + [Kept] + [ExpectedWarning ("IL2070")] + static unsafe void LocalStackAllocDeref (Type t) + { + // Code pattern from CoreLib which caused problems in AOT port + // so making sure we handle this correctly (that is without failing) + int buffSize = 256; + byte* stackSpace = stackalloc byte[buffSize]; + byte* buffer = stackSpace; + + byte* toFree = buffer; + buffer = null; + + // IL2070 - this is to make sure that DataFlow ran on the method's body + t.GetProperties (); + } + [Kept] [ExpectedWarning ("IL2026")] public static void Test () { IntPtrDeref (); + LocalStackAllocDeref (null); } } } -- cgit v1.2.3