Skip to content

parser: apply the C# rules to binary operators on enums - #31

Merged
rameel merged 1 commit into
mainfrom
parser/enum-binary-operators
Nov 18, 2026
Merged

rameel merged 1 commit into
mainfrom
parser/enum-binary-operators

Conversation

@rameel

@rameel rameel commented Nov 18, 2026

Copy link
Copy Markdown
Owner

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.

This PR follows the C# rules for an enum type E with the underlying type U:

  • Accepts 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 accepts U - E, which returns E.
  • Returns U from E - E.
  • Accepts &, |, ^, and the comparison operators only for two operands of type E, so x == 1 and x & 1 are now rejected, as in C#.
  • Rejects E + E and the operators that C# does not define for enums, such as *, /, %, and the shifts.
  • Converts 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.
  • Gives the null literal the type U? in x + null for an operand of a nullable enum type, since E + E is not defined.

Expressions that compare an enum with a nonzero number or combine them with &, |, or ^, such as Status == 1 or Flags & 4, no longer parse. Either operand can be converted explicitly, as in Status == 1 :> Status or (Status :> int) == 1. Expressions that applied *, /, %, a shift, or + to two enum operands no longer parse either. E - E now returns the underlying type, so x - DayOfWeek.Monday for a DayOfWeek? x returns an int? instead of a DayOfWeek?.

Only the literal 0 converts to an enum. Other constant expressions with the value zero, such as -0 or 0 :> byte, are not recognized, so x & -0, which parsed before, is now rejected, though C# accepts it. The C# compiler also converts a zero of a floating-point or decimal type, as in x == 0.0, which the specification does not allow; such expressions are still rejected.

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.
@rameel
rameel merged commit 9cc3647 into main Nov 18, 2026
1 check passed
@rameel
rameel deleted the parser/enum-binary-operators branch November 18, 2026 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant