Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating to a version of Roslyn that contains the NumericIntPtr feature #69627

Merged
merged 8 commits into from
May 21, 2022
1 change: 1 addition & 0 deletions eng/ApiCompatExcludeAttributes.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
T:System.Diagnostics.CodeAnalysis.MemberNotNullAttribute
T:System.Diagnostics.DebuggerGuidedStepThroughAttribute
T:System.Runtime.CompilerServices.EagerStaticClassConstructionAttribute
T:System.Runtime.CompilerServices.NativeIntegerAttribute
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<!--
TODO: Remove pinned version once arcade supplies a 4.3 compiler.
-->
<MicrosoftNetCompilersToolsetVersion>4.3.0-2.22261.3</MicrosoftNetCompilersToolsetVersion>
<MicrosoftNetCompilersToolsetVersion>4.3.0-2.22270.2</MicrosoftNetCompilersToolsetVersion>
<!-- SDK dependencies -->
<MicrosoftDotNetCompatibilityVersion>2.0.0-alpha.1.21525.11</MicrosoftDotNetCompatibilityVersion>
<!-- Arcade dependencies -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static unsafe void CreateReadOnlySpanFromNullTerminated_Char_ExceedsMaxim
char* mem;
try
{
mem = (char*)Marshal.AllocHGlobal((IntPtr)(sizeof(char) * (2L + int.MaxValue)));
mem = (char*)Marshal.AllocHGlobal(unchecked((nint)(sizeof(char) * (2L + int.MaxValue))));
}
catch (OutOfMemoryException)
{
Expand Down Expand Up @@ -96,7 +96,7 @@ public static unsafe void CreateReadOnlySpanFromNullTerminated_Byte_ExceedsMaxim
byte* mem;
try
{
mem = (byte*)Marshal.AllocHGlobal((IntPtr)(2L + int.MaxValue));
mem = (byte*)Marshal.AllocHGlobal(unchecked((nint)(2L + int.MaxValue)));
}
catch (OutOfMemoryException)
{
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Memory/tests/ReadOnlySpan/Overflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static void IndexOverflow()

unsafe
{
if (!AllocationHelper.TryAllocNative((IntPtr)ThreeGiB, out IntPtr memBlock))
if (!AllocationHelper.TryAllocNative(unchecked((nint)ThreeGiB), out IntPtr memBlock))
return; // It's not implausible to believe that a 3gb allocation will fail - if so, skip this test to avoid unnecessary test flakiness.

try
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Memory/tests/Span/Clear.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ static unsafe void ClearLongerThanUintMaxValueBytes()
if (sizeof(IntPtr) == sizeof(long))
{
// Arrange
IntPtr bytes = (IntPtr)(((long)int.MaxValue) * sizeof(int));
nint bytes = unchecked((nint)(((long)int.MaxValue) * sizeof(int)));
int length = (int)(((long)bytes) / sizeof(int));

if (!AllocationHelper.TryAllocNative(bytes, out IntPtr memory))
Expand Down
6 changes: 3 additions & 3 deletions src/libraries/System.Memory/tests/Span/Overflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void IndexOverflow()
//
unsafe
{
if (!AllocationHelper.TryAllocNative((IntPtr)ThreeGiB, out IntPtr memBlock))
if (!AllocationHelper.TryAllocNative(unchecked((nint)ThreeGiB), out IntPtr memBlock))
{
Console.WriteLine($"Span.Overflow test {nameof(IndexOverflow)} skipped (could not alloc memory).");
return; // It's not implausible to believe that a 3gb allocation will fail - if so, skip this test to avoid unnecessary test flakiness.
Expand Down Expand Up @@ -78,7 +78,7 @@ public static void SliceStartInt32Overflow()
{
unsafe
{
if (!AllocationHelper.TryAllocNative((IntPtr)ThreeGiB, out IntPtr memory))
if (!AllocationHelper.TryAllocNative(unchecked((nint)ThreeGiB), out IntPtr memory))
Copy link
Contributor

Choose a reason for hiding this comment

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

Did IntPtr used to behave as an unchecked nint, hence this update being needed now that IntPtr has different behavior?

Copy link
Member Author

Choose a reason for hiding this comment

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

IntPtr had no language support while nint did. This meant that (IntPtr)TooLargeConstant would issue no error while (nint)TooLargeConstant would issue a error.

Now that IntPtr/nint are aliases and have all the same functionality, IntPtr issues an error requiring you to acknowledge the potential overflow that existed here and use unchecked.

{
Console.WriteLine($"Span.Overflow test {nameof(SliceStartInt32Overflow)} skipped (could not alloc memory).");
return;
Expand Down Expand Up @@ -122,7 +122,7 @@ public static void ReadOnlySliceStartInt32Overflow()
{
unsafe
{
if (!AllocationHelper.TryAllocNative((IntPtr)ThreeGiB, out IntPtr memory))
if (!AllocationHelper.TryAllocNative(unchecked((nint)ThreeGiB), out IntPtr memory))
{
Console.WriteLine($"Span.Overflow test {nameof(ReadOnlySliceStartInt32Overflow)} skipped (could not alloc memory).");
return;
Expand Down
Loading