Fix up `InlineArray` tests (#90192)
authorAaron Robinson <arobins@microsoft.com>
Wed, 9 Aug 2023 23:15:05 +0000 (16:15 -0700)
committerGitHub <noreply@github.com>
Wed, 9 Aug 2023 23:15:05 +0000 (16:15 -0700)
* Convert invalid C# to IL project

src/tests/Loader/classloader/InlineArray/InlineArrayInvalid.cs
src/tests/Loader/classloader/InlineArray/InlineArrayInvalid.csproj
src/tests/Loader/classloader/InlineArray/InlineArrayValid.cs
src/tests/Loader/classloader/InlineArray/InlineArrayValid.csproj
src/tests/Loader/classloader/InlineArray/InvalidCSharp.il [new file with mode: 0644]
src/tests/Loader/classloader/InlineArray/InvalidCSharp.ilproj [new file with mode: 0644]

index 6704199..70aeef5 100644 (file)
@@ -10,14 +10,6 @@ using Xunit;
 
 unsafe class Validate
 {
-    [InlineArray(1)]
-    [StructLayout(LayoutKind.Explicit)]
-    private struct Explicit
-    {
-        [FieldOffset(0)]
-        public Guid Guid;
-    }
-
     [Fact]
     public static void Explicit_Fails()
     {
@@ -30,12 +22,6 @@ unsafe class Validate
         });
     }
 
-    [InlineArray(0)]
-    private struct ZeroLength
-    {
-        public int field;
-    }
-
     [Fact]
     public static void ZeroLength_Fails()
     {
@@ -74,12 +60,6 @@ unsafe class Validate
         });
     }
 
-    [InlineArray(-1)]
-    private struct NegativeLength
-    {
-        public long field;
-    }
-
     [Fact]
     public static void NegativeLength_Fails()
     {
@@ -96,13 +76,6 @@ unsafe class Validate
         });
     }
 
-
-    [InlineArray(123)]
-    private struct NoFields
-    {
-        public static int x;
-    }
-
     [Fact]
     public static void NoFields_Fails()
     {
@@ -115,13 +88,6 @@ unsafe class Validate
         });
     }
 
-    [InlineArray(1)]
-    private struct TwoFields
-    {
-        int a;
-        int b;
-    }
-
     [Fact]
     public static void TwoFields_Fails()
     {
index 3b0db2c..c9c4d3b 100644 (file)
@@ -2,13 +2,12 @@
   <PropertyGroup>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
     <OutputType>Exe</OutputType>
-    <!-- Issue: https://github.com/dotnet/runtime/issues/88861 -->
-    <DisableProjectBuild>true</DisableProjectBuild>
   </PropertyGroup>
   <ItemGroup>
     <Compile Include="InlineArrayInvalid.cs" />
   </ItemGroup>
   <ItemGroup>
+    <ProjectReference Include="InvalidCSharp.ilproj" />
     <ProjectReference Include="$(TestSourceDir)Common/CoreCLRTestLibrary/CoreCLRTestLibrary.csproj" />
   </ItemGroup>
 </Project>
index 82ebaa0..60da6c0 100644 (file)
@@ -23,18 +23,6 @@ struct MyArray<T> : IEnumerable<T>
     public int Length => LengthConst;
 
     [UnscopedRef]
-    public ref T this[int i]
-    {
-        get
-        {
-            if ((uint)i >= (uint)Length)
-                throw new IndexOutOfRangeException(nameof(i));
-
-            return ref Unsafe.Add(ref _element, i);
-        }
-    }
-
-    [UnscopedRef]
     public Span<T> AsSpan() => MemoryMarshal.CreateSpan<T>(ref _element, Length);
 
     IEnumerator IEnumerable.GetEnumerator() => (IEnumerator<T>)this.GetEnumerator();
@@ -107,9 +95,6 @@ unsafe class Validate
     {
         public const int Length = 42;
         public E e;
-
-        [UnscopedRef]
-        public ref E this[int i] => ref Unsafe.Add(ref e, i);
     }
 
     static object s;
@@ -168,10 +153,7 @@ unsafe class Validate
     struct ObjShortArr
     {
         public const int Length = 100;
-        public (object, short) element;
-
-        [UnscopedRef]
-        public ref (object o, short s) this[int i] => ref Unsafe.Add(ref element, i);
+        public (object o, short s) element;
 
         [MethodImpl(MethodImplOptions.NoInlining)]
         public static ObjShortArr CreateArray(int recCount) {
@@ -219,7 +201,7 @@ unsafe class Validate
         public (object, short) element;
 
         [UnscopedRef]
-        public ref (object o, short s) this[int i] => ref Unsafe.Add(ref element, i);
+        public ref (object o, short s) At(int i) => ref Unsafe.Add(ref element, i);
     }
 
     [MethodImpl(MethodImplOptions.NoInlining)]
@@ -229,8 +211,8 @@ unsafe class Validate
 
         for (short i = 0; i < ObjShortArrRef.Length; i++)
         {
-            Assert.Equal(i * 2, arr[i].o);
-            Assert.Equal(i * 2 + 1, arr[i].s);
+            Assert.Equal(i * 2, arr.At(i).o);
+            Assert.Equal(i * 2 + 1, arr.At(i).s);
         }
     }
 
@@ -242,48 +224,28 @@ unsafe class Validate
         var arr = new ObjShortArrRef();
         for (short i = 0; i < ObjShortArrRef.Length; i++)
         {
-            arr[i].o = i;
-            arr[i].s = (short)(i + 1);
+            arr.At(i).o = i;
+            arr.At(i).s = (short)(i + 1);
         }
 
         GC.Collect(2, GCCollectionMode.Forced, true, true);
 
         for (short i = 0; i < ObjShortArrRef.Length; i++)
         {
-            Assert.Equal(i, arr[i].o);
-            Assert.Equal(i + 1, arr[i].s);
+            Assert.Equal(i, arr.At(i).o);
+            Assert.Equal(i + 1, arr.At(i).s);
         }
 
         for (short i = 0; i < ObjShortArrRef.Length; i++)
         {
-            arr[i].o = i * 2;
-            arr[i].s = (short)(i * 2 + 1);
+            arr.At(i).o = i * 2;
+            arr.At(i).s = (short)(i * 2 + 1);
         }
 
         TestRefLikeOuterMethodArg(arr);
     }
 
     // ====================== RefLikeInner ========================================================
-    [InlineArray(LengthConst)]
-    ref struct SpanArr
-    {
-        private const int LengthConst = 100;
-        public Span<object> element;
-
-        public Span<object>* this[int i]
-        {
-            get
-            {
-                fixed (Span<object>* p = &element)
-                {
-                    return p + i;
-                }
-            }
-        }
-
-        public int Length => LengthConst;
-    }
-
     [MethodImpl(MethodImplOptions.NoInlining)]
     static void TestRefLikeInnerMethodArg(SpanArr arr)
     {
@@ -291,8 +253,8 @@ unsafe class Validate
 
         for (int i = 1; i < arr.Length; i++)
         {
-            Assert.Equal(i, arr[i]->Length);
-            Assert.Equal(i, (*arr[i])[0]);
+            Assert.Equal(i, arr.At(i)->Length);
+            Assert.Equal(i, (*arr.At(i))[0]);
         }
     }
 
@@ -306,7 +268,7 @@ unsafe class Validate
         {
             var objArr = new object[i];
             objArr[0] = i;
-            *arr[i] = objArr;
+            *arr.At(i) = objArr;
         }
 
         TestRefLikeInnerMethodArg(arr);
index 75ca700..125a6c3 100644 (file)
@@ -2,13 +2,12 @@
   <PropertyGroup>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
     <OutputType>Exe</OutputType>
-    <!-- Issue: https://github.com/dotnet/runtime/issues/88861 -->
-    <DisableProjectBuild>true</DisableProjectBuild>
   </PropertyGroup>
   <ItemGroup>
     <Compile Include="InlineArrayValid.cs" />
   </ItemGroup>
   <ItemGroup>
+    <ProjectReference Include="InvalidCSharp.ilproj" />
     <ProjectReference Include="$(TestSourceDir)Common/CoreCLRTestLibrary/CoreCLRTestLibrary.csproj" />
   </ItemGroup>
 </Project>
diff --git a/src/tests/Loader/classloader/InlineArray/InvalidCSharp.il b/src/tests/Loader/classloader/InlineArray/InvalidCSharp.il
new file mode 100644 (file)
index 0000000..ff8d65c
--- /dev/null
@@ -0,0 +1,110 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+.assembly extern System.Runtime { .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A ) }
+
+.assembly InvalidCSharp { }
+
+.class public explicit ansi sealed beforefieldinit Explicit
+    extends [System.Runtime]System.ValueType
+{
+    .custom instance void [System.Runtime]System.Runtime.CompilerServices.InlineArrayAttribute::.ctor(int32) = (
+        01 00 01 00 00 00 00 00
+    )
+    .field [0] public valuetype [System.Runtime]System.Guid Guid
+}
+
+.class public sequential ansi sealed beforefieldinit ZeroLength
+    extends [System.Runtime]System.ValueType
+{
+    .custom instance void [System.Runtime]System.Runtime.CompilerServices.InlineArrayAttribute::.ctor(int32) = (
+        01 00 00 00 00 00 00 00
+    )
+    .field public int32 'field'
+}
+
+.class public sequential ansi sealed beforefieldinit TooLarge
+    extends [System.Runtime]System.ValueType
+{
+    .custom instance void [System.Runtime]System.Runtime.CompilerServices.InlineArrayAttribute::.ctor(int32) = (
+        01 00 00 00 00 20 00 00
+    )
+    .field public int64 'field'
+}
+
+.class public sequential ansi sealed beforefieldinit NegativeLength
+    extends [System.Runtime]System.ValueType
+{
+    .custom instance void [System.Runtime]System.Runtime.CompilerServices.InlineArrayAttribute::.ctor(int32) = (
+        01 00 ff ff ff ff 00 00
+    )
+    .field public int64 'field'
+}
+
+.class public sequential ansi sealed beforefieldinit NoFields
+    extends [System.Runtime]System.ValueType
+{
+    .custom instance void [System.Runtime]System.Runtime.CompilerServices.InlineArrayAttribute::.ctor(int32) = (
+        01 00 7b 00 00 00 00 00
+    )
+    .pack 0
+    .size 1
+    .field public static int32 x
+}
+
+.class public sequential ansi sealed beforefieldinit TwoFields
+    extends [System.Runtime]System.ValueType
+{
+    .custom instance void [System.Runtime]System.Runtime.CompilerServices.InlineArrayAttribute::.ctor(int32) = (
+        01 00 01 00 00 00 00 00
+    )
+    .field private int32 a
+    .field private int32 b
+}
+
+.class public sequential ansi sealed beforefieldinit SpanArr
+    extends [System.Runtime]System.ValueType
+{
+    .custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = (
+        01 00 00 00
+    )
+    .custom instance void [System.Runtime]System.Runtime.CompilerServices.InlineArrayAttribute::.ctor(int32) = (
+        01 00 64 00 00 00 00 00
+    )
+
+    .field private static literal int32 LengthConst = int32(100)
+    .field public valuetype [System.Runtime]System.Span`1<object> element
+
+    .method public hidebysig
+        instance valuetype [System.Runtime]System.Span`1<object>* At (
+            int32
+        ) cil managed
+    {
+        .locals init (
+            [0] valuetype [System.Runtime]System.Span`1<object>& pinned
+        )
+        ldarg.0
+        ldflda valuetype [System.Runtime]System.Span`1<object> SpanArr::element
+        stloc.0
+        ldloc.0
+        conv.u
+        ldarg.1
+        conv.i
+        sizeof valuetype [System.Runtime]System.Span`1<object>
+        mul
+        add
+        ret
+    }
+
+    .method public hidebysig specialname
+        instance int32 get_Length () cil managed
+    {
+        ldc.i4.s 100
+        ret
+    }
+
+    .property instance int32 Length()
+    {
+        .get instance int32 SpanArr::get_Length()
+    }
+}
diff --git a/src/tests/Loader/classloader/InlineArray/InvalidCSharp.ilproj b/src/tests/Loader/classloader/InlineArray/InvalidCSharp.ilproj
new file mode 100644 (file)
index 0000000..d577c8f
--- /dev/null
@@ -0,0 +1,8 @@
+<Project Sdk="Microsoft.NET.Sdk.IL">
+  <PropertyGroup>
+    <OutputType>Library</OutputType>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="InvalidCSharp.il" />
+  </ItemGroup>
+</Project>