Merge pull request #503 from kngwyu/reject-generics
Reject generics explicitly for #[pyclass]
This commit is contained in:
commit
f2a7c64365
|
@ -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"
|
||||
|
|
|
@ -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",
|
||||
))
|
||||
}
|
||||
}
|
||||
|
|
6
tests/test_compile_error.rs
Normal file
6
tests/test_compile_error.rs
Normal 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");
|
||||
}
|
8
tests/ui/reject_generics.rs
Normal file
8
tests/ui/reject_generics.rs
Normal file
|
@ -0,0 +1,8 @@
|
|||
use pyo3::prelude::*;
|
||||
|
||||
#[pyclass]
|
||||
struct ClassWithGenerics<A> {
|
||||
a: A,
|
||||
}
|
||||
|
||||
fn main() {}
|
5
tests/ui/reject_generics.stderr
Normal file
5
tests/ui/reject_generics.stderr
Normal file
|
@ -0,0 +1,5 @@
|
|||
error: #[pyclass] cannot have generic parameters
|
||||
--> $DIR/reject_generics.rs:4:25
|
||||
|
|
||||
4 | struct ClassWithGenerics<A> {
|
||||
| ^^^
|
Loading…
Reference in a new issue