Browse Source

Fix #1924: preserve hexadecimal format when converting literal to wider type

pull/1930/head
Daniel Grunwald 5 years ago
parent
commit
ad5ba9295e
  1. 2
      ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj
  2. 6
      ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs
  3. 15
      ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1922.cs
  4. 59
      ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1922.il
  5. 6
      ICSharpCode.Decompiler/CSharp/TranslatedExpression.cs

2
ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj

@ -60,6 +60,7 @@ @@ -60,6 +60,7 @@
<None Include="TestCases\Correctness\StackTests.il" />
<None Include="TestCases\Correctness\StackTypes.il" />
<None Include="TestCases\Correctness\Uninit.vb" />
<None Include="TestCases\ILPretty\Issue1922.il" />
<None Include="TestCases\ILPretty\WeirdEnums.il" />
<None Include="TestCases\ILPretty\ConstantBlobs.il" />
<None Include="TestCases\ILPretty\CS1xSwitch_Debug.il" />
@ -81,6 +82,7 @@ @@ -81,6 +82,7 @@
<ItemGroup>
<Compile Include="DisassemblerPrettyTestRunner.cs" />
<Compile Include="TestCases\Correctness\StringConcat.cs" />
<Compile Include="TestCases\ILPretty\Issue1922.cs" />
<Compile Include="TestCases\Ugly\NoLocalFunctions.cs" />
<Compile Include="TestCases\VBPretty\Issue1906.cs" />
<Compile Include="TestCases\VBPretty\Select.cs" />

6
ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs

@ -94,6 +94,12 @@ namespace ICSharpCode.Decompiler.Tests @@ -94,6 +94,12 @@ namespace ICSharpCode.Decompiler.Tests
Run();
}
[Test]
public void Issue1922()
{
Run();
}
[Test]
public void FSharpUsing_Debug()
{

15
ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1922.cs

@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
namespace Issue1922
{
public class Program
{
public static long fnWorks(int x, int y)
{
return (((long)y & 0xFFFFFFL) << 24) | ((long)x & 0xFFFFFFL);
}
public static long fnFails(int x, int y)
{
return ((y & 0xFFFFFFL) << 24) | (x & 0xFFFFFFL);
}
}
}

59
ICSharpCode.Decompiler.Tests/TestCases/ILPretty/Issue1922.il

@ -0,0 +1,59 @@ @@ -0,0 +1,59 @@
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
.ver 4:0:0:0
}
.assembly Issue1922
{
.hash algorithm 0x00008004
.ver 1:0:4059:39717
}
.module Issue1389.dll
.imagebase 0x00400000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000003 // ILONLY 32BITREQUIRED
.class public auto ansi beforefieldinit Issue1922.Program
extends [mscorlib]System.Object
{
// Methods
.method public hidebysig static int64 fnWorks (int32 x, int32 y) cil managed
{
.maxstack 8
ldarg.1
conv.i8
ldc.i4 16777215
conv.i8
and
ldc.i4.s 24
shl
ldarg.0
conv.i8
ldc.i4 16777215
conv.i8
and
or
ret
}
.method public hidebysig static int64 fnFails (int32 x, int32 y) cil managed
{
.maxstack 8
ldarg.1
conv.i8
ldc.i8 16777215
and
ldc.i4.s 24
shl
ldarg.0
conv.i8
ldc.i8 16777215
and
or
ret
}
} // end of class Issue1922.Program

6
ICSharpCode.Decompiler/CSharp/TranslatedExpression.cs

@ -431,8 +431,12 @@ namespace ICSharpCode.Decompiler.CSharp @@ -431,8 +431,12 @@ namespace ICSharpCode.Decompiler.CSharp
}
var rr = expressionBuilder.resolver.WithCheckForOverflow(checkForOverflow).ResolveCast(targetType, ResolveResult);
if (rr.IsCompileTimeConstant && !rr.IsError) {
return expressionBuilder.ConvertConstantValue(rr, allowImplicitConversion)
var convertedResult = expressionBuilder.ConvertConstantValue(rr, allowImplicitConversion)
.WithILInstruction(this.ILInstructions);
if (convertedResult.Expression is PrimitiveExpression outputLiteral && this.Expression is PrimitiveExpression inputLiteral) {
outputLiteral.Format = inputLiteral.Format;
}
return convertedResult;
} else if (rr.IsError && targetType.IsReferenceType == true && type.IsReferenceType == true) {
// Conversion between two reference types, but no direct cast allowed? cast via object
// Just make sure we avoid infinite recursion even if the resolver falsely claims we can't cast directly:

Loading…
Cancel
Save