Skip to content

Commit 19f0385

Browse files
authored
Replace most corelib uses of Unsafe.SizeOf<T> with sizeof(T) (#104923)
Also suppress CS8500 globally rather than in hundreds of files individually and fix NoWarns that weren't inheriting global NoWarns.
1 parent 769ead4 commit 19f0385

File tree

140 files changed

+112
-442
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+112
-442
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@
388388
<Features>strict;nullablePublicOnly</Features>
389389
<TreatWarningsAsErrors Condition="'$(TreatWarningsAsErrors)' == ''">true</TreatWarningsAsErrors>
390390
<!-- Warnings to always disable -->
391-
<NoWarn>$(NoWarn),CS8969</NoWarn>
391+
<NoWarn>$(NoWarn);CS8500;CS8969</NoWarn>
392392
<!-- Always pass portable to override arcade sdk which uses embedded for local builds -->
393393
<DebugType>portable</DebugType>
394394
<KeepNativeSymbols Condition="'$(KeepNativeSymbols)' == '' and '$(DotNetBuildSourceOnly)' == 'true'">true</KeepNativeSymbols>

src/coreclr/System.Private.CoreLib/src/System/ArgIterator.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public ArgIterator(RuntimeArgumentHandle arglist, void* ptr)
5858
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ArgIterator_Init2")]
5959
private static partial void Init(ArgIterator* thisPtr, IntPtr cookie, void* ptr);
6060

61-
#pragma warning disable CS8500 // Takes a pointer to a managed type
6261
// Fetch an argument as a typed referece, advance the iterator.
6362
// Throws an exception if past end of argument list
6463
[CLSCompliant(false)]

src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -790,9 +790,7 @@ public static unsafe T[] AllocateUninitializedArray<T>(int length, bool pinned =
790790
// for debug builds we always want to call AllocateNewArray to detect AllocateNewArray bugs
791791
#if !DEBUG
792792
// small arrays are allocated using `new[]` as that is generally faster.
793-
#pragma warning disable 8500 // sizeof of managed types
794793
if (length < 2048 / sizeof(T))
795-
#pragma warning restore 8500
796794
{
797795
return new T[length];
798796
}
@@ -892,9 +890,7 @@ public static unsafe IReadOnlyDictionary<string, object> GetConfigurationVariabl
892890
Configurations = new Dictionary<string, object>()
893891
};
894892

895-
#pragma warning disable CS8500 // takes address of managed type
896893
_EnumerateConfigurationValues(&context, &ConfigCallback);
897-
#pragma warning restore CS8500
898894
return context.Configurations!;
899895
}
900896

src/coreclr/System.Private.CoreLib/src/System/Reflection/RtFieldInfo.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ public override int GetHashCode() =>
9393
throw new ArgumentException(SR.Arg_TypedReference_Null);
9494

9595
// Passing TypedReference by reference is easier to make correct in native code
96-
#pragma warning disable CS8500 // Takes a pointer to a managed type
9796
return RuntimeFieldHandle.GetValueDirect(this, (RuntimeType)FieldType, &obj, (RuntimeType?)DeclaringType);
98-
#pragma warning restore CS8500
9997
}
10098

10199
[DebuggerStepThrough]
@@ -111,9 +109,7 @@ public override void SetValueDirect(TypedReference obj, object value)
111109
throw new ArgumentException(SR.Arg_TypedReference_Null);
112110

113111
// Passing TypedReference by reference is easier to make correct in native code
114-
#pragma warning disable CS8500 // Takes a pointer to a managed type
115112
RuntimeFieldHandle.SetValueDirect(this, (RuntimeType)FieldType, &obj, value, (RuntimeType?)DeclaringType);
116-
#pragma warning restore CS8500
117113
}
118114

119115
public override RuntimeFieldHandle FieldHandle => new RuntimeFieldHandle(this);

src/coreclr/System.Private.CoreLib/src/System/Runtime/DependentHandle.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,7 @@ private static IntPtr InternalAllocWithGCTransition(object? target, object? depe
253253
// This optimization is the same that is used in GCHandle in RELEASE mode.
254254
// This is not used in DEBUG builds as the runtime performs additional checks.
255255
// The logic below is the inlined copy of ObjectFromHandle in the unmanaged runtime.
256-
#pragma warning disable 8500 // address of managed types
257256
private static unsafe object? InternalGetTarget(IntPtr dependentHandle) => *(object*)dependentHandle;
258-
#pragma warning restore 8500
259257
#endif
260258

261259
[MethodImpl(MethodImplOptions.InternalCall)]

src/coreclr/System.Private.CoreLib/src/System/Runtime/ExceptionServices/InternalCalls.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ internal static partial class InternalCalls
2323
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ResumeAtInterceptionLocation")]
2424
internal static unsafe partial void ResumeAtInterceptionLocation(void* pvRegDisplay);
2525

26-
#pragma warning disable CS8500
2726
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "CallCatchFunclet")]
2827
internal static unsafe partial IntPtr RhpCallCatchFunclet(
2928
ObjectHandleOnStack exceptionObj, byte* pHandlerIP, void* pvRegDisplay, EH.ExInfo* exInfo);
@@ -38,7 +37,6 @@ internal static unsafe partial bool RhpCallFilterFunclet(
3837

3938
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "AppendExceptionStackFrame")]
4039
internal static unsafe partial void RhpAppendExceptionStackFrame(ObjectHandleOnStack exceptionObj, IntPtr ip, UIntPtr sp, int flags, EH.ExInfo* exInfo);
41-
#pragma warning restore CS8500
4240

4341
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "EHEnumInitFromStackFrameIterator")]
4442
[return: MarshalAs(UnmanagedType.Bool)]

src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/GCHandle.CoreCLR.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ private static void InternalFreeWithGCTransition(IntPtr dependentHandle)
4646
[MethodImpl(MethodImplOptions.InternalCall)]
4747
internal static extern object? InternalGet(IntPtr handle);
4848
#else
49-
#pragma warning disable 8500 // address of managed types
5049
internal static unsafe object? InternalGet(IntPtr handle) => *(object*)handle;
51-
#pragma warning restore 8500
5250
#endif
5351

5452
[MethodImpl(MethodImplOptions.InternalCall)]

src/coreclr/nativeaot/Common/src/Internal/Runtime/CompilerHelpers/StartupCodeHelpers.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ private static unsafe object[] InitializeStatics(IntPtr gcStaticRegionStart, int
206206
nint blockAddr = MethodTable.SupportsRelativePointers ? (nint)ReadRelPtr32(pBlock) : *pBlock;
207207
if ((blockAddr & GCStaticRegionConstants.Uninitialized) == GCStaticRegionConstants.Uninitialized)
208208
{
209-
#pragma warning disable CS8500 // takes address of managed type
210209
object? obj = null;
211210
RuntimeImports.RhAllocateNewObject(
212211
new IntPtr(blockAddr & ~GCStaticRegionConstants.Mask),
@@ -234,7 +233,6 @@ private static unsafe object[] InitializeStatics(IntPtr gcStaticRegionStart, int
234233

235234
// Update the base pointer to point to the pinned object
236235
*pBlock = *(IntPtr*)&obj;
237-
#pragma warning restore CS8500
238236
}
239237

240238
currentBase++;

src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/CachedInterfaceDispatch.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ internal static unsafe class CachedInterfaceDispatch
1515
private static unsafe IntPtr RhpCidResolve(IntPtr callerTransitionBlockParam, IntPtr pCell)
1616
{
1717
IntPtr locationOfThisPointer = callerTransitionBlockParam + TransitionBlock.GetThisOffset();
18-
#pragma warning disable 8500 // address of managed types
1918
object pObject = *(object*)locationOfThisPointer;
20-
#pragma warning restore 8500
2119
IntPtr dispatchResolveTarget = RhpCidResolve_Worker(pObject, pCell);
2220
return dispatchResolveTarget;
2321
}

src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ExceptionHandling.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,11 @@ private static void AppendExceptionStackFrameViaClasslib(object exception, IntPt
279279
isFirstFrame = false;
280280
}
281281
#else
282-
#pragma warning disable CS8500
283282
fixed (EH.ExInfo* pExInfo = &exInfo)
284283
{
285284
InternalCalls.RhpAppendExceptionStackFrame(ObjectHandleOnStack.Create(ref exception), ip, sp, flags, pExInfo);
286285
}
287-
#pragma warning restore CS8500
286+
288287
// Clear flags only if we called the function
289288
isFirstRethrowFrame = false;
290289
isFirstFrame = false;
@@ -686,13 +685,11 @@ public static void RhUnwindAndIntercept(ref ExInfo exInfo, UIntPtr interceptStac
686685
if (unwoundReversePInvoke)
687686
{
688687
object exceptionObj = exInfo.ThrownException;
689-
#pragma warning disable CS8500
690688
fixed (EH.ExInfo* pExInfo = &exInfo)
691689
{
692690
InternalCalls.RhpCallCatchFunclet(
693691
ObjectHandleOnStack.Create(ref exceptionObj), null, exInfo._frameIter.RegisterSet, pExInfo);
694692
}
695-
#pragma warning restore CS8500
696693
}
697694
else
698695
{
@@ -922,13 +919,11 @@ private static void DispatchEx(scoped ref StackFrameIterator frameIter, ref ExIn
922919
InternalCalls.RhpCallCatchFunclet(
923920
exceptionObj, pCatchHandler, frameIter.RegisterSet, ref exInfo);
924921
#else // NATIVEAOT
925-
#pragma warning disable CS8500
926922
fixed (EH.ExInfo* pExInfo = &exInfo)
927923
{
928924
InternalCalls.RhpCallCatchFunclet(
929925
ObjectHandleOnStack.Create(ref exceptionObj), pCatchHandler, frameIter.RegisterSet, pExInfo);
930926
}
931-
#pragma warning restore CS8500
932927
#endif // NATIVEAOT
933928
// currently, RhpCallCatchFunclet will resume after the catch
934929
Debug.Assert(false, "unreachable");
@@ -1209,12 +1204,10 @@ private static void InvokeSecondPass(ref ExInfo exInfo, uint idxStart, uint idxL
12091204
#if NATIVEAOT
12101205
InternalCalls.RhpCallFinallyFunclet(pFinallyHandler, exInfo._frameIter.RegisterSet);
12111206
#else // NATIVEAOT
1212-
#pragma warning disable CS8500
12131207
fixed (EH.ExInfo* pExInfo = &exInfo)
12141208
{
12151209
InternalCalls.RhpCallFinallyFunclet(pFinallyHandler, exInfo._frameIter.RegisterSet, pExInfo);
12161210
}
1217-
#pragma warning restore CS8500
12181211
#endif // NATIVEAOT
12191212
exInfo._idxCurClause = MaxTryRegionIdx;
12201213
}

0 commit comments

Comments
 (0)