From cc9fe8904983ea1226a2615e8f185fad43bfb719 Mon Sep 17 00:00:00 2001 From: Jaro Habiger Date: Wed, 3 Jan 2024 14:11:33 +0100 Subject: [PATCH] hdl.ast: fix Array not being indexable by ValueCastable --- amaranth/hdl/ast.py | 2 ++ tests/test_hdl_ast.py | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/amaranth/hdl/ast.py b/amaranth/hdl/ast.py index 70e4763..c7822aa 100644 --- a/amaranth/hdl/ast.py +++ b/amaranth/hdl/ast.py @@ -1382,6 +1382,8 @@ class Array(MutableSequence): 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() diff --git a/tests/test_hdl_ast.py b/tests/test_hdl_ast.py index b97b2a1..2d9ef2a 100644 --- a/tests/test_hdl_ast.py +++ b/tests/test_hdl_ast.py @@ -1005,6 +1005,18 @@ class ArrayTestCase(FHDLTestCase): 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])")