Skip to content

Commit

Permalink
fix[ux]: improve error message for bad hex literals (#4244)
Browse files Browse the repository at this point in the history
improve the error message when a hex literal starts with `0X`. it is
accepted by the parser, but prior to this commit, would result in a
compiler panic. this commit validates the literal and provides a proper
error message.
  • Loading branch information
charles-cooper authored Sep 19, 2024
1 parent e1de93a commit d76d750
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/unit/ast/nodes/test_hex.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def foo():
"""
foo: constant(bytes4) = 0x12_34_56
""",
"""
foo: constant(bytes4) = 0X12345678
""",
]


Expand Down
5 changes: 5 additions & 0 deletions vyper/ast/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,10 +854,15 @@ class Hex(Constant):

def validate(self):
if "_" in self.value:
# TODO: revisit this, we should probably allow underscores
raise InvalidLiteral("Underscores not allowed in hex literals", self)
if len(self.value) % 2:
raise InvalidLiteral("Hex notation requires an even number of digits", self)

if self.value.startswith("0X"):
hint = f"Did you mean `0x{self.value[2:]}`?"
raise InvalidLiteral("Hex literal begins with 0X!", self, hint=hint)

@property
def n_nibbles(self):
"""
Expand Down

0 comments on commit d76d750

Please # to comment.