Address comments from PR#692

This commit is contained in:
David Hewitt 2019-12-18 14:26:25 +00:00
parent 0032508c3c
commit b245e71c14
3 changed files with 28 additions and 59 deletions

View File

@ -37,8 +37,8 @@ pub struct FnSpec<'a> {
pub tp: FnType, pub tp: FnType,
// Rust function name // Rust function name
pub name: &'a syn::Ident, pub name: &'a syn::Ident,
// Wrapped python name. This should have been sent through syn::IdentExt::unraw() // Wrapped python name. This should not have any leading r#.
// to ensure that any leading r# is removed. // r# can be removed by syn::ext::IdentExt::unraw()
pub python_name: syn::Ident, pub python_name: syn::Ident,
pub attrs: Vec<Argument>, pub attrs: Vec<Argument>,
pub args: Vec<FnArg<'a>>, pub args: Vec<FnArg<'a>>,
@ -162,14 +162,8 @@ impl<'a> FnSpec<'a> {
"text_signature not allowed on __new__; if you want to add a signature on \ "text_signature not allowed on __new__; if you want to add a signature on \
__new__, put it on the struct definition instead", __new__, put it on the struct definition instead",
)?, )?,
FnType::FnCall => { FnType::FnCall | FnType::Getter | FnType::Setter => {
parse_erroneous_text_signature("text_signature not allowed on __call__")? parse_erroneous_text_signature("text_signature not allowed with this attribute")?
}
FnType::Getter => {
parse_erroneous_text_signature("text_signature not allowed on getter")?
}
FnType::Setter => {
parse_erroneous_text_signature("text_signature not allowed on setter")?
} }
}; };
@ -497,28 +491,10 @@ fn parse_method_name_attribute(
// Reject some invalid combinations // Reject some invalid combinations
if let Some(name) = &name { if let Some(name) = &name {
match ty { match ty {
FnType::FnNew => { FnType::FnNew | FnType::FnCall | FnType::Getter | FnType::Setter => {
return Err(syn::Error::new_spanned( return Err(syn::Error::new_spanned(
name, name,
"name can not be specified with #[new]", "name not allowed with this attribute",
))
}
FnType::FnCall => {
return Err(syn::Error::new_spanned(
name,
"name can not be specified with #[call]",
))
}
FnType::Getter => {
return Err(syn::Error::new_spanned(
name,
"name can not be specified for getter",
))
}
FnType::Setter => {
return Err(syn::Error::new_spanned(
name,
"name can not be specified for setter",
)) ))
} }
_ => {} _ => {}

View File

@ -209,33 +209,26 @@ pub fn parse_name_attribute(attrs: &mut Vec<syn::Attribute>) -> syn::Result<Opti
_ => true, _ => true,
}); });
let mut name = None; match &*name_attrs {
[] => Ok(None),
for (lit, span) in name_attrs { [(syn::Lit::Str(s), span)] => {
if name.is_some() { let mut ident: syn::Ident = s.parse()?;
return Err(syn::Error::new( // This span is the whole attribute span, which is nicer for reporting errors.
span, ident.set_span(*span);
"#[name] can not be specified multiple times", Ok(Some(ident))
));
} }
[(_, span)] => Err(syn::Error::new(
name = match lit { *span,
syn::Lit::Str(s) => { "Expected string literal for #[name] argument",
let mut ident: syn::Ident = s.parse()?; )),
// This span is the whole attribute span, which is nicer for reporting errors. // TODO: The below pattern is unstable, so instead we match the wildcard.
ident.set_span(span); // slice_patterns due to be stable soon: https://github.com/rust-lang/rust/issues/62254
Some(ident) // [(_, span), _, ..] => {
} _ => Err(syn::Error::new(
_ => { name_attrs[0].1,
return Err(syn::Error::new( "#[name] can not be specified multiple times",
span, )),
"Expected string literal for #[name] argument",
))
}
};
} }
Ok(name)
} }
pub fn build_py_function(ast: &mut syn::ItemFn, args: PyFunctionAttr) -> syn::Result<TokenStream> { pub fn build_py_function(ast: &mut syn::ItemFn, args: PyFunctionAttr) -> syn::Result<TokenStream> {

View File

@ -1,16 +1,16 @@
error: name can not be specified for getter error: name not allowed with this attribute
--> $DIR/invalid_pymethod_names.rs:10:5 --> $DIR/invalid_pymethod_names.rs:10:5
| |
10 | #[name = "num"] 10 | #[name = "num"]
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
error: #[name] can not be specified multiple times error: #[name] can not be specified multiple times
--> $DIR/invalid_pymethod_names.rs:18:5 --> $DIR/invalid_pymethod_names.rs:17:5
| |
18 | #[name = "bar"] 17 | #[name = "foo"]
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
error: name can not be specified with #[new] error: name not allowed with this attribute
--> $DIR/invalid_pymethod_names.rs:24:5 --> $DIR/invalid_pymethod_names.rs:24:5
| |
24 | #[name = "makenew"] 24 | #[name = "makenew"]