Repository navigation
parser: apply the C# rules to binary operators on enums - #31
Merged
Merged
Conversation
Binary operators accepted an enum operand together with an operand of the same enum or of exactly its underlying type, applied any operator to the underlying values, and converted the result back to the enum, except for comparisons. So DayOfWeek.Friday * 2 returned 10 as a DayOfWeek, '>>>' was a signed shift like '>>', and DayOfWeek.Friday - DayOfWeek.Monday returned Thursday instead of 4. An operand of a type that converts implicitly to the underlying type, such as a byte, was rejected. Follow the C# rules for an enum type E with the underlying type U: - Accept E + U, U + E, and E - U, which return E, when the other operand converts implicitly to U, as for a byte or char operand of an int-based enum. Like the C# compiler, also accept U - E, which returns E. - Return U from E - E. - Accept '&', '|', '^', and the comparison operators only for two operands of type E, so x == 1 and x & 1 are now rejected, as in C#. - Reject E + E and the operators that C# does not define for enums, such as '*', '/', '%', and the shifts. - Convert the integer literal 0 to E, so x & 0 and x == 0 keep working. As in the C# compiler, x - 0 selects E - U if the literal is of type U, and E - E otherwise, as in x - 0L or 0 - x. - Give the null literal the type U? in x + null for an operand of a nullable enum type, since E + E is not defined.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Binary operators accepted an enum operand together with an operand of the same enum or of exactly its underlying type, applied any operator to the underlying values, and converted the result back to the enum, except for comparisons. So
DayOfWeek.Friday * 2returned10as aDayOfWeek,>>>was a signed shift like>>, andDayOfWeek.Friday - DayOfWeek.MondayreturnedThursdayinstead of4. An operand of a type that converts implicitly to the underlying type, such as abyte, was rejected.This PR follows the C# rules for an enum type
Ewith the underlying typeU:E + U,U + E, andE - U, which returnE, when the other operand converts implicitly toU, as for abyteorcharoperand of anint-based enum. Like the C# compiler, also acceptsU - E, which returnsE.UfromE - E.&,|,^, and the comparison operators only for two operands of typeE, sox == 1andx & 1are now rejected, as in C#.E + Eand the operators that C# does not define for enums, such as*,/,%, and the shifts.0toE, sox & 0andx == 0keep working. As in the C# compiler,x - 0selectsE - Uif the literal is of typeU, andE - Eotherwise, as inx - 0Lor0 - x.U?inx + nullfor an operand of a nullable enum type, sinceE + Eis not defined.Expressions that compare an enum with a nonzero number or combine them with
&,|, or^, such asStatus == 1orFlags & 4, no longer parse. Either operand can be converted explicitly, as inStatus == 1 :> Statusor(Status :> int) == 1. Expressions that applied*,/,%, a shift, or+to two enum operands no longer parse either.E - Enow returns the underlying type, sox - DayOfWeek.Mondayfor aDayOfWeek? xreturns anint?instead of aDayOfWeek?.Only the literal
0converts to an enum. Other constant expressions with the value zero, such as-0or0 :> byte, are not recognized, sox & -0, which parsed before, is now rejected, though C# accepts it. The C# compiler also converts a zero of a floating-point ordecimaltype, as inx == 0.0, which the specification does not allow; such expressions are still rejected.