From 3180a17fd9283b059187cd1d259b16956545c7e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcelina=20Ko=C5=9Bcielnicka?= Date: Wed, 7 Jun 2023 14:09:19 +0200 Subject: [PATCH] hdl.ast: fix `Slice` validation. Fixes #810. --- amaranth/hdl/ast.py | 4 ++-- tests/test_hdl_ast.py | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/amaranth/hdl/ast.py b/amaranth/hdl/ast.py index c844627..d181305 100644 --- a/amaranth/hdl/ast.py +++ b/amaranth/hdl/ast.py @@ -796,11 +796,11 @@ class Slice(Value): raise TypeError("Slice stop must be an integer, not {!r}".format(stop)) n = len(value) - if start not in range(-(n+1), n+1): + if start not in range(-n, n+1): raise IndexError("Cannot start slice {} bits into {}-bit value".format(start, n)) if start < 0: start += n - if stop not in range(-(n+1), n+1): + if stop not in range(-n, n+1): raise IndexError("Cannot stop slice {} bits into {}-bit value".format(stop, n)) if stop < 0: stop += n diff --git a/tests/test_hdl_ast.py b/tests/test_hdl_ast.py index 012f77e..25c6be8 100644 --- a/tests/test_hdl_ast.py +++ b/tests/test_hdl_ast.py @@ -714,6 +714,9 @@ class SliceTestCase(FHDLTestCase): with self.assertRaisesRegex(IndexError, r"^Slice start 4 must be less than slice stop 2$"): Slice(c, 4, 2) + with self.assertRaisesRegex(IndexError, + r"^Cannot start slice -9 bits into 8-bit value$"): + Slice(c, -9, -5) def test_repr(self): s1 = Const(10)[2]