Skip to content

Commit 7ea2e17

Browse files
committed
lib.enum: fix shape calculation for const-castable member values.
1 parent de36e3c commit 7ea2e17

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

amaranth/lib/enum.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import warnings
33

44
from ..hdl.ast import Shape, ShapeCastable, Const
5-
from .._utils import bits_for
65

76

87
__all__ = py_enum.__all__
@@ -38,20 +37,21 @@ def __new__(metacls, name, bases, namespace, shape=None, **kwargs):
3837
cls._amaranth_shape_ = shape = Shape.cast(shape)
3938
for member in cls:
4039
try:
41-
Const.cast(member.value)
40+
member_shape = Const.cast(member.value).shape()
4241
except TypeError as e:
4342
raise TypeError("Value of enumeration member {!r} must be "
4443
"a constant-castable expression"
4544
.format(member)) from e
46-
width = bits_for(member.value, shape.signed)
47-
if member.value < 0 and not shape.signed:
45+
if member_shape.signed and not shape.signed:
4846
warnings.warn(
4947
message="Value of enumeration member {!r} is signed, but enumeration "
5048
"shape is {!r}" # the repr will be `unsigned(X)`
5149
.format(member, shape),
5250
category=RuntimeWarning,
5351
stacklevel=2)
54-
elif width > shape.width:
52+
elif (member_shape.width > shape.width or
53+
member_shape.width == shape.width and
54+
shape.signed and not member_shape.signed):
5555
warnings.warn(
5656
message="Value of enumeration member {!r} will be truncated to "
5757
"enumeration shape {!r}"

0 commit comments

Comments
 (0)