-
Couldn't load subscription status.
- Fork 4.2k
Improve compiler error recovery for null coalesce assignment operator #80907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| { | ||
| { IsReferenceType: true } or { IsValueType: false, TypeKind: TypeKind.TypeParameter } => leftType, | ||
| { IsValueType: true } when leftType.IsNullableType() => leftType.GetNullableUnderlyingType(), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| _ => 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return new BoundNullCoalescingAssignmentOperator(node, leftOperand, conversion, rightOperandTargetType, hasErrors: true); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| // 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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably don't want to do this calculation for success scenarios.