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

[API Proposal]: Create array from array type #88620

Merged
merged 26 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/coreclr/classlibnative/bcltype/arraynative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ static void CheckElementType(TypeHandle elementType)
COMPlusThrow(kNotSupportedException, W("NotSupported_ByRefLikeArray"));

// Check for open generic types.
if (pMT->IsGenericTypeDefinition() || pMT->ContainsGenericVariables())
if (pMT->ContainsGenericVariables())
COMPlusThrow(kNotSupportedException, W("NotSupported_OpenType"));

// Check for Void.
Expand Down Expand Up @@ -765,7 +765,8 @@ void QCALLTYPE Array_CreateInstance(QCall::TypeHandle pTypeHnd, INT32 rank, INT3
_ASSERTE((INT32)typeHnd.GetRank() == rank);
_ASSERTE(typeHnd.IsArray());

CheckElementType(typeHnd.GetArrayElementTypeHandle());
if (typeHnd.GetArrayElementTypeHandle().ContainsGenericVariables())
COMPlusThrow(kNotSupportedException, W("NotSupported_OpenType"));

if (!typeHnd.AsMethodTable()->IsMultiDimArray())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ internal unsafe bool IsSzArray
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
private static unsafe Array InternalCreate(RuntimeType elementType, int rank, int* pLengths, int* pLowerBounds)
{
ValidateElementType(elementType);
if (elementType.IsByRef || elementType.IsByRefLike)
throw new NotSupportedException(SR.NotSupported_ByRefLikeArray);
if (elementType == typeof(void))
throw new NotSupportedException(SR.NotSupported_VoidArray);
if (elementType.ContainsGenericParameters)
throw new NotSupportedException(SR.NotSupported_OpenType);

if (pLowerBounds != null)
{
Expand Down Expand Up @@ -93,7 +98,8 @@ private static unsafe Array InternalCreateFromArrayType(RuntimeType arrayType, i
Debug.Assert(arrayType.IsArray);
Debug.Assert(arrayType.GetArrayRank() == rank);

ValidateElementType(arrayType.GetElementType());
if (arrayType.ContainsGenericParameters)
throw new NotSupportedException(SR.NotSupported_OpenType);

if (pLowerBounds != null)
{
Expand Down Expand Up @@ -125,16 +131,6 @@ private static unsafe Array InternalCreateFromArrayType(RuntimeType arrayType, i
}
}

private static void ValidateElementType(Type elementType)
{
if (elementType.IsByRef || elementType.IsByRefLike)
throw new NotSupportedException(SR.NotSupported_ByRefLikeArray);
if (elementType == typeof(void))
throw new NotSupportedException(SR.NotSupported_VoidArray);
if (elementType.ContainsGenericParameters)
throw new NotSupportedException(SR.NotSupported_OpenType);
}

public unsafe void Initialize()
{
EETypePtr pElementEEType = ElementEEType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1826,10 +1826,6 @@ public static unsafe IEnumerable<object[]> CreateInstance_TestData()
new object[] { typeof(GenericInterface<int>), default(GenericInterface<int>) },
new object[] { typeof(AbstractClass), default(AbstractClass) },
new object[] { typeof(StaticClass), default(StaticClass) },

// Arrays of void arrays
new object[] { typeof(void).MakeArrayType(), null },
new object[] { typeof(void).MakeArrayType(1), null },
};
}

Expand Down Expand Up @@ -1950,10 +1946,7 @@ public void CreateInstance_NotSupportedType_ThrowsNotSupportedException(Type ele
}

[Theory]
[InlineData(typeof(void))]
[InlineData(typeof(GenericClass<>))]
// not using any by-ref type here as MakeArrayType throws for them, same goes for Type.GetType("SomeByRef[]")
[ActiveIssue("https://github.com/dotnet/runtime/issues/94086", typeof(PlatformDetection), nameof(PlatformDetection.IsMonoRuntime))]
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
public void CreateInstanceFromArrayType_NotSupportedArrayType_ThrowsNotSupportedException(Type elementType)
{
Assert.Throws<NotSupportedException>(() => Array.CreateInstanceFromArrayType(elementType.MakeArrayType(), 0));
Expand Down
Loading