hdl.ast: warn on fencepost error in Signal(range(x), reset=x).

Also, relax the language reference inset from "warning" to "note"
since this is no longer something developers have to keep in mind
explicitly.
This commit is contained in:
Catherine 2023-03-13 20:27:13 +00:00
parent 32eabd9372
commit 80343d1c4c
3 changed files with 33 additions and 4 deletions

View file

@ -632,6 +632,13 @@ class Const(Value):
elif isinstance(shape, int):
shape = Shape(shape, signed=self.value < 0)
else:
if isinstance(shape, range) and self.value == shape.stop:
warnings.warn(
message="Value {!r} equals the non-inclusive end of the constant "
"shape {!r}; this is likely an off-by-one error"
.format(self.value, shape),
category=SyntaxWarning,
stacklevel=2)
shape = Shape.cast(shape, src_loc_at=1 + src_loc_at)
self.width = shape.width
self.signed = shape.signed
@ -1023,6 +1030,14 @@ class Signal(Value, DUID):
self.reset = reset.value
self.reset_less = bool(reset_less)
if isinstance(orig_shape, range) and self.reset == orig_shape.stop:
warnings.warn(
message="Reset value {!r} equals the non-inclusive end of the signal "
"shape {!r}; this is likely an off-by-one error"
.format(self.reset, orig_shape),
category=SyntaxWarning,
stacklevel=2)
self.attrs = OrderedDict(() if attrs is None else attrs)
if decoder is None and isinstance(orig_shape, type) and issubclass(orig_shape, Enum):