Skip to content

parser: restrict '!' to logical negation - #14

Merged
rameel merged 1 commit into
mainfrom
parser/logical-not
Nov 17, 2026
Merged

rameel merged 1 commit into
mainfrom
parser/logical-not

Conversation

@rameel

@rameel rameel commented Nov 17, 2026

Copy link
Copy Markdown
Owner

The ! operator silently worked as ~ for operands other than bool. It was built with Expression.Not, which performs a bitwise complement on integers and falls back to op_OnesComplement for types without op_LogicalNot. As a result, !1 evaluated to -2, !DayOfWeek.Friday to -6, and !x called operator ~ on a type that defined only that operator.

This PR applies ! only to bool, bool?, and types that define operator !, as C# does, and reports Unary operator '!' cannot be applied to operand of type '…' for other operands. A user-defined operator ! is now also found when it is declared in a base class, and it is still lifted for nullable operands.

Expressions that applied ! to integers or enums now fail and should use ~ instead.

The '!' operator was built with Expression.Not, which performs a
bitwise complement on integers and falls back to op_OnesComplement for
types without op_LogicalNot. As a result, '!' silently worked as '~':
'!1' evaluated to -2, '!DayOfWeek.Friday' to -6, and '!x' called
'operator ~' on a type that defined only that operator.

Apply '!' only to bool, bool?, and types that define 'operator !', as
C# does, and report an error for other operands. A user-defined
operator is now also found when it is declared in a base class, and
it is still lifted for nullable operands.
@rameel
rameel merged commit 6441a08 into main Nov 17, 2026
1 check passed
@rameel
rameel deleted the parser/logical-not branch November 17, 2026 16:13
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