-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
System.TypeLoadException When Interface Type Contains Static Field/Property for Derived Struct Type #104511
Comments
@EgorBo it seems an exception is being thrown from runtime/src/coreclr/vm/prestub.cpp Line 939 in e733c2f
Perhaps this is a JIT issue? |
Mono does something funny here too. If you run with the JIT it prints:
If you run with the interpreter, it works. using System;
public interface IExample
{
public static Example DefaultExample { get; } = new();
}
public struct Example : IExample { }
public class Program
{
public static void Main()
{
var example = IExample.DefaultExample;
Console.WriteLine(example.GetType());
}
} <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseMonoRuntime>true</UseMonoRuntime>
<SelfContained>true</SelfContained>
</PropertyGroup>
</Project>
% dotnet run
Unhandled Exception:
System.TypeLoadException: Recursive type definition detected .IExample
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeLoadException: Recursive type definition detected .IExample
% MONO_ENV_OPTIONS=--interp dotnet run
Example |
Actually if you change it to public interface IExample
{
public static Example DefaultExample { get; } = default(Example);
} That's a TLE in CoreCLR (and Mono JIT) too - and in that case we don't even have a cctor in |
It does look similar. I didn't find those beforehand when searching for existing issues to comment on because my search was focused on I will leave this issue open for the time being until there is further confirmation that this is indeed a duplicate. |
The good news is that we know the problem is a recursive type loading issue and that if we defer the type being fully loaded to a second pass, then it'll likely start working. The bad news is that it's too late in the cycle to take such a change as it has the potential to create other type loading issues. That's pretty risky and so we'll try to fix this early on in the .NET 10 cycle. @AndrewDRX my advice is to continue to use the workaround that you posted. Thanks for raising this issue 👍 |
Just wanted to flag one of the cases raised in one of the duplicate issues, in case its helpful. This isn't unique to interfaces: using System;
using System.Collections.Immutable;
Console.WriteLine(new MyStruct());
public struct MyStruct
{
static ImmutableArray<MyStruct> One;
} For this case, the workaround isn't really suitable (as there could be a fair bit of data that we don't want to recreate every time). Specifically, |
@JakeYallop thanks! We won't lose sight of what's in the duplicate issues as test cases to help validate the fixes. |
struct Bar<T>;
struct Foo
{
public Inner inner;
public struct Inner
{
public Bar<Foo> a;
}
} |
Description
A
System.TypeLoadException
exception is thrown when aninterface
contains either a static field or property with an initial value for a derivedstruct
type. This works for derivedrecord
andclass
types.Reproduction Steps
Expected behavior
Behavior should be consistent for where an
interface
can be statically aware of derived types.Actual behavior
Behavior is inconsistent for where an
interface
can be statically aware of derived types.Regression?
No response
Known Workarounds
See "Other Information" section.
Configuration
No response
Other information
If the
struct
type is changed to arecord
orclass
type then the error is not seen.E.g.
Or
If the static property or field is changed from having an initial value to instead be a property with a getter or an expression-bodied property then the error is not seen.
E.g.
Or
The text was updated successfully, but these errors were encountered: