lib.data: at most one Union field can have annotation with a default.

This commit is contained in:
Catherine 2023-03-04 09:34:36 +00:00
parent e2ce959c90
commit ae1aeff0f2
2 changed files with 13 additions and 0 deletions

View file

@ -419,6 +419,11 @@ class _AggregateMeta(ShapeCastable, type):
if name in namespace:
reset[name] = namespace.pop(name)
cls = type.__new__(metacls, name, bases, namespace)
if cls.__layout_cls is UnionLayout:
if len(reset) > 1:
raise ValueError("Reset value for at most one field can be provided for "
"a union class (specified: {})"
.format(", ".join(reset.keys())))
cls.__layout = cls.__layout_cls(layout)
cls.__reset = reset
return cls

View file

@ -679,6 +679,14 @@ class UnionTestCase(FHDLTestCase):
self.assertEqual(s.attrs, {"debug": 1})
self.assertEqual(s.decoder, decoder)
def test_define_reset_two_wrong(self):
with self.assertRaisesRegex(ValueError,
r"^Reset value for at most one field can be provided for a union class "
r"\(specified: a, b\)$"):
class U(Union):
a: unsigned(1) = 1
b: unsigned(2) = 1
def test_construct_reset_two_wrong(self):
class U(Union):
a: unsigned(1)