-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Clarify diagnostic when constant pattern has non-constant conversion to input type #64741
Clarify diagnostic when constant pattern has non-constant conversion to input type #64741
Conversation
It looks like there are no tests added or modified. |
var conversion = convertedExpression as BoundConversion; | ||
if (conversion != null && conversion.ConversionKind == ConversionKind.ImplicitUserDefined) | ||
{ | ||
diagnostics.Add(ErrorCode.ERR_NonConstantConversionInConstantPattern, patternExpression.Location, conversion.Operand.Type!, conversion.Type); |
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.
I think in some cases conversion.Operand
might not correspond to the syntactic operand for the conversion, Therefore, the type could be surprising and the error message would not make sense because, for example, there is a conversion that supports constant folding between the types.
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.
I have updated this to use expression
instead - this should be the expression being bound and as I understand that does makes sense. Stepping through a few unit tests confirmed my beliefs. This change however, caused errors when the expression was a null literal so I have written a case to show ErrorCode.ERR_ValueCantBeNull when null literals are attempted to be converted to a non-nullable type.
For the following snipped:
static bool M1(Span<char> chars) => chars is null;
This yields the following message:
// (5,50): error CS0037: Cannot convert null to 'Span<char>' because it is a non-nullable value type
instead of
// (5,50): error CS0150: A constant value is expected
Affected test cases were adjusted to this change.
Looking forward to hear your feedback on this change.
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.
What kind of conversion is actually used for Span<char> x = null
?
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.
That appears to be an ImplicitReference conversion.
This was wrong -- its an implicit user defined conversion. I have reverted the changes with respect to the null literal changes as that was not necessary.
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.
The approach using expression
did not work when explicit casts were involved, then the message would effectively turn into "cannot convert 'A' to 'A'". I have now used the SymbolDistinguisher with conversion.Operand
as I have seen being used here and there when reporting 'no possible conversion' errors.
I did read up a little on constant folding and I do wonder, are there actually situations where constant folding results in type conversions?
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.
Now, the error message in a pattern which attempts to convert null to Span would become CS9098: Cannot implicitly convert type 'char[]' to type 'Span<char>' using user-defined conversion in pattern expression
.
Im feeling conflicted about this, one one hand its technically correct but it could be confusing. Should there perhaps be a special message for null?
Done with review pass (commit 6). |
Thanks for opening the PR. Will take a look as soon as I am able. |
fabef2c
to
2500017
Compare
It looks like some test baselines needs to be updated. https://dev.azure.com/dnceng-public/public/_build/results?buildId=214143&view=ms.vss-test-web.build-test-results-tab For example:
|
I've updated the tests (I hope I got all of them). Oddly enough The failure reported here I couldn't reproduce locally as the baseline was already set to expect the new error code.. any clue what might be going on there? |
src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
Outdated
Show resolved
Hide resolved
Done with review pass (commit 37) |
src/Compilers/CSharp/Test/Emit2/FlowAnalysis/FlowDiagnosticTests.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Test/IOperation/IOperation/IOperationTests_ISwitchOperation.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesVsPatterns.cs
Outdated
Show resolved
Hide resolved
Done with review pass (commit 40) |
addressed feedback |
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.
LGTM (commit 43)
@RikkiGibson For the second review |
Thanks for the contribution @ArcadeMode! |
This proposes a fix for #63476
The newly introduced error message goes for example:
error CS9096: Cannot implicitly convert type 'string' to type 'XName' in constant pattern using non-constant conversion
error CS9098: Cannot implicitly convert type 'string' to type 'XName' using user-defined conversion in pattern expression