Skip to content

Conversation

Copy link

Copilot AI commented Nov 12, 2025

  • Understand the repository structure and identify the branches
  • Locate the fork's requiresunsafe-corelib2 branch (agocke/requiresunsafe-corelib2)
  • Identify the target branch (origin/feature/requires-unsafe)
  • Create a new branch based on feature/requires-unsafe
  • Merge agocke/requiresunsafe-corelib2 into the new branch (fast-forward merge, no conflicts)
  • Update the PR branch with the merged content
  • Push the changes to GitHub
  • Verify the merge was successful
  • Address PR feedback: revert Versions.props, Directory.Build.props, and CODEOWNERS
  • Format all inline unsafe blocks to multi-line format
  • Add FIXME comments to all unsafe blocks
  • Move ArraySegment comment in MemoryMarshal.cs
  • Remove FIXME comments from Array.CoreCLR.cs where Warning comment exists
  • Refactor Delegate.CoreCLR.cs to return directly from unsafe block
  • Add clarifying comments to ConfiguredValueTaskAwaitable.cs
  • Refactor all other temp variable patterns to return directly from unsafe blocks
  • Add unsafe to fields with type requirement comments
  • Add RequiresUnsafe attributes to methods accessing unsafe fields
  • Refactor additional return patterns and add clarifying comments
  • Add Field to RequiresUnsafeAttribute targets
  • Add RequiresUnsafe to all unsafe fields
  • Mark TryPush as RequiresUnsafe instead of unsafe
  • Add RequiresUnsafe to ContextCallback delegate
  • Add FIXME comments where requested
  • Refactor methods to remove nested unsafe blocks

✅ All PR Feedback Addressed!

Latest Changes:

  • Added Field to RequiresUnsafeAttribute AttributeUsage targets
  • Added [RequiresUnsafe] to unsafe fields across multiple files
  • Changed TryPush method from unsafe to [RequiresUnsafe]
  • Added [RequiresUnsafe] to ContextCallback delegate and s_ecCallback lambda
  • Removed nested unsafe blocks from ComWrappers methods and added [RequiresUnsafe]
  • Refactored PointerArrayMarshaller.GetManagedValuesSource to return directly with comment
  • Refactored PinnedGCHandle.Target getter to return directly from unsafe block
  • Added FIXME comments to ThreadPoolWorkQueue, String.Manipulation, MemoryExtensions, and Delegate
  • Fixed nested unsafe blocks in Delegate.cs

The branch copilot/merge-requiresunsafe-corelib2 now contains all the work from requiresunsafe-corelib2 properly merged with feature/requires-unsafe and is ready for review.

Original prompt

Please create a new branch that merges my fork's requiresunsafe-corelib2 into feature/requires-unsafe


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Merge requiresunsafe-corelib2 into feature/requires-unsafe Merge requiresunsafe-corelib2 into feature/requires-unsafe Nov 12, 2025
Copilot AI requested a review from agocke November 12, 2025 22:52
Copilot finished work on behalf of agocke November 12, 2025 22:52
@agocke agocke changed the base branch from docs to feature/requires-unsafe November 12, 2025 23:44
@agocke agocke force-pushed the copilot/merge-requiresunsafe-corelib2 branch from 28bb667 to 8aec308 Compare November 12, 2025 23:46
Copy link
Member

@agocke agocke left a comment

Choose a reason for hiding this comment

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

@copilot Please fix

…OWNERS and format unsafe blocks

Co-authored-by: agocke <[email protected]>
Copy link
Author

Copilot AI commented Nov 13, 2025

@copilot Please fix

Fixed in commit f2e66af. All PR feedback has been addressed:

  • Reverted eng/Versions.props and Directory.Build.props
  • Restored .github/CODEOWNERS
  • Formatted all unsafe blocks to multi-line format
  • Added FIXME comments to all unsafe blocks

Copilot finished work on behalf of agocke November 13, 2025 00:10
Copilot AI requested a review from agocke November 13, 2025 00:10
Copy link
Member

@agocke agocke left a comment

Choose a reason for hiding this comment

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

@copilot please fix

// or a U[] which is blittable to a T[] (e.g., int[] and uint[]).

// The array may be prepinned, so remove the high bit from the start index in the line below.
// The ArraySegment<T> ctor will perform bounds checking on index & length.
Copy link
Member

Choose a reason for hiding this comment

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

Please move this comment down to keep it on ArraySegment.

Copy link
Author

Choose a reason for hiding this comment

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

Done in commit 9a8c31b. Moved the comment about ArraySegment.<T> constructor bounds checking down to the line before the constructor call.

// ! or you may introduce a security hole!
T[] @this = Unsafe.As<T[]>(this);
T[] @this;
// FIXME: review unsafe to confirm correct annotation
Copy link
Member

Choose a reason for hiding this comment

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

For every place in this file with the // ! Warning: "this" is an array comment, please remove the FIXME, as it is stated what the invariant is in the comment.

Copy link
Author

Choose a reason for hiding this comment

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

Done in commit 9a8c31b. Removed FIXME comments from all 7 methods in Array.CoreCLR.cs that have the "Warning: this is an array" comment explaining the invariant.

…, refactor Delegate.CoreCLR.cs, add clarifying comments

Co-authored-by: agocke <[email protected]>
Copy link
Author

Copilot AI commented Nov 13, 2025

@copilot please fix

Fixed in commit 9a8c31b. All PR feedback has been addressed:

  • Moved ArraySegment comment in MemoryMarshal.cs
  • Removed FIXME from Array.CoreCLR.cs where Warning comment exists
  • Refactored Delegate.CoreCLR.cs to return directly from unsafe block
  • Added clarifying comments to ConfiguredValueTaskAwaitable.cs

Copilot AI requested a review from agocke November 13, 2025 08:29
Copilot finished work on behalf of agocke November 13, 2025 08:29
Copilot finished work on behalf of agocke November 14, 2025 09:30
Copy link
Member

@agocke agocke left a comment

Choose a reason for hiding this comment

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

@copilot Please address comments

Debug.Assert(obj is MulticastDelegate, "Shouldn't have failed here since we already checked the types are the same!");
MulticastDelegate d = Unsafe.As<MulticastDelegate>(obj);
MulticastDelegate d;
// FIXME: review unsafe to confirm correct annotation
Copy link
Member

Choose a reason for hiding this comment

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

This looks good to me, I think we can remove the comment.

Copy link
Author

Choose a reason for hiding this comment

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

Done in commit c0df804. Removed the FIXME comment.

return false;
}

private unsafe MulticastDelegate NewMulticastDelegate(object[] invocationList, int invocationCount, bool thisIsMultiCastAlready)
Copy link
Member

Choose a reason for hiding this comment

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

You can remove unsafe from this method since we have an unsafe block.

Copy link
Author

Choose a reason for hiding this comment

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

Done in commit c0df804. Removed unsafe modifier from the method signature.

/// <summary>Schedules the continuation action for this ValueTask.</summary>
public void OnCompleted(Action continuation)
{
object? obj = _value._obj;
Copy link
Member

Choose a reason for hiding this comment

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

Please make the same changes here as suggested in the ConfiguredValueTaskAwaitable class

Copy link
Author

Choose a reason for hiding this comment

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

Done in commit c0df804. Made the same changes to ValueTaskAwaiter._value._obj field.

IntPtr[]? result;
unsafe
{
result = Unsafe.As<IntPtr[]>(managed);
Copy link
Member

Choose a reason for hiding this comment

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

Just return the value directly instead of saving to a local.

Copy link
Author

Choose a reason for hiding this comment

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

Done in commit c0df804. Refactored to return the value directly.

IntPtr[]? result;
unsafe
{
result = Unsafe.As<IntPtr[]>(_managedArray);
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

Copy link
Member

Choose a reason for hiding this comment

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

Also add a comment above the unsafe block in both of the instances in this file saying

// IntPtr[] and T*[] have the same representation

Copy link
Author

Choose a reason for hiding this comment

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

Done in commit c0df804. Refactored to return directly and added comment "// IntPtr[] and T*[] have the same representation" to both instances.

Copy link
Author

Choose a reason for hiding this comment

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

Done in commit c0df804. Added comment "// IntPtr[] and T*[] have the same representation" to both instances.

T result;
unsafe
{
result = Unsafe.As<T>(GCHandle.InternalGet(handle)!);
Copy link
Member

Choose a reason for hiding this comment

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

Just return the value and remove the result variable.

Copy link
Author

Choose a reason for hiding this comment

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

Done in commit c0df804. Refactored to return the value directly and removed the result variable.

ManagedObjectWrapperHolder? holder;
unsafe
{
holder = Unsafe.As<ManagedObjectWrapperHolder>(GCHandle.FromIntPtr(handle).Target);
Copy link
Member

Choose a reason for hiding this comment

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

Just return, and remove the holder variable

Copy link
Author

Choose a reason for hiding this comment

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

Done in commit c0df804. Refactored to return directly and removed the holder variable.

…urn statements, add clarifying comments

Co-authored-by: agocke <[email protected]>
Copy link
Author

Copilot AI commented Nov 19, 2025

@copilot Please address comments

Addressed all comments in commit c0df804.

Copilot AI requested a review from agocke November 19, 2025 22:34
Copilot finished work on behalf of agocke November 19, 2025 22:34
Copy link
Member

@agocke agocke left a comment

Choose a reason for hiding this comment

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

@copilot Please address comments. Also, there are new warnings/errors from [RequiresUnsafe] additions. Please fix those by adding a new unsafe block with the comment

// FIXME: review unsafe to confirm correct annotation
``` above

buffer = Unsafe.As<T[]>(b.TryPop());
unsafe
{
buffer = Unsafe.As<T[]>(b.TryPop());
Copy link
Member

Choose a reason for hiding this comment

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

Please mark the TryPush method RequiresUnsafe instead.

/// This allows tools to understand which methods are unsafe to call when removing unreferenced
/// code from an application.
/// </remarks>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, 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.

Please add "field" to the valid targets

Copy link
Author

Choose a reason for hiding this comment

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

Done in commit b73ffce. Added Field to AttributeTargets.

#pragma warning restore CA1823
private IntPtr m_cache;
// Must be a handle to a RuntimeTypeCache type
private unsafe IntPtr m_cache;
Copy link
Member

Choose a reason for hiding this comment

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

Please add [RequiresUnsafe] as well

Copy link
Author

Choose a reason for hiding this comment

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

Done in commit b73ffce. Added [RequiresUnsafe] to m_cache field.

/// <summary>The stored array.</summary>
public Array? Array;
// Must be an array of T[] at runtime
public unsafe Array? Array;
Copy link
Member

Choose a reason for hiding this comment

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

Please also add [RequiresUnsafe]

Copy link
Author

Choose a reason for hiding this comment

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

Done in commit b73ffce. Added [RequiresUnsafe] to SharedArrayPoolThreadLocalArray.Array field.

/// <summary>The comparer to use for lookups in the tables.</summary>
internal readonly IEqualityComparer<TKey>? _comparer;
// Must be IAlternateEqualityComparer<TAlternateKey, TKey>
internal readonly unsafe IEqualityComparer<TKey>? _comparer;
Copy link
Member

Choose a reason for hiding this comment

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

Please add [RequiresUnsafe]

Copy link
Author

Choose a reason for hiding this comment

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

Done in commit b73ffce. Added [RequiresUnsafe] to Tables._comparer field.

/// <summary>null if representing a successful synchronous completion, otherwise a <see cref="Task"/> or a <see cref="IValueTaskSource"/>.</summary>
internal readonly object? _obj;
// Must be either a Task or IValueTaskSource
internal readonly unsafe object? _obj;
Copy link
Member

Choose a reason for hiding this comment

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

Please add [RequiresUnsafe]

Copy link
Author

Choose a reason for hiding this comment

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

Done in commit b73ffce. Added [RequiresUnsafe] to both _obj fields in ValueTask structs.

{
Debug.Assert(workItem is IThreadPoolWorkItem);
IThreadPoolWorkItem tpWorkItem;
unsafe
Copy link
Member

Choose a reason for hiding this comment

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

Please add // FIXME: parent should be unsafe and specify input must be Task or IThreadPoolWorkItem

Copy link
Author

Choose a reason for hiding this comment

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

Done in commit b73ffce. Added the FIXME comment.

// enumerable-based equivalent of new string(char[]).

IEnumerator<char> en = Unsafe.As<IEnumerator<char>>(e);
IEnumerator<char> en;
Copy link
Member

Choose a reason for hiding this comment

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

Please add // FIXME: Consider if (e is IEnumerator<char> en) instead

Copy link
Author

Choose a reason for hiding this comment

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

Done in commit b73ffce. Added the FIXME comment.

if (typeof(T) == typeof(char) && separators.Length == 0)
{
_searchValues = Unsafe.As<SearchValues<T>>(string.SearchValuesStorage.WhiteSpaceChars);
unsafe
Copy link
Member

Choose a reason for hiding this comment

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

Please add // FIXME: review unsafe to confirm correct annotation to all unsafe blocks added in this file.

Copy link
Author

Choose a reason for hiding this comment

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

Done in commit b73ffce. Added "// FIXME: review unsafe to confirm correct annotation" to the unsafe block in MemoryExtensions.cs.

{
int index = _index + 1;
if ((_current = Unsafe.As<TDelegate>(_delegate?.TryGetAt(index))) == null)
unsafe
Copy link
Member

Choose a reason for hiding this comment

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

Please add // FIXME: review unsafe to confirm correct annotation

Copy link
Author

Choose a reason for hiding this comment

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

Done in commit b73ffce. Added the FIXME comment and removed nested unsafe block.

…esUnsafe to fields and methods, add FIXME comments, refactor return statements

Co-authored-by: agocke <[email protected]>
Copy link
Author

Copilot AI commented Nov 20, 2025

@copilot Please address comments. Also, there are new warnings/errors from [RequiresUnsafe] additions. Please fix those by adding a new unsafe block with the comment

Addressed all comments in commit b73ffce. Added Field to RequiresUnsafeAttribute, added RequiresUnsafe to fields, added FIXME comments, and refactored return statements.

Copilot AI requested a review from agocke November 20, 2025 00:24
Copilot finished work on behalf of agocke November 20, 2025 00:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants