Skip to content

feat[lang]: enable bitwise ops for bytesM types#4538

Merged
charles-cooper merged 16 commits intovyperlang:masterfrom
charles-cooper:feat/bitwise-bytes
Apr 8, 2025
Merged

feat[lang]: enable bitwise ops for bytesM types#4538
charles-cooper merged 16 commits intovyperlang:masterfrom
charles-cooper:feat/bitwise-bytes

Conversation

@charles-cooper
Copy link
Copy Markdown
Member

@charles-cooper charles-cooper commented Mar 25, 2025

What I did

implement #4305

How I did it

How to verify it

Commit message

this commit adds bitwise ops (`&`, `|`, `<<`, `>>`, `^`, `~`) for
`bytesM` types. for implementation simplicity, shift and invert
operators are only implemented for `bytes32`.

Description for the changelog

Cute Animal Picture

Put a link to a cute animal picture inside the parenthesis-->

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 25, 2025

Codecov Report

Attention: Patch coverage is 55.55556% with 8 lines in your changes missing coverage. Please review.

Project coverage is 92.55%. Comparing base (0e737fc) to head (0a8d6a2).
Report is 89 commits behind head on master.

Files with missing lines Patch % Lines
vyper/codegen/expr.py 40.00% 2 Missing and 4 partials ⚠️
vyper/semantics/types/primitives.py 75.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4538      +/-   ##
==========================================
- Coverage   92.57%   92.55%   -0.02%     
==========================================
  Files         123      123              
  Lines       17456    17468      +12     
  Branches     2946     2951       +5     
==========================================
+ Hits        16160    16168       +8     
- Misses        894      896       +2     
- Partials      402      404       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cyberthirst
Copy link
Copy Markdown
Collaborator

cyberthirst commented Mar 26, 2025

def foo(x: bytes32, y: uint256) -> bytes32:
    return x >> y
vyper.exceptions.CodegenPanic: unhandled exception 'BytesM_T' object has no attribute 'is_signed', parse_BinOp

  contract "tests/custom/test.vy:5", function "foo", line 5:11 
       4 def foo(x: bytes32, y: uint256) -> bytes32:
  ---> 5     return x >> y
  ------------------^


This is an unhandled internal compiler error. Please create an issue on Github to notify the developers!
https://github.com/vyperlang/vyper/issues/new?template=bug.md
Error compiling: tests/custom/test.vy

@cyberthirst
Copy link
Copy Markdown
Collaborator

def foo(x: bytes1, y: uint256) -> bytes1:
    return x >> y
vyper.exceptions.TypeCheckFailure: unreachable

  contract "tests/custom/test.vy:2", function "foo", line 2:11 
       1 def foo(x: bytes1, y: uint256) -> bytes1:
  ---> 2     return x >> y
  ------------------^
       3


This is an unhandled internal compiler error. Please create an issue on Github to notify the developers!
https://github.com/vyperlang/vyper/issues/new?template=bug.md

@cyberthirst
Copy link
Copy Markdown
Collaborator

def test_bytes_unary(get_contract):
    src = """
def foo() -> bytes32:
    return ~0x0000000000000000000000000000000000000000000000000000000000000000
    """

    c = get_contract(src)

    vyper_ast = parse_and_fold(f"~0x0000000000000000000000000000000000000000000000000000000000000000")
    old_node = vyper_ast.body[0].value
    new_node = old_node.get_folded_value()

call to get_contract passes but then I get:

self = vyper.ast.nodes.UnaryOp:
---> 1 ~0x0000000000000000000000000000000000000000000000000000000000000000
-------^

    def get_folded_value(self) -> "ExprNode":
        """
        Attempt to get the folded value, bubbling up UnfoldableNode if the node
        is not foldable.
        """
        try:
            return self._metadata["folded_value"]
        except KeyError:
>           raise UnfoldableNode("not foldable", self)
E           vyper.exceptions.UnfoldableNode: not foldable
E           
E             line 1:0 
E             ---> 1 ~0x0000000000000000000000000000000000000000000000000000000000000000
E             -------^
E           
E           
E           This is an unhandled internal compiler error. Please create an issue on Github to notify the developers!
E           https://github.com/vyperlang/vyper/issues/new?template=bug.md

../../../../vyper/ast/nodes.py:401: UnfoldableNode

@cyberthirst
Copy link
Copy Markdown
Collaborator

i think it would be cleaner to disallow stuff like ~ for < 32 bytesm in semantics

@cyberthirst
Copy link
Copy Markdown
Collaborator

https://github.com/charles-cooper/vyper/pull/67/files adds a roundtrip test which fails on vyper grammar, we should raise an issue

@cyberthirst
Copy link
Copy Markdown
Collaborator

https://github.com/charles-cooper/vyper/pull/67/files adds a roundtrip test which fails on vyper grammar, we should raise an issue

created the issue: #4564

@charles-cooper
Copy link
Copy Markdown
Member Author

i think it would be cleaner to disallow stuff like ~ for < 32 bytesm in semantics

we may add those later. so to keep "locality" (the exception thrown near where it will be fixed), let's leave it as is.

@charles-cooper charles-cooper changed the title enable bitwise ops for bytesM types feat[lang]: enable bitwise ops for bytesM types Apr 7, 2025
@charles-cooper charles-cooper marked this pull request as ready for review April 7, 2025 17:00
@charles-cooper charles-cooper merged commit d5677b1 into vyperlang:master Apr 8, 2025
161 of 162 checks passed
@charles-cooper charles-cooper deleted the feat/bitwise-bytes branch April 8, 2025 08:21
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.

2 participants