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

Skip to content

System.TypeLoadException occurred when there is static ReadOnlyMemory<> field #95544

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

Open
bysdxt opened this issue Dec 2, 2023 · 3 comments
Open

Comments

@bysdxt
Copy link

bysdxt commented Dec 2, 2023

Description

dotnet runtime throws System.TypeLoadException when there is static field of ReadOnlyMemory<> with struct itself.

Reproduction Steps

1.new console project,
2.write code to Program.cs:

Console.WriteLine(new Test());
struct Test{
    public static ReadOnlyMemory<Test> s = ReadOnlyMemory<Test>.Empty;
}

3.build and run

Expected behavior

no exception

Actual behavior

System.TypeLoadException occurred
e

Regression?

I don't know.This is my first time using ReadOnlyMemory.

Known Workarounds

No response

Configuration

.NET SDK:
Version: 8.0.100
Commit: 57efcf1350
Workload version: 8.0.100-manifests.8d38d0cc

运行时环境:
OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.100\

已安装 .NET 工作负载:
Workload version: 8.0.100-manifests.8d38d0cc
没有要显示的已安装工作负载。

Host:
Version: 8.0.0
Architecture: x64
Commit: 5535e31

.NET SDKs installed:
8.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
Not set

global.json file:
Not found
i

Other information

The exception occurred when I using VS 17.7 with dotnet 7.0.
Then I upgrade VS to 17.8.2 with dotnet 8.0,but the exception still occurred.

@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 Dec 2, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Dec 2, 2023
@bysdxt
Copy link
Author

bysdxt commented Dec 2, 2023

code:

test.zip

@jkotas jkotas 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 Dec 2, 2023
@huoyaoyuan
Copy link
Member

Similar to #88030

@steveisok steveisok removed the untriaged New issue has not been triaged by the area owner label Jun 21, 2024
@steveisok steveisok added this to the Future milestone Jun 21, 2024
@leidegre
Copy link

leidegre commented Apr 20, 2025

Got some more information about this, seems related.

This runs fine.

Console.WriteLine(new Foo());

struct Foo
{
  public ReadOnlyMemory<Foo> Bar;
}

This does not.

Console.WriteLine(new Foo());

struct Bar
{
  public Foo Foo;
}

struct Foo
{
  public ReadOnlyMemory<Bar> Bar;
}

// System.TypeLoadException: Could not load type 'Foo' from assembly...

And neither does this.

Console.WriteLine(new Foo());

struct Foo
{
  public ReadOnlyMemory<(Foo, object)> Bar;
}

// Could not load type 'Foo' from assembly [..] because of an invalid self-referential generic field.

It is surprising to me that this succeeds in compiling but fails at runtime. Memory is just that, there should be no recursive instantiation of these struct types.


> dotnet --version
9.0.203

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants