Skip to content

parser: reject unary '-' and '+' on enum operands - #29

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

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

Conversation

@rameel

@rameel rameel commented Nov 18, 2026

Copy link
Copy Markdown
Owner

The unary - and + operators accepted an enum operand and applied to its underlying value, returning a number: for example, -DayOfWeek.Friday returned -5 and +DayOfWeek.Friday returned 5. C# does not define these operators for an enum.

This PR reports Unary operator '-' cannot be applied to operand of type 'System.DayOfWeek', as for !. The value can still be negated after an explicit conversion, such as -(DayOfWeek.Friday :> int). The ~ operator is not affected: it applies to the underlying type and returns the enum, as in C#.

Expressions that applied - or + to an enum no longer parse.

The unary '-' and '+' operators accepted an enum operand and applied
to its underlying value, returning a number: for example,
-DayOfWeek.Friday returned -5 and +DayOfWeek.Friday returned 5. C#
does not define these operators for an enum.

Report that the operator cannot be applied to the enum type, as for
'!'. The value can still be negated after an explicit conversion,
such as -(DayOfWeek.Friday :> int).
@rameel
rameel merged commit 5afa5b1 into main Nov 18, 2026
1 check passed
@rameel
rameel deleted the parser/enum-unary-operators branch November 18, 2026 12:41
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