unsafe class Validate
{
- [InlineArray(1)]
- [StructLayout(LayoutKind.Explicit)]
- private struct Explicit
- {
- [FieldOffset(0)]
- public Guid Guid;
- }
-
[Fact]
public static void Explicit_Fails()
{
});
}
- [InlineArray(0)]
- private struct ZeroLength
- {
- public int field;
- }
-
[Fact]
public static void ZeroLength_Fails()
{
});
}
- [InlineArray(-1)]
- private struct NegativeLength
- {
- public long field;
- }
-
[Fact]
public static void NegativeLength_Fails()
{
});
}
-
- [InlineArray(123)]
- private struct NoFields
- {
- public static int x;
- }
-
[Fact]
public static void NoFields_Fails()
{
});
}
- [InlineArray(1)]
- private struct TwoFields
- {
- int a;
- int b;
- }
-
[Fact]
public static void TwoFields_Fails()
{
<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>
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();
{
public const int Length = 42;
public E e;
-
- [UnscopedRef]
- public ref E this[int i] => ref Unsafe.Add(ref e, i);
}
static object s;
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) {
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)]
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);
}
}
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)
{
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]);
}
}
{
var objArr = new object[i];
objArr[0] = i;
- *arr[i] = objArr;
+ *arr.At(i) = objArr;
}
TestRefLikeInnerMethodArg(arr);
<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>
--- /dev/null
+// 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()
+ }
+}
--- /dev/null
+<Project Sdk="Microsoft.NET.Sdk.IL">
+ <PropertyGroup>
+ <OutputType>Library</OutputType>
+ </PropertyGroup>
+ <ItemGroup>
+ <Compile Include="InvalidCSharp.il" />
+ </ItemGroup>
+</Project>