Skip to content

Mark Array.Initialize() with [RequiresUnreferencedCode] - #131474

Draft
sbomer wants to merge 1 commit into
dotnet:mainfrom
sbomer:runtime-array-initialize
Draft

Mark Array.Initialize() with [RequiresUnreferencedCode]#131474
sbomer wants to merge 1 commit into
dotnet:mainfrom
sbomer:runtime-array-initialize

Conversation

@sbomer

@sbomer sbomer commented Jul 28, 2026

Copy link
Copy Markdown
Member

Summary

Adds [RequiresUnreferencedCode] (RUC) to Array.Initialize() across all runtime flavors, per the discussion in #77426. Array.Initialize() calls the default constructor of the array's element type via reflection-style lookup, which is not visible to the trimmer/AOT unless annotated.

Changes

  • System.Private.CoreLib (CoreCLR, Mono, NativeAOT): added [RequiresUnreferencedCode] to Array.Initialize().
  • System.Runtime ref assembly: added the matching attribute to keep the API contract in sync (required for ApiCompat).
  • System.Private.DataContractSerialization: propagated the resulting IL2026 warning by adding [RequiresUnreferencedCode] to ByteArrayDataContract's constructor, the one real ripple site found. Both call sites that construct it are already inside [RequiresUnreferencedCode]-scoped methods, so this does not cascade further.

Verification

  • Full clr+libs build: 0 warnings, 0 errors.
  • Ran illink.dll directly against the entire built shared framework (all assemblies + System.Private.CoreLib, rooted in -a name all mode) to confirm no other callers or DAM-flow diagnostics are hit anywhere in the framework: 0 matches for Array.Initialize-related warnings.

Note

This content was created with assistance from AI.

Array.Initialize() invokes the array element's default constructor via
reflection, which can be trimmed if not otherwise referenced. Annotate
the method across all three implementations (CoreCLR, Mono, NativeAOT)
and the shared reference-assembly contract.

Propagate the resulting single ripple-effect warning into
DataContractSerialization's ByteArrayDataContract constructor instead
of suppressing it: both call sites that construct it are already
inside [RequiresUnreferencedCode]-scoped methods, so the warning is
absorbed with no further cascade.

Verified with a full clr+libs build (0 warnings/errors) and by running
the ILLink trimmer directly over the entire built shared framework
(256 assemblies, full 'keep everything' root mode) to confirm no other
Array.Initialize call sites or DynamicallyAccessedMembers-flow warnings
exist beyond the one addressed here.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: a3ca7782-a1bb-4dfb-8a99-8dd5b39d7a54
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @steveisok, @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR annotates System.Array.Initialize() with [RequiresUnreferencedCode] across CoreCLR, Mono, and NativeAOT implementations and updates the System.Runtime ref contract accordingly, ensuring trimming/AOT scenarios surface the correct warning when the method is used.

Changes:

  • Added [RequiresUnreferencedCode] to Array.Initialize() in CoreCLR, Mono, and NativeAOT implementations.
  • Added the matching attribute to the System.Runtime ref assembly declaration for Array.Initialize().
  • Propagated the resulting trimming warning in DataContractSerialization by annotating ByteArrayDataContract’s constructor.
Show a summary per file
File Description
src/mono/System.Private.CoreLib/src/System/Array.Mono.cs Marks Mono’s Array.Initialize() as RUC.
src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs Marks CoreCLR’s Array.Initialize() as RUC.
src/coreclr/nativeaot/System.Private.CoreLib/src/System/Array.NativeAot.cs Marks NativeAOT’s Array.Initialize() as RUC.
src/libraries/System.Runtime/ref/System.Runtime.cs Updates ref contract to include the RUC attribute on Array.Initialize().
src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/PrimitiveDataContract.cs Adds RUC propagation on ByteArrayDataContract ctor (one ripple site).

Copilot's findings

  • Files reviewed: 5/5 changed files
  • Comments generated: 1


internal sealed class ByteArrayDataContract : PrimitiveDataContract
{
[RequiresUnreferencedCode("The default constructor of the array's element type may be trimmed if not referenced elsewhere.")]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants