Emit a better error for bad argument names

This commit is contained in:
mejrs 2023-03-07 15:18:57 +01:00 committed by David Hewitt
parent 658bc6bb7c
commit 1ecc71638d
3 changed files with 36 additions and 5 deletions

View File

@ -40,14 +40,14 @@ impl<'a> FnArg<'a> {
}
let arg_attrs = PyFunctionArgPyO3Attributes::from_attrs(&mut cap.attrs)?;
let (ident, by_ref, mutability) = match *cap.pat {
let (ident, by_ref, mutability) = match &*cap.pat {
syn::Pat::Ident(syn::PatIdent {
ref ident,
ref by_ref,
ref mutability,
ident,
by_ref,
mutability,
..
}) => (ident, by_ref, mutability),
_ => bail_spanned!(cap.pat.span() => "unsupported argument"),
other => return Err(handle_argument_error(other)),
};
Ok(FnArg {
@ -67,6 +67,19 @@ impl<'a> FnArg<'a> {
}
}
fn handle_argument_error(pat: &syn::Pat) -> syn::Error {
let span = pat.span();
let msg = match pat {
syn::Pat::Wild(_) => "wildcard argument names are not supported",
syn::Pat::Struct(_)
| syn::Pat::Tuple(_)
| syn::Pat::TupleStruct(_)
| syn::Pat::Slice(_) => "destructuring in arguments is not supported",
_ => "unsupported argument",
};
syn::Error::new(span, msg)
}
#[derive(Clone, PartialEq, Debug, Copy, Eq)]
pub enum MethodTypeAttribute {
/// `#[new]`

View File

@ -9,4 +9,10 @@ fn impl_trait_function(impl_trait: impl AsRef<PyAny>) {}
#[pyfunction]
async fn async_function() {}
#[pyfunction]
fn wildcard_argument(_: i32) {}
#[pyfunction]
fn destructured_argument((a, b): (i32, i32)) {}
fn main() {}

View File

@ -17,3 +17,15 @@ error: `async fn` is not yet supported for Python functions.
|
10 | async fn async_function() {}
| ^^^^^
error: wildcard argument names are not supported
--> tests/ui/invalid_pyfunctions.rs:13:22
|
13 | fn wildcard_argument(_: i32) {}
| ^
error: destructuring in arguments is not supported
--> tests/ui/invalid_pyfunctions.rs:16:26
|
16 | fn destructured_argument((a, b): (i32, i32)) {}
| ^^^^^^