|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Mono.Linker.Tests.Cases.Expectations.Assertions; |
| 9 | +using Mono.Linker.Tests.Cases.Expectations.Helpers; |
| 10 | +using Mono.Linker.Tests.Cases.Expectations.Metadata; |
| 11 | + |
| 12 | +namespace Mono.Linker.Tests.Cases.DataFlow |
| 13 | +{ |
| 14 | + [SkipKeptItemsValidation] |
| 15 | + [IgnoreTestCase("NativeAOT doesn't support runtime async yet", IgnoredBy = Tool.NativeAot)] |
| 16 | + [SetupCompileArgument("/features:runtime-async=on")] |
| 17 | + [SetupCompileArgument("/nowarn:SYSLIB5007")] |
| 18 | + public class RuntimeAsyncMethods |
| 19 | + { |
| 20 | + public static async Task Main() |
| 21 | + { |
| 22 | + await BasicRuntimeAsyncMethod(); |
| 23 | + await RuntimeAsyncWithDataFlowAnnotations(null); |
| 24 | + await RuntimeAsyncWithCapturedLocalDataFlow(); |
| 25 | + await RuntimeAsyncWithMultipleAwaits(); |
| 26 | + await RuntimeAsyncWithReassignment(true); |
| 27 | + await RuntimeAsyncReturningAnnotatedType(); |
| 28 | + await RuntimeAsyncWithCorrectParameter(null); |
| 29 | + await RuntimeAsyncWithLocalAll(); |
| 30 | + } |
| 31 | + |
| 32 | + static async Task BasicRuntimeAsyncMethod() |
| 33 | + { |
| 34 | + await Task.Delay(1); |
| 35 | + Console.WriteLine("Basic runtime async"); |
| 36 | + } |
| 37 | + |
| 38 | + [ExpectedWarning("IL2067", "type", nameof(DataFlowTypeExtensions.RequiresAll), nameof(RuntimeAsyncWithDataFlowAnnotations))] |
| 39 | + static async Task RuntimeAsyncWithDataFlowAnnotations([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type type) |
| 40 | + { |
| 41 | + await Task.Delay(1); |
| 42 | + type.RequiresAll(); |
| 43 | + } |
| 44 | + |
| 45 | + [ExpectedWarning("IL2072", nameof(DataFlowTypeExtensions.RequiresAll), nameof(GetWithPublicMethods))] |
| 46 | + static async Task RuntimeAsyncWithCapturedLocalDataFlow() |
| 47 | + { |
| 48 | + Type t = GetWithPublicMethods(); |
| 49 | + await Task.Delay(1); |
| 50 | + t.RequiresAll(); |
| 51 | + } |
| 52 | + |
| 53 | + [ExpectedWarning("IL2072", nameof(DataFlowTypeExtensions.RequiresAll), nameof(GetWithPublicMethods))] |
| 54 | + static async Task RuntimeAsyncWithMultipleAwaits() |
| 55 | + { |
| 56 | + Type t = GetWithPublicMethods(); |
| 57 | + await Task.Delay(1); |
| 58 | + t.RequiresPublicMethods(); |
| 59 | + await Task.Delay(1); |
| 60 | + t.RequiresAll(); |
| 61 | + } |
| 62 | + |
| 63 | + [ExpectedWarning("IL2072", nameof(DataFlowTypeExtensions.RequiresAll), nameof(GetWithPublicMethods))] |
| 64 | + [ExpectedWarning("IL2072", nameof(DataFlowTypeExtensions.RequiresAll), nameof(GetWithPublicFields))] |
| 65 | + static async Task RuntimeAsyncWithReassignment(bool condition) |
| 66 | + { |
| 67 | + Type t = GetWithPublicMethods(); |
| 68 | + await Task.Delay(1); |
| 69 | + if (condition) |
| 70 | + { |
| 71 | + t = GetWithPublicFields(); |
| 72 | + } |
| 73 | + await Task.Delay(1); |
| 74 | + t.RequiresAll(); |
| 75 | + } |
| 76 | + |
| 77 | + [ExpectedWarning("IL2106", nameof(RuntimeAsyncReturningAnnotatedType))] |
| 78 | + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] |
| 79 | + static async Task<Type> RuntimeAsyncReturningAnnotatedType() |
| 80 | + { |
| 81 | + await Task.Delay(1); |
| 82 | + return GetWithPublicMethods(); |
| 83 | + } |
| 84 | + |
| 85 | + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] |
| 86 | + static Type GetWithPublicMethods() => null; |
| 87 | + |
| 88 | + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] |
| 89 | + static Type GetWithPublicFields() => null; |
| 90 | + |
| 91 | + [return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] |
| 92 | + static Type GetWithAllMembers() => null; |
| 93 | + |
| 94 | + static async Task RuntimeAsyncWithCorrectParameter([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type) |
| 95 | + { |
| 96 | + type ??= GetWithAllMembers(); |
| 97 | + await Task.Delay(1); |
| 98 | + type.RequiresAll(); |
| 99 | + } |
| 100 | + |
| 101 | + static async Task RuntimeAsyncWithLocalAll() |
| 102 | + { |
| 103 | + Type t = GetWithAllMembers(); |
| 104 | + await Task.Delay(1); |
| 105 | + t.RequiresAll(); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments