diff --git a/amaranth/hdl/ast.py b/amaranth/hdl/ast.py index 7f7dfc7..76643a0 100644 --- a/amaranth/hdl/ast.py +++ b/amaranth/hdl/ast.py @@ -370,10 +370,14 @@ class Value(metaclass=ABCMeta): 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}") diff --git a/tests/test_hdl_ast.py b/tests/test_hdl_ast.py index 81ba518..3333a5a 100644 --- a/tests/test_hdl_ast.py +++ b/tests/test_hdl_ast.py @@ -303,6 +303,15 @@ class ValueTestCase(FHDLTestCase): 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),