Skip to content

Commit

Permalink
Ban pyo3(get, set) on tuple struct field
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Mar 18, 2021
1 parent 0c396fa commit a284b88
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
25 changes: 14 additions & 11 deletions pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,18 +506,21 @@ fn impl_descriptors(
.flat_map(|(field, fns)| {
fns.iter()
.map(|desc| {
let name = field.ident.as_ref().unwrap().unraw();
let doc = utils::get_doc(&field.attrs, None, true)
.unwrap_or_else(|_| syn::LitStr::new(&name.to_string(), name.span()));
let property_type = PropertyType::Descriptor(&field);
match desc {
FnType::Getter(self_ty) => {
impl_py_getter_def(cls, property_type, self_ty, &name, &doc)
}
FnType::Setter(self_ty) => {
impl_py_setter_def(cls, property_type, self_ty, &name, &doc)
if let Some(name) = field.ident.as_ref().map(|ident| ident.unraw()) {
let doc = utils::get_doc(&field.attrs, None, true)
.unwrap_or_else(|_| syn::LitStr::new(&name.to_string(), name.span()));
let property_type = PropertyType::Descriptor(&field);
match desc {
FnType::Getter(self_ty) => {
impl_py_getter_def(cls, property_type, self_ty, &name, &doc)
}
FnType::Setter(self_ty) => {
impl_py_setter_def(cls, property_type, self_ty, &name, &doc)
}
_ => unreachable!(),
}
_ => unreachable!(),
} else {
bail_spanned!(field.span() => "get/set are not supported on tuple struct field");
}
})
.collect::<Vec<syn::Result<TokenStream>>>()
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/invalid_property_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ impl ClassWithSetter {
fn setter_with_too_many_args(&mut self, py: Python, foo: u32, bar: u32) {}
}

#[pyclass]
struct TupleGetterSetter(#[pyo3(get, set)] i32);

fn main() {}
6 changes: 6 additions & 0 deletions tests/ui/invalid_property_args.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ error: setter function can have at most two arguments ([pyo3::Python,] and value
|
24 | fn setter_with_too_many_args(&mut self, py: Python, foo: u32, bar: u32) {}
| ^^^

error: get/set are not supported on tuple struct field
--> $DIR/invalid_property_args.rs:28:44
|
28 | struct TupleGetterSetter(#[pyo3(get, set)] i32);
| ^^^

0 comments on commit a284b88

Please sign in to comment.