From ca4cf356157282f07c6a33e57bed78a55a53f2e7 Mon Sep 17 00:00:00 2001 From: jeff-k Date: Sat, 14 Sep 2024 18:45:24 +0100 Subject: [PATCH] spanned compiler error when cfg features disable enum variants --- pyo3-macros-backend/src/pyclass.rs | 7 +++++-- tests/ui/invalid_pyclass_enum.stderr | 10 ++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/pyo3-macros-backend/src/pyclass.rs b/pyo3-macros-backend/src/pyclass.rs index e89435aaf0d..291aeb0125a 100644 --- a/pyo3-macros-backend/src/pyclass.rs +++ b/pyo3-macros-backend/src/pyclass.rs @@ -2444,6 +2444,8 @@ fn generate_cfg_check(variants: &[PyClassEnumUnitVariant<'_>], cls: &syn::Ident) let cfg_attrs = &variant.cfg_attrs; if cfg_attrs.is_empty() { + // There's at least one variant of the enum without cfg attributes, + // so the check is not necessary return quote! {}; } @@ -2455,9 +2457,10 @@ fn generate_cfg_check(variants: &[PyClassEnumUnitVariant<'_>], cls: &syn::Ident) } } - quote! { + quote_spanned! { + cls.span() => #[cfg(all(#(#conditions),*))] - ::core::compile_error!(concat!("All variants of enum `", stringify!(#cls), "` have been disabled by cfg attributes")); + ::core::compile_error!(concat!("#[pyclass] can't be used on enums without any variants - all variants of enum `", stringify!(#cls), "` have been configured out by cfg attributes")); } } diff --git a/tests/ui/invalid_pyclass_enum.stderr b/tests/ui/invalid_pyclass_enum.stderr index 51e246cb812..80dc9539748 100644 --- a/tests/ui/invalid_pyclass_enum.stderr +++ b/tests/ui/invalid_pyclass_enum.stderr @@ -66,13 +66,11 @@ error: The `ord` option requires the `eq` option. 83 | #[pyclass(ord)] | ^^^ -error: All variants of enum `AllEnumVariantsDisabled` have been disabled by cfg attributes - --> tests/ui/invalid_pyclass_enum.rs:96:1 +error: #[pyclass] can't be used on enums without any variants - all variants of enum `AllEnumVariantsDisabled` have been configured out by cfg attributes + --> tests/ui/invalid_pyclass_enum.rs:98:6 | -96 | #[pyclass(eq)] - | ^^^^^^^^^^^^^^ - | - = note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info) +98 | enum AllEnumVariantsDisabled { + | ^^^^^^^^^^^^^^^^^^^^^^^ error[E0369]: binary operation `==` cannot be applied to type `&SimpleEqOptRequiresPartialEq` --> tests/ui/invalid_pyclass_enum.rs:31:11