Skip to content

Enable trim and AOT analyzers for selected library tests#128355

Draft
Copilot wants to merge 4 commits into
mainfrom
copilot/fix-trim-aot-warnings
Draft

Enable trim and AOT analyzers for selected library tests#128355
Copilot wants to merge 4 commits into
mainfrom
copilot/fix-trim-aot-warnings

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 19, 2026

Description

Several library test projects suppressed trim/AOT analyzer warnings by disabling the analyzers at the project level. This change enables the analyzers and replaces broad suppressions with targeted fixes or narrow warning pragmas at known test-only reflection and dynamic-code sites.

  • Removed the dotnet/runtime#126862 analyzer-disable blocks from the listed test projects.
  • Replaced avoidable warnings with analyzer-friendly code, such as DiagnosticMethodInfo, Enum.GetValues<TEnum>(), and LINQ-based collection conversion.
  • Updated System.Threading.RateLimiting.Tests to reference the source project with SkipUseReferenceAssembly="true" and avoid local reflection suppressions.
  • Added narrow #pragma warning disable/restore scopes only for remaining emitted IL warnings that require reflection, dynamic code, or unsupported trim/AOT analysis patterns.
  • Added explanatory context to the remaining System.IO.Packaging dynamic-code suppression.

Copilot AI and others added 2 commits May 19, 2026 02:37
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/5977beb1-58f4-4a75-9a1c-02ff1bb17294

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/5977beb1-58f4-4a75-9a1c-02ff1bb17294

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Copilot AI self-assigned this May 19, 2026
Copilot AI review requested due to automatic review settings May 19, 2026 03:06
Copilot AI review requested due to automatic review settings May 19, 2026 03:06
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-infrastructure-libraries
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
Member

@MichalStrehovsky MichalStrehovsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foreach (var stackFrame in new StackTrace(1).GetFrames().Take(2))
{
#pragma warning disable IL2026
if (SkippedTests.Contains(stackFrame.GetMethod().Name))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace stackFrame.GetMethod().Name with DiagnosticMethodInfo.Create(stackFrame).Name and remove the pragma warning suppression.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 768bc81 by switching to DiagnosticMethodInfo.Create(stackFrame)?.Name and removing the pragma.

using var zipArchive = new ZipArchive(ms, ZipArchiveMode.Read);
string[] archiveNames = partPieceLists.Split(',');

#pragma warning disable IL3050
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#pragma warning disable IL3050
#pragma warning disable IL3050 // s_ZipPackagePartPieceType is a reference type, this is safe to suppress

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 737b3d6 by adding the explanatory comment to the IL3050 suppression.

var syncSendAdapterType = Type.GetType("System.Net.SyncReadWriteAdapter, System.Net.Mail");

// Send the message.
#pragma warning disable IL3050
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be triggering a warning. It probably triggers a warning because this test is compiled against a reference assembly that doesn't have the non-public SendAsync method. Is it possible to remove this suppression and instead fix the project to reference the src\libraries\System.Net.Mail\src\System.Net.Mail.csproj project? You might need to set SkipUseReferenceAssembly="true"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested referencing the source project with SkipUseReferenceAssembly, but it did not remove the MakeGenericMethod IL3050 warning and introduced unrelated LoggingTest trim warnings, so I left this targeted suppression unchanged.

{
var tls13cs = new HashSet<TlsCipherSuite>(GetTls13CipherSuites());
#pragma warning disable IL3050
foreach (TlsCipherSuite cs in typeof(TlsCipherSuite).GetEnumValues())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
foreach (TlsCipherSuite cs in typeof(TlsCipherSuite).GetEnumValues())
foreach (TlsCipherSuite cs in Enum.GetValues<TlsCipherSuite>())

And remove the suppression.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 737b3d6 by using Enum.GetValues<TlsCipherSuite>() and removing the suppression.

Assert.Equal(bypassLocal, p.BypassProxyOnLocal);
Assert.Equal(bypassedAddresses, p.BypassList);
#pragma warning disable IL3050
Assert.Equal(bypassedAddresses, (string[])p.BypassArrayList.ToArray(typeof(string)));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Assert.Equal(bypassedAddresses, (string[])p.BypassArrayList.ToArray(typeof(string)));
Assert.Equal(bypassedAddresses, p.BypassArrayList.Cast<string>().ToArray());

Then remove the suppression, add using System.Linq. Do the same change for the below lines too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 737b3d6 by switching the BypassArrayList assertions to Cast<string>().ToArray() and removing the suppressions.

if (string.Empty.Length > 0)
limiterType = limiterTypeDef;

#pragma warning disable IL2075
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This warning is probably also because we're compiling the test against the reference assembly that doesn't have the DefaultPartitionedRateLimiter. See if we can compile against the source project.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 737b3d6 by referencing the source project with SkipUseReferenceAssembly="true" and making the helper use the source-visible constructed type without local pragmas.

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/5a0b0c7b-d13e-4936-8392-ff08d859bc72

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot May 19, 2026 04:24
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/5a0b0c7b-d13e-4936-8392-ff08d859bc72

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants