Skip to content

Commit

Permalink
fix[ux]: improve error message for bad hex literals (vyperlang#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 committed Oct 5, 2024
1 parent 48cd967 commit fba1749
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 fba1749

Please # to comment.