Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Static field in value type with empty generic causes TypeLoadException #88030

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

Closed
Aidiakapi opened this issue Jun 26, 2023 · 2 comments
Closed

Comments

@Aidiakapi
Copy link

Description

public struct Foo<T>
{
}

public struct Bar
{
    public static readonly Foo<Bar> Baz;
}

When loading the Bar type, a TypeLoadException is thrown.

Reproduction Steps

new Bar("Hello world!");

public struct Foo<T>
{
}

public struct Bar
{
    public static readonly Foo<Bar> Baz;
    public Bar(string message) => System.Console.WriteLine(message);
}

Expected behavior

I expect the type to load successfully, or at the very least for it to be a compile time diagnostic, rather than a runtime exception.

Actual behavior

TypeLoadException at runtime.

Regression?

I don't know any specifics, but testing on SharpLab shows that it's been broken on all .NET (Framework & Core) versions.

Known Workarounds

Moving the static field to a different type. For example:

public struct Foo<T>
{
}

public struct Bar
{
    public static class Workaround {
        public static readonly Foo<Bar> Baz;
    }
}

Alternatively, making either type a class will also workaround the issue.

Configuration

No response

Other information

This issue is an overlap of #5479 (but that has an actually recursive type in a static field), and #6924 (which is generally about unused generics).

The other issues are both closed/merged and locked to contributors. Yet the issue remains, hence I'm opening this issue.

@ghost ghost added area-TypeSystem-coreclr untriaged New issue has not been triaged by the area owner labels Jun 26, 2023
@mangod9 mangod9 removed the untriaged New issue has not been triaged by the area owner label Jun 28, 2023
@mangod9 mangod9 added this to the Future milestone Jun 28, 2023
@hamarb123
Copy link
Contributor

hamarb123 commented Apr 12, 2024

I ran into a very similar issue to this also in #100950 with Nullable<S1>

@steveisok
Copy link
Member

Closing at 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

4 participants