hdl: disallow signed(0) values with unclear semantics.

Fixes #807.
This commit is contained in:
Catherine 2023-09-03 04:27:17 +00:00
parent 21b5451036
commit f135226a79
3 changed files with 36 additions and 13 deletions

View file

@ -153,8 +153,8 @@ Shapes from ranges
Casting a shape from a :class:`range` ``r`` produces a shape that:
* has a width large enough to represent both ``min(r)`` and ``max(r)``, and
* is signed if either ``min(r)`` or ``max(r)`` are negative, unsigned otherwise.
* has a width large enough to represent both ``min(r)`` and ``max(r)``, but not larger, and
* is signed if ``r`` contains any negative values, unsigned otherwise.
Specifying a shape with a range is convenient for counters, indexes, and all other values whose width is derived from a set of numbers they must be able to fit:
@ -184,6 +184,16 @@ Specifying a shape with a range is convenient for counters, indexes, and all oth
Amaranth detects uses of :class:`Const` and :class:`Signal` that invoke such an off-by-one error, and emits a diagnostic message.
.. note::
An empty range always casts to an ``unsigned(0)``, even if both of its bounds are negative.
This happens because, being empty, it does not contain any negative values.
.. doctest::
>>> Shape.cast(range(-1, -1))
unsigned(0)
.. _lang-shapeenum: