dotnet / csharpstandard

Working space for ECMA-TC49-TG2, the C# standard committee.
Creative Commons Attribution 4.0 International
704 stars 83 forks source link

[C# language specification 7th edition] Incorrect rule for definite-assignment state for || expressions #1070

Open fmdkara opened 5 months ago

fmdkara commented 5 months ago

I downloaded the C# language specification 7th edition via https://ecma-international.org/publications-and-standards/standards/ecma-334/. In chapter 9.4.4.27 about definite-assignment state of || expressions, it was written

The definite-assignment state of v before expr_second is definitely assigned if and only if the state of v after expr_first is either definitely assigned or “definitely assigned after true expression”. Otherwise, it is not definitely assigned.

However, i believe that is incorrect, the correct one should be

The definite-assignment state of v before expr_second is definitely assigned if and only if the state of v after expr_first is either definitely assigned or “definitely assigned after false expression”. Otherwise, it is not definitely assigned.

Because if the former is correct, then v can be definitely-assigned when it shouldn't be, consider the following situation:

  1. The definite-assignment state of v after expr_first is definitely assigned after true expression
  2. expr_first is false
  3. expr_second is evaluated and the definite-assignment state of v before expr_second is definitely-assigned, however because number 1 and 2, then v is not actually assigned.

From the reason above, i believe that the former is incorrect and the latter is the correct one

jskeet commented 5 months ago

Assigned to @gafter to confirm. The fact that it's currently the same for || as for && (both use "definitely assigned after true expression") rings alarm bells with me though.