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

ArrayList.ToArray(type) throws exception for enum type in Blazor WebAssembly #64387

Closed
1 task done
jjzhang12 opened this issue Jan 27, 2022 · 6 comments · Fixed by #64469
Closed
1 task done

ArrayList.ToArray(type) throws exception for enum type in Blazor WebAssembly #64387

jjzhang12 opened this issue Jan 27, 2022 · 6 comments · Fixed by #64469
Assignees
Milestone

Comments

@jjzhang12
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Code snippet:

ArrayList list = new ArrayList();
list.Add(Day.Monday);
var array = list.ToArray(typeof(Day));

public enum Day { Monday, Tuesday }

This is working fine in .net 6 desktop. But in Blazor WebAssembly, it throws exception System.InvalidCastException: At least one element in the source array could not be cast down to the destination array type.

list.ToArray() without type is fine. But we are using type which is loaded in runtime, so the type is required.

Expected Behavior

Array created without exception

Steps To Reproduce

https://github.com/jjzhang12/WasmToArrayIssue.git

I added above code snippet to index page and you can observe the exception.

Also the .net console app is working fine.

Exceptions (if any)

System.InvalidCastException: At least one element in the source array could not be cast down to the destination array type.

.NET Version

6.0.101

Anything else?

No response

@pranavkm pranavkm transferred this issue from dotnet/aspnetcore Jan 27, 2022
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Jan 27, 2022
@pranavkm pranavkm added the arch-wasm WebAssembly architecture label Jan 27, 2022
@ghost
Copy link

ghost commented Jan 27, 2022

Tagging subscribers to 'arch-wasm': @lewing
See info in area-owners.md if you want to be subscribed.

Issue Details

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Code snippet:

ArrayList list = new ArrayList();
list.Add(Day.Monday);
var array = list.ToArray(typeof(Day));

public enum Day { Monday, Tuesday }

This is working fine in .net 6 desktop. But in Blazor WebAssembly, it throws exception System.InvalidCastException: At least one element in the source array could not be cast down to the destination array type.

list.ToArray() without type is fine. But we are using type which is loaded in runtime, so the type is required.

Expected Behavior

Array created without exception

Steps To Reproduce

https://github.com/jjzhang12/WasmToArrayIssue.git

I added above code snippet to index page and you can observe the exception.

Also the .net console app is working fine.

Exceptions (if any)

System.InvalidCastException: At least one element in the source array could not be cast down to the destination array type.

.NET Version

6.0.101

Anything else?

No response

Author: jjzhang12
Assignees: -
Labels:

arch-wasm, untriaged

Milestone: -

@ghost
Copy link

ghost commented Jan 27, 2022

Tagging subscribers to this area: @BrzVlad
See info in area-owners.md if you want to be subscribed.

Issue Details

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Code snippet:

ArrayList list = new ArrayList();
list.Add(Day.Monday);
var array = list.ToArray(typeof(Day));

public enum Day { Monday, Tuesday }

This is working fine in .net 6 desktop. But in Blazor WebAssembly, it throws exception System.InvalidCastException: At least one element in the source array could not be cast down to the destination array type.

list.ToArray() without type is fine. But we are using type which is loaded in runtime, so the type is required.

Expected Behavior

Array created without exception

Steps To Reproduce

https://github.com/jjzhang12/WasmToArrayIssue.git

I added above code snippet to index page and you can observe the exception.

Also the .net console app is working fine.

Exceptions (if any)

System.InvalidCastException: At least one element in the source array could not be cast down to the destination array type.

.NET Version

6.0.101

Anything else?

No response

Author: jjzhang12
Assignees: -
Labels:

arch-wasm, untriaged, area-Codegen-Interpreter-mono

Milestone: -

@lewing lewing removed the untriaged New issue has not been triaged by the area owner label Jan 27, 2022
@lewing lewing added this to the 6.0.x milestone Jan 27, 2022
@lewing
Copy link
Member

lewing commented Jan 27, 2022

cc @vargaz

@lewing
Copy link
Member

lewing commented Jan 27, 2022

System.InvalidCastException: InvalidCast_DownCastArrayElement
dotnet.js:13    at System.Array.CopySlow(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable) in /Users/lewing/Source/backup/runtime/src/mono/System.Private.CoreLib/src/System/Array.Mono.cs:line 203
dotnet.js:13    at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable) in /Users/lewing/Source/backup/runtime/src/mono/System.Private.CoreLib/src/System/Array.Mono.cs:line 147
dotnet.js:13    at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length) in /Users/lewing/Source/backup/runtime/src/mono/System.Private.CoreLib/src/System/Array.Mono.cs:line 121
dotnet.js:13    at System.Array.Copy(Array sourceArray, Array destinationArray, Int32 length) in /Users/lewing/Source/backup/runtime/src/mono/System.Private.CoreLib/src/System/Array.Mono.cs:line 115
dotnet.js:13    at System.Collections.ArrayList.ToArray(Type type) in /Users/lewing/Source/backup/runtime/src/libraries/System.Private.CoreLib/src/System/Collections/ArrayList.cs:line 727

@lewing lewing assigned lambdageek and unassigned BrzVlad Jan 27, 2022
@lambdageek
Copy link
Member

lambdageek commented Jan 28, 2022

It's hitting this check added by mono/mono#15585

if (!src_type.IsValueType && dst_is_enum)
throw new InvalidCastException(SR.InvalidCast_DownCastArrayElement);

which doesn't look right to me (it's comparing src_type which is the array element type which doesn't change each time through the loop. I think it actually wants srcval.GetType())

Repro with:

var list = new System.Collections.ArrayList();
list.Add(Day.Monday);
Console.WriteLine ($"First elt is a {list[0].GetType()}");
var array = list.ToArray(typeof(Day));

foreach (var x in array)
    Console.WriteLine ($"{x}");;


public enum Day { Monday, Tuesday }
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <PropertyGroup>
    <RuntimeIdentifier>linux-x64</RuntimeIdentifier>
    <UseMonoRuntime>true</UseMonoRuntime>
    <SelfContained>true</SelfContained>
  </PropertyGroup>
</Project>

Issue happens with both the JIT and the interpreter (as expected)

lambdageek added a commit to lambdageek/runtime that referenced this issue Jan 28, 2022
When we have to resort to checking element by element, compare the type of each
actual element with the destination type. In particular, not the destinations
underlying type when it's an enum - we don't want to allow unrelated enums
using the same representation to copy over.

Fixes dotnet#64387
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jan 28, 2022
github-actions bot pushed a commit that referenced this issue Jan 28, 2022
When we have to resort to checking element by element, compare the type of each
actual element with the destination type. In particular, not the destinations
underlying type when it's an enum - we don't want to allow unrelated enums
using the same representation to copy over.

Fixes #64387
lambdageek added a commit that referenced this issue Jan 30, 2022
* Add regression test for object[] -> Int32Enum[] array copy

where each element in the source array is the appropriate type

* Fix downcast check in slow array copy

When we have to resort to checking element by element, compare the type of each
actual element with the destination type. In particular, not the destinations
underlying type when it's an enum - we don't want to allow unrelated enums
using the same representation to copy over.

Fixes #64387
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jan 30, 2022
safern pushed a commit that referenced this issue Feb 7, 2022
* Add regression test for object[] -> Int32Enum[] array copy

where each element in the source array is the appropriate type

* Fix downcast check in slow array copy

When we have to resort to checking element by element, compare the type of each
actual element with the destination type. In particular, not the destinations
underlying type when it's an enum - we don't want to allow unrelated enums
using the same representation to copy over.

Fixes #64387

Co-authored-by: Aleksey Kliger <alklig@microsoft.com>
@ghost ghost locked as resolved and limited conversation to collaborators Mar 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants