Browse Source

Merge pull request #4086 from icsharpcode/fix/ref-conditional-assignment-parens

Parenthesize a ref-conditional assignment target
fix/lambda-parameter-syntax
Daniel Grunwald 2 weeks ago committed by GitHub
parent
commit
a9acf77193
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 7
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/RefLocalsAndReturns.cs
  2. 6
      ICSharpCode.Decompiler/CSharp/OutputVisitor/InsertParenthesesVisitor.cs

7
ICSharpCode.Decompiler.Tests/TestCases/Pretty/RefLocalsAndReturns.cs

@ -343,6 +343,13 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -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);

6
ICSharpCode.Decompiler/CSharp/OutputVisitor/InsertParenthesesVisitor.cs

@ -475,8 +475,10 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor @@ -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);
}

Loading…
Cancel
Save