|
| 1 | +// Copyright (c) .NET Foundation and contributors. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | +using Mono.Linker.Tests.Cases.Expectations.Assertions; |
| 8 | +using Mono.Linker.Tests.Cases.Expectations.Metadata; |
| 9 | + |
| 10 | +namespace Mono.Linker.Tests.Cases.DataFlow |
| 11 | +{ |
| 12 | + [SkipKeptItemsValidation] |
| 13 | + [ExpectedNoWarnings] |
| 14 | + [Reference ("Microsoft.CSharp.dll")] |
| 15 | + public class DynamicObjects |
| 16 | + { |
| 17 | + public static void Main () |
| 18 | + { |
| 19 | + InvocationOnDynamicType.Test (); |
| 20 | + DynamicMemberReference.Test (); |
| 21 | + DynamicIndexerAccess.Test (); |
| 22 | + DynamicInRequiresUnreferencedCodeClass.Test (); |
| 23 | + InvocationOnDynamicTypeInMethodWithRUCDoesNotWarnTwoTimes.Test (); |
| 24 | + } |
| 25 | + |
| 26 | + class InvocationOnDynamicType |
| 27 | + { |
| 28 | + // Analyzer hole: https://github.com/dotnet/runtime/issues/94057 |
| 29 | + [ExpectedWarning ("IL2026", "Invoking members on dynamic types is not trimming-compatible.", ProducedBy = Tool.Analyzer)] |
| 30 | + [ExpectedWarning ("IL2026", "Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember", ProducedBy = Tool.Trimmer | Tool.NativeAot)] |
| 31 | + [ExpectedWarning ("IL3050", ProducedBy = Tool.NativeAot)] |
| 32 | + static void DynamicArgument () |
| 33 | + { |
| 34 | + dynamic dynamicObject = "Some string"; |
| 35 | + Console.WriteLine (dynamicObject); |
| 36 | + } |
| 37 | + |
| 38 | + static void DynamicParameter () |
| 39 | + { |
| 40 | + MethodWithDynamicParameterDoNothing (0); |
| 41 | + MethodWithDynamicParameterDoNothing ("Some string"); |
| 42 | + MethodWithDynamicParameter(-1); |
| 43 | + } |
| 44 | + |
| 45 | + static void MethodWithDynamicParameterDoNothing (dynamic arg) |
| 46 | + { |
| 47 | + } |
| 48 | + |
| 49 | + // Analyzer hole: https://github.com/dotnet/runtime/issues/94057 |
| 50 | + [ExpectedWarning ("IL2026", "Invoking members on dynamic types is not trimming-compatible.", ProducedBy = Tool.Analyzer)] |
| 51 | + [ExpectedWarning ("IL2026", "Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember", ProducedBy = Tool.Trimmer | Tool.NativeAot)] |
| 52 | + [ExpectedWarning ("IL3050", ProducedBy = Tool.NativeAot)] |
| 53 | + static void MethodWithDynamicParameter (dynamic arg) |
| 54 | + { |
| 55 | + arg.MethodWithDynamicParameter (arg); |
| 56 | + } |
| 57 | + |
| 58 | + [ExpectedWarning ("IL2026", "Microsoft.CSharp.RuntimeBinder.Binder.InvokeConstructor", ProducedBy = Tool.Trimmer | Tool.NativeAot)] |
| 59 | + [ExpectedWarning ("IL3050", ProducedBy = Tool.NativeAot)] |
| 60 | + // TODO: analyzer hole! |
| 61 | + static void ObjectCreationDynamicArgument () |
| 62 | + { |
| 63 | + dynamic dynamicObject = "Some string"; |
| 64 | + var x = new ClassWithDynamicCtor (dynamicObject); |
| 65 | + } |
| 66 | + |
| 67 | + class ClassWithDynamicCtor |
| 68 | + { |
| 69 | + public ClassWithDynamicCtor (dynamic arg) |
| 70 | + { |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public static void Test () |
| 75 | + { |
| 76 | + DynamicArgument (); |
| 77 | + DynamicParameter (); |
| 78 | + ObjectCreationDynamicArgument (); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + class DynamicMemberReference |
| 83 | + { |
| 84 | + // Analyzer hole: https://github.com/dotnet/runtime/issues/94057 |
| 85 | + [ExpectedWarning ("IL2026", "Microsoft.CSharp.RuntimeBinder.Binder.GetMember", ProducedBy = Tool.Trimmer | Tool.NativeAot)] |
| 86 | + [ExpectedWarning ("IL3050", ProducedBy = Tool.NativeAot)] |
| 87 | + static void Read (dynamic d) |
| 88 | + { |
| 89 | + var x = d.Member; |
| 90 | + } |
| 91 | + |
| 92 | + // Analyzer hole: https://github.com/dotnet/runtime/issues/94057 |
| 93 | + [ExpectedWarning ("IL2026", "Microsoft.CSharp.RuntimeBinder.Binder.SetMember", ProducedBy = Tool.Trimmer | Tool.NativeAot)] |
| 94 | + [ExpectedWarning ("IL3050", ProducedBy = Tool.NativeAot)] |
| 95 | + static void Write (dynamic d) |
| 96 | + { |
| 97 | + d.Member = 0; |
| 98 | + } |
| 99 | + |
| 100 | + public static void Test () |
| 101 | + { |
| 102 | + Read (null); |
| 103 | + Write (null); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + class DynamicIndexerAccess |
| 108 | + { |
| 109 | + // Analyzer hole: https://github.com/dotnet/runtime/issues/94057 |
| 110 | + [ExpectedWarning ("IL2026", "Microsoft.CSharp.RuntimeBinder.Binder.GetIndex", ProducedBy = Tool.Trimmer | Tool.NativeAot)] |
| 111 | + [ExpectedWarning ("IL3050", ProducedBy = Tool.NativeAot)] |
| 112 | + static void Read (dynamic d) |
| 113 | + { |
| 114 | + var x = d[0]; |
| 115 | + } |
| 116 | + |
| 117 | + // Analyzer hole: https://github.com/dotnet/runtime/issues/94057 |
| 118 | + [ExpectedWarning ("IL2026", "Microsoft.CSharp.RuntimeBinder.Binder.SetIndex", ProducedBy = Tool.Trimmer | Tool.NativeAot)] |
| 119 | + [ExpectedWarning ("IL3050", ProducedBy = Tool.NativeAot)] |
| 120 | + static void Write (dynamic d) |
| 121 | + { |
| 122 | + d[0] = 0; |
| 123 | + } |
| 124 | + |
| 125 | + public static void Test () |
| 126 | + { |
| 127 | + Read (null); |
| 128 | + Write (null); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + class DynamicInRequiresUnreferencedCodeClass |
| 133 | + { |
| 134 | + [RequiresUnreferencedCode("message")] |
| 135 | + class ClassWithRequires |
| 136 | + { |
| 137 | + // Analyzer hole: https://github.com/dotnet/runtime/issues/94057 |
| 138 | + [ExpectedWarning ("IL3050", ProducedBy = Tool.NativeAot)] |
| 139 | + public static void MethodWithDynamicArg (dynamic arg) |
| 140 | + { |
| 141 | + arg.DynamicInvocation (); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + [ExpectedWarning ("IL2026", nameof (ClassWithRequires))] |
| 146 | + public static void Test () |
| 147 | + { |
| 148 | + ClassWithRequires.MethodWithDynamicArg (null); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + class InvocationOnDynamicTypeInMethodWithRUCDoesNotWarnTwoTimes () |
| 153 | + { |
| 154 | + // Analyzer hole: https://github.com/dotnet/runtime/issues/94057 |
| 155 | + [RequiresUnreferencedCode ("We should only see the warning related to this annotation, and none about the dynamic type.")] |
| 156 | + [RequiresDynamicCode ("We should only see the warning related to this annotation, and none about the dynamic type.")] |
| 157 | + static void MethodWithRequires () |
| 158 | + { |
| 159 | + dynamic dynamicField = "Some string"; |
| 160 | + Console.WriteLine (dynamicField); |
| 161 | + } |
| 162 | + |
| 163 | + [ExpectedWarning ("IL2026", nameof (MethodWithRequires))] |
| 164 | + [ExpectedWarning ("IL3050", nameof (MethodWithRequires), ProducedBy = Tool.Analyzer | Tool.NativeAot)] |
| 165 | + public static void Test () |
| 166 | + { |
| 167 | + MethodWithRequires (); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments