@@ -462,6 +462,26 @@ public static void ByteOffsetStackByte4()
462
462
Assert . Equal ( new IntPtr ( - 3 ) , Unsafe . ByteOffset ( ref byte4 . B3 , ref byte4 . B0 ) ) ;
463
463
}
464
464
465
+ private static unsafe class StaticReadonlyHolder
466
+ {
467
+ public static readonly void * Pointer = ( void * ) RuntimeHelpers . AllocateTypeAssociatedMemory ( typeof ( StaticReadonlyHolder ) , 1 ) ;
468
+ }
469
+
470
+ [ Fact ]
471
+ public static unsafe void ByteOffsetConstantRef ( )
472
+ {
473
+ // https://github.com/dotnet/runtime/pull/99019
474
+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
475
+ static nint NullTest ( ref byte origin ) => Unsafe . ByteOffset ( ref origin , ref Unsafe . NullRef < byte > ( ) ) ;
476
+ Assert . Equal ( 0 , NullTest ( ref Unsafe . NullRef < byte > ( ) ) ) ;
477
+
478
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
479
+ static ref byte GetStatic ( ref byte x ) => ref x ;
480
+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
481
+ static nint StaticReadonlyTest ( ref byte x ) => Unsafe . ByteOffset ( ref GetStatic ( ref Unsafe . AsRef < byte > ( StaticReadonlyHolder . Pointer ) ) , ref x ) ;
482
+ Assert . Equal ( 0 , StaticReadonlyTest ( ref Unsafe . AsRef < byte > ( StaticReadonlyHolder . Pointer ) ) ) ;
483
+ }
484
+
465
485
[ Fact ]
466
486
public static unsafe void AsRef ( )
467
487
{
@@ -597,7 +617,7 @@ public static void RefAddNuintByteOffset()
597
617
}
598
618
599
619
[ Fact ]
600
- public static void RefSubtract( )
620
+ public static unsafe void RefSubtract( )
601
621
{
602
622
string [ ] a = new string [ ] { "abc" , "def" , "ghi" , "jkl" } ;
603
623
@@ -609,6 +629,11 @@ public static void RefSubtract()
609
629
610
630
ref string r3 = ref Unsafe . Subtract ( ref r2 , 3 ) ;
611
631
Assert. Equal ( "abc" , r3 ) ;
632
+
633
+ // https://github.com/dotnet/runtime/pull/99019
634
+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
635
+ static ref byte NullTest( nuint offset ) => ref Unsafe. Subtract ( ref Unsafe . NullRef < byte > ( ) , offset ) ;
636
+ Assert. True ( Unsafe . IsNullRef ( ref NullTest ( 0 ) ) ) ;
612
637
}
613
638
614
639
[ Fact ]
0 commit comments