-
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
Generic method constrained by struct & interface can not see readonly modifier #76355
Comments
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. |
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch Issue DetailsDescriptionIf you have a generic method with a struct & interface constrain and that interface contains a method, it is not able to see that the method implemented in the struct is readonly, so it ends up creating defensive copies even though it shouldn't need to. ConfigurationAny version, even on main Regression?No. Datausing System;
using SharpLab.Runtime;
using System.Runtime.CompilerServices;
public class C {
[JitGeneric(typeof(S))]
public void MIn<T>(in T s)
where T : struct, IInterface
{
s.Test();
}
[JitGeneric(typeof(S))]
public void MRef<T>(ref T s)
where T : struct, IInterface
{
s.Test();
}
}
public interface IInterface {
void Test();
}
public readonly struct S : IInterface
{
public readonly long P1;
public readonly long P2;
public readonly long P3;
public readonly long P4;
[MethodImpl(MethodImplOptions.NoInlining)]
public readonly void Test()
{
}
}
|
This is rather a problem with C#. The IL already includes codes to create a defensive copy, as the C# compiler does not know whether |
See also dotnet/csharplang#3055. |
Ah, there was that issue that I once saw ... I know I read it somewhere, but I couldn't find it anymore. Thanks for linking it. |
I don't think there is much the JIT can do here. From the IL it sees the proposed optimization would be illegal -- it would change output of a program such as the following: public class C
{
public void MIn<T>(in T s)
where T : struct, IInterface
{
s.Test();
}
public void MRef<T>(ref T s)
where T : struct, IInterface
{
s.Test();
}
public unsafe static void Main()
{
S s = new S();
S.F = &s;
new C().MIn(in s);
}
}
public interface IInterface
{
void Test();
}
public unsafe readonly struct S : IInterface
{
public readonly long P1;
public readonly long P2;
public readonly long P3;
public readonly long P4;
[MethodImpl(MethodImplOptions.NoInlining)]
public readonly void Test()
{
fixed (S* pThis = &this)
Console.WriteLine(pThis == F);
}
public static S* F;
} |
Closing the issue. @bollhals, please reopen if you still think that JIT has to change something.
|
Description
If you have a generic method with a struct & interface constrain and that interface contains a method, it is not able to see that the method implemented in the struct is readonly, so it ends up creating defensive copies even though it shouldn't need to.
Configuration
Any version, even on main
Regression?
No.
Data
Example on SharpLab:
The text was updated successfully, but these errors were encountered: