Null coalescing in definite assignment#51567
Null coalescing in definite assignment#51567RikkiGibson merged 12 commits intodotnet:features/improved-definite-assignmentfrom
Conversation
5da748d to
b86f85d
Compare
| int x, y; | ||
| _ = c?.M(out x, out y) ?? true | ||
| ? x.ToString() // 1 | ||
| : y.ToString(); |
There was a problem hiding this comment.
Note that DefiniteAssignment has a behavior where once a diagnostic is reported on a variable, it will not report a diagnostic on that variable again. So in order to verify that e.g. a diagnostic is produced or not in both branches, it's more reliable to just use a different variable in each branch.
| else | ||
| { | ||
| var savedState = this.State.Clone(); | ||
| var savedState = node.LeftOperand is BoundConditionalAccess |
There was a problem hiding this comment.
node.LeftOperand is BoundConditionalAccess [](start = 33, length = 42)
This doesn't feel right. What if the left side is a cast on top of a null-conditional access? #Closed
There was a problem hiding this comment.
There might be a need to change this to handle certain BoundConversions here, but the specification doesn't currently indicate that a ?? operand which is a cast containing a conditional access should do anything differently than today. However, it may be a good idea to change that. #Closed
There was a problem hiding this comment.
I was unable to find scenarios where the LHS of a ?? is a BoundConversion where:
- there is no explicit cast
- there are no errors on the node
- Operand is a BoundConditionalAccess
Therefore, I put an assertion and a PROTOTYPE in the code for the moment.
When debugging the bootstrap failures I did experiment a fair bit with different scenarios to try and hit the assertion. If you have ideas for scenarios that would actually hit the assertion as written, that would be welcome.
When we start handling more kinds of containing expressions, I feel like the correct way to handle these conversion nodes is going to become more clear, so I'd like to punt it to the next PR (for ==/!=) if that is acceptable.
#Closed
There was a problem hiding this comment.
I would expect to see a conversion on the left side for a scenario like this:
C c = null;
D d = c ?? default(D);
class C {}
struct D
{
public static implicit operator D(C c) => default;
}Make the c variable a conditionally-access method with a null, and we should be able to observe behavior.
In reply to: 595506242 [](ancestors = 595506242)
|
Done review pass (commit 2) #Closed |
|
I believe I have addressed your comments @333fred. Please let me know if you have any more. |
This comment has been minimized.
This comment has been minimized.
jcouv
left a comment
There was a problem hiding this comment.
Done with review pass (iteration 7)
|
Changes in f4d9231 and dfcef64 are dependent on this spec change: dotnet/csharplang#4554 |
|
Done review pass (commit 9). I got through some of the tests, but some assumptions appear to be incorrect and I'll wait to look more in depth until they're addressed. |
|
Have addressed most of the feedback, but have not yet removed the tests of dubious value. |
| [Theory] | ||
| [InlineData("explicit")] | ||
| [InlineData("implicit")] | ||
| public void NullCoalescing_CondAccess_ExplicitUserDefinedConv_05(string conversionKind) |
There was a problem hiding this comment.
Could you please add a test where the left-hand side is an NVT of an enum type?
333fred
left a comment
There was a problem hiding this comment.
LGTM (commit 9), modulo one more test suggestion.
|
@jcouv Please let me know if you have any feedback on the changes since your last sign-off. |
Related to #51463
This PR implements the following section of the spec: