Skip to content

Commit

Permalink
Merge pull request #1277 from Viridovics/issue-1256
Browse files Browse the repository at this point in the history
Fix #1256: Add cast to object for reference types in ExpressionBuilder.VisitUnboxAny
  • Loading branch information
siegfriedpammer committed Oct 1, 2018
2 parents 1f9ea4c + 57cea66 commit 4f0c218
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 4 deletions.
Expand Up @@ -70,9 +70,11 @@
<Compile Include="DataFlowTest.cs" />
<Compile Include="TestCases\Correctness\LocalFunctions.cs" />
<Compile Include="TestCases\Correctness\RefLocalsAndReturns.cs" />
<Compile Include="TestCases\ILPretty\Issue1256.cs" />
<Compile Include="TestCases\Pretty\OptionalArguments.cs" />
<Compile Include="TestCases\Pretty\CustomShortCircuitOperators.cs" />
<Compile Include="TestCases\Pretty\TypeTests.cs" />
<None Include="TestCases\ILPretty\Issue1256.il" />
<None Include="TestCases\Ugly\NoDecimalConstants.Expected.cs" />
<Compile Include="TestCases\Ugly\NoDecimalConstants.cs" />
<None Include="TestCases\Disassembler\Pretty\SecurityDeclarations.il" />
Expand Down
6 changes: 6 additions & 0 deletions ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs
Expand Up @@ -118,6 +118,12 @@ public void Issue1157()
Run();
}

[Test]
public void Issue1256()
{
Run();
}

[Test]
public void FSharpLoops_Debug()
{
Expand Down
17 changes: 17 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1256.cs
@@ -0,0 +1,17 @@
using System;

namespace ICSharpCode.Decompiler.Tests.TestCases.ILPretty
{
internal class Issue1256
{
public void Method(Enum e, object o, string s)
{
int num = (int)(object)e;
object obj = new object();
int num2 = (int)obj;
long num3 = (long)o;
int num4 = (int)(object)s;
int num5 = (int)num3;
}
}
}
58 changes: 58 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1256.il
@@ -0,0 +1,58 @@
.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.ILPretty.Issue1256
extends [mscorlib]System.Object
{
// Methods
.method public hidebysig
instance void Method (
class [mscorlib]System.Enum e,
object o,
string s
) cil managed
{
// Method begins at RVA 0x2050
// Code size 41 (0x29)
.maxstack 1
.locals init (
[0] int32,
[1] object,
[2] int32,
[3] int64,
[4] int32,
[5] int32
)

IL_0000: nop
IL_0001: ldarg.1
IL_0002: unbox.any [mscorlib]System.Int32
IL_0007: stloc.0
IL_0008: newobj instance void [mscorlib]System.Object::.ctor()
IL_000d: stloc.1
IL_000e: ldloc.1
IL_000f: unbox.any [mscorlib]System.Int32
IL_0014: stloc.2
IL_0015: ldarg.2
IL_0016: unbox.any [mscorlib]System.Int64
IL_001b: stloc.3
IL_001c: ldarg.3
IL_001d: unbox.any [mscorlib]System.Int32
IL_0022: stloc.s 4
IL_0024: ldloc.3
IL_0025: conv.i4
IL_0026: stloc.s 5
IL_0028: ret
} // end of method Issue1256::Method

.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// Method begins at RVA 0x2085
// Code size 8 (0x8)
.maxstack 8

IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: nop
IL_0007: ret
} // end of method Issue1256::.ctor

} // end of class ICSharpCode.Decompiler.Tests.TestCases.ILPretty.Issue1256
9 changes: 5 additions & 4 deletions ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
Expand Up @@ -1877,10 +1877,6 @@ protected internal override TranslatedExpression VisitUnboxAny(UnboxAny inst, Tr
// isinst followed by unbox.any of the same type is used for as-casts to generic types
return arg.WithILInstruction(inst);
}
if (arg.Type.IsReferenceType != true) {
// ensure we treat the input as a reference type
arg = arg.ConvertTo(compilation.FindType(KnownTypeCode.Object), this);
}

IType targetType = inst.Type;
if (targetType.Kind == TypeKind.TypeParameter) {
Expand All @@ -1892,6 +1888,11 @@ protected internal override TranslatedExpression VisitUnboxAny(UnboxAny inst, Tr
arg = arg.ConvertTo(((ITypeParameter)targetType).EffectiveBaseClass, this);
}
}
else {
// Before unboxing arg must be a object
arg = arg.ConvertTo(compilation.FindType(KnownTypeCode.Object), this);
}

return new CastExpression(ConvertType(targetType), arg.Expression)
.WithILInstruction(inst)
.WithRR(new ConversionResolveResult(targetType, arg.ResolveResult, Conversion.UnboxingConversion));
Expand Down

0 comments on commit 4f0c218

Please sign in to comment.