diff --git a/amaranth/lib/data.py b/amaranth/lib/data.py index 4bd010d..1252792 100644 --- a/amaranth/lib/data.py +++ b/amaranth/lib/data.py @@ -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 diff --git a/tests/test_lib_data.py b/tests/test_lib_data.py index 6ed9693..6b9d8f7 100644 --- a/tests/test_lib_data.py +++ b/tests/test_lib_data.py @@ -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)