Skip to content

Commit

Permalink
fix: block bitwise ops on decimals (#3219)
Browse files Browse the repository at this point in the history
it doesn't make too much sense to allow like `x ^ y` for decimals
  • Loading branch information
charles-cooper authored Jan 6, 2023
1 parent 13ebcbe commit 77f1c31
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
12 changes: 11 additions & 1 deletion tests/parser/types/numbers/test_decimals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from vyper.exceptions import DecimalOverrideException, TypeMismatch
from vyper.exceptions import DecimalOverrideException, InvalidOperation, TypeMismatch
from vyper.utils import DECIMAL_EPSILON, SizeLimits


Expand All @@ -23,6 +23,16 @@ def test_decimal_override():
)


@pytest.mark.parametrize("op", ["**", "&", "|", "^"])
def test_invalid_ops(get_contract, assert_compile_failed, op):
code = f"""
@external
def foo(x: decimal, y: decimal) -> decimal:
return x {op} y
"""
assert_compile_failed(lambda: get_contract(code), InvalidOperation)


def quantize(x: Decimal) -> Decimal:
return x.quantize(DECIMAL_EPSILON, rounding=ROUND_DOWN)

Expand Down
3 changes: 3 additions & 0 deletions vyper/ast/nodes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ class Mult(VyperNode): ...
class Div(VyperNode): ...
class Mod(VyperNode): ...
class Pow(VyperNode): ...
class BitAnd(VyperNode): ...
class BitOr(VyperNode): ...
class BitXor(VyperNode): ...

class BoolOp(ExprNode):
op: VyperNode = ...
Expand Down
2 changes: 1 addition & 1 deletion vyper/semantics/types/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class DecimalT(NumericT):
_decimal_places = 10 # TODO generalize
_id = "decimal"
_is_signed = True
_invalid_ops = (vy_ast.Pow,)
_invalid_ops = (vy_ast.Pow, vy_ast.BitAnd, vy_ast.BitOr, vy_ast.BitXor)
_valid_literal = (vy_ast.Decimal,)

_equality_attrs = ("_bits", "_decimal_places")
Expand Down

0 comments on commit 77f1c31

Please sign in to comment.