Add [RequiresUnsafe] to Unsafe.As<> and baseline#124668
Draft
agocke wants to merge 1 commit intodotnet:mainfrom
Draft
Add [RequiresUnsafe] to Unsafe.As<> and baseline#124668agocke wants to merge 1 commit intodotnet:mainfrom
agocke wants to merge 1 commit intodotnet:mainfrom
Conversation
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.
Contributor
There was a problem hiding this comment.
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 bothUnsafe.As<T>(object? o)andUnsafe.As<TFrom, TTo>(ref TFrom source)methods - Wraps all existing call sites in
unsafeblocks 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 |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-numerics |
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.
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.