Skip to content

Commit 5a902c4

Browse files
committed
Add more specific diagnostic message for conditional-access on unconstrained generic
1 parent 670884f commit 5a902c4

File tree

4 files changed

+9
-1
lines changed

4 files changed

+9
-1
lines changed

src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11496,7 +11496,10 @@ internal static bool MethodOrLambdaRequiresValue(Symbol symbol, CSharpCompilatio
1149611496

1149711497
private BoundConditionalAccess GenerateBadConditionalAccessNodeError(ConditionalAccessExpressionSyntax node, BoundExpression receiver, BoundExpression access, BindingDiagnosticBag diagnostics)
1149811498
{
11499-
DiagnosticInfo diagnosticInfo = new CSDiagnosticInfo(ErrorCode.ERR_CannotBeMadeNullable, access.Display);
11499+
var errorCode = !accessType.IsReferenceType && !accessType.IsValueType
11500+
? ErrorCode.ERR_ConditionalAccessNotReferenceOrValueType
11501+
: ErrorCode.ERR_CannotBeMadeNullable;
11502+
DiagnosticInfo diagnosticInfo = new CSDiagnosticInfo(errorCode, access.Display);
1150011503
diagnostics.Add(new CSDiagnostic(diagnosticInfo, access.Syntax.Location));
1150111504
receiver = BadExpression(receiver.Syntax, receiver);
1150211505

src/Compilers/CSharp/Portable/CSharpResources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8183,4 +8183,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
81838183
<data name="ERR_AmbigOperator" xml:space="preserve">
81848184
<value>Operator resolution is ambiguous between the following members: '{0}' and '{1}'</value>
81858185
</data>
8186+
<data name="ERR_ConditionalAccessNotReferenceTypeOrValueType" xml:space="preserve">
8187+
<value>Cannot use the result of a conditional access of type '{0}' because 'null' cannot be converted to '{0}?'.</value>
8188+
</data>
81868189
</root>

src/Compilers/CSharp/Portable/Errors/ErrorCode.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,6 +2441,7 @@ internal enum ErrorCode
24412441
ERR_AmbigOperator = 9342,
24422442

24432443
ERR_UnexpectedArgumentListInBaseTypeWithoutParameterList = 9343,
2444+
ERR_ConditionalAccessNotReferenceOrValueType = 9344,
24442445

24452446
// Note: you will need to do the following after adding errors:
24462447
// 1) Update ErrorFacts.IsBuildOnlyDiagnostic (src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs)

src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2548,6 +2548,7 @@ or ErrorCode.ERR_SingleInapplicableBinaryOperator
25482548
or ErrorCode.ERR_SingleInapplicableUnaryOperator
25492549
or ErrorCode.ERR_AmbigOperator
25502550
or ErrorCode.ERR_UnexpectedArgumentListInBaseTypeWithoutParameterList
2551+
or ErrorCode.ERR_ConditionalAccessNotReferenceOrValueType
25512552
=> false,
25522553
};
25532554
#pragma warning restore CS8524 // The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value.

0 commit comments

Comments
 (0)