From c649045f354db58e2b7e2ced4b7fbd0de3d71f90 Mon Sep 17 00:00:00 2001 From: Catherine Date: Mon, 10 Jun 2024 13:28:43 +0100 Subject: [PATCH] lib.data: add a diagnostic for slicing `data.View`. This is meaningless for a view but meaningful for the underlying value. Fixes #1375. --- amaranth/lib/data.py | 3 +++ tests/test_lib_data.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/amaranth/lib/data.py b/amaranth/lib/data.py index 302aaf1..623d327 100644 --- a/amaranth/lib/data.py +++ b/amaranth/lib/data.py @@ -792,6 +792,9 @@ class View(ValueCastable): :exc:`TypeError` If :meth:`.ShapeCastable.__call__` does not return a value or a value-castable object. """ + if isinstance(key, slice): + raise TypeError( + "View cannot be indexed with a slice; did you mean to call `.as_value()` first?") if isinstance(self.__layout, ArrayLayout): if not isinstance(key, (int, Value, ValueCastable)): raise TypeError( diff --git a/tests/test_lib_data.py b/tests/test_lib_data.py index 7eb90a1..697f366 100644 --- a/tests/test_lib_data.py +++ b/tests/test_lib_data.py @@ -670,6 +670,12 @@ class ViewTestCase(FHDLTestCase): r"with a value$"): Signal(data.StructLayout({}))[Signal(1)] + def test_index_wrong_slice(self): + with self.assertRaisesRegex(TypeError, + r"^View cannot be indexed with a slice; did you mean to call `.as_value\(\)` " + r"first\?$"): + Signal(data.StructLayout({}))[0:1] + def test_getattr(self): v = Signal(data.UnionLayout({ "a": unsigned(2),