diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/NullPropagation.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/NullPropagation.cs index a38a7e4b0..b47aa5f58 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/NullPropagation.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/NullPropagation.cs @@ -316,5 +316,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty { return a?.b.c(1)?.d[10]; } + + private static string Issue3181() + { +#if EXPECTED_OUTPUT + return ((int?)null)?.ToString(); +#else + int? x = null; + return x?.ToString(); +#endif + } } } diff --git a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs index 2b32f842b..63737a2d4 100644 --- a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs @@ -626,19 +626,29 @@ namespace ICSharpCode.Decompiler.CSharp Expression expr; IType constantType; object constantValue; - if (type.IsReferenceType == true || type.IsKnownType(KnownTypeCode.NullableOfT)) + if (type.IsReferenceType == true) { expr = new NullReferenceExpression(); constantType = SpecialType.NullType; constantValue = null; + return expr.WithRR(new ConstantResolveResult(constantType, constantValue)); + } + else if (type.IsKnownType(KnownTypeCode.NullableOfT)) + { + expr = new NullReferenceExpression(); + constantType = SpecialType.NullType; + constantValue = null; + var crr = new ConstantResolveResult(constantType, constantValue); + return new CastExpression(ConvertType(type), expr.WithRR(crr)) + .WithRR(new ConversionResolveResult(type, crr, Conversion.NullLiteralConversion)); } else { expr = new DefaultValueExpression(ConvertType(type)); constantType = type; constantValue = CSharpResolver.GetDefaultValue(type); + return expr.WithRR(new ConstantResolveResult(constantType, constantValue)); } - return expr.WithRR(new ConstantResolveResult(constantType, constantValue)); } protected internal override TranslatedExpression VisitSizeOf(SizeOf inst, TranslationContext context)