Registry Mirror
Skip to content

Conversation

@siegfriedpammer

Copy link
Copy Markdown
Member

Assigning through a ref-conditional was emitted without parentheses:

// before: does not compile (CS8156 / CS0201)
c ? ref a : ref b = 3;

// after
(c ? ref a : ref b) = 3;

Without them the statement re-parses as c ? ref a : (ref b = 3), which is neither a valid expression statement nor a legal assignment to a ref-returning conditional branch.

VisitAssignmentExpression in InsertParenthesesVisitor parenthesized the target only above assignment precedence, on the grounds that assignment is right-associative. A conditional binds tighter than assignment, so a ref-conditional target cleared that bar and lost its parentheses. The target now has to have precedence above the conditional operator.

Ordinary lvalues are unaffected: locals, fields, array accesses and indexers are primary expressions, well above ?:. The postfix ++ form already came out right, because UnaryOperatorExpression parenthesizes its operand at unary precedence.

TestCases/Pretty/RefLocalsAndReturns.cs covers plain assignment, compound assignment and the postfix increment:

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)++;
}

Full ICSharpCode.Decompiler.Tests run: 3546 passed, 0 failed, 45 skipped.

🤖 Generated with Claude Code

Assigning through a ref-conditional, (cond ? ref a : ref b) = value, was
emitted without the parentheses, so it re-parsed as
cond ? ref a : (ref b = value) and failed to compile (CS8156 / CS0201).
The target was only parenthesized above assignment precedence, but a
conditional binds tighter than assignment, so the check let it through.

Require the assignment target to have precedence above the conditional
operator. Ordinary lvalues (locals, fields, indexers) are primary
expressions and are unaffected; the postfix ++ form already parenthesized
correctly via unary precedence. Covers plain and compound assignments alike.

Assisted-by: Claude:claude-opus-4-8:Claude Code
@dgrunwald
dgrunwald merged commit a9acf77 into master Aug 30, 2026
17 checks passed
@siegfriedpammer
siegfriedpammer deleted the fix/ref-conditional-assignment-parens branch August 30, 2026 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants