Repository navigation
parser: restrict '!' to logical negation - #14
Merged
Merged
Conversation
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.
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.
The
!operator silently worked as~for operands other thanbool. It was built withExpression.Not, which performs a bitwise complement on integers and falls back toop_OnesComplementfor types withoutop_LogicalNot. As a result,!1evaluated to-2,!DayOfWeek.Fridayto-6, and!xcalledoperator ~on a type that defined only that operator.This PR applies
!only tobool,bool?, and types that defineoperator !, as C# does, and reportsUnary operator '!' cannot be applied to operand of type '…'for other operands. A user-definedoperator !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.