Skip to content
Merged
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
36 changes: 27 additions & 9 deletions src/DelegateDecompiler.Tests/Issue298.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,46 @@ public class Issue298 : DecompilerTestsBase
[Test]
public void TestNotConstrained()
{
static int NotConstrained<T>(T value) where T : ITestInterface => ((ITestInterface)value).Value;

Expression<Func<TestClass, int>> expected = value => value.Value;
Test(NotConstrained, expected);
static int Actual<T>(T value) where T : ITestInterface => ((ITestInterface)value).Field;

Test(Actual<TestClass>,
value => value.Field,
value => ((ITestInterface)value).Field
);
}

[Test]
public void TestConstrained()
{
static int Constrained<T>(T value) where T : ITestInterface => value.Value;
static int Actual<T>(T value) where T : ITestInterface => value.Field;

Expression<Func<TestClass, int>> expected = value => value.Value;
Test(Constrained, expected);
Test(Actual<ITestInterface>, value => value.Field);
Test(Actual<TestClass>, value => value.Field);
}

#if NET6_0_OR_GREATER
[Test]
public void TestConstrainedDefaultImplementation()
{
static int Actual<T>(T value, int i) where T : ITestInterface => value.Method(i);
static Expression<Func<T, int, int>> Expected<T>() where T : ITestInterface => (value, i) => value.Method(i);

Test(Actual, Expected<ITestInterface>());
Test(Actual, Expected<TestClass>());
}
#endif

interface ITestInterface
{
public int Value { get; }
public int Field { get; }

#if NET6_0_OR_GREATER
public int Method(int i) => i;
#endif
}

class TestClass : ITestInterface
{
public int Value { get; set; }
public int Field { get; set; }
}
}
3 changes: 2 additions & 1 deletion src/DelegateDecompiler/Processor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,10 @@ static int ProcessInstruction(ProcessorState state, Instruction instruction)
{
Debug.WriteLine(instruction);

if (instruction.OpCode == OpCodes.Nop ||
if (instruction.OpCode == OpCodes.Nop ||
instruction.OpCode == OpCodes.Break ||
instruction.OpCode == OpCodes.Ret ||
instruction.OpCode.OpCodeType == OpCodeType.Prefix ||
instruction.OpCode.FlowControl == FlowControl.Branch)
{
// Do nothing
Expand Down
12 changes: 4 additions & 8 deletions src/DelegateDecompiler/Processors/ConvertTypeProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,16 @@ internal class ConvertTypeProcessor : IProcessor
{
public static void Register(Dictionary<OpCode, IProcessor> processors)
{
processors.Register(new ConvertTypeProcessor(),
OpCodes.Castclass,
processors.Register(new ConvertTypeProcessor(),
OpCodes.Castclass,
OpCodes.Unbox,
OpCodes.Unbox_Any,
OpCodes.Constrained);
OpCodes.Unbox_Any);
}

public void Process(ProcessorState state, Instruction instruction)
{
var expression = state.Stack.Pop();
var targetType = (Type)instruction.Operand;
state.Stack.Push(expression.Type != targetType
? Expression.Convert(expression, targetType)
: expression);
state.Stack.Push(Expression.Convert(expression, (Type)instruction.Operand));
}
}
}