Skip to content

Commit

Permalink
Merge pull request #503 from kngwyu/reject-generics
Browse files Browse the repository at this point in the history
Reject generics explicitly for #[pyclass]
  • Loading branch information
kngwyu committed Jun 6, 2019
2 parents 45f23c0 + 35f5301 commit f2a7c64
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 12 additions & 0 deletions pyo3-derive-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down Expand Up @@ -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 changes: 6 additions & 0 deletions tests/test_compile_error.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions tests/ui/reject_generics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use pyo3::prelude::*;

#[pyclass]
struct ClassWithGenerics<A> {
a: A,
}

fn main() {}
5 changes: 5 additions & 0 deletions tests/ui/reject_generics.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: #[pyclass] cannot have generic parameters
--> $DIR/reject_generics.rs:4:25
|
4 | struct ClassWithGenerics<A> {
| ^^^

0 comments on commit f2a7c64

Please sign in to comment.