Browse Source

Do not convert extension method syntax to LINQ, if the null conditional operator is used on the target, as this introduces a syntax error.

pull/1213/head
Siegfried Pammer 7 years ago
parent
commit
10a0c9093c
  1. 2
      ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs
  2. 11
      ICSharpCode.Decompiler/CSharp/Transforms/IntroduceQueryExpressions.cs

2
ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs

@ -70,7 +70,7 @@ namespace ICSharpCode.Decompiler.Tests
try { try {
RunWithTest("ICSharpCode.Decompiler", "ICSharpCode.Decompiler.dll", "ICSharpCode.Decompiler.Tests.exe"); RunWithTest("ICSharpCode.Decompiler", "ICSharpCode.Decompiler.dll", "ICSharpCode.Decompiler.Tests.exe");
} catch (CompilationFailedException) { } catch (CompilationFailedException) {
Assert.Ignore("C# 7 tuples not yet supported."); Assert.Ignore("C# 7 local functions not yet supported.");
} }
} }

11
ICSharpCode.Decompiler/CSharp/Transforms/IntroduceQueryExpressions.cs

@ -84,7 +84,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
if (invocation == null) if (invocation == null)
return null; return null;
MemberReferenceExpression mre = invocation.Target as MemberReferenceExpression; MemberReferenceExpression mre = invocation.Target as MemberReferenceExpression;
if (mre == null) if (mre == null || IsNullConditional(mre.Target))
return null; return null;
switch (mre.MemberName) { switch (mre.MemberName) {
case "Select": case "Select":
@ -135,6 +135,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
Expression collectionSelector; Expression collectionSelector;
if (!MatchSimpleLambda(invocation.Arguments.ElementAt(0), out parameterName, out collectionSelector)) if (!MatchSimpleLambda(invocation.Arguments.ElementAt(0), out parameterName, out collectionSelector))
return null; return null;
if (IsNullConditional(collectionSelector))
return null;
LambdaExpression lambda = invocation.Arguments.ElementAt(1) as LambdaExpression; LambdaExpression lambda = invocation.Arguments.ElementAt(1) as LambdaExpression;
if (lambda != null && lambda.Parameters.Count == 2 && lambda.Body is Expression) { if (lambda != null && lambda.Parameters.Count == 2 && lambda.Body is Expression) {
ParameterDeclaration p1 = lambda.Parameters.ElementAt(0); ParameterDeclaration p1 = lambda.Parameters.ElementAt(0);
@ -210,6 +212,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
return null; return null;
Expression source1 = mre.Target; Expression source1 = mre.Target;
Expression source2 = invocation.Arguments.ElementAt(0); Expression source2 = invocation.Arguments.ElementAt(0);
if (IsNullConditional(source2))
return null;
string elementName1, elementName2; string elementName1, elementName2;
Expression key1, key2; Expression key1, key2;
if (!MatchSimpleLambda(invocation.Arguments.ElementAt(1), out elementName1, out key1)) if (!MatchSimpleLambda(invocation.Arguments.ElementAt(1), out elementName1, out key1))
@ -243,6 +247,11 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
} }
} }
bool IsNullConditional(Expression target)
{
return target is UnaryOperatorExpression uoe && uoe.Operator == UnaryOperatorType.NullConditional;
}
/// <summary> /// <summary>
/// This fixes #437: Decompilation of query expression loses material parentheses /// This fixes #437: Decompilation of query expression loses material parentheses
/// We wrap the expression in parentheses if: /// We wrap the expression in parentheses if:

Loading…
Cancel
Save