From 10a0c9093c8f9f3281e646ed1ce04cd0fe304a89 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Tue, 26 Jun 2018 11:51:13 +0200 Subject: [PATCH] Do not convert extension method syntax to LINQ, if the null conditional operator is used on the target, as this introduces a syntax error. --- ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs | 2 +- .../CSharp/Transforms/IntroduceQueryExpressions.cs | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs b/ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs index 96890b853..e80b714d0 100644 --- a/ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs +++ b/ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs @@ -70,7 +70,7 @@ namespace ICSharpCode.Decompiler.Tests try { RunWithTest("ICSharpCode.Decompiler", "ICSharpCode.Decompiler.dll", "ICSharpCode.Decompiler.Tests.exe"); } catch (CompilationFailedException) { - Assert.Ignore("C# 7 tuples not yet supported."); + Assert.Ignore("C# 7 local functions not yet supported."); } } diff --git a/ICSharpCode.Decompiler/CSharp/Transforms/IntroduceQueryExpressions.cs b/ICSharpCode.Decompiler/CSharp/Transforms/IntroduceQueryExpressions.cs index 1be532a46..38e1c35a1 100644 --- a/ICSharpCode.Decompiler/CSharp/Transforms/IntroduceQueryExpressions.cs +++ b/ICSharpCode.Decompiler/CSharp/Transforms/IntroduceQueryExpressions.cs @@ -84,7 +84,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms if (invocation == null) return null; MemberReferenceExpression mre = invocation.Target as MemberReferenceExpression; - if (mre == null) + if (mre == null || IsNullConditional(mre.Target)) return null; switch (mre.MemberName) { case "Select": @@ -135,6 +135,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms Expression collectionSelector; if (!MatchSimpleLambda(invocation.Arguments.ElementAt(0), out parameterName, out collectionSelector)) return null; + if (IsNullConditional(collectionSelector)) + return null; LambdaExpression lambda = invocation.Arguments.ElementAt(1) as LambdaExpression; if (lambda != null && lambda.Parameters.Count == 2 && lambda.Body is Expression) { ParameterDeclaration p1 = lambda.Parameters.ElementAt(0); @@ -210,6 +212,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms return null; Expression source1 = mre.Target; Expression source2 = invocation.Arguments.ElementAt(0); + if (IsNullConditional(source2)) + return null; string elementName1, elementName2; Expression key1, key2; 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; + } + /// /// This fixes #437: Decompilation of query expression loses material parentheses /// We wrap the expression in parentheses if: