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

TypeLoadException when using a struct containing a static ImmutableArray<T> field/auto-property #100077

Closed
colejohnson66 opened this issue Mar 21, 2024 · 4 comments
Assignees
Milestone

Comments

@colejohnson66
Copy link

colejohnson66 commented Mar 21, 2024

Description

An interesting one: consuming a DLL that exposes a type containing a static ImmutableArray<T> property. When Program.Main is async, you get a TypeLoadException at stateMachine.MoveNext(). When Program.Main is sync, the program simply never starts.

I've uploaded a minimum reproducible example. For preservation purposes, I've also attached a ZIP archive of the repository.

If you change the provided Test() method to directly print Struct.One (as below, and replace Console.WriteLine(Test()) to Test()), then both sync and async versions will run all the way until Test() (including printing "Start" to the console, even for the sync version).

private static void Test()
{
    Console.WriteLine(Struct.One);
}

Reproduction Steps

Create a library that contains a type, MyType. That type should have a static property of type ImmutableArray<MyType>. Optionally, initialize it with a collection expression in a static constructor. Our original code does contain a collection expression in the static constructor, but, if I recall correctly, commenting out the static constructor does not solve the issue (so it's not an exception in .cctor).

Build the library and take the DLL. Manually reference the DLL in the consuming project and attempt to use the type. Notice that the type exists, and that the compiler does not complain. At runtime, observe a TypeLoadException.

Expected behavior

The program runs normally.

Actual behavior

A TypeLoadException at runtime once usage of the type is encountered.

Regression?

No response

Known Workarounds

Use a ReadOnlyCollection<MyType>. We were previously doing so with Array.AsReadOnly(new MyType[] {...}), but changing to use ImmutableArray<MyType> broke it.

Configuration

  • .NET 8.0.3
  • Windows 11 x86-64 23H2

Other information

ImmutableArrayPropertyTypeLoadException-main.zip

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Mar 21, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Mar 21, 2024
@vcsjones vcsjones added area-TypeSystem-coreclr and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Mar 21, 2024
@colejohnson66
Copy link
Author

Opening Library.dll in dotPeek confirms that the type is present:

image

@steveisok
Copy link
Member

/cc @kg

@steveisok steveisok added this to the 9.0.0 milestone Jun 14, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Jun 14, 2024
@JakeYallop
Copy link
Contributor

Just ran into this one myself. I'd done some refactoring and replaced a MyStruct[] with an ImmutableArray<MyStruct>. I was able to reproduce it running unit tests. You also don't need to reference a library to reproduce this, just creating a brand new console app with the struct in causes the issue:

This is all that's necessary:
sharplab

using System;
using System.Collections.Immutable;

Console.WriteLine(new MyStruct());

public struct MyStruct
{
    static ImmutableArray<MyStruct> One;
}

@colejohnson66 colejohnson66 changed the title TypeLoadException when using a type containing a static ImmutableArray<T> property when Main is async TypeLoadException when using a struct containing a static ImmutableArray<T> field/auto-property Jul 8, 2024
@steveisok
Copy link
Member

Closing as it's a duplicate of #104511

@github-actions github-actions bot locked and limited conversation to collaborators Aug 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants