Skip to content

Commit 9bd16ac

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 9bd16ac

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

amaranth/hdl/_ast.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,6 +2294,9 @@ 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")
22992302
else:

tests/test_hdl_ast.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,10 @@ 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+
14761480
def test_enum_case(self):
14771481
s = Switch(Const(0, UnsignedEnum), {UnsignedEnum.FOO: []})
14781482
self.assertEqual(s.cases, {("01",): []})

0 commit comments

Comments
 (0)