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]: show user error in error map #4286

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
29 changes: 27 additions & 2 deletions tests/unit/compiler/test_source_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,33 @@ def update_foo():
self.foo += 1
"""
error_map = compile_code(code, output_formats=["source_map"])["source_map"]["error_map"]
assert "safeadd" in list(error_map.values())
assert "fallback function" in list(error_map.values())
assert "safeadd" in error_map.values()
assert "fallback function" in error_map.values()


def test_error_map_with_user_error():
code = """
@external
def foo():
raise "some error"
"""
error_map = compile_code(code, output_formats=["source_map"])["source_map"]["error_map"]
charles-cooper marked this conversation as resolved.
Show resolved Hide resolved
assert "user revert with reason" in error_map.values()


def test_error_map_not_overriding_errors():
code = """
@external
def foo(i: uint256):
raise self.bar(5%i)

@pure
def bar(i: uint256) -> String[32]:
return "foo foo"
"""
error_map = compile_code(code, output_formats=["source_map"])["source_map"]["error_map"]
assert "user revert with reason" in error_map.values()
assert "safemod" in error_map.values()


def test_compress_source_map():
Expand Down
2 changes: 2 additions & 0 deletions vyper/codegen/ir_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ def is_complex_ir(self):
# useful for overriding an error message generated by a helper
# function with a more specific error message.
def set_error_msg(self, error_msg: str) -> None:
if self.error_msg is not None:
return
self.error_msg = error_msg
for arg in self.args:
arg.set_error_msg(error_msg)
Expand Down
5 changes: 4 additions & 1 deletion vyper/codegen/stmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ def _assert_reason(self, test_expr, msg):
ir_node = revert_seq
else:
ir_node = ["if", ["iszero", test_expr], revert_seq]
return IRnode.from_list(ir_node, error_msg="user revert with reason")

ir_node = IRnode.from_list(ir_node)
ir_node.set_error_msg("user revert with reason")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if some of the arguments have their own error message? Then it gets overwritten although it might be more specific.

eg in the case of raise self.bar(5 % i) the mod is annotated with a safemod error msg, which will be replaced with user revert with reason

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I did implement set_error_msg to override, but that could be changed. @sandbubbles could you change that in this PR to not override? And add @cyberthirst's test case raise self.foo(5%i) to the test suite.

return ir_node

def parse_Assert(self):
test_expr = Expr.parse_value_expr(self.stmt.test, self.context)
Expand Down
Loading