Reject generics explicitly for #[pyclass]

This commit is contained in:
kngwyu 2019-06-04 14:17:07 +09:00
parent 99fdafbb88
commit 35f5301675
5 changed files with 32 additions and 0 deletions

View file

@ -30,6 +30,7 @@ inventory = "0.1.3"
[dev-dependencies]
assert_approx_eq = "1.1.0"
indoc = "0.3.3"
trybuild = "1.0"
[build-dependencies]
regex = "1.1.6"

View file

@ -154,6 +154,7 @@ pub fn build_py_class(class: &mut syn::ItemStruct, attr: &PyClassArgs) -> syn::R
let doc = utils::get_doc(&class.attrs, true);
let mut descriptors = Vec::new();
check_generics(class)?;
if let syn::Fields::Named(ref mut fields) = class.fields {
for field in fields.named.iter_mut() {
let field_descs = parse_descriptors(field)?;
@ -461,3 +462,14 @@ fn impl_descriptors(cls: &syn::Type, descriptors: Vec<(syn::Field, Vec<FnType>)>
}
}
}
fn check_generics(class: &mut syn::ItemStruct) -> syn::Result<()> {
if class.generics.params.is_empty() {
Ok(())
} else {
Err(syn::Error::new_spanned(
&class.generics,
"#[pyclass] cannot have generic parameters",
))
}
}

View file

@ -0,0 +1,6 @@
#[test]
#[cfg(testkcovstopmarker)]
fn test_compile_errors() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/reject_generics.rs");
}

View file

@ -0,0 +1,8 @@
use pyo3::prelude::*;
#[pyclass]
struct ClassWithGenerics<A> {
a: A,
}
fn main() {}

View file

@ -0,0 +1,5 @@
error: #[pyclass] cannot have generic parameters
--> $DIR/reject_generics.rs:4:25
|
4 | struct ClassWithGenerics<A> {
| ^^^