Skip to content

Commit c06fd12

Browse files
committed
hdl._ast: fix using 0-width Switch with integer keys.
This comes up in `AssignmentLegalizer`-produced `Switch`es for `ArrayProxy`.
1 parent 353a8ce commit c06fd12

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

amaranth/hdl/_ast.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,8 +2294,14 @@ def __init__(self, test, cases, *, src_loc=None, src_loc_at=0, case_src_locs={})
22942294
key = "".join(key.split()) # remove whitespace
22952295
elif isinstance(key, int):
22962296
key = format(key & key_mask, "b").rjust(len(self.test), "0")
2297+
# fixup for 0-width test
2298+
if key_mask == 0:
2299+
key = ""
22972300
elif isinstance(key, Enum):
22982301
key = format(key.value & key_mask, "b").rjust(len(self.test), "0")
2302+
# fixup for 0-width test
2303+
if key_mask == 0:
2304+
key = ""
22992305
else:
23002306
raise TypeError("Object {!r} cannot be used as a switch key"
23012307
.format(key))

tests/test_hdl_ast.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,16 @@ def test_int_neg_case(self):
14731473
s = Switch(Const(0, 8), {-10: []})
14741474
self.assertEqual(s.cases, {("11110110",): []})
14751475

1476+
def test_int_zero_width(self):
1477+
s = Switch(Const(0, 0), {0: []})
1478+
self.assertEqual(s.cases, {("",): []})
1479+
1480+
def test_int_zero_width_enum(self):
1481+
class ZeroEnum(Enum):
1482+
A = 0
1483+
s = Switch(Const(0, 0), {ZeroEnum.A: []})
1484+
self.assertEqual(s.cases, {("",): []})
1485+
14761486
def test_enum_case(self):
14771487
s = Switch(Const(0, UnsignedEnum), {UnsignedEnum.FOO: []})
14781488
self.assertEqual(s.cases, {("01",): []})

0 commit comments

Comments
 (0)