Skip to content

Commit

Permalink
Fix #1906: RRs for default expressions of primitive types should use …
Browse files Browse the repository at this point in the history
…the correct constant value.

VB's `New Long()` is emitted as `initobj` whereas C#'s `default(long)` is emitted as `ldc.i4.0; conv.i8`.
  • Loading branch information
siegfriedpammer committed Jan 15, 2020
1 parent 91425e8 commit 6973dec
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
Expand Up @@ -82,6 +82,7 @@
<Compile Include="DisassemblerPrettyTestRunner.cs" />
<Compile Include="TestCases\Correctness\StringConcat.cs" />
<Compile Include="TestCases\Ugly\NoLocalFunctions.cs" />
<Compile Include="TestCases\VBPretty\Issue1906.cs" />
<Compile Include="TestCases\VBPretty\Select.cs" />
<None Include="TestCases\ILPretty\WeirdEnums.cs" />
<Compile Include="TestCases\ILPretty\ConstantBlobs.cs" />
Expand All @@ -93,6 +94,7 @@
<Compile Include="TestCases\Ugly\NoForEachStatement.cs" />
<None Include="TestCases\Ugly\NoExtensionMethods.Expected.cs" />
<Compile Include="TestCases\Ugly\NoExtensionMethods.cs" />
<None Include="TestCases\VBPretty\Issue1906.vb" />
<None Include="TestCases\VBPretty\Select.vb" />
<None Include="TestCases\VBPretty\VBCompoundAssign.cs" />
<Compile Include="TestCases\Pretty\ThrowExpressions.cs" />
Expand Down
9 changes: 9 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/VBPretty/Issue1906.cs
@@ -0,0 +1,9 @@
using System;

public class Issue1906
{
public void M()
{
Console.WriteLine(Math.Min(Math.Max(long.MinValue, default(long)), long.MaxValue));
}
}
6 changes: 6 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/VBPretty/Issue1906.vb
@@ -0,0 +1,6 @@
Imports System
Public Class Issue1906
Public Sub M()
Console.WriteLine(Math.Min(Math.Max(Int64.MinValue, New Long), Int64.MaxValue))
End Sub
End Class
6 changes: 6 additions & 0 deletions ICSharpCode.Decompiler.Tests/VBPrettyTestRunner.cs
Expand Up @@ -78,6 +78,12 @@ public void Select([ValueSource(nameof(defaultOptions))] CompilerOptions options
Run(options: options | CompilerOptions.Library);
}

[Test]
public void Issue1906([ValueSource(nameof(defaultOptions))] CompilerOptions options)
{
Run(options: options | CompilerOptions.Library);
}

void Run([CallerMemberName] string testName = null, CompilerOptions options = CompilerOptions.UseDebug, DecompilerSettings settings = null)
{
var vbFile = Path.Combine(TestCasePath, testName + ".vb");
Expand Down
5 changes: 4 additions & 1 deletion ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
Expand Up @@ -536,14 +536,17 @@ internal ExpressionWithResolveResult GetDefaultValueExpression(IType type)
{
Expression expr;
IType constantType;
object constantValue;
if (type.IsReferenceType == true || type.IsKnownType(KnownTypeCode.NullableOfT)) {
expr = new NullReferenceExpression();
constantType = SpecialType.NullType;
constantValue = null;
} else {
expr = new DefaultValueExpression(ConvertType(type));
constantType = type;
constantValue = CSharpResolver.GetDefaultValue(type);
}
return expr.WithRR(new ConstantResolveResult(constantType, null));
return expr.WithRR(new ConstantResolveResult(constantType, constantValue));
}

protected internal override TranslatedExpression VisitSizeOf(SizeOf inst, TranslationContext context)
Expand Down

0 comments on commit 6973dec

Please sign in to comment.