Skip to content

Add DynamicallyAccessedMembers attributes to variance test helpers#121734

Merged
MichalStrehovsky merged 3 commits intoMichalStrehovsky-patch-1from
copilot/sub-pr-121697
Nov 18, 2025
Merged

Add DynamicallyAccessedMembers attributes to variance test helpers#121734
MichalStrehovsky merged 3 commits intoMichalStrehovsky-patch-1from
copilot/sub-pr-121697

Conversation

Copy link
Contributor

Copilot AI commented Nov 18, 2025

Trimming with TrimMode=full was removing default constructors used by Activator.CreateInstance in variance tests, causing test failures in src/tests/Loader/classloader/generics/Variance/IL.

Changes

  • Added [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] to the U type parameter in the following methods across 6 test files (Unbox001-006.cs):
    • UnboxUToTInternal<T,U>()
    • CaseClassUToTWrapper<T,U>()
    • UnboxUToT<T,U>()
  • Added required using System.Diagnostics.CodeAnalysis; directive
  • The attribute is propagated transitively through all generic callsites in the call chain
// Before
private static void UnboxUToTInternal<T,U>()
{
    T t = (T) Activator.CreateInstance(typeof(U));
}

private static void CaseClassUToTWrapper<T,U>()
{
    UnboxUToTInternal<T,U>();
}

public static bool UnboxUToT<T,U>(bool expected)
{
    CaseClassUToTWrapper<T,U>();
    ...
}

// After
private static void UnboxUToTInternal<T, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] U>()
{
    T t = (T) Activator.CreateInstance(typeof(U));
}

private static void CaseClassUToTWrapper<T, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] U>()
{
    UnboxUToTInternal<T,U>();
}

public static bool UnboxUToT<T, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] U>(bool expected)
{
    CaseClassUToTWrapper<T,U>();
    ...
}

This preserves the parameterless constructors required by the generic variance tests when building with trimming enabled by ensuring the attribute is present at every level of the generic method call chain.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Build the test tree with TrimMode set to full Add DynamicallyAccessedMembers attributes to variance test helpers Nov 18, 2025
@MichalStrehovsky
Copy link
Member

@copilot do the same thing transitively for all generic callsites. For example, method CaseClassUToTWrapper<T,U>() calls UnboxUToTInternal<T,U>(), therefore, the generic parameter U on CaseClassUToTWrapper also needs to be annotated as DynamicallyAccessedMembers

Copy link
Contributor Author

Copilot AI commented Nov 18, 2025

@copilot do the same thing transitively for all generic callsites. For example, method CaseClassUToTWrapper<T,U>() calls UnboxUToTInternal<T,U>(), therefore, the generic parameter U on `CaseClas...

I've added the DynamicallyAccessedMembers attribute transitively to all generic callsites. The U parameter now has the attribute in:

  • UnboxUToTInternal<T,U>()
  • CaseClassUToTWrapper<T,U>()
  • UnboxUToT<T,U>()

All 6 test files updated.

Commit: 63c99f4

@MichalStrehovsky MichalStrehovsky changed the base branch from MichalStrehovsky-patch-1 to main November 18, 2025 10:10
@MichalStrehovsky MichalStrehovsky changed the base branch from main to MichalStrehovsky-patch-1 November 18, 2025 10:11
@MichalStrehovsky MichalStrehovsky marked this pull request as ready for review November 18, 2025 12:44
@MichalStrehovsky MichalStrehovsky merged commit b24ee67 into MichalStrehovsky-patch-1 Nov 18, 2025
13 of 17 checks passed
@MichalStrehovsky MichalStrehovsky deleted the copilot/sub-pr-121697 branch November 18, 2025 12:45
MichalStrehovsky pushed a commit to MichalStrehovsky/runtime that referenced this pull request Nov 21, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Dec 19, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants