Mark Array.Initialize() with [RequiresUnreferencedCode] - #131474
Draft
sbomer wants to merge 1 commit into
Draft
Conversation
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: 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. |
Contributor
|
Tagging subscribers to this area: @steveisok, @dotnet/area-system-reflection |
Contributor
There was a problem hiding this comment.
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]toArray.Initialize()in CoreCLR, Mono, and NativeAOT implementations. - Added the matching attribute to the
System.Runtimeref assembly declaration forArray.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.")] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
[RequiresUnreferencedCode](RUC) toArray.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]toArray.Initialize().System.Runtimeref assembly: added the matching attribute to keep the API contract in sync (required for ApiCompat).System.Private.DataContractSerialization: propagated the resultingIL2026warning by adding[RequiresUnreferencedCode]toByteArrayDataContract'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
clr+libsbuild: 0 warnings, 0 errors.illink.dlldirectly against the entire built shared framework (all assemblies +System.Private.CoreLib, rooted in-a name allmode) to confirm no other callers or DAM-flow diagnostics are hit anywhere in the framework: 0 matches forArray.Initialize-related warnings.Note
This content was created with assistance from AI.