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;