Skip to content

Commit ae77671

Browse files
Fix #3181: missing type information on NullReferenceExpression used with nullable value types.
1 parent 1853439 commit ae77671

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

ICSharpCode.Decompiler.Tests/TestCases/Pretty/NullPropagation.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,5 +316,15 @@ private static dynamic DynamicNullProp(dynamic a)
316316
{
317317
return a?.b.c(1)?.d[10];
318318
}
319+
320+
private static string Issue3181()
321+
{
322+
#if EXPECTED_OUTPUT
323+
return ((int?)null)?.ToString();
324+
#else
325+
int? x = null;
326+
return x?.ToString();
327+
#endif
328+
}
319329
}
320330
}

ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,19 +626,29 @@ internal ExpressionWithResolveResult GetDefaultValueExpression(IType type)
626626
Expression expr;
627627
IType constantType;
628628
object constantValue;
629-
if (type.IsReferenceType == true || type.IsKnownType(KnownTypeCode.NullableOfT))
629+
if (type.IsReferenceType == true)
630630
{
631631
expr = new NullReferenceExpression();
632632
constantType = SpecialType.NullType;
633633
constantValue = null;
634+
return expr.WithRR(new ConstantResolveResult(constantType, constantValue));
635+
}
636+
else if (type.IsKnownType(KnownTypeCode.NullableOfT))
637+
{
638+
expr = new NullReferenceExpression();
639+
constantType = SpecialType.NullType;
640+
constantValue = null;
641+
var crr = new ConstantResolveResult(constantType, constantValue);
642+
return new CastExpression(ConvertType(type), expr.WithRR(crr))
643+
.WithRR(new ConversionResolveResult(type, crr, Conversion.NullLiteralConversion));
634644
}
635645
else
636646
{
637647
expr = new DefaultValueExpression(ConvertType(type));
638648
constantType = type;
639649
constantValue = CSharpResolver.GetDefaultValue(type);
650+
return expr.WithRR(new ConstantResolveResult(constantType, constantValue));
640651
}
641-
return expr.WithRR(new ConstantResolveResult(constantType, constantValue));
642652
}
643653

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

0 commit comments

Comments
 (0)