Raise an error if an enum has no members. Such enums cannot be populated with a valid value. Do not extend the same limitation to enum whose members are all compiled out; they can still be used as optional members.
Signed-off-by: Paolo Bonzini <[email protected]> --- scripts/qapi/schema.py | 4 ++++ tests/qapi-schema/enum-empty.err | 2 ++ tests/qapi-schema/enum-empty.json | 2 ++ tests/qapi-schema/enum-empty.out | 0 tests/qapi-schema/qapi-schema-test.json | 3 --- tests/qapi-schema/qapi-schema-test.out | 1 - tests/qapi-schema/union-empty.err | 4 ++-- 7 files changed, 10 insertions(+), 6 deletions(-) create mode 100644 tests/qapi-schema/enum-empty.err create mode 100644 tests/qapi-schema/enum-empty.json create mode 100644 tests/qapi-schema/enum-empty.out diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py index 8d88b40de2e..78c2a25fc9a 100644 --- a/scripts/qapi/schema.py +++ b/scripts/qapi/schema.py @@ -428,6 +428,10 @@ def __init__( def check(self, schema: QAPISchema) -> None: super().check(schema) + if not self.members: + raise QAPISemError( + self.info, + "enum '%s' must have at least one value" % self.name) seen: Dict[str, QAPISchemaMember] = {} for m in self.members: m.check_clash(self.info, seen) diff --git a/tests/qapi-schema/enum-empty.err b/tests/qapi-schema/enum-empty.err new file mode 100644 index 00000000000..6070bf62cbd --- /dev/null +++ b/tests/qapi-schema/enum-empty.err @@ -0,0 +1,2 @@ +enum-empty.json: In enum 'TestEmpty': +enum-empty.json:2: enum 'TestEmpty' must have at least one value diff --git a/tests/qapi-schema/enum-empty.json b/tests/qapi-schema/enum-empty.json new file mode 100644 index 00000000000..3b3dfb2e3d8 --- /dev/null +++ b/tests/qapi-schema/enum-empty.json @@ -0,0 +1,2 @@ +# An enum must have at least one value +{ 'enum': 'TestEmpty', 'data': [] } diff --git a/tests/qapi-schema/enum-empty.out b/tests/qapi-schema/enum-empty.out new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/qapi-schema/qapi-schema-test.json b/tests/qapi-schema/qapi-schema-test.json index 8ca977c49d2..195f1c4847b 100644 --- a/tests/qapi-schema/qapi-schema-test.json +++ b/tests/qapi-schema/qapi-schema-test.json @@ -23,9 +23,6 @@ 'data': { 'enum1': 'EnumOne', # Intentional forward reference '*enum2': 'EnumOne', 'enum3': 'EnumOne', '*enum4': 'EnumOne' } } -# An empty enum, although unusual, is currently acceptable -{ 'enum': 'MyEnum', 'data': [ ] } - # Likewise for an empty struct, including an empty base { 'struct': 'Empty1', 'data': { } } { 'struct': 'Empty2', 'base': 'Empty1', 'data': { } } diff --git a/tests/qapi-schema/qapi-schema-test.out b/tests/qapi-schema/qapi-schema-test.out index 4617eb4e98a..ddd8bf80d66 100644 --- a/tests/qapi-schema/qapi-schema-test.out +++ b/tests/qapi-schema/qapi-schema-test.out @@ -18,7 +18,6 @@ object NestedEnumsOne member enum2: EnumOne optional=True member enum3: EnumOne optional=False member enum4: EnumOne optional=True -enum MyEnum object Empty1 object Empty2 base Empty1 diff --git a/tests/qapi-schema/union-empty.err b/tests/qapi-schema/union-empty.err index d4284399621..c07dcf32a5a 100644 --- a/tests/qapi-schema/union-empty.err +++ b/tests/qapi-schema/union-empty.err @@ -1,2 +1,2 @@ -union-empty.json: In union 'Union': -union-empty.json:4: union has no branches +union-empty.json: In enum 'Empty': +union-empty.json:2: enum 'Empty' must have at least one value -- 2.54.0
