Files
icsharpcode-ILSpy/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Issue3965.cs
T
dualfroz 167098573c Keep a discarded method-group conversion out of the delegate cache
CachedDelegateInitializationWithField only fired when the instruction
after the caching `if` read the cache field exactly once. A discarded
conversion -- `_ = (Action<int>)M;` -- reads it zero times: Roslyn emits
the null check and the store, and nothing else. The transform bailed out,
so the `<>O` cache class and its `<0>__M` field survived into the output
under names no C# compiler will accept.

Zero usages is now handled: the `if` is replaced by the delegate
construction it guarded, which is what the source expressed, and the
caching disappears with the field. The rest of the method is checked for
loads of the same field first, since only then is this `if` the sole
initialization.

Closes #3965
2026-09-19 11:14:31 +02:00

30 lines
412 B
C#

using System;
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
internal class Issue3965
{
private static void M(int x)
{
}
private static void Use(Action<int> action)
{
}
public static void DiscardedMethodGroupConversion()
{
#if EXPECTED_OUTPUT
new Action<int>(M);
#else
_ = (Action<int>)M;
#endif
}
public static void UsedMethodGroupConversion()
{
Use(M);
}
}
}