Skip to content

Commit e9299cc

Browse files
wanda-phiwhitequark
authored andcommitted
hdl.ast: change warning on out-of-range reset to an error, improve it.
Fixes #1019.
1 parent 8501d9d commit e9299cc

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

amaranth/hdl/ast.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,13 +1170,14 @@ def __init__(self, shape=None, *, name=None, reset=None, reset_less=False,
11701170
self.reset = reset.value
11711171
self.reset_less = bool(reset_less)
11721172

1173-
if isinstance(orig_shape, range) and self.reset == orig_shape.stop:
1174-
warnings.warn(
1175-
message="Reset value {!r} equals the non-inclusive end of the signal "
1176-
"shape {!r}; this is likely an off-by-one error"
1177-
.format(self.reset, orig_shape),
1178-
category=SyntaxWarning,
1179-
stacklevel=2)
1173+
if isinstance(orig_shape, range) and orig_reset is not None and orig_reset not in orig_shape:
1174+
if orig_reset == orig_shape.stop:
1175+
raise SyntaxError(
1176+
f"Reset value {orig_reset!r} equals the non-inclusive end of the signal "
1177+
f"shape {orig_shape!r}; this is likely an off-by-one error")
1178+
else:
1179+
raise SyntaxError(
1180+
f"Reset value {orig_reset!r} is not within the signal shape {orig_shape!r}")
11801181

11811182
self.attrs = OrderedDict(() if attrs is None else attrs)
11821183

docs/changes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ Language changes
5050
* Added: :func:`amaranth.utils.ceil_log2`, :func:`amaranth.utils.exact_log2`. (`RFC 17`_)
5151
* Changed: ``m.Case()`` with no patterns is never active instead of always active. (`RFC 39`_)
5252
* Changed: ``Value.matches()`` with no patterns is ``Const(0)`` instead of ``Const(1)``. (`RFC 39`_)
53+
* Changed: ``Signal(range(stop), reset=stop)`` warning has been changed into a hard error and made to trigger on any out-of range value.
54+
* Changed: ``Signal(range(0))`` is now valid without a warning.
5355
* Deprecated: :func:`amaranth.utils.log2_int`. (`RFC 17`_)
5456
* Removed: (deprecated in 0.4) :meth:`Const.normalize`. (`RFC 5`_)
5557
* Removed: (deprecated in 0.4) :class:`ast.Sample`, :class:`ast.Past`, :class:`ast.Stable`, :class:`ast.Rose`, :class:`ast.Fell`.

tests/test_hdl_ast.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,10 +1106,8 @@ def test_shape(self):
11061106
self.assertEqual(s8.shape(), signed(5))
11071107
s9 = Signal(range(-20, 16))
11081108
self.assertEqual(s9.shape(), signed(6))
1109-
with warnings.catch_warnings():
1110-
warnings.filterwarnings(action="ignore", category=SyntaxWarning)
1111-
s10 = Signal(range(0))
1112-
self.assertEqual(s10.shape(), unsigned(0))
1109+
s10 = Signal(range(0))
1110+
self.assertEqual(s10.shape(), unsigned(0))
11131111
s11 = Signal(range(1))
11141112
self.assertEqual(s11.shape(), unsigned(1))
11151113

@@ -1184,10 +1182,22 @@ def test_reset_wrong_too_wide(self):
11841182
Signal(signed(1), reset=-2)
11851183

11861184
def test_reset_wrong_fencepost(self):
1187-
with self.assertWarnsRegex(SyntaxWarning,
1185+
with self.assertRaisesRegex(SyntaxError,
11881186
r"^Reset value 10 equals the non-inclusive end of the signal shape "
11891187
r"range\(0, 10\); this is likely an off-by-one error$"):
11901188
Signal(range(0, 10), reset=10)
1189+
with self.assertRaisesRegex(SyntaxError,
1190+
r"^Reset value 0 equals the non-inclusive end of the signal shape "
1191+
r"range\(0, 0\); this is likely an off-by-one error$"):
1192+
Signal(range(0), reset=0)
1193+
1194+
def test_reset_wrong_range(self):
1195+
with self.assertRaisesRegex(SyntaxError,
1196+
r"^Reset value 11 is not within the signal shape range\(0, 10\)$"):
1197+
Signal(range(0, 10), reset=11)
1198+
with self.assertRaisesRegex(SyntaxError,
1199+
r"^Reset value 0 is not within the signal shape range\(1, 10\)$"):
1200+
Signal(range(1, 10), reset=0)
11911201

11921202
def test_attrs(self):
11931203
s1 = Signal()

0 commit comments

Comments
 (0)