Skip to content

Commit

Permalink
fix[lang]: fix pow folding when args are not literals (#3949)
Browse files Browse the repository at this point in the history
this commit fixes folding of the `**` operator when the arguments are
constants, but not literals.
  • Loading branch information
charles-cooper committed Apr 15, 2024
1 parent 54c033b commit e1adb7b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions tests/functional/codegen/types/numbers/test_exponents.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ def f0():
compile_code(code)


def test_fold_nonliteral(get_contract):
# test we can fold non-literal constants
code = """
N: constant(uint256) = 3
N2: public(constant(uint256)) = N**N
"""
c = get_contract(code)
assert c.N2() == 3**3


@pytest.mark.fuzzing
@pytest.mark.parametrize("power", range(2, 255))
def test_exp_uint256(get_contract, tx_failed, power):
Expand Down
4 changes: 2 additions & 2 deletions vyper/semantics/types/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ def validate_numeric_op(

def _get_lr():
if isinstance(node, vy_ast.BinOp):
return node.left, node.right
return node.left.reduced(), node.right.reduced()
elif isinstance(node, vy_ast.AugAssign):
return node.target, node.value
return node.target.reduced(), node.value.reduced()
else:
raise CompilerPanic(f"Unexpected node type for numeric op: {type(node).__name__}")

Expand Down

0 comments on commit e1adb7b

Please sign in to comment.