Skip to content

Commit

Permalink
hdl.ast: suggest bit_select or word_select when indexing with Value.
Browse files Browse the repository at this point in the history
Fixes #1044.
  • Loading branch information
wanda-phi authored and whitequark committed Jan 18, 2024
1 parent 9e97903 commit b40c18f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions amaranth/hdl/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,14 @@ def __getitem__(self, key):
key += n
return Slice(self, key, key + 1, src_loc_at=1)
elif isinstance(key, slice):
if isinstance(key.start, Value) or isinstance(key.stop, Value):
raise TypeError(f"Cannot slice value with a value; use Value.bit_select() or Value.word_select() instead")
start, stop, step = key.indices(n)
if step != 1:
return Cat(self[i] for i in range(start, stop, step))
return Slice(self, start, stop, src_loc_at=1)
elif isinstance(key, Value):
raise TypeError(f"Cannot index value with a value; use Value.bit_select() instead")
else:
raise TypeError(f"Cannot index value with {key!r}")

Expand Down
9 changes: 9 additions & 0 deletions tests/test_hdl_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@ def test_getitem_wrong(self):
with self.assertRaisesRegex(TypeError,
r"^Cannot index value with 'str'$"):
Const(31)["str"]
with self.assertRaisesRegex(TypeError,
r"^Cannot index value with a value; use Value.bit_select\(\) instead$"):
Const(31)[Signal(3)]
s = Signal(3)
with self.assertRaisesRegex(TypeError,
r"^Cannot slice value with a value; use Value.bit_select\(\) or Value.word_select\(\) instead$"):
Const(31)[s:s+3]



def test_shift_left(self):
self.assertRepr(Const(256, unsigned(9)).shift_left(0),
Expand Down

0 comments on commit b40c18f

Please # to comment.