From d339cd0dc9c65da76ab8ed8b76ebbbbae72165c9 Mon Sep 17 00:00:00 2001 From: b05902132 Date: Tue, 23 Nov 2021 00:58:33 +0800 Subject: [PATCH] Let #[pyclass] reject empty enums. --- pyo3-macros-backend/src/pyclass.rs | 3 +++ tests/ui/invalid_pyclass_enum.rs | 3 +++ tests/ui/invalid_pyclass_enum.stderr | 6 ++++++ 3 files changed, 12 insertions(+) diff --git a/pyo3-macros-backend/src/pyclass.rs b/pyo3-macros-backend/src/pyclass.rs index 3e59a960..52f7c319 100644 --- a/pyo3-macros-backend/src/pyclass.rs +++ b/pyo3-macros-backend/src/pyclass.rs @@ -386,6 +386,9 @@ pub fn build_py_enum( args: PyClassArgs, method_type: PyClassMethodsType, ) -> syn::Result { + if enum_.variants.is_empty() { + bail_spanned!(enum_.brace_token.span => "Empty enums can't be #[pyclass]."); + } let variants: Vec = enum_ .variants .iter() diff --git a/tests/ui/invalid_pyclass_enum.rs b/tests/ui/invalid_pyclass_enum.rs index 62f2a3d6..a12accbf 100644 --- a/tests/ui/invalid_pyclass_enum.rs +++ b/tests/ui/invalid_pyclass_enum.rs @@ -12,4 +12,7 @@ enum NotDrivedClass { y, } +#[pyclass] +enum NoEmptyEnum {} + fn main() {} diff --git a/tests/ui/invalid_pyclass_enum.stderr b/tests/ui/invalid_pyclass_enum.stderr index 2dd0e737..eea36e5c 100644 --- a/tests/ui/invalid_pyclass_enum.stderr +++ b/tests/ui/invalid_pyclass_enum.stderr @@ -9,3 +9,9 @@ error: enums cannot extend from other classes | 9 | #[pyclass(extends = PyList)] | ^^^^^^^ + +error: Empty enums can't be #[pyclass]. + --> tests/ui/invalid_pyclass_enum.rs:16:18 + | +16 | enum NoEmptyEnum {} + | ^^