Skip to content

Commit 9c5b2e8

Browse files
committed
hdl: make Const(enum_value) work.
Fixes #1413.
1 parent 8542252 commit 9c5b2e8

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

amaranth/hdl/_ast.py

+4
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,10 @@ def cast(obj):
15801580

15811581
def __init__(self, value, shape=None, *, src_loc_at=0):
15821582
# We deliberately do not call Value.__init__ here.
1583+
if isinstance(value, Enum):
1584+
if shape is None:
1585+
shape = Shape.cast(type(value))
1586+
value = value.value
15831587
value = int(operator.index(value))
15841588
if shape is None:
15851589
shape = Shape(bits_for(value), signed=value < 0)

tests/test_hdl_ast.py

+14
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,20 @@ def test_hash(self):
502502
with self.assertRaises(TypeError):
503503
hash(Const(0))
504504

505+
def test_enum(self):
506+
e1 = Const(UnsignedEnum.FOO)
507+
self.assertIsInstance(e1, Const)
508+
self.assertEqual(e1.shape(), unsigned(2))
509+
e2 = Const(SignedEnum.FOO)
510+
self.assertIsInstance(e2, Const)
511+
self.assertEqual(e2.shape(), signed(2))
512+
e3 = Const(TypedEnum.FOO)
513+
self.assertIsInstance(e3, Const)
514+
self.assertEqual(e3.shape(), unsigned(2))
515+
e4 = Const(UnsignedEnum.FOO, 4)
516+
self.assertIsInstance(e4, Const)
517+
self.assertEqual(e4.shape(), unsigned(4))
518+
505519
def test_shape_castable(self):
506520
class MockConstValue(ValueCastable):
507521
def __init__(self, value):

0 commit comments

Comments
 (0)