From 6973dec0ef46bb48c1c4227e03d3d01b6d9b410a Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Wed, 15 Jan 2020 10:18:14 +0100 Subject: [PATCH] Fix #1906: RRs for default expressions of primitive types should use 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`. --- .../ICSharpCode.Decompiler.Tests.csproj | 2 ++ .../TestCases/VBPretty/Issue1906.cs | 9 +++++++++ .../TestCases/VBPretty/Issue1906.vb | 6 ++++++ ICSharpCode.Decompiler.Tests/VBPrettyTestRunner.cs | 6 ++++++ ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs | 5 ++++- 5 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 ICSharpCode.Decompiler.Tests/TestCases/VBPretty/Issue1906.cs create mode 100644 ICSharpCode.Decompiler.Tests/TestCases/VBPretty/Issue1906.vb diff --git a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj index 888df6e51..358d3fbd8 100644 --- a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj +++ b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj @@ -82,6 +82,7 @@ + @@ -93,6 +94,7 @@ + diff --git a/ICSharpCode.Decompiler.Tests/TestCases/VBPretty/Issue1906.cs b/ICSharpCode.Decompiler.Tests/TestCases/VBPretty/Issue1906.cs new file mode 100644 index 000000000..9d85ecd89 --- /dev/null +++ b/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)); + } +} diff --git a/ICSharpCode.Decompiler.Tests/TestCases/VBPretty/Issue1906.vb b/ICSharpCode.Decompiler.Tests/TestCases/VBPretty/Issue1906.vb new file mode 100644 index 000000000..70f2bbe3e --- /dev/null +++ b/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 \ No newline at end of file diff --git a/ICSharpCode.Decompiler.Tests/VBPrettyTestRunner.cs b/ICSharpCode.Decompiler.Tests/VBPrettyTestRunner.cs index d41f6aa70..6c1df2b1e 100644 --- a/ICSharpCode.Decompiler.Tests/VBPrettyTestRunner.cs +++ b/ICSharpCode.Decompiler.Tests/VBPrettyTestRunner.cs @@ -78,6 +78,12 @@ namespace ICSharpCode.Decompiler.Tests 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"); diff --git a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs index 08506d1ee..1a5a0b2da 100644 --- a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs @@ -536,14 +536,17 @@ namespace ICSharpCode.Decompiler.CSharp { 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)