Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using Xunit;
Expand All @@ -15,6 +16,7 @@ internal static class GeneratorTestHelpers
/// <summary>
/// Asserts for structural equality, returning a path to the mismatching data when not equal.
/// </summary>
[RequiresUnreferencedCode("Checks structural equality using reflection")]
public static void AssertStructurallyEqual<T>(T expected, T actual)
{
CheckAreEqualCore(expected, actual, new());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;

namespace System.Threading.Tasks
{
/// <summary>
/// Fragile trick for getting a description of the current state of a .NET Core async method state machine.
/// To use, first await FetchAsync to get back an object:
/// Fragile trick for getting the current state of a .NET Core async method state machine.
/// To use, await FetchAsync to get back an object:
/// object box = await GetStateMachineData.FetchAsync();
/// and then when the description is desired, use:
/// string description = GetStateMachineData.Describe(box);
/// For example:
/// using (new Timer(s => Console.WriteLine(GetStateMachineData.Describe(s)), await GetStateMachineData.FetchAsync(), 60_000, 60_000))
/// </summary>
internal sealed class GetStateMachineData : ICriticalNotifyCompletion
{
Expand All @@ -25,83 +17,6 @@ internal sealed class GetStateMachineData : ICriticalNotifyCompletion
/// <summary>Returns an awaitable whose awaited result will be the boxed state machine for the async method.</summary>
public static GetStateMachineData FetchAsync() => new GetStateMachineData();

/// <summary>Creates a string representation of a boxed state machine object.</summary>
public static string Describe(object box)
{
var sb = new StringBuilder();
var seen = new HashSet<object>();
Describe(box, sb, seen, 0);
return sb.ToString();

static void Describe(object box, StringBuilder sb, HashSet<object> seen, int indentLevel)
{
string indent = string.Concat(Enumerable.Repeat(" ", indentLevel));

// If we were handed a null object (which should only happen from recursion),
// state that the object was null.
if (box is null)
{
sb.Append(indent).AppendLine($"(Object was null.)");
return;
}

// If as we're walking a graph we happen upon a cycle, break it.
if (!seen.Add(box))
{
sb.Append(indent).AppendLine($"(Object already seen in graph walk.)");
return;
}

// Try to get the StateMachine field from the AsyncStateMachineBox<>. If we can't,
// just print out the details we can and bail.
FieldInfo stateMachineField = box.GetType().GetField("StateMachine", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (stateMachineField is null)
{
sb.Append(indent).AppendLine($"(Couldn't get state machine field from {box}.)");
sb.Append(indent).AppendLine(ToString(box));
return;
}

// Get the state machine from the StateMachine field.
IAsyncStateMachine stateMachine = stateMachineField.GetValue(box) as IAsyncStateMachine;
if (stateMachine is null)
{
sb.Append(indent).AppendLine($"(Null state machine from {box}.)");
return;
}

// Print out the name of the state machine.
Type stateMachineType = stateMachine.GetType();
sb.Append(indent).AppendLine(stateMachineType.FullName);

// Get all of the fields on the state machine, and print them all out.
FieldInfo[] fields = stateMachineType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
foreach (FieldInfo fi in fields)
{
// Print out the field name and its value.
object fiValue = fi.GetValue(stateMachine);
sb.Append(indent).AppendLine($" {fi.Name}: {ToString(fiValue)}");

// If the field is an awaiter, recursively examine any tasks it directly references.
if (fiValue is ICriticalNotifyCompletion)
{
foreach (FieldInfo possibleTask in fiValue.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
if (possibleTask.GetValue(fiValue) is Task awaitedTask)
{
Describe(awaitedTask, sb, seen, indentLevel + 1);
}
}
}
}
}

static string ToString(object value) =>
value is null ? "(null)" :
value is Task t ? $"Status: {t.Status}, Exception: {t.Exception?.InnerException}" :
value.ToString();
}

private GetStateMachineData() { }
public GetStateMachineData GetAwaiter() => this;
public bool IsCompleted => false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public async Task CompilingTheSameSourceResultsInEqualModels()
SourceGenerationSpec spec2 = (await new ConfigBindingGenTestDriver().RunGeneratorAndUpdateCompilation(BindCallSampleCode)).GenerationSpec;

Assert.NotSame(spec1, spec2);
#pragma warning disable IL2026 // https://github.com/dotnet/runtime/issues/126862
GeneratorTestHelpers.AssertStructurallyEqual(spec1, spec2);
#pragma warning restore IL2026

Assert.Equal(spec1, spec2);
Assert.Equal(spec1.GetHashCode(), spec2.GetHashCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkCurrent)</TargetFrameworks>
<EnableDefaultItems>true</EnableDefaultItems>
<!-- https://github.com/dotnet/runtime/issues/126862 -->
<EnableTrimAnalyzer>false</EnableTrimAnalyzer>
<EnableAotAnalyzer>false</EnableAotAnalyzer>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkCurrent)</TargetFrameworks>
<EnableDefaultItems>true</EnableDefaultItems>
<!-- https://github.com/dotnet/runtime/issues/126862 -->
<EnableTrimAnalyzer>false</EnableTrimAnalyzer>
<EnableAotAnalyzer>false</EnableAotAnalyzer>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkCurrent)</TargetFrameworks>
<EnableDefaultItems>true</EnableDefaultItems>
<!-- https://github.com/dotnet/runtime/issues/126862 -->
<EnableTrimAnalyzer>false</EnableTrimAnalyzer>
<EnableAotAnalyzer>false</EnableAotAnalyzer>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ public virtual void Bind_to_object()
{
var configuration = BuildConfigRoot(LoadThroughProvider(TestSection.TestConfig));

#pragma warning disable IL2026, IL3050 // https://github.com/dotnet/runtime/issues/126862
var options = configuration.Get<AsOptions>();
#pragma warning restore IL2026, IL3050

Assert.Equal("Value1", options.Key1);
Assert.Equal("Value12", options.Section1.Key2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkCurrent)</TargetFrameworks>
<!-- https://github.com/dotnet/runtime/issues/126862 -->
<EnableTrimAnalyzer>false</EnableTrimAnalyzer>
<EnableAotAnalyzer>false</EnableAotAnalyzer>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkCurrent)</TargetFrameworks>
<EnableDefaultItems>true</EnableDefaultItems>
<NoWarn>$(NoWarn);CS8002</NoWarn>
<!-- https://github.com/dotnet/runtime/issues/126862 -->
<EnableTrimAnalyzer>false</EnableTrimAnalyzer>
</PropertyGroup>

<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected sealed override IServiceProvider CreateServiceProvider(IServiceCollect
{
foreach (var stackFrame in new StackTrace(1).GetFrames().Take(2))
{
if (SkippedTests.Contains(stackFrame.GetMethod().Name))
if (SkippedTests.Contains(DiagnosticMethodInfo.Create(stackFrame)?.Name))
{
// We skip tests by returning MEDI service provider that we know passes the test
return serviceCollection.BuildServiceProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
<!-- SYSLIB0003: CAS is obsolete, but we still have tests referencing it -->
<NoWarn>$(NoWarn);SYSLIB0003</NoWarn>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetFrameworkCurrent)</TargetFrameworks>
<!-- https://github.com/dotnet/runtime/issues/126862 -->
<EnableTrimAnalyzer>false</EnableTrimAnalyzer>
</PropertyGroup>
<ItemGroup>
<Compile Include="System\DirectoryServices\ActiveDirectorySecurityTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using Xunit;
using Xunit.Sdk;
using System.Reflection;

namespace System.DirectoryServices.Tests
{
Expand All @@ -18,7 +18,9 @@ public partial class DirectoryServicesTests
[Fact]
public void TestGetAllTypes()
{
#pragma warning disable IL2026 // Test validates Assembly.GetTypes in an untrimmed test build.
Type[] allTypes = typeof(DirectoryEntry).Assembly.GetTypes();
#pragma warning restore IL2026
Assert.Contains(typeof(DirectoryEntry), allTypes);
}

Expand Down
2 changes: 2 additions & 0 deletions src/libraries/System.IO.Packaging/tests/PartPieceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ public void OutOfOrderPartPiecesAreParsable(string partPieceLists, int expectedC
using var zipArchive = new ZipArchive(ms, ZipArchiveMode.Read);
string[] archiveNames = partPieceLists.Split(',');

#pragma warning disable IL3050 // s_ZipPackagePartPieceType is a reference type, this is safe to suppress
Type genericSortedSetType = typeof(SortedSet<>).MakeGenericType(s_ZipPackagePartPieceType);
#pragma warning restore IL3050
MethodInfo sortedSetAddMethod = genericSortedSetType.GetMethod("Add");
PropertyInfo zipPackagePartPieceNumberProperty = s_ZipPackagePartPieceType.GetProperty("PieceNumber", BindingFlags.NonPublic | BindingFlags.Instance);
System.Collections.IEnumerable partPieces = (System.Collections.IEnumerable)Activator.CreateInstance(genericSortedSetType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkCurrent)</TargetFrameworks>
<!-- https://github.com/dotnet/runtime/issues/126862 -->
<EnableAotAnalyzer>false</EnableAotAnalyzer>
</PropertyGroup>
<ItemGroup>
<Compile Include="LargeFilesTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetFrameworkCurrent)</TargetFrameworks>
<!-- https://github.com/dotnet/runtime/issues/126862 -->
<EnableTrimAnalyzer>false</EnableTrimAnalyzer>
<EnableAotAnalyzer>false</EnableAotAnalyzer>
</PropertyGroup>
<ItemGroup>
<Compile Include="MofHelpers\MofCollection.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.DotNet.XUnitExtensions;
Expand Down Expand Up @@ -105,6 +106,10 @@ public void Invoke_Instance_And_Static_Method_Win32_Process()
[ConditionalFact(typeof(WmiTestHelper), nameof(WmiTestHelper.IsWmiSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34689", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
[OuterLoop]
#if NET
[RequiresDynamicCode("BinaryFormatter serialization uses dynamic code generation")]
[RequiresUnreferencedCode("BinaryFormatter serialization is not trim compatible")]
#endif
public void Serialize_ManagementException()
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
<StringResourcesPath>..\..\src\Resources\Strings.resx</StringResourcesPath>
<TargetFramework>$(NetCoreAppCurrent)-windows</TargetFramework>
<DefineConstants>$(DefineConstants);UNITTEST</DefineConstants>
<!-- https://github.com/dotnet/runtime/issues/126862 -->
<EnableTrimAnalyzer>false</EnableTrimAnalyzer>
</PropertyGroup>
<ItemGroup>
<DefaultReferenceExclusion Include="System.Net.Http.WinHttpHandler" />
Expand Down
13 changes: 5 additions & 8 deletions src/libraries/System.Net.Http/tests/FunctionalTests/Watchdog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
Expand All @@ -13,7 +11,7 @@ namespace System.Net.Http.Functional.Tests
{
/// <summary>
/// This is using similar trick to GetStateMachineData
/// If marked test runs for more than 60s it will print machine state and make sure it fails
/// If marked test runs for more than 60s it will make sure it fails
/// Usage (await MUST be run directly in the test, should not be called from other async method):
/// using (await Watchdog.CreateAsync())
/// {
Expand All @@ -30,7 +28,7 @@ public static Watchdog CreateAsync()
=> new Watchdog();

public IDisposable GetResult()
=> new WatchdogImpl(_box);
=> new WatchdogImpl();

public Watchdog GetAwaiter() => this;
public bool IsCompleted => false;
Expand All @@ -46,14 +44,13 @@ private class WatchdogImpl : IDisposable
private bool _passed = true;
private Timer _timer;

public WatchdogImpl(object stateMachineData)
public WatchdogImpl()
{
_timer = new Timer(s =>
_timer = new Timer(_ =>
{
_passed = false;
Console.WriteLine(GetStateMachineData.Describe(s));
},
stateMachineData,
null,
60_000,
60_000);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,12 @@ private static (string Raw, string Attachment) DecodeSentMailMessage(MailMessage
var syncSendAdapterType = Type.GetType("System.Net.SyncReadWriteAdapter, System.Net.Mail");

// Send the message.
#pragma warning disable IL3050 // Roslyn analyzer can't see through the private reflection, but publish process can. This is safe.
typeof(MailMessage)
.GetMethod("SendAsync", BindingFlags.Instance | BindingFlags.NonPublic)
.MakeGenericMethod(syncSendAdapterType)
.Invoke(mail, new object[] { mailWriter, true, true, CancellationToken.None });
#pragma warning restore IL3050

// Decode contents.
string result = Encoding.UTF8.GetString(stream.ToArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
<EventSourceSupport Condition="'$(TestNativeAot)' == 'true'">true</EventSourceSupport>
<!-- https://github.com/dotnet/runtime/issues/126862 -->
<EnableAotAnalyzer>false</EnableAotAnalyzer>
</PropertyGroup>
<ItemGroup>
<Compile Include="AlternateViewCollectionTest.cs" />
Expand Down
Loading
Loading