Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,12 @@ public async Task Issue3877([ValueSource(nameof(roslyn2OrNewerWithNet40Options))
await RunForLibrary(cscOptions: cscOptions);
}

[Test]
public async Task Issue3965([ValueSource(nameof(roslyn4OrNewerOptions))] CompilerOptions cscOptions)
{
await RunForLibrary(cscOptions: cscOptions);
}

[Test]
public async Task AssemblyCustomAttributes([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
{
Expand Down
29 changes: 29 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Pretty/Issue3965.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public void Run(Block block, BlockTransformContext context)
{
if (CachedDelegateInitializationWithField(inst))
{
block.Instructions.RemoveAt(i);
context.IndexOfFirstAlreadyTransformedInstruction = block.Instructions.Count;
continue;
}
Expand Down Expand Up @@ -95,13 +94,25 @@ bool CachedDelegateInitializationWithField(IfInstruction inst)
if (!DelegateConstruction.MatchDelegateConstruction(value.UnwrapConv(ConversionKind.Invalid) as NewObj, out _, out _, out _, true))
return false;
var nextInstruction = inst.Parent.Children.ElementAtOrDefault(inst.ChildIndex + 1);
if (nextInstruction == null)
return false;
var usages = nextInstruction.Descendants.Where(i => i.MatchLdsFld(field)).ToArray();
var usages = nextInstruction?.Descendants.Where(i => i.MatchLdsFld(field)).ToArray() ?? [];
if (usages.Length == 0)
{
// A discarded method-group conversion ("_ = (Action)M;") caches the
// delegate without ever reading the cache back. Keep the conversion,
// which still allocates, and drop the caching around it -- but only
// once the rest of the method is known not to read the field either.
if (context.Function.Descendants.Any(i => i != left && i.MatchLdsFld(field)))
return false;
context.Step("CachedDelegateInitializationWithField (unused)", inst);
inst.ReplaceWith(value);
context.EndStep(value);
return true;
}
if (usages.Length != 1)
return false;
context.Step("CachedDelegateInitializationWithField", inst);
usages[0].ReplaceWith(value);
((Block)inst.Parent).Instructions.RemoveAt(inst.ChildIndex);
context.EndStep(value);
return true;
}
Expand Down
Loading