Skip to content

Comments

Add [RequiresUnsafe] to Unsafe.As<> and baseline#124668

Draft
agocke wants to merge 1 commit intodotnet:mainfrom
agocke:spc-unsafe-baseline
Draft

Add [RequiresUnsafe] to Unsafe.As<> and baseline#124668
agocke wants to merge 1 commit intodotnet:mainfrom
agocke:spc-unsafe-baseline

Conversation

@agocke
Copy link
Member

@agocke agocke commented Feb 20, 2026

This intentionally wraps every use in an unsafe block, with a comment. The intent is that we review every case and, if the unsafe block is correct, we remove the TODO comment and replace it with an explanation why it is correct.

This intentionally wraps every use in an unsafe block, with a comment.
The intent is that we review every case and, if the unsafe block is
correct, we remove the TODO comment and replace it with an explanation
why it is correct.
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 adds the [RequiresUnsafe] attribute to the Unsafe.As<> methods and systematically wraps every existing usage in an unsafe block with a "TODO(unsafe): Baselining unsafe usage" comment. The intent, as stated in the PR description, is to baseline all current usages so they can be individually reviewed and documented with proper safety justifications.

Changes:

  • Adds [RequiresUnsafe] attribute to both Unsafe.As<T>(object? o) and Unsafe.As<TFrom, TTo>(ref TFrom source) methods
  • Wraps all existing call sites in unsafe blocks with TODO comments
  • No functional changes—purely mechanical wrapping for future review

Reviewed changes

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

Show a summary per file
File Description
System/Runtime/CompilerServices/Unsafe.cs Adds [RequiresUnsafe] attribute to As<> methods
System/WeakReference.T.cs Wraps Unsafe.As calls in Target property getter
System/UIntPtr.cs Wraps Unsafe.As calls in multiple TryParse overloads
System/Threading/Volatile.cs Wraps Unsafe.As calls in Read/Write methods for double, ulong, and generic T
System/Threading/ThreadPoolWorkQueue.cs Wraps Unsafe.As call in DispatchWorkItem
System/Threading/Tasks/*.cs Wraps Unsafe.As calls in ValueTask, Task, and related types
System/Text/Ascii*.cs Wraps Unsafe.As calls in ASCII validation and equality methods
System/String*.cs Wraps Unsafe.As calls in string manipulation, searching, and comparison
System/Span*.cs Wraps Unsafe.As calls in span operations
System/SearchValues/*.cs Wraps Unsafe.As calls across search value implementations
System/Runtime/Intrinsics/*.cs Wraps Unsafe.As calls in Vector64/128/256/512 and debug views
System/Runtime/InteropServices/*.cs Wraps Unsafe.As calls in GCHandle, MemoryMarshal, NFloat
System/Runtime/CompilerServices/*.cs Wraps Unsafe.As calls in async helpers, generic cache, awaiter types
System/ReadOnlySpan.cs, System/ReadOnlyMemory.cs, System/Memory.cs Wraps Unsafe.As calls in memory/span operations
System/Numerics/*.cs Wraps Unsafe.As calls in Vector, Matrix, Vector2/3 types
System/MemoryExtensions.cs Wraps Unsafe.As calls in IndexOfAny range methods
System/Marvin.OrdinalIgnoreCase.cs Wraps Unsafe.As calls in hash computation
System/IntPtr.cs Wraps Unsafe.As calls in TryParse overloads
System/IO/BinaryReader.cs Wraps Unsafe.As calls in MemoryStream operations
System/Guid.cs Wraps Unsafe.As call in GuidResult.ToGuid
System/Globalization/*.cs Wraps Unsafe.As calls in Ordinal and CharUnicodeInfo
System/Delegate.cs Wraps Unsafe.As calls in invocation list enumeration
System/Decimal.DecCalc.cs Wraps Unsafe.As calls in decimal calculations
System/ComAwareWeakReference.cs Wraps Unsafe.As calls in COM-aware weak references
System/Collections/*.cs Wraps Unsafe.As calls in Dictionary, HashSet, ConcurrentDictionary, BitArray
System/Buffers/*.cs Wraps Unsafe.As calls in Utf8Parser and SharedArrayPool
System/Array*.cs Wraps Unsafe.As calls in array operations
System/Activator.RuntimeType.cs Wraps Unsafe.As calls in CreateInstance
System/ByReference.cs Wraps Unsafe.As call in Create method
CoreCLR-specific files Wraps Unsafe.As calls in RuntimeType, RuntimeHelpers, AsyncHelpers, MulticastDelegate, Enum, Delegate, Array

@dotnet-policy-service
Copy link
Contributor

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

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.

1 participant