Skip to content

Add unsafe evolution attributes#125721

Open
jjonescz wants to merge 4 commits intodotnet:mainfrom
jjonescz:125134-UnsafeEvolutionAttributes
Open

Add unsafe evolution attributes#125721
jjonescz wants to merge 4 commits intodotnet:mainfrom
jjonescz:125134-UnsafeEvolutionAttributes

Conversation

@jjonescz
Copy link
Member

Resolves #125134.

Copilot AI review requested due to automatic review settings March 18, 2026 10:59
@dotnet-policy-service
Copy link
Contributor

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

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds two runtime attributes to the public System.Runtime contract to support unsafe-context analysis and module-level memory-safety metadata, with corresponding implementations in System.Private.CoreLib.

Changes:

  • Exposes System.Diagnostics.CodeAnalysis.RequiresUnsafeAttribute as a public attribute applicable to constructors/events/methods/properties.
  • Introduces System.Runtime.CompilerServices.MemorySafetyRulesAttribute (module-level, editor-browsable never) with a Version payload.
  • Implements MemorySafetyRulesAttribute in System.Private.CoreLib and updates RequiresUnsafeAttribute to be public (non-conditional) with updated documentation.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/libraries/System.Runtime/ref/System.Runtime.cs Adds the new public attribute declarations to the System.Runtime reference contract.
src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/MemorySafetyRulesAttribute.cs Implements MemorySafetyRulesAttribute with version storage and hidden editor-browsing.
src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/RequiresUnsafeAttribute.cs Makes RequiresUnsafeAttribute public and expands its applicable targets.

You can also share your feedback on Copilot code review. Take the survey.

internal sealed class RequiresUnsafeAttribute : Attribute
[AttributeUsage(
AttributeTargets.Constructor | AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Property,
Inherited = false,
Copy link
Member

Choose a reason for hiding this comment

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

The inherited=false is interesting. Is that correct? From a reflection standpoint, we wouldn't expect a derived type to inherit the requires unsafe nature of its base?

Copy link
Member Author

Choose a reason for hiding this comment

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

Compiler doesn't consider inherited attributes though, so it seems better to disallow in reflection too, for consistency.

Copy link
Member Author

Choose a reason for hiding this comment

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

Btw, as part of the language feature, we require that a derived API is not RequiresUnsafe if the base API is not RequiresUnsafe too.

/// <summary>
/// Initializes a new instance of the <see cref="RequiresUnsafeAttribute"/> class.
/// </summary>
public RequiresUnsafeAttribute() { }
Copy link
Member

Choose a reason for hiding this comment

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

nit: I'm pretty sure we have really basic attribute tests that just validate the attribute can be constructed without throwing, or in the case of something like MemorySafetyRulesAttribute, that ctor arguments are properly roundtripped through properties. Could you do a quick search and add a couple tests in the right place?

@jjonescz jjonescz marked this pull request as ready for review March 18, 2026 15:28
Copilot AI review requested due to automatic review settings March 18, 2026 15:28
@jjonescz

This comment was marked as resolved.

Copy link
Contributor

Copilot AI left a comment

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 exposes RequiresUnsafeAttribute publicly and introduces a new MemorySafetyRulesAttribute API, updating the reference surface and adding basic tests to validate constructor behavior.

Changes:

  • Add MemorySafetyRulesAttribute to System.Runtime.CompilerServices (ref + implementation) and test its Version roundtrip.
  • Make RequiresUnsafeAttribute a public attribute with broader AttributeTargets and add a smoke test.
  • Add CoreLib package-validation suppressions for the new/changed APIs.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/AttributesTests.cs Adds a theory test for MemorySafetyRulesAttribute.Version.
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Diagnostics/CodeAnalysis/RequiresUnsafeAttributeTests.cs Adds a minimal constructor smoke test for RequiresUnsafeAttribute.
src/libraries/System.Runtime/ref/System.Runtime.cs Updates the public ref surface to include RequiresUnsafeAttribute and MemorySafetyRulesAttribute.
src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/MemorySafetyRulesAttribute.cs Introduces the CoreLib implementation of MemorySafetyRulesAttribute.
src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/RequiresUnsafeAttribute.cs Changes RequiresUnsafeAttribute from internal/debug-only to public and updates usage/summary.
src/coreclr/System.Private.CoreLib/CompatibilitySuppressions.xml Adds compat suppressions for the new attribute and attribute-usage differences.
src/coreclr/nativeaot/System.Private.CoreLib/src/CompatibilitySuppressions.xml Mirrors the compat suppressions for NativeAOT CoreLib.

You can also share your feedback on Copilot code review. Take the survey.

@EgorBo
Copy link
Member

EgorBo commented Mar 18, 2026

Does ILLink strip these attributes? If it does - should we fix it? cc @agocke

@agocke
Copy link
Member

agocke commented Mar 18, 2026

The linker doesn’t strip attributes

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.

[API Proposal]: Unsafe evolution attributes

5 participants