Skip to content

Commit

Permalink
hdl.ast: fix Array not being indexable by ValueCastable
Browse files Browse the repository at this point in the history
  • Loading branch information
anuejn authored and whitequark committed Jan 3, 2024
1 parent 5d9ad62 commit cc9fe89
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions amaranth/hdl/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,8 @@ def __init__(self, iterable=()):
self._mutable = True

def __getitem__(self, index):
if isinstance(index, ValueCastable):
index = Value.cast(index)
if isinstance(index, Value):
if self._mutable:
self._proxy_at = tracer.get_src_loc()
Expand Down
12 changes: 12 additions & 0 deletions tests/test_hdl_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,18 @@ def test_becomes_immutable(self):
r"^Array can no longer be mutated after it was indexed with a value at "):
a.insert(1, 2)

def test_index_value_castable(self):
class MyValue(ValueCastable):
@ValueCastable.lowermethod
def as_value(self):
return Signal()

def shape():
return unsigned(1)

a = Array([1,2,3])
a[MyValue()]

def test_repr(self):
a = Array([1,2,3])
self.assertEqual(repr(a), "(array mutable [1, 2, 3])")
Expand Down

0 comments on commit cc9fe89

Please sign in to comment.