vyper icon indicating copy to clipboard operation
vyper copied to clipboard

Ternary operator unable to return `empty` type

Open pcaversaccio opened this issue 2 years ago • 1 comments

Version Information

  • vyper Version (output of vyper --version): 0.3.9
  • OS: linux
  • Python Version (output of python --version): 3.11.3

What's your issue about?

The following snippet will throw the error Exception: Weird code element: ~empty <empty(uint256)>:

@external
@pure
def ceil_div(x: uint256, y: uint256) -> uint256:
    assert y != empty(uint256), "Math: ceil_div division by zero"
    return empty(uint256) if (x == empty(uint256)) else unsafe_add(unsafe_div(x - 1, y), 1)

To fix it, simply replace empty(uint256) with 0.

@external
@pure
def ceil_div(x: uint256, y: uint256) -> uint256:
    assert y != empty(uint256), "Math: ceil_div division by zero"
    return 0 if (x == empty(uint256)) else unsafe_add(unsafe_div(x - 1, y), 1)

How can it be fixed?

Allow returning empty type with the ternary operator.

pcaversaccio avatar Jun 05 '23 14:06 pcaversaccio

Some minimal repro for the tests when the issue will be fixed:

@external
def foo():
    success: address = empty(address) if True else empty(address)

trocher avatar Sep 18 '23 16:09 trocher