Skip to content

Commit

Permalink
spanned compiler error when cfg features disable enum variants
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-k committed Sep 14, 2024
1 parent c2dfa77 commit ca4cf35
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
7 changes: 5 additions & 2 deletions pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {};
}

Expand All @@ -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"));
}
}

Expand Down
10 changes: 4 additions & 6 deletions tests/ui/invalid_pyclass_enum.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ca4cf35

Please sign in to comment.