Skip to content

Commit fd4ba36

Browse files
wanda-phiwhitequark
authored andcommitted
hdl._ast: fix using 0-width Switch with integer keys.
This comes up in `AssignmentLegalizer`-produced `Switch`es for `ArrayProxy`.
1 parent 5d91132 commit fd4ba36

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
@@ -1769,8 +1769,14 @@ def __init__(self, test, cases, *, src_loc=None, src_loc_at=0, case_src_locs={})
17691769
key = "".join(key.split()) # remove whitespace
17701770
elif isinstance(key, int):
17711771
key = format(key & key_mask, "b").rjust(len(self.test), "0")
1772+
# fixup for 0-width test
1773+
if key_mask == 0:
1774+
key = ""
17721775
elif isinstance(key, Enum):
17731776
key = format(key.value & key_mask, "b").rjust(len(self.test), "0")
1777+
# fixup for 0-width test
1778+
if key_mask == 0:
1779+
key = ""
17741780
else:
17751781
raise TypeError("Object {!r} cannot be used as a switch key"
17761782
.format(key))

tests/test_hdl_ast.py

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

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

0 commit comments

Comments
 (0)