Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5631,17 +5631,32 @@ private BoundExpression BindNullCoalescingAssignmentOperator(AssignmentExpressio
ReportSuppressionIfNeeded(leftOperand, diagnostics);
BoundExpression rightOperand = BindValue(node.Right, diagnostics, BindValueKind.RValue);

// If either operand is bad, bail out preventing more cascading errors
if (leftOperand.HasAnyErrors || rightOperand.HasAnyErrors)
TypeSymbol leftType = leftOperand.Type;

var rightOperandTargetType = leftType switch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var rightOperandTargetType = leftType switch

We probably don't want to do this calculation for success scenarios.

{
{ IsReferenceType: true } or { IsValueType: false, TypeKind: TypeKind.TypeParameter } => leftType,
{ IsValueType: true } when leftType.IsNullableType() => leftType.GetNullableUnderlyingType(),
Copy link
Contributor

@AlekseyTs AlekseyTs Oct 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leftType.GetNullableUnderlyingType()

Perhaps this would make sense when we are dealing with target-typed new, but I am not sure about other forms of the right operand.

_ => null
};

// If left operand is bad or right operator is bad and we cannot determine its type, take the default error recovery path
if (leftOperand.HasAnyErrors || (rightOperandTargetType is null && rightOperand.HasAnyErrors))
{
leftOperand = BindToTypeForErrorRecovery(leftOperand);
rightOperand = BindToTypeForErrorRecovery(rightOperand);
return new BoundNullCoalescingAssignmentOperator(node, leftOperand, rightOperand, CreateErrorType(), hasErrors: true);
}
// If right operand is bad, but we know its type, make sure conversion is in place for better error recovery
else if (rightOperand.HasAnyErrors)
{
leftOperand = BindToTypeForErrorRecovery(leftOperand);
var conversion = GenerateConversionForAssignment(rightOperandTargetType, rightOperand, diagnostics, ConversionForAssignmentFlags.CompoundAssignment);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diagnostics

Let's suppress errors from this recovery.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GenerateConversionForAssignment

Perhaps this would make sense when we are dealing with target-typed new, but I am not sure about other forms of the right operand.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GenerateConversionForAssignment

We probably can pass the type to BindToTypeForErrorRecovery to get the same effect.

return new BoundNullCoalescingAssignmentOperator(node, leftOperand, conversion, rightOperandTargetType, hasErrors: true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rightOperandTargetType

Why is this the right value to use?

}

// Given a ??= b, the type of a is A, the type of B is b, and if A is a nullable value type, the underlying
// non-nullable value type of A is A0.
TypeSymbol leftType = leftOperand.Type;
Debug.Assert((object)leftType != null);

// If A is a non-nullable value type, a compile-time error occurs
Expand Down
Loading