Skip to content
Permalink
Browse files

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`.
  • Loading branch information
siegfriedpammer committed Jan 15, 2020
1 parent 91425e8 commit 6973dec0ef46bb48c1c4227e03d3d01b6d9b410a
@@ -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" />
@@ -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" />
@@ -0,0 +1,9 @@
using System;

public class Issue1906
{
public void M()
{
Console.WriteLine(Math.Min(Math.Max(long.MinValue, default(long)), long.MaxValue));
}
}
@@ -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
@@ -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");
@@ -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)

0 comments on commit 6973dec

Please sign in to comment.
You can’t perform that action at this time.