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
{ {
return a?.b.c(1)?.d[10]; 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,19 +626,29 @@ namespace ICSharpCode.Decompiler.CSharp
Expression expr; Expression expr;
IType constantType; IType constantType;
object constantValue; object constantValue;
if (type.IsReferenceType == true || type.IsKnownType(KnownTypeCode.NullableOfT)) if (type.IsReferenceType == true)
{ {
expr = new NullReferenceExpression(); expr = new NullReferenceExpression();
constantType = SpecialType.NullType; constantType = SpecialType.NullType;
constantValue = null; 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 else
{ {
expr = new DefaultValueExpression(ConvertType(type)); expr = new DefaultValueExpression(ConvertType(type));
constantType = type; constantType = type;
constantValue = CSharpResolver.GetDefaultValue(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) protected internal override TranslatedExpression VisitSizeOf(SizeOf inst, TranslationContext context)

Loading…
Cancel
Save