ast: fix const-castable expression handling in Signal(reset=).

The code to accept const-castable expressions was previously added in
0c4fda92fe, but it was untested and had
a few bugs.

Fixes #911.
This commit is contained in:
Wanda 2023-09-23 20:29:22 +02:00 committed by Catherine
parent 11d5bb19eb
commit 05cb82b8fc
2 changed files with 9 additions and 2 deletions

View file

@ -1056,12 +1056,15 @@ class Signal(Value, DUID, metaclass=_SignalMeta):
.format(orig_shape, Shape.cast(orig_shape), .format(orig_shape, Shape.cast(orig_shape),
reset.shape())) reset.shape()))
else: else:
if reset is None:
reset = 0
try: try:
reset = Const.cast(reset or 0) reset = Const.cast(reset)
except TypeError: except TypeError:
raise TypeError("Reset value must be a constant-castable expression, not {!r}" raise TypeError("Reset value must be a constant-castable expression, not {!r}"
.format(orig_reset)) .format(orig_reset))
if orig_reset not in (None, 0, -1): # Avoid false positives for all-zeroes and all-ones # Avoid false positives for all-zeroes and all-ones
if orig_reset is not None and not (isinstance(orig_reset, int) and orig_reset in (0, -1)):
if reset.shape().signed and not self.signed: if reset.shape().signed and not self.signed:
warnings.warn( warnings.warn(
message="Reset value {!r} is signed, but the signal shape is {!r}" message="Reset value {!r} is signed, but the signal shape is {!r}"

View file

@ -1053,6 +1053,10 @@ class SignalTestCase(FHDLTestCase):
r"not <StringEnum\.FOO: 'a'>$"): r"not <StringEnum\.FOO: 'a'>$"):
Signal(1, reset=StringEnum.FOO) Signal(1, reset=StringEnum.FOO)
def test_reset_const_castable(self):
s1 = Signal(4, reset=Cat(Const(0, 1), Const(1, 1), Const(0, 2)))
self.assertEqual(s1.reset, 2)
def test_reset_shape_castable_const(self): def test_reset_shape_castable_const(self):
class CastableFromHex(ShapeCastable): class CastableFromHex(ShapeCastable):
def as_shape(self): def as_shape(self):