diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/RefLocalsAndReturns.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/RefLocalsAndReturns.cs index 8428b684c..9c8f34130 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/RefLocalsAndReturns.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/RefLocalsAndReturns.cs @@ -343,6 +343,13 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty RefReassignment(ref reference.GetHashCode() == 4 ? ref reference : ref s); } + public static void ConditionalRefAssignment(bool c, ref int a, ref int b) + { + (c ? ref a : ref b) = 3; + (c ? ref a : ref b) += 10; + (c ? ref b : ref a)++; + } + public static void Main(string[] args) { DoubleNumber(ref args.Length == 1 ? ref numbers[0] : ref DefaultInt); diff --git a/ICSharpCode.Decompiler/CSharp/OutputVisitor/InsertParenthesesVisitor.cs b/ICSharpCode.Decompiler/CSharp/OutputVisitor/InsertParenthesesVisitor.cs index 01148bc5f..86dce99f4 100644 --- a/ICSharpCode.Decompiler/CSharp/OutputVisitor/InsertParenthesesVisitor.cs +++ b/ICSharpCode.Decompiler/CSharp/OutputVisitor/InsertParenthesesVisitor.cs @@ -475,8 +475,10 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor { Parenthesize(assignmentExpression); } - // assignment is right-associative - ParenthesizeIfRequired(assignmentExpression.Left, PrecedenceLevel.Assignment + 1); + // assignment is right-associative. A ref-conditional target (cond ? ref a : ref b) + // has conditional precedence and would otherwise re-parse as cond ? ref a : (ref b = value), + // so the target needs precedence above ?: to keep its parentheses. + ParenthesizeIfRequired(assignmentExpression.Left, PrecedenceLevel.Conditional + 1); HandleAssignmentRHS(assignmentExpression.Right); base.VisitAssignmentExpression(assignmentExpression); }