Skip to content

Commit b245e71

Browse files
committed
Address comments from PR#692
1 parent 0032508 commit b245e71

File tree

3 files changed

+28
-59
lines changed

3 files changed

+28
-59
lines changed

pyo3-derive-backend/src/method.rs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ pub struct FnSpec<'a> {
3737
pub tp: FnType,
3838
// Rust function name
3939
pub name: &'a syn::Ident,
40-
// Wrapped python name. This should have been sent through syn::IdentExt::unraw()
41-
// to ensure that any leading r# is removed.
40+
// Wrapped python name. This should not have any leading r#.
41+
// r# can be removed by syn::ext::IdentExt::unraw()
4242
pub python_name: syn::Ident,
4343
pub attrs: Vec<Argument>,
4444
pub args: Vec<FnArg<'a>>,
@@ -162,14 +162,8 @@ impl<'a> FnSpec<'a> {
162162
"text_signature not allowed on __new__; if you want to add a signature on \
163163
__new__, put it on the struct definition instead",
164164
)?,
165-
FnType::FnCall => {
166-
parse_erroneous_text_signature("text_signature not allowed on __call__")?
167-
}
168-
FnType::Getter => {
169-
parse_erroneous_text_signature("text_signature not allowed on getter")?
170-
}
171-
FnType::Setter => {
172-
parse_erroneous_text_signature("text_signature not allowed on setter")?
165+
FnType::FnCall | FnType::Getter | FnType::Setter => {
166+
parse_erroneous_text_signature("text_signature not allowed with this attribute")?
173167
}
174168
};
175169

@@ -497,28 +491,10 @@ fn parse_method_name_attribute(
497491
// Reject some invalid combinations
498492
if let Some(name) = &name {
499493
match ty {
500-
FnType::FnNew => {
501-
return Err(syn::Error::new_spanned(
502-
name,
503-
"name can not be specified with #[new]",
504-
))
505-
}
506-
FnType::FnCall => {
507-
return Err(syn::Error::new_spanned(
508-
name,
509-
"name can not be specified with #[call]",
510-
))
511-
}
512-
FnType::Getter => {
513-
return Err(syn::Error::new_spanned(
514-
name,
515-
"name can not be specified for getter",
516-
))
517-
}
518-
FnType::Setter => {
494+
FnType::FnNew | FnType::FnCall | FnType::Getter | FnType::Setter => {
519495
return Err(syn::Error::new_spanned(
520496
name,
521-
"name can not be specified for setter",
497+
"name not allowed with this attribute",
522498
))
523499
}
524500
_ => {}

pyo3-derive-backend/src/pyfunction.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -209,33 +209,26 @@ pub fn parse_name_attribute(attrs: &mut Vec<syn::Attribute>) -> syn::Result<Opti
209209
_ => true,
210210
});
211211

212-
let mut name = None;
213-
214-
for (lit, span) in name_attrs {
215-
if name.is_some() {
216-
return Err(syn::Error::new(
217-
span,
218-
"#[name] can not be specified multiple times",
219-
));
212+
match &*name_attrs {
213+
[] => Ok(None),
214+
[(syn::Lit::Str(s), span)] => {
215+
let mut ident: syn::Ident = s.parse()?;
216+
// This span is the whole attribute span, which is nicer for reporting errors.
217+
ident.set_span(*span);
218+
Ok(Some(ident))
220219
}
221-
222-
name = match lit {
223-
syn::Lit::Str(s) => {
224-
let mut ident: syn::Ident = s.parse()?;
225-
// This span is the whole attribute span, which is nicer for reporting errors.
226-
ident.set_span(span);
227-
Some(ident)
228-
}
229-
_ => {
230-
return Err(syn::Error::new(
231-
span,
232-
"Expected string literal for #[name] argument",
233-
))
234-
}
235-
};
220+
[(_, span)] => Err(syn::Error::new(
221+
*span,
222+
"Expected string literal for #[name] argument",
223+
)),
224+
// TODO: The below pattern is unstable, so instead we match the wildcard.
225+
// slice_patterns due to be stable soon: https://github.com/rust-lang/rust/issues/62254
226+
// [(_, span), _, ..] => {
227+
_ => Err(syn::Error::new(
228+
name_attrs[0].1,
229+
"#[name] can not be specified multiple times",
230+
)),
236231
}
237-
238-
Ok(name)
239232
}
240233

241234
pub fn build_py_function(ast: &mut syn::ItemFn, args: PyFunctionAttr) -> syn::Result<TokenStream> {

tests/ui/invalid_pymethod_names.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
error: name can not be specified for getter
1+
error: name not allowed with this attribute
22
--> $DIR/invalid_pymethod_names.rs:10:5
33
|
44
10 | #[name = "num"]
55
| ^^^^^^^^^^^^^^^
66

77
error: #[name] can not be specified multiple times
8-
--> $DIR/invalid_pymethod_names.rs:18:5
8+
--> $DIR/invalid_pymethod_names.rs:17:5
99
|
10-
18 | #[name = "bar"]
10+
17 | #[name = "foo"]
1111
| ^^^^^^^^^^^^^^^
1212

13-
error: name can not be specified with #[new]
13+
error: name not allowed with this attribute
1414
--> $DIR/invalid_pymethod_names.rs:24:5
1515
|
1616
24 | #[name = "makenew"]

0 commit comments

Comments
 (0)