Browse Source

Fix #3181: missing type information on NullReferenceExpression used with nullable value types.

pull/3365/head
Siegfried Pammer 4 months ago
parent
commit
ae776716dd
  1. 10
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/NullPropagation.cs
  2. 14
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

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

@ -316,5 +316,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -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
}
}
}

14
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -626,20 +626,30 @@ namespace ICSharpCode.Decompiler.CSharp @@ -626,20 +626,30 @@ 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));
}
}
protected internal override TranslatedExpression VisitSizeOf(SizeOf inst, TranslationContext context)
{

Loading…
Cancel
Save