From 2f00eef4eec50acc304c18913eca69a1f22457a7 Mon Sep 17 00:00:00 2001 From: ds5678 <49847914+ds5678@users.noreply.github.com> Date: Mon, 28 Apr 2025 20:13:52 -0700 Subject: [PATCH] Implement revisions * Update Lambda1 to be invariant * Visit descendents before deciding whether or not to parenthesize an interpolation expression * Rename local function * Remove branch for conditional expressions * Handle Lambda expressions without a block body * Check for parenthesized expressions --- ...ballyQualifiedTypeInStringInterpolation.cs | 4 ++++ .../OutputVisitor/InsertParenthesesVisitor.cs | 20 ++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/GloballyQualifiedTypeInStringInterpolation.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/GloballyQualifiedTypeInStringInterpolation.cs index c4d1acb50..0759a9e51 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/GloballyQualifiedTypeInStringInterpolation.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/GloballyQualifiedTypeInStringInterpolation.cs @@ -4,7 +4,11 @@ { public static string Root => $"Prefix {(global::System.DateTime.Now)} suffix"; public static string Cast => $"Prefix {((int)global::System.DateTime.Now.Ticks)} suffix"; +#if CS100 && !NET40 + public static string Lambda1 => $"Prefix {(() => global::System.DateTime.Now)} suffix"; +#else public static string Lambda1 => $"Prefix {(global::System.Func)(() => global::System.DateTime.Now)} suffix"; +#endif public static string Lambda2 => $"Prefix {((global::System.Func)(() => global::System.DateTime.Now))()} suffix"; public static string Method1 => $"Prefix {M(global::System.DateTime.Now)} suffix"; public static string Method2 => $"Prefix {(global::System.DateTime.Now.Ticks)} suffix"; diff --git a/ICSharpCode.Decompiler/CSharp/OutputVisitor/InsertParenthesesVisitor.cs b/ICSharpCode.Decompiler/CSharp/OutputVisitor/InsertParenthesesVisitor.cs index ac20204c4..ff6c22651 100644 --- a/ICSharpCode.Decompiler/CSharp/OutputVisitor/InsertParenthesesVisitor.cs +++ b/ICSharpCode.Decompiler/CSharp/OutputVisitor/InsertParenthesesVisitor.cs @@ -392,28 +392,30 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor public override void VisitInterpolation(Interpolation interpolation) { + // Need to do this first, in case the descendents parenthesize themselves. + base.VisitInterpolation(interpolation); + // If an interpolation contains global::, we need to parenthesize the expression. - if (InterpolationNeedsParenthesized(interpolation)) + if (InterpolationNeedsParenthesis(interpolation)) Parenthesize(interpolation.Expression); - base.VisitInterpolation(interpolation); - static bool InterpolationNeedsParenthesized(AstNode node) + static bool InterpolationNeedsParenthesis(AstNode node) { if (node is MemberType { IsDoubleColon: true }) return true; - if (node is ConditionalExpression) - return false; // Conditional expressions are parenthesized in their own visit method. - if (node is AnonymousMethodExpression or LambdaExpression) + if (node is ParenthesizedExpression) + return false; + if (node is AnonymousMethodExpression or LambdaExpression { Body: BlockStatement }) return false; if (node is InvocationExpression invocation) - return InterpolationNeedsParenthesized(invocation.Target); + return InterpolationNeedsParenthesis(invocation.Target); if (node is CastExpression cast) - return InterpolationNeedsParenthesized(cast.Expression); + return InterpolationNeedsParenthesis(cast.Expression); foreach (var child in node.Children) { - if (InterpolationNeedsParenthesized(child)) + if (InterpolationNeedsParenthesis(child)) return true; } return false;