Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix[lang]: fix precedence in floordiv hint #4203

Merged
merged 10 commits into from
Aug 6, 2024
60 changes: 56 additions & 4 deletions tests/functional/codegen/types/numbers/test_unsigned_ints.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,65 @@ def foo():
compile_code(code)


def test_invalid_div():
code = """
div_code_with_hint = [
(
"""
@external
def foo():
a: uint256 = 5 / 9
"""
""",
"did you mean `5 // 9`?",
),
(
"""
@external
def foo():
a: uint256 = 10
a /= (3 + 10) // (2 + 3)
""",
"did you mean `a //= (3 + 10) // (2 + 3)`?",
),
(
"""
@external
def foo(a: uint256, b:uint256, c: uint256) -> uint256:
return (a + b) / c
""",
"did you mean `(a + b) // c`?",
),
(
"""
@external
def foo(a: uint256, b:uint256, c: uint256) -> uint256:
return (a + b) / (a + c)
""",
"did you mean `(a + b) // (a + c)`?",
),
(
"""
@external
def foo(a: uint256, b:uint256, c: uint256) -> uint256:
return (a + (c + b)) / (a + c)
""",
"did you mean `(a + (c + b)) // (a + c)`?",
),
(
"""
interface Foo:
def foo() -> uint256: view

@external
def foo(a: uint256, b:uint256, c: uint256) -> uint256:
return (a + b) / staticcall Foo(self).foo()
""",
"did you mean `(a + b) // staticcall Foo(self).foo()`?",
),
]


@pytest.mark.parametrize("code, expected_hint", div_code_with_hint)
def test_invalid_div(code, expected_hint):
with pytest.raises(InvalidOperation) as e:
compile_code(code)

assert e.value._hint == "did you mean `5 // 9`?"
assert e.value._hint == expected_hint
8 changes: 6 additions & 2 deletions vyper/semantics/types/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,13 @@
else:
return e

def _get_source(node):
Fixed Show fixed Hide fixed
source = node.node_source_code
return f"({source})" if isinstance(node, vy_ast.BinOp) else source

if isinstance(node, vy_ast.BinOp):
e._hint = f"did you mean `{node.left.node_source_code} "
e._hint += f"{suggested} {node.right.node_source_code}`?"
e._hint = f"did you mean `{get_source(node.left)} "
e._hint += f"{suggested} {get_source(node.right)}`?"
elif isinstance(node, vy_ast.AugAssign):
e._hint = f"did you mean `{node.target.node_source_code} "
e._hint += f"{suggested}= {node.value.node_source_code}`?"
Expand Down
Loading