Skip to content

Commit ca4cf35

Browse files
committed
spanned compiler error when cfg features disable enum variants
1 parent c2dfa77 commit ca4cf35

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

pyo3-macros-backend/src/pyclass.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,6 +2444,8 @@ fn generate_cfg_check(variants: &[PyClassEnumUnitVariant<'_>], cls: &syn::Ident)
24442444
let cfg_attrs = &variant.cfg_attrs;
24452445

24462446
if cfg_attrs.is_empty() {
2447+
// There's at least one variant of the enum without cfg attributes,
2448+
// so the check is not necessary
24472449
return quote! {};
24482450
}
24492451

@@ -2455,9 +2457,10 @@ fn generate_cfg_check(variants: &[PyClassEnumUnitVariant<'_>], cls: &syn::Ident)
24552457
}
24562458
}
24572459

2458-
quote! {
2460+
quote_spanned! {
2461+
cls.span() =>
24592462
#[cfg(all(#(#conditions),*))]
2460-
::core::compile_error!(concat!("All variants of enum `", stringify!(#cls), "` have been disabled by cfg attributes"));
2463+
::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"));
24612464
}
24622465
}
24632466

tests/ui/invalid_pyclass_enum.stderr

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,11 @@ error: The `ord` option requires the `eq` option.
6666
83 | #[pyclass(ord)]
6767
| ^^^
6868

69-
error: All variants of enum `AllEnumVariantsDisabled` have been disabled by cfg attributes
70-
--> tests/ui/invalid_pyclass_enum.rs:96:1
69+
error: #[pyclass] can't be used on enums without any variants - all variants of enum `AllEnumVariantsDisabled` have been configured out by cfg attributes
70+
--> tests/ui/invalid_pyclass_enum.rs:98:6
7171
|
72-
96 | #[pyclass(eq)]
73-
| ^^^^^^^^^^^^^^
74-
|
75-
= note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info)
72+
98 | enum AllEnumVariantsDisabled {
73+
| ^^^^^^^^^^^^^^^^^^^^^^^
7674

7775
error[E0369]: binary operation `==` cannot be applied to type `&SimpleEqOptRequiresPartialEq`
7876
--> tests/ui/invalid_pyclass_enum.rs:31:11

0 commit comments

Comments
 (0)