Browse Source

Workaround dotnet/roslyn#43659 in decompiled code.

pull/1994/head
Daniel Grunwald 5 years ago
parent
commit
b7754cdf5c
  1. 8
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/NullableRefTypes.cs
  2. 12
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

8
ICSharpCode.Decompiler.Tests/TestCases/Pretty/NullableRefTypes.cs

@ -79,6 +79,14 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -79,6 +79,14 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
set;
}
public event EventHandler? Event;
public static int? NullConditionalOperator(T02_EverythingIsNullableInHere? x)
{
// This code throws if `x != null && x.field1 == null`.
// But we can't decompile it to the warning-free "x?.field1!.Length",
// because of https://github.com/dotnet/roslyn/issues/43659
return x?.field1.Length;
}
}
public class T03_EverythingIsNotNullableInHere

12
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -2027,7 +2027,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -2027,7 +2027,7 @@ namespace ICSharpCode.Decompiler.CSharp
.WithRR(new ResolveResult(NullableType.GetUnderlyingType(translatedTarget.Type)))
.WithoutILInstruction();
}
translatedTarget = EnsureTargetNotNullable(translatedTarget);
translatedTarget = EnsureTargetNotNullable(translatedTarget, target);
return translatedTarget;
}
} else {
@ -2055,12 +2055,18 @@ namespace ICSharpCode.Decompiler.CSharp @@ -2055,12 +2055,18 @@ namespace ICSharpCode.Decompiler.CSharp
}
}
private TranslatedExpression EnsureTargetNotNullable(TranslatedExpression expr)
private TranslatedExpression EnsureTargetNotNullable(TranslatedExpression expr, ILInstruction inst)
{
// inst is the instruction that got translated into expr.
if (expr.Type.Nullability == Nullability.Nullable) {
if (expr.Expression is UnaryOperatorExpression uoe && uoe.Operator == UnaryOperatorType.NullConditional) {
return expr;
}
if (inst.HasFlag(InstructionFlags.MayUnwrapNull)) {
// We can't use ! in the chain of operators after a NullConditional, due to
// https://github.com/dotnet/roslyn/issues/43659
return expr;
}
return new UnaryOperatorExpression(UnaryOperatorType.SuppressNullableWarning, expr)
.WithRR(new ResolveResult(expr.Type.ChangeNullability(Nullability.Oblivious)))
.WithoutILInstruction();
@ -2153,7 +2159,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -2153,7 +2159,7 @@ namespace ICSharpCode.Decompiler.CSharp
if (arrayExpr.Type.Kind != TypeKind.Array) {
arrayExpr = arrayExpr.ConvertTo(arrayType, this);
}
arrayExpr = EnsureTargetNotNullable(arrayExpr);
arrayExpr = EnsureTargetNotNullable(arrayExpr, inst.Array);
string memberName;
KnownTypeCode code;
if (inst.ResultType == StackType.I4) {

Loading…
Cancel
Save